-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.js
336 lines (270 loc) · 9.14 KB
/
charts.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
window.charts = window.charts || {};
let charts = window.charts;
charts.barChart = function (selector) {
let data = [1, 1, 2, 3, 5, 8, 13];
const barHeight = 400;
const barWidth = 36;
const padding = 8;
const margin = 10;
const lineHeight = 20;
const labelHeight = 20;
const barDuration = 300;
const labelDuration = 200;
const barDelay = 20;
const nYears = 25;
let draw = data => {
let width = data.length * (barWidth + padding);
let height = barHeight + 2 * labelHeight;
let scaleY = d3.scaleLinear()
.domain([0, d3.max(data, d => d.patents)])
.range([0, barHeight])
let formatter = d3.formatPrefix('.0', 1e3)
let svg = d3.select(selector).append('svg')
.classed('bar-chart', true)
.attr('width', width + 2 * margin)
.attr('height', height + 2 * margin)
let content = svg.append('g')
.attr('transform', `translate(${margin}, ${margin})`)
content.append('text')
.classed('header', true)
.attr('x', 0)
.attr('y', lineHeight)
.text(`US utility patents, ${data[0].year} to ${data[data.length-1].year}`)
content.append('a')
.attr('href', 'https://www.datazar.com/file/f698590f4-2d24-4de4-918c-a198690bba2a')
.attr('target', '_blank')
.append('text')
.attr('x', 0)
.attr('y', 2 * lineHeight)
.text('Source: Datazar')
let selection = content.selectAll('g.bar-group').data(data);
let groups = selection.enter().append('g')
.classed('bar-group', true)
groups.append('rect')
.classed('bar', true)
.attr('x', (d, i) => i * (barWidth + padding))
.attr('width', barWidth)
.attr('y', d => labelHeight + barHeight)
.attr('height', 0)
.transition()
.delay((d, i) => i * barDelay)
.duration(barDuration)
.attr('y', d => labelHeight + barHeight - scaleY(d.patents))
.attr('height', d => scaleY(d.patents))
groups.append('text')
.classed('label', true)
.attr('x', (d, i) => i * (barWidth + padding) + (barWidth / 2))
.attr('y', height)
.attr('text-anchor', 'middle')
.text(d => d.year)
groups.append('text')
.classed('label', true)
.attr('x', (d, i) => i * (barWidth + padding) + (barWidth / 2))
.attr('y', d => labelHeight + barHeight - scaleY(d.patents) - padding)
.attr('text-anchor', 'middle')
.text(d => formatter(d.patents))
.style('opacity', 0)
.transition()
.delay((d, i) => barDuration - labelDuration + i * barDelay)
.duration(labelDuration)
.style('opacity', 1)
}
let transform = d => ({year: d['Calendar Year'], patents: +d['Utility Patents']})
d3.csv('data/us-patents.csv', transform, data => {
data = data.slice(0, nYears).reverse();
draw(data);
})
}
charts.globe = function (selector) {
const radius = 250;
const diameter = 2 * radius;
const margin = 20;
let svg = d3.select(selector).append('svg')
.classed('globe', true)
.attr('width', diameter + 2 * margin)
.attr('height', diameter + 2 * margin)
let content = svg.append('g')
.attr('transform', `translate(${margin}, ${margin})`)
const url = "https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json";
d3.json(url, data => {
const tilt = -15;
let projection = d3.geoOrthographic()
.translate([radius, radius])
.rotate([0, tilt])
content.append('circle')
.classed('sphere', true)
.attr('cx', radius)
.attr('cy', radius)
.attr('r', radius)
let graticulePath = content.append('path')
.classed('graticule', true)
.datum(d3.geoGraticule())
.attr('d', d3.geoPath(projection))
let countryPath = content.append('path')
.classed('country-path', true)
.datum(data)
.attr('d', d3.geoPath(projection))
let dragStart = null;
let rotStart = null;
svg.on('mousedown', function () {
dragStart = d3.mouse(this);
rotStart = projection.rotate();
});
svg.on('mousemove', function () {
if (!dragStart) return;
let current = d3.mouse(this);
let dx = current[0] - dragStart[0];
let degrees = dx * 0.25;
projection.rotate([rotStart[0] + degrees, tilt]);
graticulePath.attr('d', d3.geoPath(projection));
countryPath.attr('d', d3.geoPath(projection));
})
svg.on('mouseup', function () {
dragStart = null;
rotStart = null;
})
})
}
charts.scatterPlot = function (selector) {
const width = 600;
const height = 600;
const margin = {top: 80, right: 20, bottom: 30, left: 40};
const yearStart = 1950;
const yearEnd = 2008;
let years = d3.range(yearEnd, yearStart - 1, -1);
let svg = d3.select(selector).append('svg')
.classed('scatter-plot', true)
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
let scaleYear = d3.scaleLinear()
.domain([yearStart, yearEnd])
.range([0, width])
let yearAxis = d3.axisTop(scaleYear).tickFormat(d3.format('0'));
let yearSlider = svg.append('g')
.attr('transform', `translate(${margin.left}, 40)`);
yearSlider.call(yearAxis)
let handle = yearSlider.append('circle')
.classed('slider-handle', true)
.attr('cx', 0)
.attr('cy', 0)
.attr('r', 5)
let content = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`)
let scaleX, scaleY;
let setScaleAndAxes = () => {
let allPoints = d3.merge(d3.values(dataByYear).map(byCountry => d3.values(byCountry)));
allPoints = allPoints.filter(d => !isNaN(d.life) && !isNaN(d.gdp));
let lifeValues = allPoints.map(d => d.life);
let gdpValues = allPoints.map(d => d.gdp);
scaleX = d3.scaleLinear()
.domain([d3.min(lifeValues), d3.max(lifeValues)])
.range([0, width])
.nice()
scaleY = d3.scaleLinear()
.domain([d3.min(gdpValues), d3.max(gdpValues)])
.range([height, 0])
.nice()
let xAxis = d3.axisBottom(scaleX);
let yAxis = d3.axisLeft(scaleY).tickFormat(d3.format('$.0s'));
content.append('g')
.attr('transform', `translate(0, ${height})`)
.call(xAxis);
content.append('g').call(yAxis);
}
let lastYearDrawn = null;
let draw = () => {
let year = parseInt(scaleYear.invert(handle.attr('cx')));
if (year === lastYearDrawn) return;
lastYearDrawn = year;
data = d3.values(dataByYear[year]);
data = data.filter(d => !isNaN(d.life) && !isNaN(d.gdp));
let selection = content.selectAll('circle.point')
.data(data);
selection.enter().append('circle')
.classed('point', true)
.attr('cx', d => scaleX(d.life))
.attr('cy', d => scaleY(d.gdp))
.attr('r', 5)
.on('mouseover', function (d, i) {
content.append('text')
.classed('label', true)
.attr('x', scaleX(d.life))
.attr('y', scaleY(d.gdp) - 12)
.attr('text-anchor', 'middle')
.text(d.name)
})
.on('mouseout', function (d, i) {
content.selectAll('text.label').remove();
})
selection.transition()
.attr('cx', d => scaleX(d.life))
.attr('cy', d => scaleY(d.gdp))
selection.exit().remove();
}
let throttledDraw = _.throttle(draw, 100);
handle.call(d3.drag().on('drag', () => {
let x = d3.event.x;
if (x < 0) x = 0;
if (x > width) x = width;
d3.select('.slider-handle').attr('cx', x);
throttledDraw();
}))
// Ultimate format will be like:
//
// dataByYear = {
// '2010': {
// 'Albania': {life: ..., gdp: ...}
// }
// }
let dataByYear = {};
let q = d3.queue();
q.defer(done => {
d3.csv('data/life-expectancy.csv',
row => ({
key: row['Code'],
name: row['Entity'],
year: +row['Year'],
life: +row['Life Expectancy at Birth (both genders)']
}),
data => {
data.forEach(row => {
if (!row.year || !row.name || !row.life) return;
if (row.year < yearStart || row.year > yearEnd) return;
let byYear = dataByYear[row.year];
if (!byYear) byYear = dataByYear[row.year] = {};
let country = byYear[row.name];
if (!country) country = byYear[row.name] = {};
country.name = row.name;
if (!isNaN(row.life)) country.life = row.life;
})
done();
},
)
})
q.defer(done => {
d3.csv('data/gdp.csv', data => {
data.forEach(row => {
let year = +row['Year'];
if (!year || isNaN(year) || year < yearStart || year > yearEnd) return;
let byYear = dataByYear[year];
if (!byYear) byYear = dataByYear[year] = {};
d3.entries(row).forEach(entry => {
if (!entry.key || !entry.value) return;
if (entry.key === 'Year') return;
let name = entry.key.trim();
if (name === 'Qatar' || name === 'Kuwait') return; // These two are GDP outliers
let country = byYear[name];
if (!country) country = byYear[name] = {};
country.name = name;
let gdp = +(entry.value.replace(/,/g, ''));
if (!isNaN(gdp)) country.gdp = gdp;
})
done();
})
})
})
q.await(() => {
setScaleAndAxes();
draw();
})
}