Interaction.js
39.6 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
/**
* (c) 2010-2018 Torstein Honsi
*
* License: www.highcharts.com/license
*/
'use strict';
import H from './Globals.js';
import './Utilities.js';
import './Chart.js';
import './Options.js';
import './Legend.js';
import './Point.js';
import './Series.js';
var addEvent = H.addEvent,
Chart = H.Chart,
createElement = H.createElement,
css = H.css,
defaultOptions = H.defaultOptions,
defaultPlotOptions = H.defaultPlotOptions,
each = H.each,
extend = H.extend,
fireEvent = H.fireEvent,
hasTouch = H.hasTouch,
inArray = H.inArray,
isObject = H.isObject,
Legend = H.Legend,
merge = H.merge,
pick = H.pick,
Point = H.Point,
Series = H.Series,
seriesTypes = H.seriesTypes,
svg = H.svg,
TrackerMixin;
/**
* TrackerMixin for points and graphs.
*
* @private
* @mixin Highcharts.TrackerMixin
*/
TrackerMixin = H.TrackerMixin = {
/**
* Draw the tracker for a point.
*
* @private
* @function Highcharts.TrackerMixin.drawTrackerPoint
*
* @fires Highcharts.Series#event:afterDrawTracker
*/
drawTrackerPoint: function () {
var series = this,
chart = series.chart,
pointer = chart.pointer,
onMouseOver = function (e) {
var point = pointer.getPointFromEvent(e);
// undefined on graph in scatterchart
if (point !== undefined) {
pointer.isDirectTouch = true;
point.onMouseOver(e);
}
};
// Add reference to the point
each(series.points, function (point) {
if (point.graphic) {
point.graphic.element.point = point;
}
if (point.dataLabel) {
if (point.dataLabel.div) {
point.dataLabel.div.point = point;
} else {
point.dataLabel.element.point = point;
}
}
});
// Add the event listeners, we need to do this only once
if (!series._hasTracking) {
each(series.trackerGroups, function (key) {
if (series[key]) { // we don't always have dataLabelsGroup
series[key]
.addClass('highcharts-tracker')
.on('mouseover', onMouseOver)
.on('mouseout', function (e) {
pointer.onTrackerMouseOut(e);
});
if (hasTouch) {
series[key].on('touchstart', onMouseOver);
}
if (series.options.cursor) {
series[key]
.css(css)
.css({ cursor: series.options.cursor });
}
}
});
series._hasTracking = true;
}
fireEvent(this, 'afterDrawTracker');
},
/**
* Draw the tracker object that sits above all data labels and markers to
* track mouse events on the graph or points. For the line type charts
* the tracker uses the same graphPath, but with a greater stroke width
* for better control.
*
* @private
* @function Highcharts.TrackerMixin.drawTrackerGraph
*
* @fires Highcharts.Series#event:afterDrawTracker
*/
drawTrackerGraph: function () {
var series = this,
options = series.options,
trackByArea = options.trackByArea,
trackerPath = [].concat(
trackByArea ? series.areaPath : series.graphPath
),
trackerPathLength = trackerPath.length,
chart = series.chart,
pointer = chart.pointer,
renderer = chart.renderer,
snap = chart.options.tooltip.snap,
tracker = series.tracker,
i,
onMouseOver = function () {
if (chart.hoverSeries !== series) {
series.onMouseOver();
}
},
/*
* Empirical lowest possible opacities for TRACKER_FILL for an
* element to stay invisible but clickable
* IE6: 0.002
* IE7: 0.002
* IE8: 0.002
* IE9: 0.00000000001 (unlimited)
* IE10: 0.0001 (exporting only)
* FF: 0.00000000001 (unlimited)
* Chrome: 0.000001
* Safari: 0.000001
* Opera: 0.00000000001 (unlimited)
*/
TRACKER_FILL = 'rgba(192,192,192,' + (svg ? 0.0001 : 0.002) + ')';
// Extend end points. A better way would be to use round linecaps,
// but those are not clickable in VML.
if (trackerPathLength && !trackByArea) {
i = trackerPathLength + 1;
while (i--) {
if (trackerPath[i] === 'M') { // extend left side
trackerPath.splice(
i + 1, 0,
trackerPath[i + 1] - snap,
trackerPath[i + 2],
'L'
);
}
if (
(i && trackerPath[i] === 'M') ||
i === trackerPathLength
) { // extend right side
trackerPath.splice(
i,
0,
'L',
trackerPath[i - 2] + snap,
trackerPath[i - 1]
);
}
}
}
// draw the tracker
if (tracker) {
tracker.attr({ d: trackerPath });
} else if (series.graph) { // create
series.tracker = renderer.path(trackerPath)
.attr({
'stroke-linejoin': 'round', // #1225
stroke: TRACKER_FILL,
fill: trackByArea ? TRACKER_FILL : 'none',
'stroke-width': series.graph.strokeWidth() +
(trackByArea ? 0 : 2 * snap),
visibility: series.visible ? 'visible' : 'hidden',
zIndex: 2
})
.addClass(
trackByArea ?
'highcharts-tracker-area' :
'highcharts-tracker-line'
)
.add(series.group);
// The tracker is added to the series group, which is clipped, but
// is covered by the marker group. So the marker group also needs to
// capture events.
each([series.tracker, series.markerGroup], function (tracker) {
tracker.addClass('highcharts-tracker')
.on('mouseover', onMouseOver)
.on('mouseout', function (e) {
pointer.onTrackerMouseOut(e);
});
if (options.cursor) {
tracker.css({ cursor: options.cursor });
}
if (hasTouch) {
tracker.on('touchstart', onMouseOver);
}
});
}
fireEvent(this, 'afterDrawTracker');
}
};
/* End TrackerMixin */
/*
* Add tracking event listener to the series group, so the point graphics
* themselves act as trackers
*/
if (seriesTypes.column) {
/**
* @private
* @borrows Highcharts.TrackerMixin.drawTrackerPoint as Highcharts.seriesTypes.column#drawTracker
*/
seriesTypes.column.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
}
if (seriesTypes.pie) {
/**
* @private
* @borrows Highcharts.TrackerMixin.drawTrackerPoint as Highcharts.seriesTypes.pie#drawTracker
*/
seriesTypes.pie.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
}
if (seriesTypes.scatter) {
/**
* @private
* @borrows Highcharts.TrackerMixin.drawTrackerPoint as Highcharts.seriesTypes.scatter#drawTracker
*/
seriesTypes.scatter.prototype.drawTracker = TrackerMixin.drawTrackerPoint;
}
// Add pointer cursor to legend itemstyle in defaultOptions
/**
* @default 'pointer'
* @apioption legend.itemStyle.cursor
*/
defaultOptions.legend.itemStyle.cursor = 'pointer';
// Extend Legend for item events.
extend(Legend.prototype, {
/**
* @private
* @function Highcharts.Legend#setItemEvents
*
* @param {Highcharts.Point|Highcharts.Series} item
*
* @param {Highcharts.SVGElement} legendItem
*
* @param {boolean} [useHTML=false]
*
* @fires Highcharts.Point#event:legendItemClick
* @fires Highcharts.Series#event:legendItemClick
*/
setItemEvents: function (item, legendItem, useHTML) {
var legend = this,
boxWrapper = legend.chart.renderer.boxWrapper,
activeClass = 'highcharts-legend-' +
(item instanceof Point ? 'point' : 'series') + '-active';
// Set the events on the item group, or in case of useHTML, the item
// itself (#1249)
(useHTML ? legendItem : item.legendGroup).on('mouseover', function () {
item.setState('hover');
// A CSS class to dim or hide other than the hovered series
boxWrapper.addClass(activeClass);
legendItem.css(legend.options.itemHoverStyle);
})
.on('mouseout', function () {
legendItem.css(
merge(item.visible ? legend.itemStyle : legend.itemHiddenStyle)
);
// A CSS class to dim or hide other than the hovered series
boxWrapper.removeClass(activeClass);
item.setState();
})
.on('click', function (event) {
var strLegendItemClick = 'legendItemClick',
fnLegendItemClick = function () {
if (item.setVisible) {
item.setVisible();
}
};
// A CSS class to dim or hide other than the hovered series. Event
// handling in iOS causes the activeClass to be added prior to click
// in some cases (#7418).
boxWrapper.removeClass(activeClass);
// Pass over the click/touch event. #4.
event = {
browserEvent: event
};
// click the name or symbol
if (item.firePointEvent) { // point
item.firePointEvent(
strLegendItemClick,
event,
fnLegendItemClick
);
} else {
fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
}
});
},
/**
* @private
* @function Highcharts.Legend#createCheckboxForItem
*
* @param {Highcharts.Point|Highcharts.Series} item
*
* @fires Highcharts.Series#event:checkboxClick
*/
createCheckboxForItem: function (item) {
var legend = this;
item.checkbox = createElement('input', {
type: 'checkbox',
className: 'highcharts-legend-checkbox',
checked: item.selected,
defaultChecked: item.selected // required by IE7
}, legend.options.itemCheckboxStyle, legend.chart.container);
addEvent(item.checkbox, 'click', function (event) {
var target = event.target;
fireEvent(
item.series || item,
'checkboxClick',
{ // #3712
checked: target.checked,
item: item
},
function () {
item.select();
}
);
});
}
});
// Extend the Chart object with interaction.
extend(Chart.prototype, /** @lends Chart.prototype */ {
/**
* Display the zoom button.
*
* @private
* @function Highcharts.Chart#showResetZoom
*
* @fires Highcharts.Chart#event:beforeShowResetZoom
*/
showResetZoom: function () {
var chart = this,
lang = defaultOptions.lang,
btnOptions = chart.options.chart.resetZoomButton,
theme = btnOptions.theme,
states = theme.states,
alignTo = btnOptions.relativeTo === 'chart' ? null : 'plotBox';
function zoomOut() {
chart.zoomOut();
}
fireEvent(this, 'beforeShowResetZoom', null, function () {
chart.resetZoomButton = chart.renderer.button(
lang.resetZoom,
null,
null,
zoomOut,
theme,
states && states.hover
)
.attr({
align: btnOptions.position.align,
title: lang.resetZoomTitle
})
.addClass('highcharts-reset-zoom')
.add()
.align(btnOptions.position, false, alignTo);
});
},
/**
* Zoom the chart out after a user has zoomed in. See also
* [Axis.setExtremes](/class-reference/Highcharts.Axis#setExtremes).
*
* @function Highcharts.Chart#zoomOut
*
* @fires Highcharts.Chart#event:selection
*/
zoomOut: function () {
fireEvent(this, 'selection', { resetSelection: true }, this.zoom);
},
/**
* Zoom into a given portion of the chart given by axis coordinates.
*
* @private
* @function Highcharts.Chart#zoom
*
* @param {Highcharts.SelectEventObject} event
*/
zoom: function (event) {
var chart = this,
hasZoomed,
pointer = chart.pointer,
displayButton = false,
resetZoomButton;
// If zoom is called with no arguments, reset the axes
if (!event || event.resetSelection) {
each(chart.axes, function (axis) {
hasZoomed = axis.zoom();
});
pointer.initiated = false; // #6804
} else { // else, zoom in on all axes
each(event.xAxis.concat(event.yAxis), function (axisData) {
var axis = axisData.axis,
isXAxis = axis.isXAxis;
// don't zoom more than minRange
if (pointer[isXAxis ? 'zoomX' : 'zoomY']) {
hasZoomed = axis.zoom(axisData.min, axisData.max);
if (axis.displayBtn) {
displayButton = true;
}
}
});
}
// Show or hide the Reset zoom button
resetZoomButton = chart.resetZoomButton;
if (displayButton && !resetZoomButton) {
chart.showResetZoom();
} else if (!displayButton && isObject(resetZoomButton)) {
chart.resetZoomButton = resetZoomButton.destroy();
}
// Redraw
if (hasZoomed) {
chart.redraw(
pick(
chart.options.chart.animation,
event && event.animation,
chart.pointCount < 100
)
);
}
},
/**
* Pan the chart by dragging the mouse across the pane. This function is
* called on mouse move, and the distance to pan is computed from chartX
* compared to the first chartX position in the dragging operation.
*
* @private
* @function Highcharts.Chart#pan
*
* @param {Highcharts.PointerEventObject} e
*
* @param {string} panning
*/
pan: function (e, panning) {
var chart = this,
hoverPoints = chart.hoverPoints,
doRedraw;
// remove active points for shared tooltip
if (hoverPoints) {
each(hoverPoints, function (point) {
point.setState();
});
}
// xy is used in maps
each(panning === 'xy' ? [1, 0] : [1], function (isX) {
var axis = chart[isX ? 'xAxis' : 'yAxis'][0],
horiz = axis.horiz,
mousePos = e[horiz ? 'chartX' : 'chartY'],
mouseDown = horiz ? 'mouseDownX' : 'mouseDownY',
startPos = chart[mouseDown],
halfPointRange = (axis.pointRange || 0) / 2,
pointRangeDirection =
(axis.reversed && !chart.inverted) ||
(!axis.reversed && chart.inverted) ?
-1 :
1,
extremes = axis.getExtremes(),
panMin = axis.toValue(startPos - mousePos, true) +
halfPointRange * pointRangeDirection,
panMax = axis.toValue(startPos + axis.len - mousePos, true) -
halfPointRange * pointRangeDirection,
flipped = panMax < panMin,
newMin = flipped ? panMax : panMin,
newMax = flipped ? panMin : panMax,
paddedMin = Math.min(
extremes.dataMin,
halfPointRange ?
extremes.min :
axis.toValue(
axis.toPixels(extremes.min) - axis.minPixelPadding
)
),
paddedMax = Math.max(
extremes.dataMax,
halfPointRange ?
extremes.max :
axis.toValue(
axis.toPixels(extremes.max) + axis.minPixelPadding
)
),
spill;
// If the new range spills over, either to the min or max, adjust
// the new range.
spill = paddedMin - newMin;
if (spill > 0) {
newMax += spill;
newMin = paddedMin;
}
spill = newMax - paddedMax;
if (spill > 0) {
newMax = paddedMax;
newMin -= spill;
}
// Set new extremes if they are actually new
if (
axis.series.length &&
newMin !== extremes.min &&
newMax !== extremes.max
) {
axis.setExtremes(
newMin,
newMax,
false,
false,
{ trigger: 'pan' }
);
doRedraw = true;
}
chart[mouseDown] = mousePos; // set new reference for next run
});
if (doRedraw) {
chart.redraw(false);
}
css(chart.container, { cursor: 'move' });
}
});
// Extend the Point object with interaction
extend(Point.prototype, /** @lends Highcharts.Point.prototype */ {
/**
* Toggle the selection status of a point.
*
* @see Highcharts.Chart#getSelectedPoints
*
* @sample highcharts/members/point-select/
* Select a point from a button
* @sample highcharts/chart/events-selection-points/
* Select a range of points through a drag selection
* @sample maps/series/data-id/
* Select a point in Highmaps
*
* @function Highcharts.Point#select
*
* @param {boolean} [selected]
* When `true`, the point is selected. When `false`, the point is
* unselected. When `null` or `undefined`, the selection state is
* toggled.
*
* @param {boolean} [accumulate=false]
* When `true`, the selection is added to other selected points.
* When `false`, other selected points are deselected. Internally in
* Highcharts, when
* {@link http://api.highcharts.com/highcharts/plotOptions.series.allowPointSelect|allowPointSelect}
* is `true`, selected points are accumulated on Control, Shift or
* Cmd clicking the point.
*
* @fires Highcharts.Point#event:select
* @fires Highcharts.Point#event:unselect
*/
select: function (selected, accumulate) {
var point = this,
series = point.series,
chart = series.chart;
selected = pick(selected, !point.selected);
// fire the event with the default handler
point.firePointEvent(
selected ? 'select' : 'unselect',
{ accumulate: accumulate },
function () {
/**
* Whether the point is selected or not.
*
* @see Point#select
* @see Chart#getSelectedPoints
*
* @name Highcharts.Point#selected
* @type {boolean}
*/
point.selected = point.options.selected = selected;
series.options.data[inArray(point, series.data)] =
point.options;
point.setState(selected && 'select');
// unselect all other points unless Ctrl or Cmd + click
if (!accumulate) {
each(chart.getSelectedPoints(), function (loopPoint) {
if (loopPoint.selected && loopPoint !== point) {
loopPoint.selected = loopPoint.options.selected =
false;
series.options.data[
inArray(loopPoint, series.data)
] = loopPoint.options;
loopPoint.setState('');
loopPoint.firePointEvent('unselect');
}
});
}
}
);
},
/**
* Runs on mouse over the point. Called internally from mouse and touch
* events.
*
* @function Highcharts.Point#onMouseOver
*
* @param {Highcharts.PointerEventObject} e
* The event arguments.
*/
onMouseOver: function (e) {
var point = this,
series = point.series,
chart = series.chart,
pointer = chart.pointer;
e = e ?
pointer.normalize(e) :
// In cases where onMouseOver is called directly without an event
pointer.getChartCoordinatesFromPoint(point, chart.inverted);
pointer.runPointActions(e, point);
},
/**
* Runs on mouse out from the point. Called internally from mouse and touch
* events.
*
* @function Highcharts.Point#onMouseOut
*
* @fires Highcharts.Point#event:mouseOut
*/
onMouseOut: function () {
var point = this,
chart = point.series.chart;
point.firePointEvent('mouseOut');
each(chart.hoverPoints || [], function (p) {
p.setState();
});
chart.hoverPoints = chart.hoverPoint = null;
},
/**
* Import events from the series' and point's options. Only do it on
* demand, to save processing time on hovering.
*
* @private
* @function Highcharts.Point#importEvents
*/
importEvents: function () {
if (!this.hasImportedEvents) {
var point = this,
options = merge(point.series.options.point, point.options),
events = options.events;
point.events = events;
H.objectEach(events, function (event, eventType) {
addEvent(point, eventType, event);
});
this.hasImportedEvents = true;
}
},
/**
* Set the point's state.
*
* @function Highcharts.Point#setState
*
* @param {string} [state]
* The new state, can be one of `''` (an empty string), `hover` or
* `select`.
*
* @param {boolean} [move]
* State for animation.
*
* @fires Highcharts.Point#event:afterSetState
*/
setState: function (state, move) {
var point = this,
plotX = Math.floor(point.plotX), // #4586
plotY = point.plotY,
series = point.series,
stateOptions = series.options.states[state || 'normal'] || {},
markerOptions = defaultPlotOptions[series.type].marker &&
series.options.marker,
normalDisabled = markerOptions && markerOptions.enabled === false,
markerStateOptions = (
markerOptions &&
markerOptions.states &&
markerOptions.states[state || 'normal']
) || {},
stateDisabled = markerStateOptions.enabled === false,
stateMarkerGraphic = series.stateMarkerGraphic,
pointMarker = point.marker || {},
chart = series.chart,
halo = series.halo,
haloOptions,
markerAttribs,
hasMarkers = markerOptions && series.markerAttribs,
newSymbol;
state = state || ''; // empty string
if (
// already has this state
(state === point.state && !move) ||
// selected points don't respond to hover
(point.selected && state !== 'select') ||
// series' state options is disabled
(stateOptions.enabled === false) ||
// general point marker's state options is disabled
(state && (
stateDisabled ||
(normalDisabled && markerStateOptions.enabled === false)
)) ||
// individual point marker's state options is disabled
(
state &&
pointMarker.states &&
pointMarker.states[state] &&
pointMarker.states[state].enabled === false
) // #1610
) {
return;
}
if (hasMarkers) {
markerAttribs = series.markerAttribs(point, state);
}
// Apply hover styles to the existing point
if (point.graphic) {
if (point.state) {
point.graphic.removeClass('highcharts-point-' + point.state);
}
if (state) {
point.graphic.addClass('highcharts-point-' + state);
}
point.graphic.animate(
series.pointAttribs(point, state),
pick(
chart.options.chart.animation,
stateOptions.animation
)
);
if (markerAttribs) {
point.graphic.animate(
markerAttribs,
pick(
chart.options.chart.animation, // Turn off globally
markerStateOptions.animation,
markerOptions.animation
)
);
}
// Zooming in from a range with no markers to a range with markers
if (stateMarkerGraphic) {
stateMarkerGraphic.hide();
}
} else {
// if a graphic is not applied to each point in the normal state,
// create a shared graphic for the hover state
if (state && markerStateOptions) {
newSymbol = pointMarker.symbol || series.symbol;
// If the point has another symbol than the previous one, throw
// away the state marker graphic and force a new one (#1459)
if (
stateMarkerGraphic &&
stateMarkerGraphic.currentSymbol !== newSymbol
) {
stateMarkerGraphic = stateMarkerGraphic.destroy();
}
// Add a new state marker graphic
if (!stateMarkerGraphic) {
if (newSymbol) {
series.stateMarkerGraphic = stateMarkerGraphic =
chart.renderer.symbol(
newSymbol,
markerAttribs.x,
markerAttribs.y,
markerAttribs.width,
markerAttribs.height
)
.add(series.markerGroup);
stateMarkerGraphic.currentSymbol = newSymbol;
}
// Move the existing graphic
} else {
stateMarkerGraphic[move ? 'animate' : 'attr']({ // #1054
x: markerAttribs.x,
y: markerAttribs.y
});
}
if (stateMarkerGraphic) {
stateMarkerGraphic.attr(series.pointAttribs(point, state));
}
}
if (stateMarkerGraphic) {
stateMarkerGraphic[
state && chart.isInsidePlot(plotX, plotY, chart.inverted) ?
'show' :
'hide'
](); // #2450
stateMarkerGraphic.element.point = point; // #4310
}
}
// Show me your halo
haloOptions = stateOptions.halo;
if (haloOptions && haloOptions.size) {
if (!halo) {
series.halo = halo = chart.renderer.path()
// #5818, #5903, #6705
.add((point.graphic || stateMarkerGraphic).parentGroup);
}
halo.show()[move ? 'animate' : 'attr']({
d: point.haloPath(haloOptions.size)
});
halo.attr({
'class': 'highcharts-halo highcharts-color-' +
pick(point.colorIndex, series.colorIndex) +
(point.className ? ' ' + point.className : ''),
'zIndex': -1 // #4929, #8276
});
halo.point = point; // #6055
halo.attr(extend({
'fill': point.color || series.color,
'fill-opacity': haloOptions.opacity
}, haloOptions.attributes));
} else if (halo && halo.point && halo.point.haloPath) {
// Animate back to 0 on the current halo point (#6055)
halo.animate(
{ d: halo.point.haloPath(0) },
null,
// Hide after unhovering. The `complete` callback runs in the
// halo's context (#7681).
halo.hide
);
}
point.state = state;
fireEvent(point, 'afterSetState');
},
/**
* Get the path definition for the halo, which is usually a shadow-like
* circle around the currently hovered point.
*
* @function Highcharts.Point#haloPath
*
* @param {number} size
* The radius of the circular halo.
*
* @return {Highcharts.SVGPathArray}
* The path definition.
*/
haloPath: function (size) {
var series = this.series,
chart = series.chart;
return chart.renderer.symbols.circle(
Math.floor(this.plotX) - size,
this.plotY - size,
size * 2,
size * 2
);
}
});
// Extend the Series object with interaction
extend(Series.prototype, /** @lends Highcharts.Series.prototype */ {
/**
* Runs on mouse over the series graphical items.
*
* @function Highcharts.Series#onMouseOver
*
* @fires Highcharts.Series#event:mouseOver
*/
onMouseOver: function () {
var series = this,
chart = series.chart,
hoverSeries = chart.hoverSeries;
// set normal state to previous series
if (hoverSeries && hoverSeries !== series) {
hoverSeries.onMouseOut();
}
// trigger the event, but to save processing time,
// only if defined
if (series.options.events.mouseOver) {
fireEvent(series, 'mouseOver');
}
// hover this
series.setState('hover');
chart.hoverSeries = series;
},
/**
* Runs on mouse out of the series graphical items.
*
* @function Highcharts.Series#onMouseOut
*
* @fires Highcharts.Series#event:mouseOut
*/
onMouseOut: function () {
// trigger the event only if listeners exist
var series = this,
options = series.options,
chart = series.chart,
tooltip = chart.tooltip,
hoverPoint = chart.hoverPoint;
// #182, set to null before the mouseOut event fires
chart.hoverSeries = null;
// trigger mouse out on the point, which must be in this series
if (hoverPoint) {
hoverPoint.onMouseOut();
}
// fire the mouse out event
if (series && options.events.mouseOut) {
fireEvent(series, 'mouseOut');
}
// hide the tooltip
if (
tooltip &&
!series.stickyTracking &&
(!tooltip.shared || series.noSharedTooltip)
) {
tooltip.hide();
}
// set normal state
series.setState();
},
/**
* Set the state of the series. Called internally on mouse interaction
* operations, but it can also be called directly to visually
* highlight a series.
*
* @function Highcharts.Series#setState
*
* @param {string} [state]
* Can be either `hover` or undefined to set to normal state.
*/
setState: function (state) {
var series = this,
options = series.options,
graph = series.graph,
stateOptions = options.states,
lineWidth = options.lineWidth,
attribs,
i = 0;
state = state || '';
if (series.state !== state) {
// Toggle class names
each([
series.group,
series.markerGroup,
series.dataLabelsGroup
], function (group) {
if (group) {
// Old state
if (series.state) {
group.removeClass('highcharts-series-' + series.state);
}
// New state
if (state) {
group.addClass('highcharts-series-' + state);
}
}
});
series.state = state;
if (stateOptions[state] && stateOptions[state].enabled === false) {
return;
}
if (state) {
lineWidth = (
stateOptions[state].lineWidth ||
lineWidth + (stateOptions[state].lineWidthPlus || 0)
); // #4035
}
if (graph && !graph.dashstyle) {
attribs = {
'stroke-width': lineWidth
};
// Animate the graph stroke-width. By default a quick animation
// to hover, slower to un-hover.
graph.animate(
attribs,
pick(
(
stateOptions[state || 'normal'] &&
stateOptions[state || 'normal'].animation
),
series.chart.options.chart.animation
)
);
while (series['zone-graph-' + i]) {
series['zone-graph-' + i].attr(attribs);
i = i + 1;
}
}
}
},
/**
* Show or hide the series.
*
* @function Highcharts.Series#setVisible
*
* @param {boolean} [visible]
* True to show the series, false to hide. If undefined, the
* visibility is toggled.
*
* @param {boolean} [redraw=true]
* Whether to redraw the chart after the series is altered. If doing
* more operations on the chart, it is a good idea to set redraw to
* false and call {@link Chart#redraw|chart.redraw()} after.
*
* @fires Highcharts.Series#event:hide
* @fires Highcharts.Series#event:show
*/
setVisible: function (vis, redraw) {
var series = this,
chart = series.chart,
legendItem = series.legendItem,
showOrHide,
ignoreHiddenSeries = chart.options.chart.ignoreHiddenSeries,
oldVisibility = series.visible;
// if called without an argument, toggle visibility
series.visible =
vis =
series.options.visible =
series.userOptions.visible =
vis === undefined ? !oldVisibility : vis; // #5618
showOrHide = vis ? 'show' : 'hide';
// show or hide elements
each([
'group',
'dataLabelsGroup',
'markerGroup',
'tracker',
'tt'
], function (key) {
if (series[key]) {
series[key][showOrHide]();
}
});
// hide tooltip (#1361)
if (
chart.hoverSeries === series ||
(chart.hoverPoint && chart.hoverPoint.series) === series
) {
series.onMouseOut();
}
if (legendItem) {
chart.legend.colorizeItem(series, vis);
}
// rescale or adapt to resized chart
series.isDirty = true;
// in a stack, all other series are affected
if (series.options.stacking) {
each(chart.series, function (otherSeries) {
if (otherSeries.options.stacking && otherSeries.visible) {
otherSeries.isDirty = true;
}
});
}
// show or hide linked series
each(series.linkedSeries, function (otherSeries) {
otherSeries.setVisible(vis, false);
});
if (ignoreHiddenSeries) {
chart.isDirtyBox = true;
}
fireEvent(series, showOrHide);
if (redraw !== false) {
chart.redraw();
}
},
/**
* Show the series if hidden.
*
* @sample highcharts/members/series-hide/
* Toggle visibility from a button
*
* @function Highcharts.Series#show
*
* @fires Highcharts.Series#event:show
*/
show: function () {
this.setVisible(true);
},
/**
* Hide the series if visible. If the {@link
* https://api.highcharts.com/highcharts/chart.ignoreHiddenSeries|
* chart.ignoreHiddenSeries} option is true, the chart is redrawn without
* this series.
*
* @sample highcharts/members/series-hide/
* Toggle visibility from a button
*
* @function Highcharts.Series#hide
*
* @fires Highcharts.Series#event:hide
*/
hide: function () {
this.setVisible(false);
},
/**
* Select or unselect the series. This means its
* {@link Highcharts.Series.selected|selected}
* property is set, the checkbox in the legend is toggled and when selected,
* the series is returned by the
* {@link Highcharts.Chart#getSelectedSeries}
* function.
*
* @sample highcharts/members/series-select/
* Select a series from a button
*
* @function Highcharts.Series#select
*
* @param {boolean} [selected]
* True to select the series, false to unselect. If undefined, the
* selection state is toggled.
*
* @fires Highcharts.Series#event:select
* @fires Highcharts.Series#event:unselect
*/
select: function (selected) {
var series = this;
series.selected = selected = (selected === undefined) ?
!series.selected :
selected;
if (series.checkbox) {
series.checkbox.checked = selected;
}
fireEvent(series, selected ? 'select' : 'unselect');
},
/**
* @private
* @borrows Highcharts.TrackerMixin.drawTrackerGraph as Highcharts.Series#drawTracker
*/
drawTracker: TrackerMixin.drawTrackerGraph
});