-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
301 lines (256 loc) · 7.19 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
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
/**
* Compute the range of data with padding
* @param data
* @param variable
* @param percentage
* @returns {*[]}
*/
function extentWithPadding(data, variable, percentage) {
let [min, max] = d3.extent(data, d => d[variable]);
let diff = max - min;
return [min - diff * percentage, max + diff * percentage];
}
// SVG width and height
let svgWidth = 960;
let svgHeight = 600;
// D3 margin convention
let margin = {
top: 40,
right: 40,
bottom: 60,
left: 100
};
// Inner width and height
let width = svgWidth - margin.left - margin.right;
let height = svgHeight - margin.top - margin.bottom;
// D3 tooltip
let tip = d3.tip().attr('class', 'd3-tip').html(d => {
return (`
<div>
<div>${d.Name}</div>
<div>Year: ${d.Year}</div>
<div>Frequency: ${d.Frequency}</div>
<div>Lithography: ${d.Lithography} nm</div>
<div>Price: $${d.Price}</div>
</div>
`);
});
// Variables in data
let variables = {
x: 'Year',
y: 'Price',
size: 'Frequency',
opacity: 'Lithography'
};
let units = {
'Year': '',
'Price': ' ($)',
'Frequency': ' (GHz)',
'Lithography': ' (nm)'
};
// Add 'option' elements for all the 'select' elements
d3.selectAll('.select-variable')
.selectAll('option')
.data(Object.values(variables))
.enter()
.append('option')
.text(d => d);
// Set default variables for x, y, size and opacity
d3.select('#select-x')
.property('value', variables.x);
d3.select('#select-y')
.property('value', variables.y);
d3.select('#select-size')
.property('value', variables.size);
d3.select('#select-opacity')
.property('value', variables.opacity);
// Load data and plot
d3.csv('data.csv', d => {
// Parse CSV records
return {
Name: d['Name'],
Frequency: +d['Frequency'],
Lithography: +d['Lithography'],
Platform: d['Platform'],
Price: +d['Price'],
Year: +d['Year'] + (Math.random() * 2 - 1) * 0.2
};
}, data => {
// For every unique platform, give it an integer
// This is used later to plot CPUs for different platforms in different colors
let platforms = {};
let i = 0;
for (let d of data) {
if (!(d.Platform in platforms)) {
platforms[d.Platform] = i;
i++;
}
}
let nPlatforms = i;
// x, y, size, opacity and color scale
let xScale = d3.scaleLinear()
.domain(extentWithPadding(data, variables.x, 0.05))
.range([0, width]);
let yScale = d3.scaleLinear()
.domain(extentWithPadding(data, variables.y, 0.05))
.range([height, 0]);
let sizeScale = d3.scaleLinear()
.domain(d3.extent(data, d => d[variables.size]))
.range([4, 16]);
let opacityScale = d3.scaleLinear()
.domain(d3.extent(data, d => d[variables.opacity]))
.range([0.4, 1]);
let colorScale = d3.scaleOrdinal()
.domain(Object.keys(platforms))
.range(d3.schemeCategory10.slice(0, nPlatforms));
// x and y axis
let xAxis = d3.axisBottom()
.scale(xScale)
.tickFormat(d3.format('d'));
let yAxis = d3.axisLeft()
.scale(yScale)
.tickFormat(d3.format('d'));
/**
* Change x variable
*/
function changeXVariable() {
// Update the new variable
variables.x = this.value;
// Update data domain
xScale.domain(extentWithPadding(data, variables.x, 0.05));
xAxis.scale(xScale);
// Re-draw the axis
d3.select('#x-axis')
.call(xAxis);
// Update axis label
d3.select('#x-label')
.text(variables.x + units[variables.x]);
// Re-compute data coordinate
d3.selectAll('.data-point')
.attr('cx', d => xScale(d[variables.x]));
}
/**
* Change y variable
*/
function changeYVariable() {
variables.y = this.value;
yScale.domain(extentWithPadding(data, variables.y, 0.05));
yAxis.scale(yScale);
d3.select('#y-axis')
.call(yAxis);
d3.select('#y-label')
.text(variables.y + units[variables.y]);
d3.selectAll('.data-point')
.attr('cy', d => yScale(d[variables.y]));
}
/**
* Change size variable
*/
function changeSize() {
variables.size = this.value;
sizeScale.domain(d3.extent(data, d => d[variables.size]));
d3.selectAll('.data-point')
.attr('r', d => sizeScale(d[variables.size]));
}
/**
* Change opacity variable
*/
function changeOpacity() {
variables.opacity = this.value;
opacityScale.domain(d3.extent(data, d => d[variables.opacity]));
d3.selectAll('.data-point')
.attr('fill-opacity', d => opacityScale(d[variables.opacity]));
}
// Install event listeners
d3.select('#select-x')
.on('change', changeXVariable);
d3.select('#select-y')
.on('change', changeYVariable);
d3.select('#select-size')
.on('change', changeSize);
d3.select('#select-opacity')
.on('change', changeOpacity);
// Create SVG element
let svg = d3.select('body')
.select('svg')
.attr('width', svgWidth)
.attr('height', svgHeight)
.append('g')
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')');
// Create the tooltip
svg.call(tip);
// Add legend
svg.append('g')
.attr('class', 'legend')
.attr('transform', 'translate(25, 20)');
let legend = d3.legendColor()
.scale(colorScale)
.shape('circle')
.shapeRadius(8)
.shapePadding(10)
.on('cellclick', function (platform) {
// Dim other cells
d3.selectAll('.cell')
.style('opacity', 0.1);
d3.select(this)
.style('opacity', 1);
// Hide data points that have a different platform
d3.selectAll('.data-point')
.style('opacity', 0)
.filter(d => d.Platform == platform)
.style('opacity', 1);
});
// When resetting the platform, set normal opacity for cells and data points
d3.select('#reset-platform')
.on('click', () => {
d3.selectAll('.cell')
.style('opacity', 1);
d3.selectAll('.data-point')
.style('opacity', d => opacityScale(d[variables.opacity]));
});
// Draw the legend
svg.select('.legend')
.call(legend);
// Plot x and y axes
svg.append('g')
.attr('class', 'axis')
.attr('id', 'x-axis')
.attr('transform', 'translate(0, ' + height + ')')
.call(xAxis);
svg.append('g')
.attr('class', 'axis')
.attr('id', 'y-axis')
.call(yAxis);
// Plot x and y labels
svg.append('text')
.attr('id', 'x-label')
.attr('x', width / 2)
.attr('y', height + margin.bottom - 10)
.style('text-anchor', 'middle')
.text(variables.x + units[variables.x])
.attr('font-family', 'sans-serif')
.attr('font-size', '16px');
svg.append('text')
.attr('id', 'y-label')
.attr('y', -margin.left + 10)
.attr('x', -height / 2)
.attr('transform', 'rotate(-90)')
.attr('dy', '1em')
.style('text-anchor', 'middle')
.text(variables.y + units[variables.y])
.attr('font-family', 'sans-serif')
.attr('font-size', '16px');
// Plot data points
svg.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('class', 'data-point')
.attr('cx', d => xScale(d[variables.x]))
.attr('cy', d => yScale(d[variables.y]))
.attr('r', d => sizeScale(d[variables.size]))
.attr('fill', d => colorScale(platforms[d.Platform]))
.attr('fill-opacity', d => opacityScale(d[variables.opacity]))
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
});