-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.html
More file actions
378 lines (333 loc) · 10.3 KB
/
chart.html
File metadata and controls
378 lines (333 loc) · 10.3 KB
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
path.interpolation, path.true-interpolation {
fill: none;
stroke: #000;
stroke-width: 1px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="/colorbrewer.js"></script>
<script>
// These constants define the heatmap pixel coordinate system.
var WIDTH = 60, HEIGHT = 40;
d3.argmin = function(arr, acc){
return _.zip(arr.map(acc), arr).sort(function(a,b){
return d3.ascending(a[0], b[0]);
})[0][1];
};
function lineChart(){
var width_ = 850, height_ = 500,
margin = {top: 20, right: 20, bottom: 30, left: 40};
var xRange = [0, 100, 10],
yRange = [0, 100, 10],
xLabel = 'Sepal Width (cm)',
yLabel = 'Sepal Length (cm)';
var x = d3.scale.linear();
var y = d3.scale.linear();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var userLine = [],
trueLine = [
{x: 0, y: 20},
{x: 10, y: 25},
{x: 20, y: 27},
{x: 30, y: 32},
{x: 40, y: 34},
{x: 50, y: 39},
{x: 60, y: 42},
{x: 70, y: 59},
{x: 80, y: 68},
{x: 90, y: 72},
{x: 100, y: 79}
];
// reserve some variables inside the closure for
// methods that will be defined within chart()
var drawTrueLine, drawHeatmap, hideHeatmap,
drawUserLine, xChoices, heatmap;
heatmap = d3.range(HEIGHT).map(function(){
return d3.range(WIDTH);
});
function chart() {
var width = width_ - margin.left - margin.right,
height = height_ - margin.top - margin.right;
x.domain([xRange[0], xRange[1]])
.range([0, width]);
y.domain([yRange[0], yRange[1]])
.range([height, 0]);
var line = d3.svg.line()
.interpolate('cardinal')
.x(function(d){return x(d.x);})
.y(function(d){return y(d.y);});
xChoices = d3.range.apply(null, xRange);
//user expects fully closed interval rather than half-open
xChoices.push(xRange[1]);
var svg = this.selectAll('svg')
.data([undefined]);
//Create the skeletal chart, if it doesn't already exist.
var gEnter = svg.enter().append("svg").append('g');
gEnter.append('g').attr('class', 'heat-map');
gEnter.append('g').attr('class', 'x axis')
.append('text').attr('class', 'x label');
gEnter.append('g').attr('class', 'y axis')
.append('text').attr('class', 'y label');
gEnter.append('g').attr('class', 'warn-missing');
gEnter.append('rect').attr('class', 'drag-target');
gEnter.append('g').attr('class', 'user-line')
.append('path').attr('class', 'interpolation');
gEnter.append('g').attr('class', 'true-line')
.append('path').attr('class', 'true-interpolation');
svg
.attr("width", width_)
.attr("height", height_);
var g = svg.select('g')
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
flashMissing = function(){
var missingXs = _.difference(xChoices, _.pluck(userLine, 'x'));
if (missingXs.length === 0) return;
var rectWidth = x(1.0) - x(0);
var warningRect = g.select('g.warn-missing')
.selectAll('rect.warning')
.data(missingXs);
warningRect.enter()
.append('rect').attr('class', 'warning');
warningRect.exit().remove();
warningRect
.attr('x', function(d, ix){
if (d === 0){
return x(d);
}
else if (d === xChoices[xChoices.length - 1]){
return x(d) - rectWidth/2;
}
return x(d) - rectWidth/2;
})
.attr('height', height)
.attr('width', function (d, ix){
return d === 0 ? rectWidth/2 :
d === xChoices[xChoices.length - 1] ? rectWidth/2 :
rectWidth;
})
.attr('fill', 'orange')
.attr('fill-opacity', 0.3)
.transition()
.duration(2000)
.attr('fill-opacity', 0)
.each('end', function(){
d3.select(this).remove();
});
};
g.select("g.x.axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.select("text.label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text(xLabel);
g.select("g.y.axis")
.call(yAxis)
.select("text.label")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text(yLabel);
g.select('rect.drag-target')
.attr('width', width)
.attr('height', height)
.style('fill-opacity', 0.0);
drawUserLine = function(){
var dots = g.select('g.user-line').selectAll('.dot')
.data(userLine);
dots.enter().append('circle').attr('class', 'dot');
dots.exit().remove();
dots
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); })
.style("fill", 'tomato');
userLine.sort(function(a,b){
return d3.ascending(a.x, b.x);
});
g.select('path.interpolation')
.attr('d', line(userLine));
};
function setClosestPoint(){
var e = d3.event,
pxX = e.x,
pxY = e.y,
scaleX = x.invert(pxX),
scaleY = Math.max(yRange[0], Math.min(yRange[1], y.invert(pxY))),
snapToX = d3.argmin(xChoices, function(c){
return Math.abs(scaleX - c);
});
// Remove any values with that x choice
userLine = _.reject(userLine, function(d){ return d.x === snapToX; });
// Add the new values
userLine.push({x: snapToX, y:scaleY});
drawUserLine();
}
var drag = d3.behavior.drag()
.on('drag', setClosestPoint)
.on('dragend', flashMissing);
g.select('rect.drag-target').call(drag)
drawTrueLine = function(){
var dots = g.select('g.true-line').selectAll('.dot')
.data(trueLine);
dots.enter().append('circle').attr('class', 'dot');
dots.exit().remove();
dots
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); })
.style("fill", 'steelblue');
g.select('path.true-interpolation')
.attr('d', line(trueLine));
};
drawHeatmap = function(){
/* A brief review of the coordinate systems
we have in play:
- WIDTH x HEIGHT: this is the coordinate
system for the heatmap. It is uniform across
all clients and persists to our database.
- width x height: this is the coordinate
system for the svg pixels.
- xRange x yRange: this coordinate system is
user defined. It is the numbers drawn on the
axes on the page. The server was kind enough
to convert from this system to the heatmap
system, so we can avoid thinking about it.
We have a 2d array in heatmap coords, and we need
to project it into a canvas image in svg coords.
We'll make some scales to do this.
*/
var hm = _.flatten(heatmap).sort(d3.ascending);
var xScale = d3.scale.linear()
.domain([0, WIDTH])
.range([0, width]),
yScale = d3.scale.linear()
.domain([0, HEIGHT])
.range([height, 0]),
color = d3.scale.threshold()
.domain(d3.range(9).map(function(ix){
return d3.quantile(hm, (ix + 1) / 9) + 1;
}))
.range(colorbrewer.Purples[9])
var heatRow = g.select('g.heat-map')
.selectAll('g.heat-row')
.data(heatmap)
.enter()
.append('g').attr('class','heat-row')
.attr('transform', function(d, ix){
return 'translate(0,' + yScale(ix) + ')';
});
heatRow.selectAll('g.heat-cell')
.data(function(d){return d;})
.enter()
.append('g').attr('class', 'heat-cell')
.attr('transform', function(d, ix){
return 'translate(' + xScale(ix) + ',0)';
})
.append('rect')
.attr('y', yScale(1) - yScale(0))
.attr('width', xScale(1) - xScale(0))
.attr('height', yScale(0) - yScale(1))
.attr('fill', color);
};
hideHeatmap = function(){
g.select('g.heat-map').selectAll('g.heat-row')
.data([]).exit().remove();
};
}
//attach public methods;
chart.drawTrueLine = function(){
return drawTrueLine();
};
chart.height = function(_){
if (!arguments.length) return height_;
height_ = _; return chart;
};
chart.width = function(_){
if (!arguments.length) return width_;
width_ = _; return chart;
};
chart.margins = function(changes){
if (!arguments.length) return margin;
_.each(['top','bottom','left','right'], function(k){
if (changes[k]){
margin[k] = changes[k];
}
});
return chart;
};
chart.xRange = function(_){
if (!arguments.length) return xRange;
xRange = _; return chart;
};
chart.yRange = function(_){
if (!arguments.length) return yRange;
yRange = _; return chart;
};
chart.xLabel = function(_){
if (!arguments.length) return xLabel;
xLabel = _; return chart;
};
chart.yLabel = function(_){
if (!arguments.length) return yLabel;
yLabel = _; return chart;
};
chart.userLine = function(_){
if (!arguments.length) return userLine;
userLine = _;
drawUserLine();
return chart;
};
chart.trueLine = function(_){
if (!arguments.length) return trueLine;
trueLine = _; return chart;
};
chart.drawHeatmap = function(){
drawHeatmap();
};
chart.hideHeatmap = function(){
hideHeatmap();
};
chart.isComplete = function(){
return userLine.length === xChoices.length;
};
chart.flashMissing = function(){
return flashMissing();
};
return chart;
}
var theChart = lineChart()
.height(Math.min(500, window.innerHeight))
.width(Math.min(800, window.innerWidth))
.margins({left: 30, top:100})
.xLabel('garbage')
.yLabel('food')
.xRange([0, 20, 1])
.yRange([0, 1, 0.1]);
d3.select('body').call(theChart);
</script>