-
Notifications
You must be signed in to change notification settings - Fork 46
/
px-vis-axis-interaction-space.html
1153 lines (953 loc) · 32.6 KB
/
px-vis-axis-interaction-space.html
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 (c) 2018, General Electric
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.
-->
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="../px-icon-set/px-icon-set.html"/>
<link rel="import" href="px-vis-behavior-common.html" />
<link rel="import" href="px-vis-behavior-d3.html" />
<link rel="import" href="px-vis-behavior-axis-drag.html"/>
<link rel="import" href="css/px-vis-styles.html">
<!--
### Usage
<px-vis-axis-interaction-space
svg="[[svg]]"
width="[[width]]"
height="[[height]]"
margin="[[margin]]"
x="[[x]]"
y="[[y]]"
domain-changed="[[domainChanged]]"
complete-series-config="[[completeSeriesConfig]]"
dimension="[[dimension]]"
dimensions="[[dimensions]]"
series-key="[[seriesKey]]"
category-key="[[categoryKey]]"
chart-data="[[chartData]]"
muted-series="{{mutedSeries}}"
radial="[[radial]]"
center-offset="[[centerOffset]]"
brush-to-remove="[[brushToRemove]]"
brush-domains="[[brushDomains]]"
drag-container-elem="[[dragContainerElem]]"
cartesian-drag-behavior="[[cartesianDragBehavior]]"
redraw-series="[[redrawSeries]]"
dynamic-redraw="[[dynamicRedraw]]"
action-config="[[actionConfig]]">
</px-vis-axis-interaction-space>
### Styling
The following custom properties are available for styling:
Custom property | Description
:----------------|:-------------
`--px-vis-axis-brush-outline-color` | The stroke (border) color for the brushed box on an axis
`--px-vis-axis-brush-fill-color` | The fill (background) color for the brushed box on an axis
`--px-vis-axis-brush-fill-opacity` | The opacity of the brushed box on an axis
@element px-vis-axis-interaction-space
@blurb Element providing user interaction on an axis.
@homepage index.html
@demo demo.html
-->
<dom-module id="px-vis-axis-interaction-space">
<template>
<style include="px-vis-styles"></style>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-axis-interaction-space',
behaviors: [
PxVisBehavior.observerCheck,
PxVisBehaviorD3.svg,
PxVisBehaviorD3.axes,
PxVisBehavior.commonMethods,
PxVisBehavior.dataset,
PxVisBehavior.mutedSeries,
PxVisBehavior.combinedMutedSeries,
PxVisBehaviorD3.selectedDomain,
PxVisBehaviorD3.domainUpdate,
PxVisBehaviorD3.radialAxisConfig,
PxVisBehavior.radial,
PxVisBehaviorD3.dynamicRedraw,
PxVisBehaviorDrag.parallelAxisDrag,
PxVisBehaviorDrag.cartesianDrag,
PxVisBehavior.categories,
PxVisBehavior.completeSeriesConfig,
PxVisBehavior.applyActionConfig,
PxVisBehaviorD3.cursorIcon,
PxVisBehaviorD3.axisOrientation,
PxVisBehavior.updateStylesOverride,
PxVisBehavior.extentsData,
PxVisBehavior.brushDomains,
PxVisBehavior.timeDomain,
PxVisBehavior.gradientColorsFunction
],
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
/**
* The string identifier for the axis
*
*/
dimension: {
type: String
},
/**
* Holder object for the brush node
*/
_brushElem: {
type: Object
},
/**
* Holder object for the d3 selected brush
*/
_brushD3: {
type: Object
},
/**
* Holder for the original domain
*/
_previousRange: {
type: Array
},
/**
* Holder for the brush
*/
_brush: {
type: Object
},
brushToRemove: {
type: Boolean,
value: false
},
dragContainerElem: {
type: HTMLElement
},
cartesianDragBehavior: {
type: Boolean,
value: false
},
/**
* A holder for the drag behavior
*
*/
dragBehavior: {
type: Object
},
/**
* Set of predefined actions that can be used out of the box
*/
specialActionsList: {
type: Array,
value: function() {
return ['callAxisMute', 'callAxisDrag', 'startZooming', 'startLasso'];
}
},
/**
* set of actions which should display drawn brushes.
* Key is the actionConfig value
* value is the brushDomains key
*/
brushActions: {
type: Object,
value: function() {
return {
'callAxisMute': 'muted',
'startZooming': 'zoom',
'startLasso': 'lasso'
};
}
},
/**
* set of predefined action that can be used out of the box
*/
actionMapping: {
type: Object,
readOnly: true,
value: function() {
return {
'callAxisMute': '_startMute',
'callAxisDrag': '_enableDrag',
'calcTooltipData': '_calcTooltipData',
'calcCrosshairData': '_calcCrosshairData',
'calcTooltipAndCrosshairData': '_calcTooltipAndCrosshairData',
'resetTooltipAndCrosshairData': '_resetTooltipAndCrosshairData',
'resetTooltip': '_resetTooltipData',
'resetCrosshair': '_resetCrosshairData',
'startZooming' : '_startZoom',
'startPanning': '_startPanning',
'stopPanning': '_stopPanning',
'startLasso': '_startLasso'
}
}
},
/**
* Configuration used to define what actions happen on events. Each key represents an event,
* each value can be:
* - a predefined action found in px-vis-interaction-space `actionMapping`
* - a function, where `this` will be bound to the chart and the function's argument will be the mouse position on the chart
*/
actionConfig: {
type: Object,
value: function() {
return {
'mousedown': null,
'mouseup': null,
'mouseout': 'resetTooltip',
'mousemove': 'calcTooltipData'
};
}
},
smallerSide: {
type: Number,
value: 0
},
_isDirty: {
type: Boolean,
value: false
},
_panningLastVal: {
type: Number
},
_unselectedBrushHolder: {
type: Object
},
_continuousBrush: {
type: Boolean,
value: false
}
},
observers: [
'drawElement(svg, margin.* , height, domainChanged)',
'_showBrushes(brushDomains.*, actionConfig.*, domainChanged)',
'_showUnselectedBrushes(actionConfig.*, domainChanged)',
'_setupActions(actionConfig.*)',
'_setDragBehavior(cartesianDragBehavior, dragContainerElem)',
'_drawCursorIconAndGroup(svg, iconType)',
'_setBrushStyles(_stylesUpdated)',
'_computeGradientRange(timeDomain)'
],
attached: function() {
if(this._isDirty) {
this.drawElement();
this._isDirty = false;
}
},
detached: function() {
if(this._doesD3HaveValues(this._brushD3)) {
this.deleteAndClearBrush();
this._brushD3.remove();
this._brushD3 = null;
}
this._isDirty = true;
},
/**
* Creates the drag behavior based on whether it is a cartesian chart or a parallel/radar chart
*
*/
_setDragBehavior: function() {
if(this.hasUndefinedArguments(arguments)) {
return;
}
if(this.cartesianDragBehavior) {
this.createCartesianDrag();
} else {
this.createParallelAxisDrag();
}
},
/**
* Creates the elements to listen to.
*
* For simplicity, we just let d3.brush create the listener elements and control their size
*/
drawElement: function() {
if(this.hasUndefinedArguments(arguments)) {
return;
}
this.debounce('_axisBrush', function() {
if(this.domainChanged && this.svg) {
var range = this._calcRange();
// check if we already have a brush
// if so, update the brush when resize occurs
if(this._doesD3HaveValues(this._brushD3)) {
this._resizeBrush(range);
} else {
this._unselectedBrushHolder = this.svg.append('g')
.attr('id', 'unselected-brushes');
this._createBrush(range);
}
this._translateBrush();
this._styleBrushSelection();
}
}, 10);
},
/**
* Calculates the range to pass to the brush, i.e. the size the brush will have
*
*/
_calcRange: function() {
// TODO make brush width dynamic --> replace -10 & 10 with brushWidth/2
// brush range expects: [ [x1,y1] , [x2,y2] ]
var range = this.radial ?
[
[-10, this.centerOffset],
[10, this.height]
] :
[
[-10, 0],
[10, Math.max(this.height - this.margin.top - this.margin.bottom, 1)]
];
// check that our sizing is ok and wont freak d3 out.
// x2 and y2 must be greater than x1 & y1
range[1][0] = (range[1][0] > range[0][0]) ? range[1][0] : range[0][0];
range[1][1] = (range[1][1] > range[0][1]) ? range[1][1] : range[0][1];
return range;
},
/**
* Creates the brush
*
*/
_createBrush: function(range) {
// create a new brush
var b = this.svg.append("g")
.attr("class", "interaction");
b.call(
this._brush = Px.d3.brushY()
.extent(range)
.filter(function() {
return 1;
})
.on("start.brush", this.brushstart.bind(this))
.on("brush.brush", this.brushActive.bind(this))
.on("end.brush", this.brushEnd.bind(this))
);
// save our references
this._brushD3 = b;
this._brushElem = b.node();
this._setBrushStyles();
this._setupActions();
},
/**
* Resizes the brush elements
*
*/
_resizeBrush: function(range) {
//set appropriate range
this._brushD3.call(
this._brush.extent(range)
.on("start.brush", this.brushstart.bind(this))
.on("end.brush", this.brushEnd.bind(this))
);
},
_setBrushStyles: function() {
if(this.hasUndefinedArguments(arguments)) {
return;
}
if(this._doesD3HaveValues(this._brushD3)) {
this._brushD3.selectAll("rect.selection")
.attr("fill", this._checkThemeVariable("--px-vis-axis-brush-fill-color", 'rgb(0,0,0)'))
.attr("fill-opacity", this._checkThemeVariable("--px-vis-axis-brush-fill-opacity", 0.3))
.attr("stroke", this._checkThemeVariable("--px-vis-axis-brush-outline-color", 'rgb(50,50,50)'));
}
},
_translateBrush: function() {
var h = 0,
w = 0;
switch(this.orientation) {
case 'bottom':
h = Math.max(this.height - this.margin.bottom - this.margin.top,0);
break;
case 'right':
w = Math.max(this.width - this.margin.left - this.margin.right,0);
break;
}
this._brushD3.attr('transform', 'translate(' + w + ',' + h + ')');
},
/**
* Adds or removes the dash style for the selection based on brushToRemove
*
*/
_styleBrushSelection: function() {
if(this.brushToRemove && this.actionConfig.mousedown === 'callAxisMute') {
this._brushD3.selectAll("rect.selection")
.attr("stroke-dasharray", ("5, 5"));
} else {
this._brushD3.selectAll("rect.selection")
.attr("stroke-dasharray", null);
}
},
// ACTIONS ###########################################################################################
_setupActions: function() {
if(this.hasUndefinedArguments(arguments)) {
return;
}
if(this._doesD3HaveValues(this._brushD3)) {
// reset everything
this._disableAllAction();
//setup a default cursor setup
this._brushD3.selectAll("rect")
.attr("cursor","crosshair")
.on("mousemove.cursor", this._updateCursor.bind(this))
.on("mouseout.cursor", this._hideCursor.bind(this));
//setup a default cursor setup
this._brushD3.selectAll("rect")
.on('mouseenter.sizing', function() {
this.dispatchEvent(new CustomEvent('px-vis-tooltip-sizing-request', { bubbles: true, composed: true }));
}.bind(this));
// check if we have special actions and if so, run their caller
var dontDoUpDown = this._setupSpecialActions();
// set up others
this._setupRegularActions(this._brushD3, this._brushElem, dontDoUpDown);
}
},
/**
* Set up the special d3 call function actions
* These are d3 behaviors rather than simple mouse events
*/
_setupSpecialActions: function() {
var dontDoUpDown = false,
mouseEvent = null,
action = null;
if(this.specialActionsList.indexOf(this.actionConfig["mousedown"]) !== -1) {
dontDoUpDown = true;
mouseEvent = "mousedown";
} else if(this.specialActionsList.indexOf(this.actionConfig["mouseup"]) !== -1) {
dontDoUpDown = true;
mouseEvent = "mouseup";
}
if(dontDoUpDown) {
action = this.actionConfig[mouseEvent];
this[this.actionMapping[action]]();
}
return dontDoUpDown;
},
_disableActions: function() {
this._brushD3
.on("mousedown", null)
.on("mouseup", null)
.on("mousemove", null)
.on("mouseout", null);
},
_disableBrush: function() {
this._brushD3.call(
this._brush.filter(function() {
return 0;
})
);
},
_startMute: function() {
this.extentsAction = 'mute';
this._continuousBrush = false;
this._disableBrush();
this._enableBrush();
},
_startZoom: function() {
this.extentsAction = 'zoom';
this._continuousBrush = false;
this._disableBrush();
this._enableBrush();
},
_startLasso: function() {
this.extentsAction = 'lasso';
this._continuousBrush = true;
this._disableBrush();
this._enableBrush();
},
_enableBrush: function() {
this._brushD3.call(
this._brush.filter(function() {
return 1;
})
.on("start.brush", this.brushstart.bind(this))
.on("brush.brush", this.brushActive.bind(this))
.on("end.brush", this.brushEnd.bind(this))
);
this._brushD3.select("rect.overlay")
.attr("cursor","crosshair");
this._brushD3.select("rect.selection")
.attr("cursor","move");
this._brushD3.selectAll("rect.handle")
.attr("cursor","ns-resize");
},
_disableDrag: function() {
if(!this.dragBehavior) {
console.warn("Drag behavior not configured");
return;
}
this._brushD3.call(
this.dragBehavior.filter(function() {
return 0;
})
);
},
_enableDrag: function() {
if(!this.dragBehavior) {
console.warn("Drag behavior not configured");
return;
}
this._brushD3.call(
this.dragBehavior.filter(function() {
return 1;
})
);
this._brushD3.selectAll("rect")
.attr("cursor","move");
},
_disableAllAction: function() {
this._disableBrush();
this._disableDrag();
this._disableActions();
},
// PANNING ###########################################################################################
/**
* Initiate Panning action
*/
_startPanning: function() {
//only allow left clicks
if(Px.d3.event.button === 0) {
this.set('extentsAction', 'pan');
this._resetTooltipData();
//in case the user clicks inside the chart, and mouses out, we are waiting for a mouseup, and closing our action box with the coordinates available on the mouseup.
Px.d3.select(document).on('mouseup.action', this._stopPanning.bind(this));
Px.d3.select(document).on('mousemove.action', this._updatePanning.bind(this));
this._panningLastVal = Px.d3.mouse(this._brushElem)[1];
this.dispatchEvent(new CustomEvent('px-vis-interaction-space-start-panning', { bubbles: true, composed: true }));
}
},
_updatePanning: function() {
var mousePos = Px.d3.mouse(this._brushElem),
delta = this._panningLastVal - mousePos[1],
extents = {
dimension: this.dimension,
action: this.extentsAction,
extents: []
},
range = this._getRange();
if(this.radial) {
extents.extents.push(range[0] + delta);
extents.extents.push(range[1] + delta);
} else {
extents.extents.push(range[1] + delta);
extents.extents.push(range[0] + delta);
}
this._panningLastVal = mousePos[1];
this.dispatchEvent(new CustomEvent('px-vis-axis-interaction-space-extents-data', { bubbles: true, composed: true, detail: extents }));
},
_stopPanning: function() {
Px.d3.select(document).on('mouseup.action', null);
Px.d3.select(document).on('mousemove.action', null);
this.dispatchEvent(new CustomEvent('px-vis-interaction-space-stop-panning', { bubbles: true, composed: true }));
},
_getRange: function() {
var range;
if(typeof this.y === 'function') {
range = this.y.range();
} else {
range = this.y[this.dimension].range();
}
return range;
},
// HOVER ###########################################################################################
_resetTooltipAndCrosshairData: function() {
this._resetTooltipData();
this._resetCrosshairData();
},
_calcTooltipAndCrosshairData: function() {
this._calcTooltip = true;
this._calcCrosshair = true;
this._calcHoverData();
},
_calcTooltipData: function() {
this._calcTooltip = true;
this._calcCrosshair = false;
this._calcHoverData();
},
_calcCrosshairData: function() {
this._calcTooltip = false;
this._calcCrosshair = true;
this._calcHoverData();
},
_calcHoverData: function() {
// TODO add a check that we want it to fire these events?
// We need to do this calc before the debounce
var m = Px.d3.mouse(this._brushElem);
this.debounce('axisMouseMove', function() {
this._calcSerieData(this.dimension, m)
}, 10);
},
_resetTooltipData: function() {
var ttD = {
"axis": null,
"yVal": null,
"mouse": null,
"time": null,
"dataset": null,
'series': [],
'seriesObj': {},
"color": null,
"tooltipConfig": null
};
// make timeout larger than the debounce on _calcHoverData
window.setTimeout(function() {
this.set('tooltipData',ttD);
this.fire('px-vis-axis-interaction-space-reset-tooltip', ttD);
}.bind(this), 11);
},
_resetCrosshairData: function() {
var chD = {
'rawData': [],
'timeStamps': []
};
// make timeout larger than the debounce on _calcHoverData
window.setTimeout(function() {
this.set('crosshairData',chD);
this.generatingCrosshairData = false;
this.fire('px-vis-axis-interaction-space-reset-crosshair', chD);
}.bind(this), 11);
},
/**
* Creates the tooltipData & crosshairData
*
* Takes the dimension (aid), the raw element, and the mouse position
*/
_calcSerieData: function(aid, mousePos) {
var result = {
"distance" : Number.MAX_VALUE, //difference between cursor and value
"minDistance" : Number.MAX_VALUE, //smallest found distance
'rawData': [], //datasets for crosshair
'timeStamps': [] //list of timeStamps for crosshair
},
ttD,chD;
//loop through our chart data and find a closetest match
for(var i = 0; i < this.chartData.length; i++) {
//search everything if not hard muted, otherwise only non muted stuff
if(!this.combinedMutedSeries[this.chartData[i][this.seriesKey]]) {
this._searchData(aid, mousePos[1], result, this.chartData[i]);
}
}
if(this._calcTooltip) {
ttD = this._createTooltipData(aid, mousePos, result);
if(ttD) {
this.set('tooltipData', ttD);
this.fire('px-vis-axis-interaction-space-tooltip-data', ttD);
}
}
if(this._calcCrosshair) {
chD = this._createCrosshairData(result);
this.generatingCrosshairData = true;
this.set('crosshairData', chD);
this.fire('px-vis-axis-interaction-space-crosshair-data', chD);
}
},
/**
* Determines if we want to keep some data.
*
*/
_searchData: function(aid, mouseVal, result, d) {
// check that data is within our domain
if(this.y.domain()[0] > d[aid] || d[aid] > this.y.domain()[1]) {
return;
}
if(this.timeDomain && this.timeDomain.x &&
(d[this.seriesKey] < this.timeDomain.x[0] || d[this.seriesKey] > this.timeDomain.x[1])) {
return;
}
//figure out the differnce between the data value and our saved value
// we round our pixels while drawing the line, so should round here too
result.distance = Math.abs(Math.floor(this.y(d[aid])) - mouseVal);
// if it is smaller than last time, save the data info
if(result.distance < result.minDistance) {
result.minDistance = result.distance; //track our minDistance for later
//reset our data
result.rawData = [d];
result.timeStamps = [d[this.seriesKey]];
} else if(result.distance === result.minDistance) {
result.rawData.push(d);
result.timeStamps.push(d[this.seriesKey]);
}
},
/**
* Builds the tooltipData object from the results
*
*/
_createTooltipData: function(aid, mousePos, result) {
var tooltipData;
if(result.rawData.length) {
tooltipData = this._createSingleDataset(aid, mousePos, result.rawData[0]);
tooltipData.additionalPoints = [];
for (var i = 1; i < result.rawData.length; i++) {
tooltipData.additionalPoints.push(this._createSingleDataset(aid, mousePos, result.rawData[i]));
}
}
return tooltipData;
},
_createSingleDataset: function(aid, mousePos, d) {
var data, //our tooltipData obj
tooltipConfig = {}, //a fake configuration object we will build
value = {}; //the series obj within tooltipData
//create our fake configuration object
tooltipConfig[aid] = {};
//if using categies, use it, otherwise default. Check for gradients.
let baseColors = this.categoryKey && d[this.categoryKey] && this.completeSeriesConfig[d[this.categoryKey]] ? this.completeSeriesConfig[d[this.categoryKey]].color : this.completeSeriesConfig[this.seriesKey].color,
gradient = this.createGradientFunction(baseColors);
tooltipConfig[aid].color = gradient(d[this.seriesKey]);
//if title is specified, use it, otherwiuse id
tooltipConfig[aid]["name"] = this.completeSeriesConfig[aid] && this.completeSeriesConfig[aid]['title'] ? this.completeSeriesConfig[aid]["title"] : aid;
//if there is a unit, add it
tooltipConfig[aid]["yAxisUnit"] = this.completeSeriesConfig[aid] && this.completeSeriesConfig[aid]["yAxisUnit"] ? this.completeSeriesConfig[aid]["yAxisUnit"] : '';
//add the y series id
tooltipConfig[aid]['y'] = aid;
//create our value obj
value[aid] = d[aid];
//create our tooltipData obj
data = {
"axis": aid,
"yVal": d[aid],
"mouse": mousePos,
"time": d[this.seriesKey],
"dataset": d,
'series': [
{ 'name': aid, 'value': value }
],
'seriesObj': {},
"color": tooltipConfig[aid]["color"],
"tooltipConfig": tooltipConfig
};
data.seriesObj[aid] = {'name': aid, 'value': value };
return data;
},
_createCrosshairData: function(result) {
var data = {};
data.rawData = result.rawData;
data.timeStamps = result.timeStamps;
return data;
},
// BRUSHING ########################################################################################
/**
* Stops event propagation on a brush start event
*/
brushstart: function() {
if(Px.d3.event.sourceEvent && Px.d3.event.sourceEvent.type !== 'end') {
Px.d3.event.sourceEvent.stopPropagation();
}
this._brushD3.select("rect.selection")
.attr("width", 20);
},
// /**
// * Update brush on a brush event
// */
brushActive: function() {
if(this._continuousBrush) {
var extents;
// if it was brushed, get the extents
if(Px.d3.brushSelection(this._brushElem)) {
extents = Px.d3.brushSelection(this._brushElem);
}
var data = {
dimension: this.dimension,
extents: extents,
action: this.extentsAction
}
this.dispatchEvent(new CustomEvent('px-vis-axis-interaction-space-extents-data', { bubbles: true, composed: true, detail: data }));
}
},
/**
* Final update on a brush end event
*/
brushEnd: function() {
var extents = [];
// if it was brushed, get the extents
if(Px.d3.brushSelection(this._brushElem)) {
extents = this._checkMinSize(Px.d3.brushSelection(this._brushElem), this._brushD3);
}
var data = {
dimension: this.dimension,
extents: extents,
action: this.extentsAction
}
this.clearBrush();
this._enableBrush();
this.dispatchEvent(new CustomEvent('px-vis-axis-interaction-space-extents-data', { bubbles: true, composed: true, detail: data }));
},
/**
* Checks the min size of the brush and resizes if necessary
*/
_checkMinSize: function(sel) {
//check the size of the box and make sure it is some min
if((sel[1] - sel[0]) < 4) {
// increase size of extents while checking if we hit the top
var ext = (sel[0] - 4 - this.centerOffset) < 0 ?
[ sel[0] , sel[1] + 4 ] :
[ sel[0] -4 , sel[1] ];
return ext;
} else {
return sel;
}
},
/**
* Checks that the brush size is valid and within the domain
*/
_checkBrushSize: function(domains) {
var newAxisDomain = this.y.domain(),
oldBrushDomain = [ domains[1], domains[0] ],
// oldBrushDomain = this.radial ? [domains[1], domains[0] ] : domains,
newBrushRange = [];
// if the brushExts fall within the new axis domain
if(newAxisDomain[0] <= oldBrushDomain[0] && newAxisDomain[1] >= oldBrushDomain[1]) {
// resize brush
newBrushRange = this._getNewBrushRange(oldBrushDomain[0], oldBrushDomain[1]);
//check that it still meets our min size req
newBrushRange = this._checkMinSize(newBrushRange);
//bottom of brush is below new domain, top is within
} else if(newAxisDomain[0] > oldBrushDomain[0] && newAxisDomain[0] < oldBrushDomain[1] && newAxisDomain[1] >= oldBrushDomain[1]) {
//move bottom to the min newAxisRange
newBrushRange = this._getNewBrushRange(newAxisDomain[0], oldBrushDomain[1]);
newBrushRange = this._checkMinSize(newBrushRange);
//bottom of brush within new domain, top is out
} else if(newAxisDomain[0] <= oldBrushDomain[0] && newAxisDomain[1] < oldBrushDomain[1] && newAxisDomain[1] > oldBrushDomain[0]) {
//move top to the max newAxisRange
newBrushRange = this._getNewBrushRange(oldBrushDomain[0], newAxisDomain[1]);
newBrushRange = this._checkMinSize(newBrushRange);
} else {
return [];
}
return newBrushRange;
},
/**
* On a resize or change, returns the new valid range for the brush
*/
_getNewBrushRange: function(d1, d2) {
var newRange = [];
newRange[0] = this.y(d1);
newRange[1] = this.y(d2);