-
Notifications
You must be signed in to change notification settings - Fork 6
/
gardening_charts.js
434 lines (373 loc) · 15 KB
/
gardening_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
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
var columnInfo = {
"uid": { "index": "user_id", "text": "User ID", "show": false, "scale": "linear"},
"username": { "index": "user_name", "text": "Username", "show": false, "scale": "linear"},
"count-linear": { "index": "count", "text": "Total edited nodes (linear scale)", "show": true, "scale": "linear"},
"count-log": { "index": "count", "text": "Total edited nodes (log scale)", "show": true, "scale": "log"},
"v1count-linear": { "index": "v1count", "text": "Version 1 edits (linear scale)", "show": true, "scale": "linear"},
"v1count-log": { "index": "v1count", "text": "Version 1 edits (log scale)", "show": true, "scale": "log"},
"countlocalness-linear": { "index": "countlocalness", "text": "Edited nodes localness (linear scale)", "show": true, "scale": "linear"},
"v1countlocalness-linear": { "index": "v1countlocalness", "text": "Version 1 edit localness (linear scale)", "show": true, "scale": "linear"},
"v2countlocalness-linear": { "index": "v2countlocalness", "text": "Version 2+ edit localness (linear scale)", "show": true, "scale": "linear"},
"blankcount-linear": { "index": "blankcount", "text": "Blank spot edits (linear scale)", "show": true, "scale": "linear"},
"blankcount-log": { "index": "blankcount", "text": "Blank spot edits (log scale)", "show": true, "scale": "log"},
"v1_ratio-linear": { "index": "v1_ratio", "text": "v1 edits / total edits ratio (linear scale)", "show": true, "scale": "linear"},
"firstedit": { "index": "firstedit", "text": "First edit date", "show": true, "scale": "time"},
"firsteditv1": { "index": "firsteditv1", "text": "First v1 edit date", "show": true, "scale": "time"},
"firsteditblank": { "index": "firsteditblank", "text": "First blank spot edit date", "show": true, "scale": "time"},
"mean_date": { "index": "mean_date", "text": "Mean edit date", "show": true, "scale": "time"},
"mean_date_weighted": { "index": "mean_date_weighted", "text": "Weighted mean edit date", "show": true, "scale": "time"},
"days_active-linear": { "index": "days_active", "text": "Number of days active (linear scale)", "show": true, "scale": "linear"},
"days_active-log": { "index": "days_active", "text": "Number of days active (log scale)", "show": true, "scale": "log"}
};
var indexX = 'count'; // The currently active column for the X axis
var modeX = 'count-log'; // The currently active view for the X axis
var indexY = 'blankcount'; // The currently active column for the Y axis
var modeY = 'blankcount-log'; // The currently active view for the Y axis
var indexR = 'days_active'; // The currently active column for the radius
var modeR = 'days_active'; // The currently active column for the radius
var indexColor = 'place'; // The currently active column for the coloring
// Use mbostock's margin convention from http://bl.ocks.org/mbostock/3019563
var margin = {top: 20, right: 50, bottom: 40, left: 50};
//Width and height
var w = 500 - margin.left - margin.right,
h = 500 - margin.top - margin.bottom;
//For displaying numbers in the axes
var numberFormat = d3.format(",f");
var dateFormat = d3.time.format("%Y-%m-%d");
var xScaleLog = d3.scale.log();
var yScaleLog = d3.scale.log();
var xScaleTime = d3.time.scale();
var yScaleTime = d3.time.scale();
var xScaleLinear = d3.scale.linear();
var yScaleLinear = d3.scale.linear();
var colorScaleOrdinal = d3.scale.category20b();
var xAxis = d3.svg.axis();
var yAxis = d3.svg.axis();
var rScale = d3.scale.sqrt();
var minima = {};
var maxima = {};
var placeControls; // controls for activating and deactivating specific places
var svg,
xa,
ya;
function createScatters(data) {
data.forEach(function(d) {
// convert strings to numbers and dates
d.count = +d.count;
d.v1count = +d.v1count;
d.v1_ratio = d.count > 0 ? d.v1count/d.count : 0;
d.countlocalness = +d.countlocalness;
d.v1countlocalness = +d.v1countlocalness;
d.v2countlocalness = +d.v2countlocalness;
d.blankcount = +d.blankcount;
d.days_active = +d.days_active;
d.uid = +d.uid;
d.firstedit = dateFormat.parse(d.firstedit);
d.firsteditv1 = dateFormat.parse(d.firsteditv1);
d.firsteditblank = dateFormat.parse(d.firsteditblank);
d.mean_date = dateFormat.parse(d.mean_date);
d.mean_date_weighted = dateFormat.parse(d.mean_date_weighted);
});
var keys = d3.keys(data[0]);
for (var i = 0; i < keys.length; i++) {
minima[keys[i]] = d3.min(data, function(d) { return d[keys[i]]; });
maxima[keys[i]] = d3.max(data, function(d) { return d[keys[i]]; });
}
xScaleLog
.domain([1, maxima[indexX]])
.range([0, w]);
yScaleLog
.domain([1, maxima[indexY]])
.range([h, 0]); // Inverted so greater values are at top
xScaleTime
.domain([minima[indexX],maxima[indexX]])
.range([0, w]);
yScaleTime
.domain([minima[indexY],maxima[indexY]])
.range([h, 0]); // Inverted so greater values are at top
xScaleLinear
.domain([0, maxima[indexX]])
.range([0, w]);
yScaleLinear
.domain([0, maxima[indexY]])
.range([h, 0]); // Inverted so greater values are at top
colorScaleOrdinal
.domain(d3.map(data, function(d) { return d[indexColor]; }).values());
//.range(["#af8dc3","#7fbf7b"]); // not needed if using d3.scale.category20()
xAxis
.scale(xScaleLog) // Just for the initial state
.orient("bottom")
.ticks(5, numberFormat); // Show only 5 divisions
// Setting ticks also sets tickFormat
yAxis
.scale(yScaleLog) // Just for the initial state
.orient("left")
.ticks(5, numberFormat); // Show only 5 divisions
// Setting ticks also sets tickFormat
rScale
.domain([0, maxima[indexR]])
.range([1, 10]);
// Append the SVG element. In the html file we create a div,
// bind the data, then use call() to call this function.
// See: http://bost.ocks.org/mike/chart/
svg = d3.select("#chart").append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.top + margin.bottom)
.append("g")// Use mbostock's margin convention from http://bl.ocks.org/mbostock/3019563
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//var tooltip_div = d3.select("body").append("div")
// .attr("class", "tooltip")
// .style("opacity", 0);
var info_div = d3.select("body")
.append("div")
.attr("class", "infobox")
.style("opacity", 0);
var legend_div = d3.select("body")
.append("div")
.attr("class", "legend")
.style("left","500px");
legend_div.selectAll("div")
.data(places) // defined in the other file
.enter()
.append("div")
.style("color", function(d) { return colorScaleOrdinal(d); })
.text(function(d) { return d; });
// Create the menus to control X and Y
var controls = d3.select("body")
.append("div")
.attr("id", "controls");
controls.append("div")
.html("X axis ")
.append("select")
.attr("label", "xaxis")
.attr("id", "xaxis")
.selectAll("option")
// Use entries because columnInfo is an associative array.
// Use filter to only include entries where d.value.show == true
.data(d3.entries(columnInfo).filter(function(d) { return d.value.show; }))
.enter().append("option")
.attr("value", function(d) { return d.key; }) // d.key comes from d3.entries
.text(function(d) { return d.value.text; }) // d.value comes from d3.entries
.each(function(d) {
if (d.key == modeX) d3.select(this).attr("selected", "yes");
});
d3.select("#xaxis")
.on("change", function() { updateX(this.options[this.selectedIndex].value) });
controls.append("div")
.html("Y axis ")
.append("select")
.attr("label", "yaxis")
.attr("id", "yaxis")
.selectAll("option")
// Use entries because columnInfo is an associative array.
// Use filter to only include entries where d.value.show == true
.data(d3.entries(columnInfo).filter(function(d) { return d.value.show; }))
.enter().append("option")
.attr("value", function(d) { return d.key; }) // d.key comes from d3.entries
.text(function(d) { return d.value.text; }) // d.value comes from d3.entries
.each(function(d) {
if (d.key == modeY) d3.select(this).attr("selected", "yes");
});
d3.select("#yaxis")
.on("change", function() { updateY(this.options[this.selectedIndex].value) });
controls.append("div")
.html("Radius ")
.append("select")
.attr("label", "radius")
.attr("id", "radius")
.selectAll("option")
// Use entries because columnInfo is an associative array.
// Use filter to only include entries where d.value.show == true
.data(d3.entries(columnInfo).filter(function(d) { return d.value.show && d.value.scale == 'linear'; }))
.enter().append("option")
.attr("value", function(d) { return d.key; }) // d.key comes from d3.entries
.text(function(d) { return d.value.text; }) // d.value comes from d3.entries
.each(function(d) {
if (d.key == modeR) d3.select(this).attr("selected", "yes");
});
d3.select("#radius")
.on("change", function() { updateR(this.options[this.selectedIndex].value) });
placeControls = controls.append("div")
.selectAll("input")
.data(places)
.enter()
.append("label")
.text(function(d) { return " " + d + ":";})
.insert("input")
.attr("type","checkbox")
.attr("name", function(d) { return d; })
.property("checked", true)
.on("change", function() {
togglePlace(this.name, this.checked);
});
controls.append("div")
.selectAll("input")
.data([true,false])
.enter()
.append("input")
.attr("type","button")
.attr("value",function(d,i) { return ['show all','hide all'][i]; })
.on("click",function(d) {
placeControls.property("checked",d);
return togglePlaces(d);
});
// Add the circles to the plot
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.classed("dotclass", true)
.attr({
cx : function(d) { return xScaleLog(d[indexX] + 1); },
cy : function(d) { return yScaleLog(d[indexY] + 1); },
r : function(d) { return rScale(d[indexR]); },
fill: function(d) { return colorScaleOrdinal(d['place']); },
opacity : 0.5,
})
// Add tooltips on mouseover
// http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
.on("mouseover", function(d) {
//tooltip_div.transition()
// .duration(200)
// .style("opacity", .9);
info_div.transition().duration(200).style("opacity", .9);
//tooltip_div.html(d.username)
// .style("left", xScaleLog(d.count + 1) + "px")
// .style("top", yScaleLog(d.blankcount + 1) + "px");
if (columnInfo[modeX].scale == "time")
x_value_string = dateFormat(d[indexX]);
else
x_value_string = d[indexX];
if (columnInfo[modeY].scale == "time")
y_value_string = dateFormat(d[indexY]);
else
y_value_string = d[indexY];
console.log(d);
info_div.html("User: " + d.user_name + "<br>" + columnInfo[modeX].text + ": " + x_value_string + "<br>" + columnInfo[modeY].text + ": " + y_value_string + "<br>" + columnInfo[modeR].text + ": " + d[indexR] + "<br>" + d["place"]);
}).on("mouseout", function(d) {
//tooltip_div.transition()
// .duration(500)
// .style("opacity", 0);
info_div.transition().duration(500).style("opacity", 0);
});
// Add x axis
xa = svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
// Add x axis label
xa.append("text")
.attr("class", "axis")
.attr("id", "xAxisLabel")
.attr("transform", "translate(" + (w / 2) + "," + 0 + ")")
.attr("dy", "3em")
.style("text-anchor", "middle")
.text(columnInfo[modeX].text);
// Add y axis
ya = svg.append("g")
.attr("class", "axis")
.call(yAxis);
// Add y axis label
ya.append("text")
.attr("class", "axis")
.attr("id", "yAxisLabel")
.attr("transform", "translate(" + 0 + "," + (h / 2) + ")rotate(-90)")
// See http://stackoverflow.com/questions/11252753/rotate-x-axis-text-in-d3
.attr("dy", "-3em")
.style("text-anchor", "middle")
.text(columnInfo[modeY].text);
// TODO: add legend for symbol size
}
function updateX(newX) {
if (newX) modeX = newX;
indexX = columnInfo[modeX].index;
if (columnInfo[modeX].scale == "log") xScale = xScaleLog.domain([1, maxima[indexX]]);
else if (columnInfo[modeX].scale == "time") xScale = xScaleTime.domain([minima[indexX], maxima[indexX]]);
else xScale = xScaleLinear.domain([0, maxima[indexX]]);
if (svg) {
svg.selectAll("circle")
.transition()
.ease("linear")
.duration(1000)
.attr("cx", function(d) {
if (columnInfo[modeX].scale == "log")
return xScale(d[indexX] + 1);
else
return xScale(d[indexX] || 0);
});
}
xAxis.scale(xScale);
if (columnInfo[modeX].scale == "time")
xAxis.ticks(10, numberFormat); // Show 10 divisions for date
else
xAxis.ticks(5);
if (xa) {
xa.call(xAxis);
xa.select("#xAxisLabel")
.text(columnInfo[modeX].text);
}
};
function updateY(newY) {
if (newY) modeY = newY;
indexY = columnInfo[modeY].index;
if (columnInfo[modeY].scale == "log") yScale = yScaleLog.domain([1, maxima[indexY]]);
else if (columnInfo[modeY].scale == "time") yScale = yScaleTime.domain([minima[indexY], maxima[indexY]]);
else yScale = yScaleLinear.domain([0, maxima[indexY]]);
if (svg) {
svg.selectAll("circle")
.transition()
.ease("linear")
.duration(1000)
.attr("cy", function(d) {
if (columnInfo[modeY].scale == "log")
return yScale(d[indexY] + 1);
else
return yScale(d[indexY] || 0);
});
}
yAxis.scale(yScale);
if (columnInfo[modeY].scale == "time")
yAxis.ticks(10, numberFormat); // Show 10 divisions for date
else
yAxis.ticks(5);
if (ya) {
ya.call(yAxis);
ya.select("#yAxisLabel")
.text(columnInfo[modeY].text);
}
};
function updateR(newR) {
if (newR) modeR = newR;
indexR = columnInfo[modeR].index;
rScale.domain([0, maxima[indexR]]);
if (svg) {
svg.selectAll("circle")
.transition()
.ease("linear")
.duration(1000)
.attr("r", function(d) {
return rScale(d[indexR] || 0);
});
}
};
function setX(xstring) {
modeX = xstring;
indexX = columnInfo[modeX].index;
};
function setY(ystring) {
modeY = ystring;
indexY = columnInfo[modeY].index;
};
function setR(rstring) {
modeR = rstring;
indexR = columnInfo[modeR].index;
};
function togglePlace(place, value) {
placeControls
.filter(function(d) { return d == place; })
.property("checked", value);
svg.selectAll(".dotclass").filter(function(d) { return d.place == place; }).style("display",value ? "block" : "none");
}
function togglePlaces(value) {
svg.selectAll(".dotclass").style("display",value ? "block" : "none");
placeControls.property("checked", value);
}