-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.html
509 lines (390 loc) · 13 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body{
width: 900px;
margin: 0px auto;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background: #F5F5F5;
}
h1{
}
h1, h3{
margin: 50px;
text-align: center;
}
h3{
margin-bottom: 30px;
}
svg{
overflow: visible;
}
text {
font-size: 12px;
text-shadow: 0 1px 0 #F5F5F5, 1px 0 0 #F5F5F5, 0 -1px 0 #F5F5F5, -1px 0 0 #F5F5F5;
}
.axis line, .axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.annotations path{
fill: none;
stroke: black;
}
.annotations g:hover circle{
stroke: red;
}
.annotations g:hover text{
fill: red;
}
div.tooltip {
top: -1000px;
position: absolute;
padding: 10px;
background: rgba(255, 255, 255, .90);
border: 1px solid lightgray;
pointer-events: none;
z-index: 100;
}
.tooltip-hidden{
opacity: 0;
}
#graph{
display: inline-block;
width: 500px;
position: relative;
z-index: 2;
}
#annotations-update{
display: inline-block;
width: 380px;
position: relative;
top: -25px;
left: -120px;
/*background: #eee;*/
}
.mono, m{
font-family: monospace;
display: inline;
font-size: 14px;
background: #ddd;
padding: .05em;
padding-left: .2em;
padding-right: .2em;
border-radius: 2px;
}
p{
max-width: 600px;
margin: 0px auto;
font-size: 16px;
line-height: 1.4em;
}
pre{
font-size: 14px;
margin: 0px;
margin-left: 136px;
padding-left: 15px;
border-left: 1px solid #ccc;
}
p, pre{
margin-bottom: 30px;
}
#resizable {
position: relative;
width: 500px;
height: 520px;
margin-bottom: 40px;
}
#resizable .resizer {
position: absolute;
width: 10px;
top: 0;
bottom: 0;
right: -44px;
background: #ccc;
opacity: .5;
cursor: pointer;
}
a{
color: black;
}
</style>
<body>
<h1><a href='https://github.com/1wheel/swoopy-drag'>swoopyDrag.js</a></h1>
<h3 style='margin-bottom: 50px;'>Artisanal label placement for d3 graphics</h3>
<p>
<i>“The annotation layer is the most important thing we do”</i> —Amanda Cox
</p>
<p>
<span class='mono'>swoopyDrag</span> helps you hand place annotations on d3 graphics. It takes an array of objects representing annotations and turns them into lines and labels. Drag the text and control circles below to update the annotations array:
</p>
<div style='padding-top: 20px; padding-bottom: 15px;'>
<div id='graph'>
</div>
<div id='annotations-update'></div>
</div>
<p>
The <span class='mono'>x</span> and <span class='mono'>y</span> functions are called on each annotation to determine its position. In the annotations array here, the <span class='mono'>sepalWidth</span> and <span class='mono'>sepalLength</span> properties are the data values of point the annotation refers to. The functions passed to <span class='mono'>x</span> and <span class='mono'>y</span> look up these values and encode them as pixel position using the same scale set up to position the circles.
<pre>
var swoopy = d3.swoopyDrag()
.x(d => xScale(d.sepalWidth))
.y(d => yScale(d.sepalLength))
.draggable(true)
.annotations(annotations)
</pre>
<p>
Setting <m>draggable</m> to <m>true</m> adds control points to the path strings and enables label dragging - turn on while developing and off when you're ready to publish.
<p>
The shape of each annotation's line is determined by the <span class='mono'>path</span> property, the text by the <span class='mono'>text</span> property and the position of the text by the <span class='mono'>testOffset</span> property. Currently only <a href='https://www.w3.org/TR/SVG/paths.html#PathDataLinetoCommands'>straight paths</a> (paths of the form <m>M 0,0 L 10,10</m>), <a href='https://www.w3.org/TR/SVG/paths.html#PathDataQuadraticBezierCommands'>béziers</a> (paths of the form <m>M 0,0 C 10,10 10,15, 20,15</m>) and circular arcs are supported—see my interactive path string <a href='http://roadtolarissa.com/blog/2015/02/22/svg-path-strings/'>tutorial</a> for more details.
</p>
<p> The annotations are added to the page just like <m>d3.svg.axis</m> - append a new <m>group</m> element and use <m>call</m>:
</p>
<pre>
var swoopySel = svg.append('g').call(swoopy)
</pre>
<p>After posititioning the labels, open the <a href='https://developer.chrome.com/devtools/docs/console'>dev tools</a>, run <m>copy(annotations)</m> in the console and paste over the old <m>annotations</m> array in your text editor.
<h3>Responsive</h3>
<div id="resizable" style='padding-top: 20px; padding-bottom: 15px;'>
<div class="resizer"></div>
</div>
<p> Since each annotation's position is determined primarily by scales, lines and labels will still point to the correct position when the chart size changes. As the chart shrinks though, the annotations might overlap or cover up data points. To show fewer or differently positioned labels on mobile, you could create multiple annotation arrays for different screen sizes:
</p>
<pre>
d3.swoopyDrag()
.annotations(innerWidth < 800 ? mobileAnnotations : desktopAnnotations)
</pre>
<p>
Alternatively if there's just one or two problematic annotations that only work above or below some sizes, you could add <m>maxWidth</m> and <m>minWidth</m> properties to the overlapping annotations and filter:
</p>
<pre>
d3.swoopyDrag()
.annotations(annotations.filter(function(d){
return (typeof(d.minWidth) == 'undefined' || innerWidth > d.minWidth)
&& (typeof(d.maxWidth) == 'undefined' || innerWidth < d.maxWidth)
}))
</pre>
<h3>Arrowheads</h3>
<p>SVG has native support for <a href='https://developer.mozilla.org/en-US/docs/Web/SVG/Element/marker'>arrowheads</a>, but they can be a little fiddly to get working. First, add a <m>marker</m> element to the page the describes the shape of the arrow:
</p>
<pre>
svg.append('marker')
.attr('id', 'arrow')
.attr('viewBox', '-10 -10 20 20')
.attr('markerWidth', 20)
.attr('markerHeight', 20)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M-6.75,-6.75 L 0,0 L -6.75,6.75')
</pre>
<p>Next, select paths in each annotation and set their <m>marker-end</m> attribute:
<pre>
swoopySel.selectAll('path').attr('marker-end', 'url(#arrow)')
</pre>
<h3>Text Wrap</h3>
<p>Multiline text can be added with <a href='https://github.com/gka/d3-jetpack'>d3-jetpack</a>. Select all of the <m>text</m> elements, clear the existing text, then use <m>d3.wordwrap</m> and <m>tspans</m> to wrap the text:
</p>
<pre>
swoopySel.selectAll('text')
.each(function(d){
d3.select(this)
.text('') //clear existing text
.tspans(d3.wordwrap(d.text, 20)) //wrap after 20 char
})
</pre>
<p>
<h3>Styling</h3>
<p>On the page, annotations are made up of a <m>group</m> containing a <m>path</m> and a <m>text</m> element. Annotation data is bound to them, making it possible to control the style of individual annotations.
<p>To customize the color of the text you could add a <m>textColor</m> property to the annotation data and use it to set the <m>fill</m> of the <m>text</m>:
<pre>
swoopySel.selectAll('text').style('fill', d => d.textColor || '#000')
</pre>
<p>Or you could add a <m>highlight</m> class to some of the annotations:
<pre>
swoopySel.selectAll('g').attr('class', d => d.class)
</pre>
<p>And emphasize them with css:
<pre>
g.highlight text{ font-weight: 700; }
g.highlight path{ stroke-width: 2; }
</pre>
<h3>Examples</h3>
<p style='line-height: 2.8em;'>
<a href='http://bl.ocks.org/1wheel/68073eeba4d19c454a8c25fcd6e9e68a'>d3-module-faces</a>
<br>
<a href='http://roadtolarissa.com/nba-minutes/'>Minute by Minute Point Differentials</a>
<br>
<a href='http://roadtolarissa.com/nba-win-loss/'>NBA Win/Loss Records</a>
<br>
<a href='http://www.nytimes.com/interactive/2016/08/09/us/elections/Bush-Rubio-and-Kasich-Donors-give-to-Clinton.html'>Bush and Kasich Donors Give to Clinton</a>
</p>
<h3>Other Tools</h3>
<p style='line-height: 2.8em;'>
<a href='https://github.com/bizweekgraphics/swoopyarrows'>swoopyarrows</a> creates fancier swoops, including circular and loopy arcs.
<br>
<a href='http://twitter.github.io/labella.js/'>labella.js</a> uses a force directed layout to position timeline labels with no overlap.
<br>
<a href='http://nytimes.github.io/svg-crowbar/'>svg-crowbar</a> lets you export a <m>svg</m> file and add annotations manually.
<br>
<a href='http://ai2html.org/'>ai2html</a> is an illustrator script that creates responsive <m>html</m>.
<br>
</P>
<h3>Contribute</h3>
<p style='text-align: center; margin-bottom: 80px;'>
<a href='http://github.com/1wheel/swoopy-drag'>github.com/1wheel/swoopy-drag</a>
</p>
<div class='tooltip'></div>
</body>
<script src="lib/d3v4+jetpack.js"></script>
<script src='swoopy-drag.js'></script>
<script>
var ƒ = d3.f
var annotations = [
{
"sepalWidth": 4.4,
"sepalLength": 5.7,
"path": "M-49,-61L-6,-8",
"text": "Setosa",
"textOffset": [
-83,
-65
]
},
{
"sepalWidth": 3.8,
"sepalLength": 7.7,
"path": "M 15,114 A 128.931 128.931 0 1 1 -10,-32",
"text": "Virginica",
"textOffset": [
-11,
105
]
},
{
"sepalWidth": 2.3,
"sepalLength": 5,
"path": "M-5,86C-47,82,-55,18,-9,4",
"text": "Versicolor",
"textOffset": [
-2,
92
]
}
]
var dragToResize = {
"sepalWidth": 4.4,
"sepalLength": 5,
"path": "M22,-283C84,-279,84,-356,18,-350",
"text": "Drag to resize",
"textOffset": [
91,
-334
]
}
var c = d3.conventions({parentSel: d3.select('#graph'), width: 450})
var swoopy = d3.swoopyDrag()
.draggable(true)
.x(ƒ('sepalWidth', c.x))
.y(ƒ('sepalLength', c.y))
.annotations(annotations)
.on('drag', function(){
annotationText.text('var annotations = ' + JSON.stringify(annotations, null, 2))
})
var annotationText = d3.select('#annotations-update').append('pre')
d3.tsv('lib/data.tsv', function(res){
data = res
c.x.domain(d3.extent(data, ƒ('sepalWidth')) ).nice()
c.y.domain(d3.extent(data, ƒ('sepalLength'))).nice()
c.drawAxis()
c.svg.select('.x.axis').append("text")
.attr("class", "label")
.attr("x", c.width)
.attr("y", -6)
.attr("fill", "#000")
.style("text-anchor", "end")
.text("Sepal Width (cm)");
c.svg.select('.y.axis').append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.attr("fill", "#000")
.style("text-anchor", "end")
.text("Sepal Length (cm)")
c.svg.appendMany(data, 'circle')
.attr('cx', ƒ('sepalWidth', c.x))
.attr('cy', ƒ('sepalLength', c.y))
.attr('fill', ƒ('species', d3.scaleOrdinal(d3.schemeCategory10)))
.attr('r', 5)
.attr('stroke', '#000')
.call(d3.attachTooltip)
var swoopySel = c.svg.append('g.annotations')
.call(swoopy)
swoopySel.selectAll('path')
.attr('marker-end', 'url(#arrow)')
// swoopySel.selectAll('text')
// .each(function(d){
// d3.select(this)
// .text('')
// .tspans(d.text.split(' '))
// })
c.svg.append('marker')
.attr('id', 'arrow')
.attr('viewBox', '-10 -10 20 20')
.attr('markerWidth', 20)
.attr('markerHeight', 20)
.attr('orient', 'auto')
.append('path')
.attr('d', 'M-6.75,-6.75 L 0,0 L -6.75,6.75')
!(function(){
var resizable = d3.select('#resizable');
var resizer = resizable.select('.resizer');
var r = d3.conventions({parentSel: d3.select('#resizable'), width: 450})
r.x.domain(d3.extent(data, ƒ('sepalWidth')) ).nice()
r.y.domain(d3.extent(data, ƒ('sepalLength'))).nice()
r.drawAxis()
var circles = r.svg.appendMany(data, 'circle')
.attr('cx', ƒ('sepalWidth', r.x))
.attr('cy', ƒ('sepalLength', r.y))
.attr('fill', ƒ('species', d3.scaleOrdinal(d3.schemeCategory10)))
.attr('r', 5)
.attr('stroke', '#000')
.call(d3.attachTooltip)
swoopy.draggable(false).annotations(annotations.concat(dragToResize))
var swoopySel = r.svg.append('g.annotations').call(swoopy)
swoopySel.selectAll('path')
.attr('marker-end', 'url(#arrow)')
swoopySel.selectAll('text').style('fill', 'black')
resizer.call(d3.drag().on('drag', function(){
setWidth(d3.mouse(this.parentNode)[0])
}))
var curX, isGrowing
function setWidth(x){
curX = x
x = Math.min(Math.max(50, x - r.margin.left - r.margin.right), 1000)
r.x.range([0, x])
resizable.style('width', x + 'px');
circles.attr('cx', ƒ('sepalWidth', r.x))
swoopySel.selectAll('g').translate(function(d){
return [r.x(d.sepalWidth), r.y(d.sepalLength)]
})
r.svg.select('.x.axis').call(r.xAxis)
}
setWidth(500)
lastScroll = new Date()
d3.select(window).on('scroll.dragjigger', function(){
lastScroll = new Date()
})
d3.timer(function(){
if (lastScroll < new Date() - 600) return
var min = 400
var max = 800
var mid = (min + max)/2
if (curX < min) isGrowing = true
if (curX > max) isGrowing = false
var step = Math.max(1, Math.abs(600))
setWidth(curX + (isGrowing ? 1 : -1)*5)
})
})()
})
</script>