-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathvz-line-chart.ts
1230 lines (1113 loc) · 42 KB
/
vz-line-chart.ts
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
namespace vz_line_chart {
/**
* An interface that describes a fill area to visualize. The fill area is
* visualized with a less intense version of the color for a given series.
*/
export interface FillArea {
// The lower end of the fill area.
lowerAccessor: Plottable.IAccessor<number>;
// The higher end of the fill area.
higherAccessor: Plottable.IAccessor<number>;
}
/**
* The maximum number of marker symbols within any line for a data series. Too
* many markers clutter the chart.
*/
const _MAX_MARKERS = 20;
Polymer({
is: 'vz-line-chart',
properties: {
/**
* Scale that maps series names to colors. The default colors are from
* d3.schemeCategory10. Use this property to replace the default line
* colors with colors of your own choice.
* @type {Plottable.Scales.Color}
* @required
*/
colorScale: {
type: Object,
value: function() {
return new Plottable.Scales.Color().range(d3.schemeCategory10);
}
},
/**
* A function that takes a data series string and returns a
* Plottable.SymbolFactory to use for rendering that series. This property
* implements the vz_chart_helpers.SymbolFn interface.
*/
symbolFunction: Object,
/**
* Whether smoothing is enabled or not. If true, smoothed lines will be
* plotted in the chart while the unsmoothed lines will be ghosted in
* the background.
*/
smoothingEnabled: {
type: Boolean,
notify: true,
value: false,
},
/**
* Weight (between 0.0 and 1.0) of the smoothing. A value of 0.0
* means very little smoothing, possibly no smoothing at all. A
* value of 1.0 means a whole lot of smoothing, possibly so much as
* to make the whole plot appear as a constant function.
*
* Has no effect when `smoothingEnabled` is `false`.
*/
smoothingWeight: {type: Number, value: 0.6},
/**
* This is a helper field for automatically generating commonly used
* functions for xComponentsCreationMethod. Valid values are what can
* be processed by vz_chart_helpers.getXComponents() and include
* "step", "wall_time", and "relative".
*/
xType: {type: String, value: ''},
/**
* We accept a function for creating an XComponents object instead of such
* an object itself because the Axis must be made right when we make the
* LineChart object, lest we use a previously destroyed Axis. See the async
* logic below that uses this property.
*
* Note that this function returns a function because polymer calls the
* outer function to compute the value. We actually want the value of this
* property to be the inner function.
*
* @type {function(): vz_chart_helpers.XComponents}
*/
xComponentsCreationMethod: {
type: Object,
/* Note: We have to provide a nonsense value for
* xComponentsCreationMethod here, because Polymer observers only
* trigger after all parameters are set. */
value: ''
},
/**
* A formatter for values along the X-axis. Optional. Defaults to a
* reasonable formatter.
*
* @type {function(number): string}
*/
xAxisFormatter: Object,
/**
* A formatter for values along the Y-axis. Optional. Defaults to a
* reasonable formatter.
*
* @type {function(number): string}
*/
yAxisFormatter: Object,
/**
* A method that implements the Plottable.IAccessor<number> interface. Used
* for accessing the y value from a data point.
*
* Note that this function returns a function because polymer calls the
* outer function to compute the value. We actually want the value of this
* property to be the inner function.
*/
yValueAccessor: {type: Object, value: () => (d => d.scalar)},
/**
* An array of ChartHelper.TooltipColumn objects. Used to populate the table
* within the tooltip. The table contains 1 row per run.
*
* Note that this function returns a function because polymer calls the
* outer function to compute the value. We actually want the value of this
* property to be the inner function.
*
*/
tooltipColumns: {
type: Array,
value: function() {
const valueFormatter = vz_chart_helpers.multiscaleFormatter(
vz_chart_helpers.Y_TOOLTIP_FORMATTER_PRECISION);
const formatValueOrNaN = (x) => isNaN(x) ? 'NaN' : valueFormatter(x);
return [
{
title: 'Name',
evaluate: (d) => d.dataset.metadata().name,
},
{
title: 'Smoothed',
evaluate: (d, statusObject) => {
const smoothingEnabled =
statusObject && statusObject.smoothingEnabled;
return formatValueOrNaN(
smoothingEnabled ? d.datum.smoothed : d.datum.scalar);
},
},
{
title: 'Value',
evaluate: (d) => formatValueOrNaN(d.datum.scalar),
},
{
title: 'Step',
evaluate: (d) => vz_chart_helpers.stepFormatter(
d.datum.step),
},
{
title: 'Time',
evaluate: (d) => vz_chart_helpers.timeFormatter(
d.datum.wall_time),
},
{
title: 'Relative',
evaluate: (d) => vz_chart_helpers.relativeFormatter(
vz_chart_helpers.relativeAccessor(d.datum, -1, d.dataset)),
},
];
}
},
/**
* An optional FillArea object. If provided, the chart will
* visualize fill area alongside the primary line for each series. If set,
* consider setting ignoreYOutliers to false. Otherwise, outlier
* calculations may deem some margins to be outliers, and some portions of
* the fill area may not display.
*/
fillArea: Object,
/**
* An optional array of 2 numbers for the min and max of the default range
* of the Y axis. If not provided, a reasonable range will be generated.
* This property is a list instead of 2 individual properties to emphasize
* that both the min and the max must be specified (or neither at all).
*/
defaultXRange: Array,
/**
* An optional array of 2 numbers for the min and max of the default range
* of the Y axis. If not provided, a reasonable range will be generated.
* This property is a list instead of 2 individual properties to emphasize
* that both the min and the max must be specified (or neither at all).
*/
defaultYRange: Array,
/**
* Tooltip header innerHTML text. We cannot use a dom-repeat inside of a
* table element because Polymer does not support that. This seems like
* a bug in Polymer. Hence, we manually generate the HTML for creating a row
* of table headers.
*/
_tooltipTableHeaderHtml: {
type: String,
computed: "_computeTooltipTableHeaderHtml(tooltipColumns)",
},
/**
* The scale for the y-axis. Allows:
* - "linear" - linear scale (Plottable.Scales.Linear)
* - "log" - modified-log scale (Plottable.Scales.ModifiedLog)
*/
yScaleType: {type: String, value: 'linear'},
/**
* Whether to ignore outlier data when computing the yScale domain.
*/
ignoreYOutliers: {
type: Boolean,
value: false,
},
/**
* Change how the tooltip is sorted. Allows:
* - "default" - Sort the tooltip by input order.
* - "ascending" - Sort the tooltip by ascending value.
* - "descending" - Sort the tooltip by descending value.
* - "nearest" - Sort the tooltip by closest to cursor.
*/
tooltipSortingMethod: {type: String, value: 'default'},
/**
* Change how the tooltip is positioned. Allows:
* - "bottom" - Position the tooltip on the bottom of the chart.
* - "right" - Position the tooltip to the right of the chart.
*/
tooltipPosition: {type: String, value: 'bottom'},
/**
* A list of series for which to not show tooltips. Optional,
* defaults to the empty list.
*/
seriesWithoutTooltips: {
type: Array,
value: () => ([])
},
_attached: Boolean,
_chart: Object,
_visibleSeriesCache: {
type: Array,
value: function() {
return []
}
},
_seriesDataCache: {
type: Object,
value: function() {
return {}
}
},
_makeChartAsyncCallbackId: {type: Number, value: null},
},
observers: [
'_makeChart(xComponentsCreationMethod, xType, yValueAccessor, yScaleType, tooltipColumns, colorScale, seriesWithoutTooltips, _attached)',
'_reloadFromCache(_chart)',
'_smoothingChanged(smoothingEnabled, smoothingWeight, _chart)',
'_tooltipSortingMethodChanged(tooltipSortingMethod, _chart)',
'_tooltipPositionChanged(tooltipPosition, _chart)',
'_outliersChanged(ignoreYOutliers, _chart)'
],
/**
* Sets the series that the chart displays. Series with other names will
* not be displayed.
*
* @param {Array<String>} names Array with the names of the series to
* display.
*/
setVisibleSeries: function(names) {
this._visibleSeriesCache = names;
if (this._chart) {
this._chart.setVisibleSeries(names);
this.redraw();
}
},
/**
* Sets the data of one of the series. Note that to display this series
* its name must be in the setVisibleSeries() array.
*
* @param {string} name Name of the series.
* @param {Array< vz_chart_helpers.ScalarDatum>} data Data of the series.
* This is an array of objects with at least the following properties:
* - step: (Number) - index of the datum.
* - wall_time: (Date) - Date object with the datum's time.
* - scalar: (Number) - Value of the datum.
*/
setSeriesData: function(name, data) {
this._seriesDataCache[name] = data;
if (this._chart) {
this._chart.setSeriesData(name, data);
}
},
/**
* Reset the chart domain. If the chart has not rendered yet, a call to this
* method no-ops.
*/
resetDomain: function() {
if (this._chart) {
this._chart.resetDomain();
}
},
/**
* Re-renders the chart. Useful if e.g. the container size changed.
*/
redraw: function() {
if (this._chart) {
this._chart.redraw();
}
},
attached: function() {
this._attached = true;
},
detached: function() {
if (this._chart) this._chart.destroy();
this._attached = false;
},
ready: function() {
this.scopeSubtree(this.$.tooltip, true);
this.scopeSubtree(this.$.chartdiv, true);
},
/**
* Creates a chart, and asynchronously renders it. Fires a chart-rendered
* event after the chart is rendered.
*/
_makeChart: function(
xComponentsCreationMethod,
xType,
yValueAccessor,
yScaleType,
tooltipColumns,
colorScale,
seriesWithoutTooltips,
_attached) {
// Find the actual xComponentsCreationMethod.
if (!xType && !xComponentsCreationMethod) {
xComponentsCreationMethod = vz_chart_helpers.stepX;
} else if (xType) {
xComponentsCreationMethod = () =>
vz_chart_helpers.getXComponents(xType);
}
if (this._makeChartAsyncCallbackId !== null) {
this.cancelAsync(this._makeChartAsyncCallbackId);
this._makeChartAsyncCallbackId = null;
}
this._makeChartAsyncCallbackId = this.async(function() {
this._makeChartAsyncCallbackId = null;
if (!this._attached ||
!xComponentsCreationMethod ||
!this.yValueAccessor ||
!this.tooltipColumns) {
return;
}
var tooltip = d3.select(this.$.tooltip);
// We directly reference properties of `this` because this call is
// asynchronous, and values may have changed in between the call being
// initiated and actually being run.
var chart = new LineChart(
xComponentsCreationMethod,
this.yValueAccessor,
yScaleType,
colorScale,
tooltip,
this.tooltipColumns,
this.fillArea,
this.defaultXRange,
this.defaultYRange,
this.symbolFunction,
this.xAxisFormatter,
this.yAxisFormatter,
this.seriesWithoutTooltips);
var div = d3.select(this.$.chartdiv);
chart.renderTo(div);
if (this._chart) this._chart.destroy();
this._chart = chart;
}, 350);
},
_reloadFromCache: function() {
if (this._chart) {
this._chart.setVisibleSeries(this._visibleSeriesCache);
this._visibleSeriesCache.forEach(function(name) {
this._chart.setSeriesData(name, this._seriesDataCache[name] || []);
}.bind(this));
}
},
_smoothingChanged: function() {
if (!this._chart) {
return;
}
if (this.smoothingEnabled) {
this._chart.smoothingUpdate(this.smoothingWeight);
} else {
this._chart.smoothingDisable();
}
},
_outliersChanged: function() {
if (!this._chart) {
return;
}
this._chart.ignoreYOutliers(this.ignoreYOutliers);
},
_tooltipSortingMethodChanged: function() {
if (this._chart) {
this._chart.setTooltipSortingMethod(this.tooltipSortingMethod);
}
},
_tooltipPositionChanged: function() {
if (this._chart) {
this._chart.setTooltipPosition(this.tooltipPosition);
}
},
_computeTooltipTableHeaderHtml(tooltipColumns) {
// The first column contains the circle with the color of the run.
const titles = ["", ..._.map(tooltipColumns, 'title')];
return titles.map(title => `<th>${this._sanitize(title)}</th>`).join('');
},
_sanitize(value) {
return value.replace(/</g, '<')
.replace(/>/g, '>') // for symmetry :-)
.replace(/&/g, '&');
},
});
class LineChart {
private name2datasets: {[name: string]: Plottable.Dataset};
private seriesNames: string[];
private xAccessor: Plottable.IAccessor<number|Date>;
private xScale: Plottable.QuantitativeScale<number|Date>;
private yScale: Plottable.QuantitativeScale<number>;
private gridlines: Plottable.Components.Gridlines;
private center: Plottable.Components.Group;
private xAxis: Plottable.Axes.Numeric|Plottable.Axes.Time;
private yAxis: Plottable.Axes.Numeric;
private outer: Plottable.Components.Table;
private colorScale: Plottable.Scales.Color;
private symbolFunction: vz_chart_helpers.SymbolFn;
private tooltipColumns: vz_chart_helpers.TooltipColumn[];
private tooltip: d3.Selection<any, any, any, any>;
private tooltipInteraction: Plottable.Interactions.Pointer;
private tooltipPointsComponent: Plottable.Component;
private dzl: DragZoomLayer;
private linePlot: Plottable.Plots.Line<number|Date>;
private smoothLinePlot: Plottable.Plots.Line<number|Date>;
private marginAreaPlot?: Plottable.Plots.Area<number|Date>;
private scatterPlot: Plottable.Plots.Scatter<number|Date, Number>;
private nanDisplay: Plottable.Plots.Scatter<number|Date, Number>;
private markersScatterPlot: Plottable.Plots.Scatter<number|Date, number>;
private yValueAccessor: Plottable.IAccessor<number>;
private smoothedAccessor: Plottable.IAccessor<number>;
private lastPointsDataset: Plottable.Dataset;
private fillArea?: FillArea;
private datasets: Plottable.Dataset[];
private onDatasetChanged: (dataset: Plottable.Dataset) => void;
private nanDataset: Plottable.Dataset;
private smoothingWeight: number;
private smoothingEnabled: boolean;
private tooltipSortingMethod: string;
private tooltipPosition: string;
private seriesWithoutTooltips?: string[];
private _ignoreYOutliers: boolean;
// An optional list of 2 numbers.
private _defaultXRange: number[];
// An optional list of 2 numbers.
private _defaultYRange: number[];
private targetSVG: d3.Selection<any, any, any, any>;
constructor(
xComponentsCreationMethod: () => vz_chart_helpers.XComponents,
yValueAccessor: Plottable.IAccessor<number>,
yScaleType: string,
colorScale: Plottable.Scales.Color,
tooltip: d3.Selection<any, any, any, any>,
tooltipColumns: vz_chart_helpers.TooltipColumn[],
fillArea: FillArea,
defaultXRange?: number[],
defaultYRange?: number[],
symbolFunction?: vz_chart_helpers.SymbolFn,
xAxisFormatter?: (number) => string,
yAxisFormatter?: (number) => string,
seriesWithoutTooltips?: string[]) {
this.seriesNames = [];
this.name2datasets = {};
this.colorScale = colorScale;
this.tooltip = tooltip;
this.datasets = [];
this._ignoreYOutliers = false;
// lastPointDataset is a dataset that contains just the last point of
// every dataset we're currently drawing.
this.lastPointsDataset = new Plottable.Dataset();
this.nanDataset = new Plottable.Dataset();
this.yValueAccessor = yValueAccessor;
// The symbol function maps series to marker. It uses a special dataset that
// varies based on whether smoothing is enabled.
this.symbolFunction = symbolFunction;
// need to do a single bind, so we can deregister the callback from
// old Plottable.Datasets. (Deregistration is done by identity checks.)
this.onDatasetChanged = this._onDatasetChanged.bind(this);
this._defaultXRange = defaultXRange;
this._defaultYRange = defaultYRange;
this.tooltipColumns = tooltipColumns;
this.seriesWithoutTooltips = seriesWithoutTooltips;
this.buildChart(
xComponentsCreationMethod,
yValueAccessor,
yScaleType,
fillArea,
xAxisFormatter,
yAxisFormatter);
}
private buildChart(
xComponentsCreationMethod: () => vz_chart_helpers.XComponents,
yValueAccessor: Plottable.IAccessor<number>,
yScaleType: string,
fillArea: FillArea,
xAxisFormatter: (number) => string,
yAxisFormatter: (number) => string) {
if (this.outer) {
this.outer.destroy();
}
const xComponents = xComponentsCreationMethod();
this.xAccessor = xComponents.accessor;
this.xScale = xComponents.scale;
this.xAxis = xComponents.axis;
this.xAxis.margin(0).tickLabelPadding(3);
if (xAxisFormatter) {
this.xAxis.formatter(xAxisFormatter);
}
this.yScale = LineChart.getYScaleFromType(yScaleType);
this.yAxis = new Plottable.Axes.Numeric(this.yScale, 'left');
this.yAxis.margin(0).tickLabelPadding(5);
this.yAxis.formatter(yAxisFormatter ? yAxisFormatter
: vz_chart_helpers.multiscaleFormatter(
vz_chart_helpers.Y_AXIS_FORMATTER_PRECISION));
this.yAxis.usesTextWidthApproximation(true);
this.fillArea = fillArea;
this.dzl = new DragZoomLayer(
this.xScale, this.yScale, this.resetYDomain.bind(this));
this.tooltipInteraction = this.createTooltipInteraction(this.dzl);
this.tooltipPointsComponent = new Plottable.Component();
const plot = this.buildPlot(
this.xScale,
this.yScale,
fillArea);
this.gridlines =
new Plottable.Components.Gridlines(this.xScale, this.yScale);
let xZeroLine = new Plottable.Components.GuideLineLayer('horizontal');
xZeroLine.scale(this.yScale).value(0);
let yZeroLine = new Plottable.Components.GuideLineLayer('vertical');
yZeroLine.scale(this.xScale).value(0);
this.center = new Plottable.Components.Group([
this.gridlines, xZeroLine, yZeroLine, plot,
this.dzl, this.tooltipPointsComponent]);
this.outer = new Plottable.Components.Table(
[[this.yAxis, this.center], [null, this.xAxis]]);
}
private buildPlot(xScale, yScale, fillArea):
Plottable.Component {
if (fillArea) {
this.marginAreaPlot = new Plottable.Plots.Area<number|Date>();
this.marginAreaPlot.x(this.xAccessor, xScale);
this.marginAreaPlot.y(fillArea.higherAccessor, yScale);
this.marginAreaPlot.y0(fillArea.lowerAccessor);
this.marginAreaPlot.attr(
'fill',
(d: vz_chart_helpers.Datum, i: number, dataset: Plottable.Dataset) =>
this.colorScale.scale(dataset.metadata().name));
this.marginAreaPlot.attr('fill-opacity', 0.3);
this.marginAreaPlot.attr('stroke-width', 0);
}
this.smoothedAccessor = (d: vz_chart_helpers.ScalarDatum) => d.smoothed;
let linePlot = new Plottable.Plots.Line<number|Date>();
linePlot.x(this.xAccessor, xScale);
linePlot.y(this.yValueAccessor, yScale);
linePlot.attr(
'stroke',
(d: vz_chart_helpers.Datum, i: number, dataset: Plottable.Dataset) =>
this.colorScale.scale(dataset.metadata().name));
this.linePlot = linePlot;
this.setupTooltips(linePlot);
let smoothLinePlot = new Plottable.Plots.Line<number|Date>();
smoothLinePlot.x(this.xAccessor, xScale);
smoothLinePlot.y(this.smoothedAccessor, yScale);
smoothLinePlot.attr(
'stroke',
(d: vz_chart_helpers.Datum, i: number, dataset: Plottable.Dataset) =>
this.colorScale.scale(dataset.metadata().name));
this.smoothLinePlot = smoothLinePlot;
if (this.symbolFunction) {
const markersScatterPlot = new Plottable.Plots.Scatter<number|Date, number>();
markersScatterPlot.x(this.xAccessor, xScale);
markersScatterPlot.y(this.yValueAccessor, yScale);
markersScatterPlot.attr(
'fill',
(d: vz_chart_helpers.Datum, i: number, dataset: Plottable.Dataset) =>
this.colorScale.scale(dataset.metadata().name));
markersScatterPlot.attr('opacity', 1);
markersScatterPlot.size(vz_chart_helpers.TOOLTIP_CIRCLE_SIZE * 2);
markersScatterPlot.symbol(
(d: vz_chart_helpers.Datum, i: number, dataset: Plottable.Dataset) => {
return this.symbolFunction(dataset.metadata().name);
});
// Use a special dataset because this scatter plot should use the accesor
// that depends on whether smoothing is enabled.
this.markersScatterPlot = markersScatterPlot;
}
// The scatterPlot will display the last point for each dataset.
// This way, if there is only one datum for the series, it is still
// visible. We hide it when tooltips are active to keep things clean.
let scatterPlot = new Plottable.Plots.Scatter<number|Date, number>();
scatterPlot.x(this.xAccessor, xScale);
scatterPlot.y(this.yValueAccessor, yScale);
scatterPlot.attr('fill', (d: any) => this.colorScale.scale(d.name));
scatterPlot.attr('opacity', 1);
scatterPlot.size(vz_chart_helpers.TOOLTIP_CIRCLE_SIZE * 2);
scatterPlot.datasets([this.lastPointsDataset]);
this.scatterPlot = scatterPlot;
let nanDisplay = new Plottable.Plots.Scatter<number|Date, number>();
nanDisplay.x(this.xAccessor, xScale);
nanDisplay.y((x) => x.displayY, yScale);
nanDisplay.attr('fill', (d: any) => this.colorScale.scale(d.name));
nanDisplay.attr('opacity', 1);
nanDisplay.size(vz_chart_helpers.NAN_SYMBOL_SIZE * 2);
nanDisplay.datasets([this.nanDataset]);
nanDisplay.symbol(Plottable.SymbolFactories.triangle);
this.nanDisplay = nanDisplay;
const groups = [nanDisplay, scatterPlot, smoothLinePlot, linePlot];
if (this.marginAreaPlot) {
groups.push(this.marginAreaPlot);
}
if (this.markersScatterPlot) {
groups.push(this.markersScatterPlot);
}
return new Plottable.Components.Group(groups);
}
/** Updates the chart when a dataset changes. Called every time the data of
* a dataset changes to update the charts.
*/
private _onDatasetChanged(dataset: Plottable.Dataset) {
if (this.smoothingEnabled) {
this.resmoothDataset(dataset);
}
this.updateSpecialDatasets();
}
public ignoreYOutliers(ignoreYOutliers: boolean) {
if (ignoreYOutliers !== this._ignoreYOutliers) {
this._ignoreYOutliers = ignoreYOutliers;
this.updateSpecialDatasets();
this.resetYDomain();
}
}
/** Constructs special datasets. Each special dataset contains exceptional
* values from all of the regular datasets, e.g. last points in series, or
* NaN values. Those points will have a `name` and `relative` property added
* (since usually those are context in the surrounding dataset).
*/
private updateSpecialDatasets() {
const accessor = this.getYAxisAccessor();
let lastPointsData =
this.datasets
.map((d) => {
let datum = null;
// filter out NaNs to ensure last point is a clean one
let nonNanData =
d.data().filter((x) => !isNaN(accessor(x, -1, d)));
if (nonNanData.length > 0) {
let idx = nonNanData.length - 1;
datum = nonNanData[idx];
datum.name = d.metadata().name;
datum.relative = vz_chart_helpers.relativeAccessor(datum, -1, d);
}
return datum;
})
.filter((x) => x != null);
this.lastPointsDataset.data(lastPointsData);
if (this.markersScatterPlot) {
this.markersScatterPlot.datasets(
this.datasets.map(this.createSampledDatasetForMarkers));
}
// Take a dataset, return an array of NaN data points
// the NaN points will have a "displayY" property which is the
// y-value of a nearby point that was not NaN (0 if all points are NaN)
let datasetToNaNData = (d: Plottable.Dataset) => {
let displayY = null;
let data = d.data();
let i = 0;
while (i < data.length && displayY == null) {
if (!isNaN(accessor(data[i], -1, d))) {
displayY = accessor(data[i], -1, d);
}
i++;
}
if (displayY == null) {
displayY = 0;
}
let nanData = [];
for (i = 0; i < data.length; i++) {
if (!isNaN(accessor(data[i], -1, d))) {
displayY = accessor(data[i], -1, d);
} else {
data[i].name = d.metadata().name;
data[i].displayY = displayY;
data[i].relative = vz_chart_helpers.relativeAccessor(data[i], -1, d);
nanData.push(data[i]);
}
}
return nanData;
};
let nanData = _.flatten(this.datasets.map(datasetToNaNData));
this.nanDataset.data(nanData);
}
public resetDomain() {
this.resetXDomain();
this.resetYDomain();
}
private resetXDomain() {
let xDomain;
if (this._defaultXRange != null) {
// Use the range specified by the caller.
xDomain = this._defaultXRange;
} else {
// (Copied from DragZoomLayer.unzoom.)
const xScale = this.xScale as any;
xScale._domainMin = null;
xScale._domainMax = null;
xDomain = xScale._getExtent();
}
this.xScale.domain(xDomain);
}
private resetYDomain() {
let yDomain;
if (this._defaultYRange != null) {
// Use the range specified by the caller.
yDomain = this._defaultYRange;
} else {
// Generate a reasonable range.
const accessors = this.getAccessorsForComputingYRange();
let datasetToValues: (d: Plottable.Dataset) => number[][] = (d) => {
return accessors.map(accessor => d.data().map(x => accessor(x, -1, d)));
};
const vals = _.flattenDeep<number>(this.datasets.map(datasetToValues))
.filter(isFinite);
yDomain = vz_chart_helpers.computeDomain(vals, this._ignoreYOutliers);
}
this.yScale.domain(yDomain);
}
private getAccessorsForComputingYRange(): Plottable.IAccessor<number>[] {
const accessors = [this.getYAxisAccessor()];
if (this.fillArea) {
// Make the Y domain take margins into account.
accessors.push(
this.fillArea.lowerAccessor,
this.fillArea.higherAccessor);
}
return accessors;
}
private getYAxisAccessor() {
return this.smoothingEnabled ? this.smoothedAccessor : this.yValueAccessor;
}
private createTooltipInteraction(dzl: DragZoomLayer):
Plottable.Interactions.Pointer {
const pi = new Plottable.Interactions.Pointer();
// Disable interaction while drag zooming.
dzl.interactionStart(() => {
pi.enabled(false);
this.hideTooltips();
});
dzl.interactionEnd(() => pi.enabled(true));
pi.onPointerMove((p: Plottable.Point) => {
// Line plot must be initialized to draw.
if (!this.linePlot) return;
let target: vz_chart_helpers.Point = {
x: p.x,
y: p.y,
datum: null,
dataset: null,
};
let bbox: SVGRect = (<any>this.gridlines.content().node()).getBBox();
// pts is the closets point to the tooltip for each dataset
let pts = this.linePlot.datasets()
.map((dataset) => this.findClosestPoint(target, dataset))
.filter(Boolean);
let intersectsBBox = Plottable.Utils.DOM.intersectsBBox;
// We draw tooltips for points that are not explicity ignored,
// and are NaN or are currently visible.
let ptsForTooltips = pts.filter(
(p) => (intersectsBBox(p.x, p.y, bbox) ||
isNaN(this.yValueAccessor(p.datum, 0, p.dataset))) &&
(!this.seriesWithoutTooltips ||
this.seriesWithoutTooltips.indexOf(
p.dataset.metadata().name) == -1));
// Only draw little indicator circles for the non-NaN points
let ptsToCircle = ptsForTooltips.filter(
(p) => !isNaN(this.yValueAccessor(p.datum, 0, p.dataset)));
let ptsSelection: any =
this.tooltipPointsComponent.content().selectAll('.point').data(
ptsToCircle,
(p: vz_chart_helpers.Point) => p.dataset.metadata().name);
if (pts.length !== 0) {
ptsSelection.enter().append('circle').classed('point', true);
ptsSelection.attr('r', vz_chart_helpers.TOOLTIP_CIRCLE_SIZE)
.attr('cx', (p) => p.x)
.attr('cy', (p) => p.y)
.style('stroke', 'none')
.attr(
'fill',
(p) => this.colorScale.scale(p.dataset.metadata().name));
ptsSelection.exit().remove();
this.drawTooltips(ptsForTooltips, target, this.tooltipColumns);
} else {
this.hideTooltips();
}
});
pi.onPointerExit(() => this.hideTooltips());
return pi;
}
private hideTooltips(): void {
this.tooltip.style('opacity', 0);
this.scatterPlot.attr('opacity', 1);
this.tooltipPointsComponent.content().selectAll('.point').remove();
}
private setupTooltips(plot: Plottable.XYPlot<number|Date, number>): void {
plot.onDetach(() => {
this.tooltipInteraction.detachFrom(plot);
this.tooltipInteraction.enabled(false);
});
plot.onAnchor(() => {
this.tooltipInteraction.attachTo(plot);
this.tooltipInteraction.enabled(true);
});
}
private drawTooltips(
points: vz_chart_helpers.Point[],
target: vz_chart_helpers.Point,
tooltipColumns: vz_chart_helpers.TooltipColumn[]) {
// Formatters for value, step, and wall_time
this.scatterPlot.attr('opacity', 0);
let valueFormatter = vz_chart_helpers.multiscaleFormatter(
vz_chart_helpers.Y_TOOLTIP_FORMATTER_PRECISION);
let dist = (p: vz_chart_helpers.Point) =>
Math.pow(p.x - target.x, 2) + Math.pow(p.y - target.y, 2);
let closestDist = _.min(points.map(dist));
let valueSortMethod = this.yValueAccessor;
if (this.smoothingEnabled) {
valueSortMethod = this.smoothedAccessor;
}
if (this.tooltipSortingMethod === 'ascending') {
points = _.sortBy(points, (d) => valueSortMethod(d.datum, -1, d.dataset));
} else if (this.tooltipSortingMethod === 'descending') {
points = _.sortBy(points, (d) => valueSortMethod(d.datum, -1, d.dataset))
.reverse();
} else if (this.tooltipSortingMethod === 'nearest') {
points = _.sortBy(points, dist);
} else {
// The 'default' sorting method maintains the order of names passed to
// setVisibleSeries(). However we reverse that order when defining the
// datasets. So we must call reverse again to restore the order.
points = points.slice(0).reverse();
}
let rows = this.tooltip.select('tbody')
.html('')
.selectAll('tr')
.data(points)
.enter()
.append('tr');
// Grey out the point if any of the following are true:
// - The cursor is outside of the x-extent of the dataset
// - The point's y value is NaN
rows.classed('distant', (d) => {
let firstPoint = d.dataset.data()[0];
let lastPoint = _.last(d.dataset.data());
let firstX = this.xScale.scale(this.xAccessor(firstPoint, 0, d.dataset));
let lastX = this.xScale.scale(this.xAccessor(lastPoint, 0, d.dataset));
let s = this.smoothingEnabled ?
d.datum.smoothed : this.yValueAccessor(d.datum, 0, d.dataset);
return target.x < firstX || target.x > lastX || isNaN(s);
});
rows.classed('closest', (p) => dist(p) === closestDist);
// It is a bit hacky that we are manually applying the width to the swatch
// and the nowrap property to the text here. The reason is as follows:
// the style gets updated asynchronously by Polymer scopeSubtree observer.
// Which means we would get incorrect sizing information since the text
// would wrap by default. However, we need correct measurements so that
// we can stop the text from falling off the edge of the screen.
// therefore, we apply the size-critical styles directly.
rows.style('white-space', 'nowrap');
rows.append('td')
.append('span')
.classed('swatch', true)
.style(
'background-color',
(d) => this.colorScale.scale(d.dataset.metadata().name));
_.each(tooltipColumns, (column) => {
rows.append('td').text(
(d) => column.evaluate(d, {
smoothingEnabled: this.smoothingEnabled,
}));
});
// compute left position
let documentWidth = document.body.clientWidth;
let node: any = this.tooltip.node();
let parentRect = node.parentElement.getBoundingClientRect();
let nodeRect = node.getBoundingClientRect();
// prevent it from falling off the right side of the screen
let left = documentWidth - parentRect.left - nodeRect.width - 60, top = 0;
if (this.tooltipPosition === 'right') {
left = Math.min(parentRect.width, left);
} else { // 'bottom'
left = Math.min(0, left);
top = parentRect.height + vz_chart_helpers.TOOLTIP_Y_PIXEL_OFFSET;
}
this.tooltip.style('transform', 'translate(' + left + 'px,' + top + 'px)');
this.tooltip.style('opacity', 1);
}
private findClosestPoint(