-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamgraph.js
345 lines (228 loc) · 8.19 KB
/
streamgraph.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
function prepareDataForStreamgraph(d) {
data = deepClone(d);
//add baseline data so no variants are on the egde of the graph
data.unshift({"key" : "keyX", "xyz" : {"A" : 0, "B" : 0, "C" : 0, "D" : 0, "E" : 0, "F" : 0}});
data.push({"key" : "keyX", "xyz" : {"A" : 0, "B" : 0, "C" : 0, "D" : 0, "E" : 0, "F" : 0}});
return data;
}
function renderStreamgraph(outerElement, data) {
var features = Object.keys(data[2].xyz); //get keys from nondummy elements (there for now)
var nFeatures = features.length;
var nVariants = data.length - 2; //subtract dummy elements (there for now)
var element = "masterSVG";
d3.select("#" + element)
.selectAll("*").remove();
d3.select(outerElement)
.append("svg")
.attr("id", element);
element = "#" + element;
var h = $(element).height();
var w = $(element).width();
var flattened = $.map(data, d => d.xyz);
var stacked = d3.stack().keys(features).offset(d3.stackOffsetNone)(flattened);
var tops = $.map(stacked[stacked.length - 1], d => d[1]);
var xScale = d3.scaleLinear()
.domain([0, flattened.length - 1])
.range([0, w])
var yScale = d3.scaleLinear() //gives the height one layer should be
.domain([
d3.min(stacked, function(layer) { return d3.min(layer, function(d) { return d[0]; }); }),
d3.max(stacked, function(layer) { return d3.max(layer, function(d) { return d[1]; }); })
]).range([h - axisSpace, 40]); //leave space for axis //leave space at top?
var lastHTML = "";
var area = d3.area()
.curve(d3.curveCardinal)
.x(function(datum, index) {
var pos = xScale(index);
// if (index == 0) {
// pos = pos + (xScale(1) - pos) / 2;
// } else if (index == nVariants - 1) {
// pos = pos - (pos - xScale(nVariants - 2)) / 2;
// }
return pos;
}).y0( d => yScale(d[0]) )
.y1( d => yScale(d[1]) );
d3.select(element)
.selectAll("path")
.data(stacked)
.enter()
.append("path")
.attr("d", area)
.attr("fill", (d, i) => getColor(i, nFeatures, false) )
.on("click", function(datum, index) {
pathClicks[index]++;
var increasing = pathClicks[index] % 2 == 0;
data = data.slice(1, data.length - 1); //remove empty post values
newData = sortOnKeys(data, ["xyz", datum.key], increasing);
var info = "<span id=\"sortInfo\">" + (increasing ? "increasing" : "decreasing") + "</span>";
var finalHTML = datum.key + info;
$("span#masterText").html(finalHTML);
renderStreamgraph(element, newData);
}).on("mouseover", function(datum, index) {
d3.select(this)
.attr("fill", getColor(index, nFeatures, true));
lastHTML = $("span#masterText").html();
var info = pathClicks[index] == 0 ? "<span id=\"sortInfo\">click to sort</span>" : "";
$("span#masterText").html(datum.key + info);
}).on("mouseout", function(datum, index) {
d3.select(this)
.attr("fill", getColor(index, nFeatures, false));
$("span#masterText").html(lastHTML);
});
d3.select(element)
.append("g")
.attr("class", "xAxis")
.attr("transform", "translate(0," + (h - axisSpace) + ")");
d3.select(".xAxis")
.call(xAxis(xScale, data, element));
// resizeTicks(tops, yScale, h - axisSpace);
setTicks();
raiseText(tops, yScale, h - axisSpace);
addButtons();
drawLinesBetween(data, element, xScale, h - axisSpace);
// haze(element, nVariants + 2); //account for dummy elements with haze
}
function addButtons(height) {
var topMargin = 12;
d3.selectAll(".tick")
.append("circle")
.attr("fill", "white")
.attr("r", function(datum, index) {
return index == 0 || index == data.length - 1 ? "" : 3;
}).attr("cx", 0)
.attr("cy", 0 + topMargin)
.on("click", function(datum, index) {
var siblingLine = d3.select(this.parentNode).select("line"); //set the class on the line (that's what has the id that gives the datum)
siblingLine.classed("selectedForRadar", !siblingLine.classed("selectedForRadar")); //toggle the class
renderRadar();
}).on("mouseover", function(element, index) {
d3.select(this).attr("fill", "orange");
}).on("mouseout", function(element, index) {
d3.select(this).attr("fill", "white");
});
}
function drawLinesBetween(data, element, xScale, realHeight) { //NEW
//Where before vertical lines were being drawn to indicate where a variant was centered,
//effectively covering the important information,
//now draw vertical lines between the variants, focusing attention on the data.
var paths = d3.select(element)
.selectAll("path");
var features = Object.keys(data[2].xyz); //get keys from nondummy elements (there for now)
var nFeatures = features.length;
//find how high the lines should reach
var highestPath = paths.nodes()[nFeatures - 1];
var midPoints = [];
for (var i = 0; i < data.length - 1; i++) {
j = i + 1;
var midX = (xScale(i) + xScale(j)) / 2;
var midY = getHeightAtPointOnPath(midX, highestPath, element, realHeight);
midPoints.push([midX, midY]);
}
d3.select(element)
.selectAll("line.divider")
.data(midPoints)
.enter()
.append("line")
.attr("class", "divider")
.attr("x1", d => d[0])
.attr("x2", d => d[0])
.attr("y1", realHeight)
.attr("y2", d => d[1])
.attr("stroke", "white")
.attr("stroke-width", 1);
}
function getHeightAtPointOnPath(x, path) {
var endLength = path.getTotalLength();
var testLength = 0;
var lengthStep = 5;
var interval = [];
while (true) {
var testPoint = path.getPointAtLength(testLength);
var nextPoint = path.getPointAtLength(testLength + lengthStep);
if (testPoint.x <= x && nextPoint.x >= x) {
interval = [testLength, testLength + lengthStep];
break;
} else {
testLength += lengthStep;
}
}
//binary search in interval for point with x that matches x
var counter = 0;
var length = binarySearch(interval[0], interval[1], x, .01, 100);
var midPoint = path.getPointAtLength(length);
function binarySearch(startLength, endLength, targetX, precision, bailAfter) {
counter++;
var midLength = (startLength + endLength) / 2;
var midX = path.getPointAtLength(midLength).x;
if (counter > bailAfter) {
return midLength;
}
if (Math.abs(midX - targetX) <= precision) {
return midLength;
} else if (midX < targetX) {
return binarySearch(midLength, endLength, targetX, precision)
} else {
return binarySearch(startLength, midLength, targetX, precision);
}
}
return path.getPointAtLength(length).y;
}
function setTicks() {
d3.selectAll(".tick line")
.attr("id", function(datum, index) {
return "axisLine" + index;
});
}
function xAxis(xScale, data, element) {
return d3.axisTop(xScale)
.tickSize(0) //custom resize later
.ticks(data.length)
.tickFormat(function(datum, index) {
return index == 0 || index == data.length - 1 ? "" : data[index].key;
});
}
function resizeTicks(tops, yScale, drawingHeight) {
d3.selectAll("g.xAxis g.tick line")
.attr("y2", function(datum, index) {
if (index == 0 || index == data.length - 1) {
return 0;
}
return -(drawingHeight - yScale(tops[index]));
});
}
function raiseText(tops, yScale, drawingHeight) {
d3.selectAll("g.tick text")
.attr("transform", function(datum, index) {
var textLengthOffset = 25;
var textWidthOffset = 6;
var bar = d3.min(tops, function(item) { return yScale(item); });
console.log(bar)
var dist = (drawingHeight - bar) + textLengthOffset;
// var dist = (drawingHeight - yScale(tops[index])) + textLengthOffset; //to position the text dynamically
if (index == 0 || index == data.length - 1) {
dist = 0;
}
return "rotate(-90) translate(" + dist + "," + textWidthOffset + ")";
}) //rotating also rotates the coordiante system
}
function haze(element, nVariants) {
var buffer = 1;
var h = $(element).height() - axisSpace;
var w = $(element).width() / (nVariants - 1);
var startXs = [...Array(nVariants - 1).keys()];
startXs = $.map(startXs, e => e * w + buffer);
d3.select(element)
.append("g")
.attr("class", "hazeContainer")
.selectAll("rect.haze")
.data(startXs)
.enter()
.append("rect")
.attr("class", "haze")
.attr("x", d => d)
.attr("y", 0)
.attr("width", w - (buffer * 2))
.attr("height", h)
.attr("opacity", .3)
.attr("stroke", "black");
}