-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
159 lines (138 loc) · 4.04 KB
/
index.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
import { seriesSvgAnnotation } from "./annotation-series.js";
import {
distance,
trunc,
hashCode,
webglColor,
iterateElements
} from "./util.js";
let data = [];
let quadtree;
const createAnnotationData = datapoint => ({
note: {
label: datapoint.first_author_name + " " + datapoint.year,
bgPadding: 5,
title: trunc(datapoint.title, 100)
},
x: datapoint.x,
y: datapoint.y,
dx: 20,
dy: 20
});
// create a web worker that streams the chart data
const streamingLoaderWorker = new Worker("streaming-tsv-parser.js");
streamingLoaderWorker.onmessage = ({
data: { items, totalBytes, finished }
}) => {
const rows = items
.map(d => ({
...d,
x: Number(d.x),
y: Number(d.y),
year: Number(d.date)
}))
.filter(d => d.year);
data = data.concat(rows);
if (finished) {
document.getElementById("loading").style.display = "none";
// compute the fill color for each datapoint
const languageFill = d =>
webglColor(languageColorScale(hashCode(d.language) % 10));
const yearFill = d => webglColor(yearColorScale(d.year));
const fillColor = fc.webglFillColor().value(languageFill).data(data);
pointSeries.decorate(program => fillColor(program));
// wire up the fill color selector
iterateElements(".controls a", el => {
el.addEventListener("click", () => {
iterateElements(".controls a", el2 => el2.classList.remove("active"));
el.classList.add("active");
fillColor.value(el.id === "language" ? languageFill : yearFill);
redraw();
});
});
// create a spatial index for rapidly finding the closest datapoint
quadtree = d3
.quadtree()
.x(d => d.x)
.y(d => d.y)
.addAll(data);
}
redraw();
};
streamingLoaderWorker.postMessage("data.tsv");
const languageColorScale = d3.scaleOrdinal(d3.schemeCategory10);
const yearColorScale = d3
.scaleSequential()
.domain([1850, 2000])
.interpolator(d3.interpolateRdYlGn);
const xScale = d3.scaleLinear().domain([-50, 50]);
const yScale = d3.scaleLinear().domain([-50, 50]);
const xScaleOriginal = xScale.copy();
const yScaleOriginal = yScale.copy();
const pointSeries = fc
.seriesWebglPoint()
.equals((a, b) => a === b)
.size(1)
.crossValue(d => d.x)
.mainValue(d => d.y);
const zoom = d3
.zoom()
.scaleExtent([0.8, 10])
.on("zoom", () => {
// update the scales based on current zoom
xScale.domain(d3.event.transform.rescaleX(xScaleOriginal).domain());
yScale.domain(d3.event.transform.rescaleY(yScaleOriginal).domain());
redraw();
});
const annotations = [];
const pointer = fc.pointer().on("point", ([coord]) => {
annotations.pop();
if (!coord || !quadtree) {
return;
}
// find the closes datapoint to the pointer
const x = xScale.invert(coord.x);
const y = yScale.invert(coord.y);
const radius = Math.abs(xScale.invert(coord.x) - xScale.invert(coord.x - 20));
const closestDatum = quadtree.find(x, y, radius);
// if the closest point is within 20 pixels, show the annotation
if (closestDatum) {
annotations[0] = createAnnotationData(closestDatum);
}
redraw();
});
const annotationSeries = seriesSvgAnnotation()
.notePadding(15)
.type(d3.annotationCallout);
const chart = fc
.chartCartesian(xScale, yScale)
.webglPlotArea(
// only render the point series on the WebGL layer
fc
.seriesWebglMulti()
.series([pointSeries])
.mapping(d => d.data)
)
.svgPlotArea(
// only render the annotations series on the SVG layer
fc
.seriesSvgMulti()
.series([annotationSeries])
.mapping(d => d.annotations)
)
.decorate(sel =>
sel
.enter()
.select("d3fc-svg.plot-area")
.on("measure.range", () => {
xScaleOriginal.range([0, d3.event.detail.width]);
yScaleOriginal.range([d3.event.detail.height, 0]);
})
.call(zoom)
.call(pointer)
);
// render the chart with the required data
// Enqueues a redraw to occur on the next animation frame
const redraw = () => {
d3.select("#chart").datum({ annotations, data }).call(chart);
};