-
Notifications
You must be signed in to change notification settings - Fork 34
/
line.Rmd
383 lines (293 loc) · 8.43 KB
/
line.Rmd
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
# Line charts <i class="fa fa-line-chart"></i>
<script src="https://d3js.org/d3.v7.js"></script>
Read: *IDVW2*, Chapter 11 Using Paths
## Lecture slides <i class="fa fa-television"></i>
[line_charts.pdf](pdfs/line_charts.pdf){target="_blank"}
## SVG `<line>` element
(Use for two points only.)
``` html
<line x1="0" y1="80" x2="100" y2="20" stroke="black" />
```
``` js
const x1 = 0;
const y1 = 80;
const x2 = 100;
const y2 = 20;
d3.select("svg")
.append("line")
.attr("x1", x1)
.attr("x2", x2)
.attr("y1", y1)
.attr("y2", y2);
```
## SVG `<path>` element
(Use if you have more than two points.)
``` html
<svg width = "500" height = "400">
<path d="M 50 400 L 100 300 L 150 300 L 200 33 L 250 175
L 300 275 L 350 250 L 400 125" fill="none"
stroke="red" stroke-width="5">
</path>
</svg>
```
`d` attribute:
M = move to
L = line to
More options: [https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d)
## Data for line chart
Format that we have:
Day|High Temp
----|----
April 1|60
April 2|43
April 3|43
April 4|56
April 5|45
April 6|62
April 7|49
Format that we need looks something like this:
``` html
<path class="line" fill="none" d="M0,149.15254237288136L71.42857142857143,264.40677966101697L142.85714285714286,264.40677966101697L214.28571428571428,176.27118644067798L285.7142857142857,250.84745762711864L357.14285714285717,135.59322033898303L428.57142857142856,223.72881355932205"></path>
```
## Create a line generator
Expects data in an array of 2-dimensional arrays, that is, an array of (x,y) pairs:
``` js
const dataset = [ [0, 60], [1, 43], [2, 43], [3, 56], [4, 45], [5, 62], [6, 49] ];
const mylinegen = d3.line()
```
Test it in the Console:
``` js
mylinegen(dataset);
```
Add an ordinal scale for x:
``` js
const xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
.range([0, 500])
```
... and a linear scale for y:
``` js
const yScale = d3.scaleLinear()
.domain([d3.min(dataset, d => d[1]) - 20,
d3.max(dataset, d => d[1]) + 20])
.range([400, 0]);
```
*Why `d[1]` instead of `d`? (See p. 122)
Add accessor functions `.x()` and `.y()`:
``` js
mylinegen
.x(d => xScale(d[0]))
.y(d => yScale(d[1]));
```
Test again:
``` js
mylinegen(dataset);
```
Now let's add a `<path>` element with that `d` attribute: (this step is just for learning purposes...)
``` js
const mypath = mylinegen(dataset);
d3.select("svg").append("path").attr("d", mypath)
.attr("fill", "none").attr("stroke", "red")
.attr("stroke-width", "5");
```
## Put the line generator to work
Now let's do it the direct way: bind the *datum* and calculate the path in one step:
``` js
d3.select("svg").append("path")
.datum(dataset)
.attr("d", mylinegen)
.attr("fill", "none")
.attr("stroke", "teal")
.attr("stroke-width", "5");
```
Finally, we'll add a class and style definitions:
``` html
<style>
.linestyle {
fill: none;
stroke: teal;
stroke-width: 5px;
}
</style>
```
The `append("path")` line becomes:
``` js
svg.append("path")
.datum(dataset)
.attr("d", mylinegen)
.attr("class", "linestyle");
```
<svg id="noaxes" width="500" height="400"></svg>
<style>
.linestyle {
fill: none;
stroke: teal;
stroke-width: 5px;
}
</style>
<script>
const w = 500;
const h = 400;
let svg = d3.select("svg#noaxes");
const dataset = [ [0, 60], [1, 43], [2, 43], [3, 56],
[4, 45], [5, 62], [6, 49] ];
let xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
.range([0, w]);
let yScale = d3.scaleLinear()
.domain([d3.min(dataset, d => d[1]) - 20,
d3.max(dataset, d => d[1]) + 20])
.range([h, 0]);
const mylinegen = d3.line()
.x(d => xScale(d[0]))
.y(d => yScale(d[1]));
svg.append("path")
.datum(dataset)
.attr("d", mylinegen)
.attr("class", "linestyle");
</script>
Putting it all together, we have:
``` html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Line generator</title>
<script src="https://d3js.org/d3.v7.js"></script>
<style type ="text/css">
.linestyle {
fill: none;
stroke: teal;
stroke-width: 5px;
}
</style>
</head>
<body>
<script>
const w = 500;
const h = 400;
const svg = d3.select("svg#noaxes");
const dataset = [ [0, 60], [1, 43], [2, 43], [3, 56],
[4, 45], [5, 62], [6, 49] ];
let xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
.range([0, w]);
let yScale = d3.scaleLinear()
.domain([d3.min(dataset, d => d[1]) - 20,
d3.max(dataset, d => d[1]) + 20])
.range([h, 0]);
const mylinegen = d3.line()
.x(d => xScale(d[0]))
.y(d => yScale(d[1]));
svg.append("path")
.datum(dataset)
.attr("d", mylinegen)
.attr("class", "linestyle");
</script>
</body>
</html>
```
And another example with axes:
<svg id="withaxes" width="600" height="400"></svg>
<script>
svg = d3.select("svg#withaxes")
const margin = {top: 20, right: 50, bottom: 30, left: 50}
const width = +svg.attr("width") - margin.left - margin.right
const height = +svg.attr("height") - margin.top - margin.bottom
const g = svg.append("g").attr("transform", `translate(${margin.left}, ${margin.top})`);
const parseTime = d3.timeParse("%d-%b-%y");
xScale = d3.scaleTime().range([0, width]);
yScale = d3.scaleLinear()
.domain([20, 80])
.range([height, 0]);
const xAxis = d3.axisBottom()
.scale(xScale)
.tickFormat(d3.timeFormat("%m-%d"));
const line = d3.line()
.x(d => xScale(d.date))
.y(d => yScale(d.high));
const data =
[{"date":"1-Apr-18","high":60},
{"date":"2-Apr-18","high":43},
{"date":"3-Apr-18","high":43},
{"date":"4-Apr-18","high":56},
{"date":"5-Apr-18","high":45},
{"date":"6-Apr-18","high":62},
{"date":"7-Apr-18","high":49}];
data.forEach(function(d) {
d.date = parseTime(d.date);
});
xScale
.domain(d3.extent(data, d => d.date));
g.append("g")
.attr("transform", `translate(0, ${height})`)
.call(xAxis);
g.append("g")
.call(d3.axisLeft(yScale))
g.append("path")
.datum(data)
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 1.5)
.attr("d", line);
</script>
``` html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://d3js.org/d3.v7.js"></script>
</head>
<body>
<svg id="withaxes" width="600" height="400"></svg>
<script>
const svg2 = d3.select("svg#withaxes")
const margin = {top: 20, right: 50, bottom: 30, left: 50}
const width = +svg2.attr("width") - margin.left - margin.right
const height = +svg2.attr("height") - margin.top - margin.bottom
const g = svg2.append("g").attr("transform", `translate(${margin.left}, ${margin.top})`);
const parseTime = d3.timeParse("%d-%b-%y");
xScale = d3.scaleTime().range([0, width]);
yScale = d3.scaleLinear()
.domain([20, 80])
.range([height, 0]);
const xAxis = d3.axisBottom()
.scale(xScale)
.tickFormat(d3.timeFormat("%Y-%m-%d"));
const line = d3.line()
.x(d => xScale(d.date))
.y(d => yScale(d.high));
const data =
[{"date":"1-Apr-18","high":60},
{"date":"2-Apr-18","high":43},
{"date":"3-Apr-18","high":43},
{"date":"4-Apr-18","high":56},
{"date":"5-Apr-18","high":45},
{"date":"6-Apr-18","high":62},
{"date":"7-Apr-18","high":49}];
data.forEach(function(d) {
d.date = parseTime(d.date);
});
xScale
.domain(d3.extent(data, d => d.date));
g.append("g")
.attr("transform", `translate(0, ${height})`)
.call(xAxis);
g.append("g")
.call(d3.axisLeft(yScale))
g.append("path")
.datum(data)
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 1.5)
.attr("d", line);
</script>
</body>
</html>
```
(Also uses: `d3.timeParse()` and JavaScript [`Array.foreach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) )
## Additional Resources
[Multiple Time Series in D3](https://medium.com/@ecb2198/multiple-time-series-in-d3-5562b981236c) by Eric Boxer (EDAV 2018)