forked from gussmith23/posit-bisection-viz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.js
106 lines (95 loc) · 3.36 KB
/
viz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Draw the projective reals line on an SVG.
* @param svgSelection - the d3 selection of the SVG element.
*/
function drawProjectiveRealsLine(svgSelection, width, height) {
// An assumption I'm making right now.
console.assert(width === height);
// Create a defs block; define arrowhead and dot markers.
var arrowheadMarker = createArrowheadMarker();
var arrowheadMarkerId = d3.select(arrowheadMarker).attr('id');
var dotMarker = createDotMarker();
var dotMarkerId = d3.select(dotMarker).attr('id');
// This weird syntax is what d3 expects:
// https://stackoverflow.com/questions/23110366/d3-append-with-variable
var defs = container.append('defs');
defs.append(function () {return arrowheadMarker;});
defs.append(function(){return dotMarker;});
var fill = 'none';
var stroke = 'black';
var strokeWidth = '2';
var origin = {x : width/2, y : height};
var dest = {x : width/2, y : 0};
var radius = (width)/2;
container.append('path')
.attr('d',
'M' + origin.x + ' ' + origin.y + ' '
+ 'A' + radius + ' ' + radius + ' '
+ '0' + ' '
+ '0 0' + ' '
+ dest.x + ' ' + dest.y + ' ')
.attr('fill', fill)
.attr('stroke', stroke)
.attr('stroke-width', strokeWidth)
.attr('marker-end', 'url(#' + arrowheadMarkerId + ')')
.attr('marker-mid', 'url(#' + dotMarkerId + ')');
}
/**
* Create an arrowhead <marker> element to be appended to an <svg> (within a
* <defs>).
* See https://vanseodesign.com/web-design/svg-markers/.
*/
function createArrowheadMarker() {
var id = 'arrowhead';
var markerWidth = '10';
var markerHeight = '10';
var refX = '0';
var refY = '3';
var orient = 'auto';
var markerUnits = 'strokeWidth';
// See https://stackoverflow.com/questions/28734628/how-can-i-set-an-attribute-with-case-sensitive-name-in-a-javascript-generated-el
// We create an XML element instead of an HTML element. HTML attributes are
// case-insensitive, and so the attributes below get lowercased if we don't
// do this.
var marker = document.createElementNS('http://www.w3.org/2000/svg', 'marker');
d3.select(marker)
.attr('id', id)
.attr('markerWidth', markerWidth)
.attr('markerHeight', markerHeight)
.attr('refX', refX)
.attr('refY', refY)
.attr('orient', orient)
.attr('markerUnits', markerUnits)
.append('path')
.attr('d', 'M0,0 L0,6 L9,3 z')
.attr('fill', 'black');
return marker;
}
/**
* Create a dot <marker> element to be appended to an <svg> (within a <defs>).
*/
function createDotMarker() {
var id = 'dot';
var markerWidth = '10';
var markerHeight = '10';
var refX = '5';
var refY = '5';
var orient = 'auto';
var markerUnits = 'strokeWidth';
var radius = '3';
var marker = document.createElementNS('http://www.w3.org/2000/svg', 'marker');
d3.select(marker)
.attr('id', id)
.attr('markerWidth', markerWidth)
.attr('markerHeight', markerHeight)
.attr('refX', refX)
.attr('refY', refY)
.attr('orient', orient)
.attr('markerUnits', markerUnits)
.append('circle')
.attr('cx', '5')
.attr('cy', '5')
.attr('r', radius)
.attr('fill', 'black');
return marker;
}