-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
582 lines (522 loc) · 15.1 KB
/
script.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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
const data = {
palestinians: [
{ conversion: 274, date: "2000" },
{ conversion: 465, date: "2001" },
{ conversion: 1042, date: "2002" },
{ conversion: 600, date: "2003" },
{ conversion: 804, date: "2004" },
{ conversion: 264, date: "2005" },
{ conversion: 687, date: "2006" },
{ conversion: 180, date: "2007" },
{ conversion: 3202, date: "2008" },
{ conversion: 7460, date: "2009" },
{ conversion: 1659, date: "2010" },
{ conversion: 2260, date: "2011" },
{ conversion: 4936, date: "2012" },
{ conversion: 3031, date: "2013" },
{ conversion: 19860, date: "2014" },
{ conversion: 14813, date: "2015" },
{ conversion: 3572, date: "2016" },
{ conversion: 8526, date: "2017" },
{ conversion: 11558, date: "2018" },
{ conversion: 15628, date: "2019" },
{ conversion: 2781, date: "2020" },
{ conversion: 260, date: "2021" },
{ conversion: 204, date: "2022" },
{ conversion: 23469, date: "2023" },
{ conversion: 23469, date: "2024" },
],
children: [
{ conversion: 93, date: "2000" },
{ conversion: 98, date: "2001" },
{ conversion: 192, date: "2002" },
{ conversion: 130, date: "2003" },
{ conversion: 162, date: "2004" },
{ conversion: 52, date: "2005" },
{ conversion: 124, date: "2006" },
{ conversion: 50, date: "2007" },
{ conversion: 112, date: "2008" },
{ conversion: 315, date: "2009" },
{ conversion: 8, date: "2010" },
{ conversion: 15, date: "2011" },
{ conversion: 43, date: "2012" },
{ conversion: 5, date: "2013" },
{ conversion: 546, date: "2014" },
{ conversion: 31, date: "2015" },
{ conversion: 35, date: "2016" },
{ conversion: 15, date: "2017" },
{ conversion: 57, date: "2018" },
{ conversion: 28, date: "2019" },
{ conversion: 9, date: "2020" },
{ conversion: 78, date: "2021" },
{ conversion: 44, date: "2022" },
{ conversion: 6024, date: "2023" },
{ conversion: 6024, date: "2024" },
],
israeli: [
{ conversion: 32, date: "2000" },
{ conversion: 184, date: "2001" },
{ conversion: 415, date: "2002" },
{ conversion: 200, date: "2003" },
{ conversion: 110, date: "2004" },
{ conversion: 32, date: "2005" },
{ conversion: 20, date: "2006" },
{ conversion: 12, date: "2007" },
{ conversion: 853, date: "2008" },
{ conversion: 123, date: "2009" },
{ conversion: 185, date: "2010" },
{ conversion: 136, date: "2011" },
{ conversion: 578, date: "2012" },
{ conversion: 157, date: "2013" },
{ conversion: 2796, date: "2014" },
{ conversion: 339, date: "2015" },
{ conversion: 222, date: "2016" },
{ conversion: 174, date: "2017" },
{ conversion: 130, date: "2018" },
{ conversion: 133, date: "2019" },
{ conversion: 61, date: "2020" },
{ conversion: 56, date: "2021" },
{ conversion: 32, date: "2022" },
{ conversion: 1200, date: "2023" },
{ conversion: 1200, date: "2024" },
],
};
const height = 500;
const width = 1000;
var svg = d3.select("svg").attr("width", width).attr("height", height);
/*
Brush & Zoom area chart block to work with mulit-line charts.
Combining d3-brush and d3-zoom to implement Focus + Context.
The focus chart is the main larger one where the zooming occurs.
The context chart is the smaller one below where the brush is used to specify a focused area.
*/
// sets margins for both charts
var focusChartMargin = { top: 20, right: 20, bottom: 170, left: 100 };
var contextChartMargin = { top: 360, right: 20, bottom: 90, left: 100 };
// width of both charts
var chartWidth = width - focusChartMargin.left - focusChartMargin.right;
// height of either chart
var focusChartHeight = height - focusChartMargin.top - focusChartMargin.bottom;
var contextChartHeight =
height - contextChartMargin.top - contextChartMargin.bottom;
// bootstraps the d3 parent selection
svg.append("svg")
.attr("width", chartWidth + focusChartMargin.left + focusChartMargin.right)
.attr(
"height",
focusChartHeight + focusChartMargin.top + focusChartMargin.bottom
)
.append("g")
.attr(
"transform",
"translate(" + focusChartMargin.left + "," + focusChartMargin.top + ")"
)
.attr("overflow", "visible");
// function to parse date field
var parseTime = d3.timeParse("%Y");
// group all dates to get range for x axis later
var dates = [];
// group y axis values (value) of all lines to x axis (key)
var groupValuesByX = {};
for (let key of Object.keys(data)) {
data[key].forEach((bucketRecord) => {
dates.push(parseTime(bucketRecord.date));
!(bucketRecord.date in groupValuesByX) &&
(groupValuesByX[bucketRecord.date] = {}); // if date as key does not exist then create
groupValuesByX[bucketRecord.date][key] = bucketRecord.conversion;
});
}
var availableDates = Object.keys(groupValuesByX);
availableDates.sort(); // sort dates in increasing order
//get max Y axis value by searching for the highest conversion rate
var maxYAxisValue = -Infinity;
for (let key of Object.keys(data)) {
let maxYAxisValuePerBucket = Math.ceil(
d3.max(data[key], (d) => d["conversion"])
);
maxYAxisValue = Math.max(maxYAxisValuePerBucket, maxYAxisValue);
}
// set the height of both y axis
var yFocus = d3.scaleLinear().range([focusChartHeight, 0]);
var yContext = d3.scaleLinear().range([contextChartHeight, 0]);
// set the width of both x axis
var xFocus = d3.scaleTime().range([0, chartWidth]);
var xContext = d3.scaleTime().range([0, chartWidth]);
// create both x axis to be rendered
var xAxisFocus = d3
.axisBottom(xFocus)
.ticks(10)
.tickFormat(d3.timeFormat("%Y"));
var xAxisContext = d3
.axisBottom(xContext)
.ticks(10)
.tickFormat(d3.timeFormat("%Y"));
// create the one y axis to be rendered
var yAxisFocus = d3.axisLeft(yFocus).tickFormat((d) => d);
// build brush
var brush = d3
.brushX()
.extent([
[0, -10],
[chartWidth, contextChartHeight],
])
.on("brush end", brushed);
// build zoom for the focus chart
// as specified in "filter" - zooming in/out can be done by pinching on the trackpad while mouse is over focus chart
// zooming in can also be done by double clicking while mouse is over focus chart
var zoom = d3
.zoom()
.scaleExtent([1, Infinity])
.translateExtent([
[0, 0],
[chartWidth, focusChartHeight],
])
.extent([
[0, 0],
[chartWidth, focusChartHeight],
])
.on("zoom", zoomed)
.filter(
() =>
d3.event.ctrlKey ||
d3.event.type === "dblclick" ||
d3.event.type === "mousedown"
);
// create a line for focus chart
var lineFocus = d3
.line()
.x((d) => xFocus(parseTime(d.date)))
.y((d) => yFocus(d.conversion));
// create line for context chart
var lineContext = d3
.line()
.x((d) => xContext(parseTime(d.date)))
.y((d) => yContext(d.conversion));
// es lint disabled here so react won't warn about not using variable "clip"
/* eslint-disable */
// clip is created so when the focus chart is zoomed in the data lines don't extend past the borders
var clip = svg
.append("defs")
.append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("width", chartWidth)
.attr("height", focusChartHeight)
.attr("x", 0)
.attr("y", 0);
// append the clip
var focusChartLines = svg
.append("g")
.attr("class", "focus")
.attr(
"transform",
"translate(" + focusChartMargin.left + "," + focusChartMargin.top + ")"
)
.attr("clip-path", "url(#clip)");
/* eslint-enable */
// create focus chart
var focus = svg
.append("g")
.attr("class", "focus")
.attr(
"transform",
"translate(" + focusChartMargin.left + "," + focusChartMargin.top + ")"
);
// create context chart
var context = svg
.append("g")
.attr("class", "context")
.attr(
"transform",
"translate(" +
contextChartMargin.left +
"," +
(contextChartMargin.top + 50) +
")"
);
// add data info to axis
xFocus.domain(d3.extent(dates));
yFocus.domain([0, maxYAxisValue]);
xContext.domain(d3.extent(dates));
yContext.domain(yFocus.domain());
// add axis to focus chart
focus
.append("g")
.attr("class", "x-axis")
.attr("transform", "translate(0," + focusChartHeight + ")")
.call(xAxisFocus);
focus.append("g").attr("class", "y-axis").call(yAxisFocus);
// get list of bucket names
var bucketNames = [];
for (let key of Object.keys(data)) {
bucketNames.push(key);
}
// focus chart x label
focus
.append("text")
.attr(
"transform",
"translate(" +
chartWidth / 2 +
" ," +
(focusChartHeight + focusChartMargin.top + 25) +
")"
)
.style("text-anchor", "middle")
.style("font-size", "18px")
.text("Annual Mortality Rates in Palestine (Years)");
// match colors to bucket name
var colors = d3
.scaleOrdinal()
.domain(bucketNames)
.range(["#e74c3c", "#3cab4b", "#3498db", "#73169e", "#2ecc71"]);
// go through data and create/append lines to both charts
for (let key of Object.keys(data)) {
let bucket = data[key];
focusChartLines
.append("path")
.datum(bucket)
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", (d) => colors(key))
.attr("stroke-width", 1.5)
.attr("d", lineFocus);
context
.append("path")
.datum(bucket)
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", (d) => colors(key))
.attr("stroke-width", 1.5)
.attr("d", lineContext);
}
// add x axis to context chart (y axis is not needed)
context
.append("g")
.attr("class", "x-axis")
.attr("transform", "translate(0," + contextChartHeight + ")")
.call(xAxisContext);
// add bush to context chart
var contextBrush = context.append("g").attr("class", "brush").call(brush);
// style brush resize handle
var brushHandlePath = (d) => {
var e = +(d.type === "e"),
x = e ? 1 : -1,
y = contextChartHeight + 10;
return (
"M" +
0.5 * x +
"," +
y +
"A6,6 0 0 " +
e +
" " +
6.5 * x +
"," +
(y + 6) +
"V" +
(2 * y - 6) +
"A6,6 0 0 " +
e +
" " +
0.5 * x +
"," +
2 * y +
"Z" +
"M" +
2.5 * x +
"," +
(y + 8) +
"V" +
(2 * y - 8) +
"M" +
4.5 * x +
"," +
(y + 8) +
"V" +
(2 * y - 8)
);
};
var brushHandle = contextBrush
.selectAll(".handle--custom")
.data([{ type: "w" }, { type: "e" }])
.enter()
.append("path")
.attr("class", "handle--custom")
.attr("stroke", "#000")
.attr("cursor", "ew-resize")
.attr("d", brushHandlePath);
// overlay the zoom area rectangle on top of the focus chart
var rectOverlay = svg
.append("rect")
.attr("cursor", "move")
.attr("fill", "none")
.attr("pointer-events", "all")
.attr("class", "zoom")
.attr("width", chartWidth)
.attr("height", focusChartHeight)
.attr(
"transform",
"translate(" + focusChartMargin.left + "," + focusChartMargin.top + ")"
)
.call(zoom)
.on("mousemove", focusMouseMove)
.on("mouseover", focusMouseOver)
.on("mouseout", focusMouseOut);
var mouseLine = focus
.append("path") // create vertical line to follow mouse
.attr("class", "mouse-line")
.attr("stroke", "#303030")
.attr("stroke-width", 2)
.attr("opacity", "0");
var tooltip = focus
.append("g")
.attr("class", "tooltip-wrapper")
.attr("display", "none");
var tooltipBackground = tooltip.append("rect").attr("fill", "#e8e8e8");
var tooltipText = tooltip.append("text");
contextBrush.call(brush.move, [0, chartWidth]);
// focus chart y label
focus
.append("text")
.attr("text-anchor", "middle")
.attr(
"transform",
"translate(" +
(-focusChartMargin.left + 30) +
"," +
focusChartHeight / 2 +
")rotate(-90)"
)
.style("font-size", "18px")
.text("Deaths Count");
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
tooltip.attr("display", "none");
focus.selectAll(".tooltip-line-circles").remove();
mouseLine.attr("opacity", "0");
var s = d3.event.selection || xContext.range();
xFocus.domain(s.map(xContext.invert, xContext));
focusChartLines.selectAll(".line").attr("d", lineFocus);
focus.select(".x-axis").call(xAxisFocus);
svg.select(".zoom").call(
zoom.transform,
d3.zoomIdentity.scale(chartWidth / (s[1] - s[0])).translate(-s[0], 0)
);
brushHandle
.attr("display", null)
.attr(
"transform",
(d, i) => "translate(" + [s[i], -contextChartHeight - 20] + ")"
);
}
function zoomed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
tooltip.attr("display", "none");
focus.selectAll(".tooltip-line-circles").remove();
mouseLine.attr("opacity", "0");
var t = d3.event.transform;
xFocus.domain(t.rescaleX(xContext).domain());
focusChartLines.selectAll(".line").attr("d", lineFocus);
focus.select(".x-axis").call(xAxisFocus);
var brushSelection = xFocus.range().map(t.invertX, t);
context.select(".brush").call(brush.move, brushSelection);
brushHandle
.attr("display", null)
.attr(
"transform",
(d, i) =>
"translate(" +
[brushSelection[i], -contextChartHeight - 20] +
")"
);
}
function focusMouseMove() {
tooltip.attr("display", null);
var mouse = d3.mouse(this);
var dateOnMouse = `${xFocus.invert(mouse[0]).getFullYear()}-1-1`;
var nearestDateIndex = d3.bisect(availableDates, dateOnMouse.toString());
var d0 = new Date(availableDates[nearestDateIndex - 1]);
var d1 = new Date(availableDates[nearestDateIndex]);
var closestDate;
if (d0 < xFocus.domain()[0]) {
closestDate = availableDates[nearestDateIndex];
} else if (d1 > xFocus.domain()[1]) {
closestDate = availableDates[nearestDateIndex - 1];
} else {
// decide which date is closest to the mouse
closestDate =
dateOnMouse - d0 > d1 - dateOnMouse
? availableDates[nearestDateIndex - 1]
: availableDates[nearestDateIndex];
}
var nearestDateYValues = groupValuesByX[closestDate];
var nearestDateXCord = xFocus(new Date(closestDate));
mouseLine
.attr("d", `M ${nearestDateXCord} 0 V ${focusChartHeight}`)
.attr("opacity", "1");
tooltipText.selectAll(".tooltip-text-line").remove();
focus.selectAll(".tooltip-line-circles").remove();
var formatTime = d3.timeFormat("%Y");
tooltipText
.append("tspan")
.attr("class", "tooltip-text-line")
.attr("x", "5")
.attr("y", "5")
.attr("dy", "13px")
.attr("font-weight", "bold")
.attr("fill", "#303030")
.text(`${closestDate}`);
for (let key of Object.keys(nearestDateYValues)) {
focus
.append("circle")
.attr("class", "tooltip-line-circles")
.attr("r", 5)
.attr("fill", colors(key))
.attr("cx", nearestDateXCord)
.attr("cy", yFocus(nearestDateYValues[key]));
tooltipText
.append("tspan")
.attr("class", "tooltip-text-line")
.attr("x", "5")
.attr("dy", `14px`)
.attr("fill", colors(key))
.text(`${key}: ${nearestDateYValues[key]}`);
}
var tooltipWidth = tooltipText.node().getBBox().width;
var tooltipHeight = tooltipText.node().getBBox().height;
var rectOverlayWidth = rectOverlay.node().getBBox().width;
tooltipBackground
.attr("width", tooltipWidth + 10)
.attr("height", tooltipHeight + 10);
if (nearestDateXCord + tooltipWidth >= rectOverlayWidth) {
tooltip.attr(
"transform",
"translate(" +
(nearestDateXCord - tooltipWidth - 20) +
"," +
mouse[1] +
")"
);
} else {
tooltip.attr(
"transform",
"translate(" + (nearestDateXCord + 10) + "," + mouse[1] + ")"
);
}
}
function focusMouseOver() {
mouseLine.attr("opacity", "1");
tooltip.attr("display", null);
}
function focusMouseOut() {
mouseLine.attr("opacity", "0");
tooltip.attr("display", "none");
focus.selectAll(".tooltip-line-circles").remove();
}
const toggleDarkMode = (e) => {
e.preventDefault();
const body = document.getElementById("body");
body.classList.toggle("dark");
document.getElementById("toggleDarkMode").classList.toggle("active");
};