-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
237 lines (215 loc) · 6.75 KB
/
index.html
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!doctype html>
<meta charset="utf-8" />
<style>
body {
max-width: 700px;
margin: 0px auto;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
#chart {
margin: 0 auto;
max-width: 700px;
}
#chart svg {
margin-top: 20px;
}
#chart p {
margin: 2.5px;
}
#chart #max-anno foreignObject {
border-right: 1.5px solid black;
}
#chart #max-anno div p {
margin-right: 5px;
}
#chart .annotation {
text-align: center;
}
.comment {
color: #290BD2;
}
.mono,
m {
font-family: monospace;
display: inline;
font-size: 14px;
background: #d0d2ce;
padding: 0.05em;
padding-left: 0.2em;
padding-right: 0.2em;
border-radius: 2px;
}
pre {
background-color: #d0d2ce;
padding: 10px 5px;
line-height: 1.25;
}
.chunk {
margin: 30px 0px;
}
</style>
<body>
<h1>
<a href="https://github.com/camdenblatchly/easy-d3-annotate" target="_blank"
>easy-d3-annotate</a
>
</h1>
<p><em>Simple annotations for d3 graphics</em></p>
<div class="chunk">
<h2>Setup</h2>
<p>
Include <a href="https://d3js.org/" target="_blank">d3</a> (v7 or higher) before
including your annotation script.
</p>
<p>
To add <m>easy-d3-annotate</m> as a node module run
<m>npm i @camdenblatchly/easy-d3-annotate</m>.
</p>
</div>
<div class="chunk">
<h2>In use</h2>
<p>
Here is an example of an annotation definition:
<pre>
let annotation = {
note: "Boston, MA", <span class="comment">// Whatever you want your note to say. Accepts HTML.</span>
data: [
<span class="comment">// The coordinates of the annotation subject go first</span>
{ x: +x(max_cty.pct_ba_higher), y: y(+max_cty.pci) },
<span class="comment">// You can optionally define an intermediary point for the line to pass through</span>
{ x: x(0.4), y: y(45000) },
<span class="comment">// The coordinates of the annotation note go last</span>
{ x: x(0.42), y: y(35000) },
],
width: 100,
height: 55,
<span class="comment">// You can optionally specify an id for CSS styling</span>
id: 'max-anno',
<span class="comment">// Or a class</span>
class: 'annotation',
<span class="comment">// Use arrow_offset to offset the position of the arrow from its subject</span>
arrow_offset: { x: 0, y: dot_radius * 1.5 },
<span class="comment">// Specify where the line should connect to the note</span>
line_start: 'top',
<span class="comment">// Lines can either have linear or normal curves </span>
curve_type: 'linear'
}
</pre>
You can then add your annotation by running <m>svg.call(addAnnotation, annotation)</m> at the end of your script.
</p>
</div>
<div class="chunk">
<h2>Parameters</h2>
<p>
<m>note</m> A string containing your annotation text. It accepts HTML.
<br><br>
<m>data</m> An array containing at least two x/y coordinate pairs. The first coordinate pair demarcates
the location of what you want to point to. The final coordinate pair demarcates the location of
your annotation note. In between, you can add coordinate pairs for any location you want your line to
pass through.
<br><br>
<m>width</m> Width of the note container. Defaults to pixels.
<br><br>
<m>height</m> Height of the note container. Defaults to pixels.
<br><br>
<m>id</m> Optional id applied to the <m>g</m> element containing the note and line.
<br><br>
<m>class</m> Optional class applied to the <m>g</m> element containing the note and line.
<br><br>
<m>arrow_offset</m> Optional coordinate pair defining the offset of the arrow from the subject. Defaults to pixels.
<br><br>
<m>curve_type</m> One of either "natural" (a swoopy line) or "linear" (a straight line). Defaults to "natural".
</p>
</div>
<div class="chunk">
<h2>Styling</h2>
<p>
Annotation notes are added to the SVG using <m>foreignObject</m> elements. As such, the easiest way to customize your annotation styles is by defining the <m>foreignObject</m> element or any HTML elements you added within.
</p>
<div id="chart">
</div>
</div>
</body>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="easy-d3-annotate.js"></script>
<script>
let percent_format = d3.format(".1%");
let dollar_format = d3.format("$,");
let margin = { top: 10, right: 30, bottom: 30, left: 60 },
width = 700 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
let svg = d3
.select("#chart")
.append("svg")
.attr("viewBox", [
0,
0,
width + margin.left + margin.right,
height + margin.bottom + margin.top,
])
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("lib/annotation_data.csv").then(function (data) {
// Add X axis
let x = d3.scaleLinear().domain([0.1, 0.45]).range([0, width]);
svg
.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(6, ",%"));
// Add Y axis
let y = d3.scaleLinear().domain([25000, 55000]).range([height, 0]);
svg.append("g").call(d3.axisLeft(y).ticks(6, "$,.5"));
let dot_radius = 10;
svg
.append("g")
.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("cx", function (d) {
return x(d.pct_ba_higher);
})
.attr("cy", function (d) {
return y(d.pci);
})
.attr("r", dot_radius)
.style("fill", "#69b3a2");
let min_pci = d3.min(data, (d) => +d.pci);
let max_pci = d3.max(data, (d) => +d.pci);
let min_cty = d3.filter(data, (d) => +d.pci === min_pci)[0];
let max_cty = d3.filter(data, (d) => +d.pci === max_pci)[0];
let annotations = [{
note: min_cty.NAME,
data: [
{ x: +x(min_cty.pct_ba_higher), y: y(+min_cty.pci) },
{ x: x(0.22), y: y(35000) },
{ x: x(0.15), y: y(45000) },
],
width: 100,
height: 45,
id: "min-anno",
class: "annotation",
arrow_offset: { x: dot_radius, y: -dot_radius },
line_start: "bottom",
},
{
note: `<p><b>` + max_cty.NAME + `</b></p>`,
data: [
{ x: +x(max_cty.pct_ba_higher), y: y(+max_cty.pci) },
{ x: x(0.38), y: y(49000) },
{ x: x(0.3), y: y(49000) },
],
width: 110,
height: 45,
id: "max-anno",
class: "annotation",
arrow_offset: { x: -dot_radius*1.5, y: dot_radius },
line_start: "right",
curve_type: "linear",
}
];
svg.call(addAnnotations, annotations);
});
</script>