forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
line.js
392 lines (342 loc) · 14.7 KB
/
line.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
charts.line = function(args) {
this.args = args;
this.init = function(args) {
raw_data_transformation(args);
process_line(args);
init(args);
x_axis(args);
y_axis(args);
return this;
}
this.mainPlot = function() {
var svg = d3.select(args.target + ' svg');
var g;
var data_median = 0;
//main area
var area = d3.svg.area()
.x(args.scalefns.xf)
.y0(args.scales.Y.range()[0])
.y1(args.scalefns.yf)
.interpolate(args.interpolate);
//confidence band
var confidence_area;
if(args.show_confidence_band) {
var confidence_area = d3.svg.area()
.x(args.scalefns.xf)
.y0(function(d) {
var l = args.show_confidence_band[0];
return args.scales.Y(d[l]);
})
.y1(function(d) {
var u = args.show_confidence_band[1];
return args.scales.Y(d[u]);
})
.interpolate(args.interpolate);
}
//main line
var line = d3.svg.line()
.x(args.scalefns.xf)
.y(args.scalefns.yf)
.interpolate(args.interpolate);
//for animating line on first load
var flat_line = d3.svg.line()
.x(args.scalefns.xf)
.y(function() { return args.scales.Y(data_median); })
.interpolate(args.interpolate);
for(var i=args.data.length-1; i>=0; i--) {
//override increment if we have a custom increment series
var line_id = i+1;
if(args.custom_line_color_map.length > 0) {
line_id = args.custom_line_color_map[i];
}
//add confidence band
if(args.show_confidence_band) {
svg.append('path')
.attr('class', 'confidence-band')
.attr('d', confidence_area(args.data[i]));
}
//add the area
if(args.area && !args.y_axis_negative && args.data.length <= 1) {
//if area already exists, transition it
if($(args.target + ' svg path.area' + (line_id) + '-color').length > 0) {
d3.selectAll(args.target + ' svg path.area' + (line_id) + '-color')
.transition()
.duration(function() {
return (args.transition_on_update) ? 1000 : 0;
})
.attr('d', area(args.data[i]));
}
else { //otherwise, add the area
svg.append('path')
.attr('class', 'main-area ' + 'area' + (line_id) + '-color')
.attr('d', area(args.data[i]));
}
}
//add the line, if it already exists, transition the fine gentleman
if($(args.target + ' svg path.line' + (line_id) + '-color').length > 0) {
d3.selectAll(args.target + ' svg path.line' + (line_id) + '-color')
.transition()
.duration(function() {
return (args.transition_on_update) ? 1000 : 0;
})
.attr('d', line(args.data[i]));
}
else { //otherwise...
//if we're animating on load, animate the line from its median value
if(args.animate_on_load) {
data_median = d3.median(args.data[i], function(d) {
return d[args.y_accessor];
})
svg.append('path')
.attr('class', 'main-line ' + 'line' + (line_id) + '-color')
.attr('d', flat_line(args.data[i]))
.transition()
.duration(1000)
.attr('d', line(args.data[i]));
}
else { //or just add the line
svg.append('path')
.attr('class', 'main-line ' + 'line' + (line_id) + '-color')
.attr('d', line(args.data[i]));
}
}
}
return this;
}
this.markers = function() {
markers(args);
return this;
};
this.rollover = function() {
var svg = d3.select(args.target + ' svg');
var g;
//remove the old rollovers if they already exist
if($(args.target + ' svg .transparent-rollover-rect').length > 0) {
$(args.target + ' svg .transparent-rollover-rect').remove();
}
if($(args.target + ' svg .voronoi').length > 0) {
$(args.target + ' svg .voronoi').remove();
}
//rollover text
svg.append('text')
.attr('class', 'active_datapoint')
.attr('xml:space', 'preserve')
.attr('x', args.width - args.right)
.attr('y', args.top / 2)
.attr('text-anchor', 'end');
//append circle
svg.append('circle')
.classed('line_rollover_circle', true)
.attr('cx', 0)
.attr('cy', 0)
.attr('r', 0);
//update our data by setting a unique line id for each series
//increment from 1... unless we have a custom increment series
var line_id = 1;
for(var i=0;i<args.data.length;i++) {
for(var j=0;j<args.data[i].length;j++) {
//if custom line-color map is set, use that instead of line_id
if(args.custom_line_color_map.length > 0) {
args.data[i][j]['line_id'] = args.custom_line_color_map[i];
}
else {
args.data[i][j]['line_id'] = line_id;
}
}
line_id++;
}
//for multi-line, use voronoi
if(args.data.length > 1) {
//main rollover
var voronoi = d3.geom.voronoi()
.x(function(d) { return args.scales.X(d[args.x_accessor]); })
.y(function(d) { return args.scales.Y(d[args.y_accessor]); })
.clipExtent([[args.buffer, args.buffer], [args.width - args.buffer, args.height - args.buffer]]);
var g = svg.append('g')
.attr('class', 'voronoi')
//we'll be using these when constructing the voronoi rollovers
var data_nested = d3.nest()
.key(function(d) { return args.scales.X(d[args.x_accessor]) + "," + args.scales.Y(d[args.y_accessor]); })
.rollup(function(v) { return v[0]; })
.entries(d3.merge(args.data.map(function(d) { return d; })))
.map(function(d) { return d.values; });
//add the voronoi rollovers
g.selectAll('path')
.data(voronoi(data_nested))
.enter()
.append('path')
.attr("d", function(d) { return "M" + d.join("L") + "Z"; })
.datum(function(d) { return d.point; }) //because of d3.nest, reassign d
.attr('class', function(d) {
if(args.linked) {
var v = d[args.x_accessor];
var formatter = d3.time.format('%Y-%m-%d');
return 'line' + d['line_id'] + '-color ' + 'roll_' + formatter(v);
}
else {
return 'line' + d['line_id'] + '-color';
}
})
.on('mouseover', this.rolloverOn(args))
.on('mouseout', this.rolloverOff(args));
}
//for single line, use rects
else {
//set to 1 unless we have a custom increment series
var line_id = 1;
if(args.custom_line_color_map.length > 0) {
line_id = args.custom_line_color_map[0];
}
var g = svg.append('g')
.attr('class', 'transparent-rollover-rect')
var xf = args.data[0].map(args.scalefns.xf);
g.selectAll('.rollover-rects')
.data(args.data[0]).enter()
.append('rect')
.attr('class', function(d, i) {
if(args.linked) {
var v = d[args.x_accessor];
var formatter = d3.time.format('%Y-%m-%d');
//only format when y-axis is date
var id = (typeof v === 'number')
? i
: formatter(v);
return 'line' + line_id + '-color ' + 'roll_' + id;
}
else {
return 'line' + line_id + '-color';
}
})
.attr('x', function(d, i) {
if (i == 0) {
return xf[i];
} else {
return (xf[i-1] + xf[i])/2;
}
})
.attr('y', function(d, i) {
return (args.data.length > 1)
? args.scalefns.yf(d) - 6 //multi-line chart sensitivity
: args.top;
})
.attr('width', function(d, i) {
if (i == 0) {
return (xf[i+1] - xf[i])/2;
} else if (i == xf.length - 1) {
return (xf[i] - xf[i-1])/2;
} else {
return (xf[i+1] - xf[i-1])/2;
}
})
.attr('height', function(d, i) {
return (args.data.length > 1)
? 12 //multi-line chart sensitivity
: args.height - args.bottom - args.top - args.buffer;
})
.attr('opacity', 0)
.on('mouseover', this.rolloverOn(args))
.on('mouseout', this.rolloverOff(args));
}
return this;
}
this.rolloverOn = function(args) {
var svg = d3.select(args.target + ' svg');
var x_formatter = d3.time.format('%Y-%m-%d');
return function(d, i) {
//show circle on mouse-overed rect
svg.selectAll('circle.line_rollover_circle')
.attr('class', "")
.attr('class', 'area' + d['line_id'] + '-color')
.classed('line_rollover_circle', true)
.attr('cx', function() {
return args.scales.X(d[args.x_accessor]);
})
.attr('cy', function() {
return args.scales.Y(d[args.y_accessor]);
})
.attr('r', args.point_size)
.style('opacity', 1);
//trigger mouseover on all rects for this date in .linked charts
if(args.linked && !globals.link) {
globals.link = true;
var v = d[args.x_accessor];
var formatter = d3.time.format('%Y-%m-%d');
//only format when y-axis is date
var id = (typeof v === 'number')
? i
: formatter(v);
//trigger mouseover on matching line in .linked charts
d3.selectAll('.line' + d['line_id'] + '-color.roll_' + id)
.each(function(d, i) {
d3.select(this).on('mouseover')(d,i);
})
}
svg.selectAll('text')
.filter(function(g, j) {
return d == g;
})
.attr('opacity', 0.3);
var fmt = d3.time.format('%b %e, %Y');
if (args.format == 'count') {
var num = function(d_) {
var is_float = d_ % 1 != 0;
var n = d3.format("0,000");
d_ = is_float ? d3.round(d_, args.decimals) : d_;
return n(d_);
}
}
else {
var num = function(d_) {
var fmt_string = (args.decimals ? '.' + args.decimals : '' ) + '%';
var n = d3.format(fmt_string);
return n(d_);
}
}
//update rollover text
if (args.show_rollover_text) {
svg.select('.active_datapoint')
.text(function() {
if(args.time_series) {
var dd = new Date(+d[args.x_accessor]);
dd.setDate(dd.getDate());
return fmt(dd) + ' ' + args.yax_units
+ num(d[args.y_accessor]);
}
else {
return args.x_accessor + ': ' + d[args.x_accessor]
+ ', ' + args.y_accessor + ': ' + args.yax_units
+ num(d[args.y_accessor]);
}
});
}
if(args.rollover_callback) {
args.rollover_callback(d, i);
}
}
}
this.rolloverOff = function(args) {
var svg = d3.select(args.target + ' svg');
return function(d, i) {
if(args.linked && globals.link) {
globals.link = false;
var v = d[args.x_accessor];
var formatter = d3.time.format('%Y-%m-%d');
//only format when y-axis is date
var id = (typeof v === 'number')
? i
: formatter(v);
d3.selectAll('.roll_' + id)
.each(function(d, i){
d3.select(this).on('mouseout')(d);
});
}
//remove active datapoint text on mouse out
svg.selectAll('circle.line_rollover_circle')
.style('opacity', 0);
svg.select('.active_datapoint')
.text('');
}
}
this.init(args);
return this;
}