/
plot.js
675 lines (621 loc) · 23.5 KB
/
plot.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
"use strict"
function observable_plot(configuration){
var that = this;
/* Mode to render observable. These codes determine
* how an observable in a plot is to be rendered.
*/
this.modes = {
LINE : 0, // use dots
DOT : 1, // use a continuious line
HIDDEN : 2, // hide observable
XAXIS : 3, // observable is the x axis
MARKS : 4, // use marks
cycle : function(mode){
return (mode+1) % 3;
},
DEFAULT : 0
};
// enum for tick marks
this.ticks = {
CIRCLE : 0,
PLUS : 1,
CROSS : 2,
DOT : 3,
cycle : function(mode){
return (mode+1) % 3;
},
index : function(index){
return (index+1) % 3;
},
xlink : function(index){
return ["#plot-circle"
,"#plot-plus"
,"#plot-cross"
,"#plot-dot" ][index];
}
};
/* plotDivId - div which plot is rendered on
plotLabelDivId - div to place plot interval
plotStyleId - style sheet for the plot need to export
plot
plotShowLegendCheckboxId - checkbox to toggle plot legend
plotXAxisLogCheckboxId - checkbox to toggle log on x axis
plotYAxisLogCheckboxId - checkbox to toggle log on y axis
*/
this.configuration = configuration;
/* This should be a list of objects of the form
index - there is an assumption of could be duplicated so
an index is used.
label - label of observable
values - all values for a given observable
mode - mode for observable using the mode enumermation
Note : An entry for time is placed with the label
null. This allows an arbitary axis to serve at the
x-axis.
*/
this.state = [];
/* Return the list of labels for values in the plot.
The time axis is filtered out as it is null.
*/
this.getLabels = function(){
return this.state.filter(function(obs){ return obs.label; })
};
/* Given a label return the corresponding label.
*/
this.getObservable = function(index){
return this.state[index];
}
/* Get all time series.
*/
this.getTimeSeries = function(){
return this.state.filter(function(obs)
{ return obs.mode != that.modes.XAXIS });
}
this.getVisible = function(){
return this.state.filter(function(obs)
{ return obs.mode != that.modes.XAXIS
&& obs.mode != that.modes.HIDDEN;
});
}
this.getStatesByMode = function(mode){
return this.state.filter(function(obs){ return obs.mode == mode; })
}
this.timeLabel = "Time";
this.setRawData = function(plot){
that.rawData = plot;
}
this.getRawData = function(){
return that.rawData;
}
/* Update the plot data of the graph. This is called
when new data is to be displayed in a graph. Care
is taken not to reset the state of the plot when
data is updated.
*/
this.setPlot = function(plot){
that.setRawData(plot);
var legend = plot.legend;
/* An update new copy over state preserving settings
* where possible.
*/
var new_state = [];
var time_values = []; // store the times values
/* Initialize new state element with the corresponding
* time series.
*/
legend.forEach(function(legend,i){
var old_observable = that.getObservable(i);
var mode = that.modes.DEFAULT;
if(old_observable && old_observable.label == legend){
mode = old_observable.mode;
};
new_state.push({ label : legend ,
values : [] ,
mode : mode
});
});
// Switch over for the population
/* we need to figure out what mode the time axis is
1. the time values are the last element of the state
array.
2. lets default the time axis to be the x-axis
3. double check it is the time values are what is
selected by checking the label.
4. if it is the x-axis then copy over the
5. check that there is an x-axis
6. if there is not then use the time axis
*/
// not sure what is happening here
var time_observable = that.state.slice(-1)[0]; // 1.
var time_mode = that.modes.XAXIS; // 2.
if(time_observable && time_observable.label == that.timeLabel) // 3.
{ time_mode = time_observable.mode; // 4.
};
if(new_state.every(function(state){ return state.mode != that.modes.XAXIS; })){ // 5.
time_mode = that.modes.XAXIS; // 6.
}
that.state = new_state;
var first_time = null;
var last_time = null;
//that.start_time = null;
//that.end_time = null;
// Populate timeSeries from data.
plot.timeSeries.forEach(function(observable){
first_time = first_time || observable.time;
last_time = observable.time;
//that.start_time = that.start_time || observable.time;
//that.end_time = observable.time;
time_values.push(observable.time);
var values = observable.values;
that.state.forEach(function(state_observable,i){
var current = values[i];
state_observable.values.push(current);
});
});
if (first_time !== null && last_time !== null){
that.start_time = Math.min(first_time,last_time);
that.end_time = Math.max(first_time,last_time);
} else {
that.start_time = null;
that.end_time = null;
}
// Add time axis
that.state.push({ label : this.timeLabel ,
values : time_values ,
mode : time_mode
});
// setup colors
var color = d3.scaleOrdinal(d3.schemeCategory10);
color.domain(that.state.map(function(c,i){ return i; }));
that.state.forEach(function(s,i){ s.color = color(i);
s.tick = that.ticks.index(i);
});
that.render();
};
this.setPlot = wrap(this.setPlot);
this.formatTime = d3.format(".02f");
this.getXAxis = function(){
return this.state.find(function(state){ state.mode = that.modes.XAXIS });
}
this.setPlot = wrap(this.setPlot);
this.renderPlot = function(){
// set margin
var margin = {top: 20, right: 80, bottom: 30, left: 80},
dimensions = that.getDimensions() ,
width = dimensions.width - margin.left - margin.right,
height = dimensions.height - margin.top - margin.bottom;
// setup x-axis
var x = (that.getXAxisLog()?d3.scaleLog().clamp(true):d3.scaleLinear()).range([0, width]);
var xState = that.getStatesByMode(that.modes.XAXIS)[0];
/* Get bounds , make sure to filter out nan's caused by log
(in particular that of time equal zero).
*/
var x_bounds = [d3.min(xState
.values
.filter(function(d)
{ return !that.getXAxisLog() || d > 0; })),
d3.max(xState
.values
.filter(function(d)
{ return !that.getXAxisLog() || d > 0; }))];
x.domain(x_bounds);
var xAxis = d3.axisBottom(x);
// setup y-axis
var y = (that.getYAxisLog()?
d3.scaleLog().clamp(true):
d3.scaleLinear()).range([height, 0]);
var yAxis = d3.axisLeft(y)
.tickFormat(d3.format(".3n"));
var visible = that.getVisible();
var y_bounds = [d3.min(visible,
function(c)
{ return d3.min(c.values.filter(function(d)
{return !that.getYAxisLog() || d > 0; }));
}),
d3.max(visible,function(c) { return d3.max(c.values); })
];
y.domain(y_bounds);
// Clear div first
d3.select("#"+that.configuration.plotDivId).html("");
// Add svg element
var svg = d3.select("#"+that.configuration.plotDivId).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var svgDefs = createSVGDefs(svg);
// defs
/* Here the defs for the tick marks it was taken from
an earlier implementation of plots.
*/
// defs - plus
svg.append("defs")
.append("g")
.attr("id","plot-plus")
.append("path")
.attr("d","M-3.5,0 h7 M0,-3.5 v7")
.style("stroke", "currentColor");
// defs - cross
svg.append('defs')
.append("g")
.attr("id","plot-cross")
.append("path")
.attr("d","M-3.5,-3.5 L3.5,3.5 M3.5,-3.5 L-3.5,3.5")
.style("stroke", "currentColor");
// defs - circles
svg.append("defs")
.append("g")
.attr("id","plot-circle")
.append("circle")
.attr("r", 1.5)
.attr("fill","none")
.style("stroke", "currentColor");
// defs - dots
svg.append("defs")
.append("g")
.attr("id","plot-dot")
.append("circle")
.attr("r", 0.5)
.attr("fill","none")
.style("stroke", "currentColor");
svg = svg.append("g")
.attr("transform"
,"translate(" + margin.left + "," + margin.top + ")");
// draw axis
svg.append("g")
.attr( "class"
, "x plot-axis")
.attr("transform"
,"translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class","y plot-axis")
.call(yAxis)
.append("text")
.attr("transform"
,"rotate(-90)");
// render data
var line = d3.line()
.curve(d3.curveLinear)
.defined(function(d,i) { return !that.getXAxisLog() || xState.values[i] > 0; })
.defined(function(d,i) { return !isNaN(d) && (!that.getYAxisLog() || d > 0); })
.x(function(d,i) {
var value = xState.values[i];
value = x(value);
return value; })
.y(function(d) {
var value = d;
return y(value); });
// helper function to map values to screen coordinates to
var xMap = function(d,i){
var current = xState.values[i];
current = x(current);
return current;
};
var yMap = function(d) {
var current = d;
current = y(current);
return current;
};
var timeSeries =
svg.selectAll(".observable")
.data(that.getTimeSeries())
.enter().append("g")
.attr("class", "plot-observable")
.each(function(d)
{ switch(d.mode) {
case that.modes.DOT:
var values = d.values
.filter(function(d,i)
{ var x = xMap(d,i);
var y = yMap(d);
return (!isNaN(x) && !isNaN(y)) });
var tick =
d3.select(this)
.selectAll(".plot-tick")
.data(values)
.enter()
.append("g")
.attr("class", "plot-tick")
.attr("fill",d.color);
// add link to definitions
tick.append("use")
.attr("xlink:href",that.ticks.xlink(that.ticks.DOT))
.style("color",d.color)
.attr("transform"
,function(d,i)
{ var x = xMap(d,i);
var y = yMap(d);
var t = "translate(" + x + "," + y + ")";
return t;
});
break;
case that.modes.MARKS:
var values = d.values
.filter(function(d,i)
{ var x = xMap(d,i);
var y = yMap(d);
return (!isNaN(x) && !isNaN(y)) });
var tick =
d3.select(this)
.selectAll(".plot-tick")
.data(values)
.enter()
.append("g")
.attr("class", "plot-tick")
.attr("fill",d.color);
// add link to definitions
tick.append("use")
.attr("xlink:href",that.ticks.xlink(d.tick))
.style("color",d.color)
.attr("transform"
,function(d,i){ var x = xMap(d,i);
var y = yMap(d);
var t = "translate(" + x + "," + y + ")";
return t;
});
break;
case that.modes.LINE:
d3.select(this)
.append("path")
.attr("class", "plot-line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return d.color; });
break;
case that.modes.HIDDEN:
break;
case that.modes.XAXIS:
break;
default:
break;
} });
// render legend
var legendRectSize = 12;
var legendSpacing = 4;
var legend = svg.selectAll('.legend')
.data(that.getTimeSeries())
.enter()
.append('g')
.attr('class', 'plot-legend')
.attr('transform',
function(d, i) {
var height = legendRectSize + legendSpacing;
var offset = 0;
var horz = legendRectSize;
var vert = i * height - offset;
return 'translate(' + horz + ',' + vert + ')';
});
// cycle through styles
var cycle = function(d,i)
{ d.mode = that.modes.cycle(d.mode);
that.renderPlot();
};
// legend swatches
legend.append('rect')
.attr("class", "plot-legend-swatch")
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill',
function(d){
var color = d.color;
if(that.modes.HIDDEN == d.mode){
color = "white";
};
return color;
})
.style('stroke',
function(d){
var color = d.color;
if(that.modes.HIDDEN == d.mode){
color = "white";
};
return color;
}) // handle clicking on legend
.style('opacity',
function(d){
var opacity = 1.0;
if(that.modes.HIDDEN == d.mode){
opacity = 0.0;
};
return opacity;
})
.style('stroke-opacity',
function(d){
var opacity = 1.0;
if(that.modes.HIDDEN == d.mode){
opacity = 0.0;
};
return opacity;
}) // handle clicking on legend
.on('click', cycle);
// legend text
legend.append('text')
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize - legendSpacing)
.text(function(d) { return d.label; })
.on('click', cycle);
}
this.renderPlot = wrap(this.renderPlot);
/* add select for the x-axis */
this.setupAxisSelect = function(){
var that = this;
/* check for the div to add the axis */
if(that.configuration.plotDivAxisSelectId){
// Clear div first
that.divAxisSelect = d3.select("#"+configuration.plotDivAxisSelectId);
that.divAxisSelect.html("");
/* Handler to update when the select changes */
var changeHandler = function(){
var index = parseInt(this.options[this.selectedIndex].value);
assert(typeof index !== 'undefined',"plot selection invalid");
that.state.forEach(function(state,i){
if(state.mode == that.modes.XAXIS){
state.mode = that.modes.DEFAULT;
}
if(i == index){
state.mode = that.modes.XAXIS;
}
});
// re-render plot
that.renderPlot();
}
that.divAxisSelect = that.divAxisSelect
.append("select")
.attr("class","form-control")
.on("change", changeHandler);
that.divAxisSelect
.selectAll('option')
.data([])
.enter()
.append('option');
}
}
this.setupAxisSelect = wrap(this.setupAxisSelect);
this.updateAxisSelect = function(){
that.divAxisSelect
.selectAll('option')
.remove();
that.divAxisSelect
.selectAll('option')
.data(that.state)
.enter().append("option")
.attr("value",function(d,i) { return i; })
.text(function(d) { return d.label; });
that.divAxisSelect
.selectAll('option')
.filter(function(d){ return d.mode == that.modes.XAXIS; })
.attr("selected","true");
}
this.updateAxisSelect = wrap(this.updateAxisSelect);
this.renderAxisSelect = function(){
var reRender = true;
if(that.select){
var oldState = that.select.selectAll('option').data();
var oldLabels = oldState.map(function(option){ return option.label; });
var newLabels = that.state.map(function(observable){ return observable.label; });
reRender = !is_same(oldLabels,newLabels);
}
if (reRender){
/* check for the div to add the axis */
if(that.configuration.plotDivAxisSelectId){
// Clear div first
var divAxisSelect = d3.select("#"+configuration.plotDivAxisSelectId);
divAxisSelect.html("");
/* Handler to update when the select changes */
var changeHandler = function(){
var index = parseInt(this.options[this.selectedIndex].value);
assert(typeof index !== 'undefined',"plot selection invalid");
that.state.forEach(function(state,i){
if(state.mode == that.modes.XAXIS){
state.mode = that.modes.DEFAULT;
}
if(i == index){
state.mode = that.modes.XAXIS;
}
});
// re-render plot
that.renderPlot();
}
that.select = divAxisSelect.append("select")
.attr("class","form-control")
.on("change", changeHandler);
that.select.selectAll('option')
.data(that.state)
.enter().append("option")
.attr("value",function(d,i) { return i; })
.text(function(d) { return d.label; });
that.select.selectAll('option')
.filter(function(d){ return d.mode == that.modes.XAXIS; })
.attr("selected","true");
}
}
}
this.renderAxisSelect = wrap(this.renderAxisSelect);
/* add label for plot */
this.renderLabel = function(){
var that = this;
if(that.configuration.plotLabelDivId){
if (that.start_time !== null){
var label =
"Plot between t = "
+that.formatTime(that.start_time)
+"s and t = "
+that.formatTime(that.end_time)
+"s";
d3.select("#"+configuration.plotLabelDivId)
.html(label);
}
}
}
this.showLegend = true;
this.setShowLegend = function(showLegend){
that.showLegend = showLegend;
}
this.getShowLegend = function(){
return that.showLegend;
}
this.xAxisLog = false;
this.setXAxisLog = function(xAxisLog){
that.xAxisLog = xAxisLog;
}
this.getXAxisLog = function(){
return that.xAxisLog;
}
this.yAxisLog = false;
this.setYAxisLog = function(yAxisLog){
that.yAxisLog = yAxisLog;
}
this.getYAxisLog = function(){
return that.yAxisLog;
}
this.dimensions = { width : 960 , height : 500 };
this.setDimensions = function(dimensions) {
that.dimensions = dimensions;
}
this.getDimensions = function(){
return that.dimensions;
}
/* define how to export to tsv */
this.plotName = "kappa-plot";
this.getPlotName = function(suffix){
var filename = that.plotName;
filename = (filename.indexOf('.') === -1)?filename+suffix:filename;
return filename;
}
this.setPlotName = function(plotName){ that.plotName = plotName; }
this.handlePlotTSV = function(){
var rawData = that.getRawData();
var timeSeries = rawData.timeSeries;
var legend = rawData.legend;
var header = legend.join("\t");
var body = timeSeries.map(function(d)
{ var row = [d["time"]];
row = row.concat(d["values"]);
return row.join("\t") })
.join("\n");
var tsv = header+"\n"+body;
saveFile(tsv,"text/tab-separated-values",that.getPlotName(".tsv"));
}
this.handlePlotTSV = wrap(this.handlePlotTSV);
/* add checkbox handlers for display options */
this.addHandlers = function(){
function checkboxHandler(id,setter){
if(id){
setter(document.getElementById(id).checked);
var handler = function(){ setter(document.getElementById(id).checked);
that.render();};
d3.select("#"+id).on("change",handler);
}
}
checkboxHandler(configuration.plotShowLegendCheckboxId,that.setShowLegend);
checkboxHandler(configuration.plotXAxisLogCheckboxId,that.setXAxisLog);
checkboxHandler(configuration.plotYAxisLogCheckboxId,that.setYAxisLog);
}
/* render plot */
this.render = function(){
that.renderPlot();
that.renderLabel();
that.updateAxisSelect();
that.renderAxisSelect();
};
this.addHandlers();
this.setupAxisSelect();
}