Tooltip.js
36.1 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
/**
* (c) 2010-2018 Torstein Honsi
*
* License: www.highcharts.com/license
*/
'use strict';
import H from './Globals.js';
import './Utilities.js';
var doc = H.doc,
each = H.each,
extend = H.extend,
format = H.format,
isNumber = H.isNumber,
map = H.map,
merge = H.merge,
pick = H.pick,
splat = H.splat,
syncTimeout = H.syncTimeout,
timeUnits = H.timeUnits;
/**
* Tooltip of a chart.
*
* @class
* @name Highcharts.Tooltip
*
* @param {Highcharts.Chart} chart
* The chart instance.
*
* @param {Highcharts.TooltipOptions} options
* Tooltip options.
*/
H.Tooltip = function () {
this.init.apply(this, arguments);
};
H.Tooltip.prototype = {
/**
* @private
* @function Highcharts.Tooltip#init
*
* @param {Highcharts.Chart} chart
* The chart instance.
*
* @param {Highcharts.TooltipOptions} options
* Tooltip options.
*/
init: function (chart, options) {
/**
* Chart of the tooltip.
*
* @readonly
* @name Highcharts.Tooltip#chart
* @type {Highcharts.Chart}
*/
this.chart = chart;
/**
* Used tooltip options.
*
* @readonly
* @name Highcharts.Tooltip#options
* @type {Highcharts.TooltipOptions}
*/
this.options = options;
/**
* List of crosshairs.
*
* @private
* @readonly
* @name Highcharts.Tooltip#crosshairs
* @type {Array<*>}
*/
this.crosshairs = [];
/**
* Current values of x and y when animating.
*
* @private
* @readonly
* @name Highcharts.Tooltip#now
* @type {*}
*/
this.now = { x: 0, y: 0 };
/**
* Tooltips are initially hidden.
*
* @readonly
* @name Highcharts.Tooltip#isHidden
* @type {boolean}
*/
this.isHidden = true;
/**
* True, if the tooltip is splitted into one label per series, with the
* header close to the axis.
*
* @readonly
* @name Highcharts.Tooltip#split
* @type {boolean}
*/
this.split = options.split && !chart.inverted;
/**
* When the tooltip is shared, the entire plot area will capture mouse
* movement or touch events.
*
* @readonly
* @name Highcharts.Tooltip#shared
* @type {boolean}
*/
this.shared = options.shared || this.split;
/**
* Whether to allow the tooltip to render outside the chart's SVG
* element box. By default (false), the tooltip is rendered within the
* chart's SVG element, which results in the tooltip being aligned
* inside the chart area.
*
* @readonly
* @name Highcharts.Tooltip#outside
* @type {boolean}
*
* @todo
* Split tooltip does not support outside in the first iteration. Should
* not be too complicated to implement.
*/
this.outside = options.outside && !this.split;
},
/**
* Destroy the single tooltips in a split tooltip.
* If the tooltip is active then it is not destroyed, unless forced to.
*
* @private
* @function Highcharts.Tooltip#cleanSplit
*
* @param {boolean} force
* Force destroy all tooltips.
*/
cleanSplit: function (force) {
each(this.chart.series, function (series) {
var tt = series && series.tt;
if (tt) {
if (!tt.isActive || force) {
series.tt = tt.destroy();
} else {
tt.isActive = false;
}
}
});
},
/**
* In styled mode, apply the default filter for the tooltip drop-shadow. It
* needs to have an id specific to the chart, otherwise there will be issues
* when one tooltip adopts the filter of a different chart, specifically one
* where the container is hidden.
*
* @private
* @function Highcharts.Tooltip#applyFilter
*/
applyFilter: function () {
var chart = this.chart;
chart.renderer.definition({
tagName: 'filter',
id: 'drop-shadow-' + chart.index,
opacity: 0.5,
children: [{
tagName: 'feGaussianBlur',
in: 'SourceAlpha',
stdDeviation: 1
}, {
tagName: 'feOffset',
dx: 1,
dy: 1
}, {
tagName: 'feComponentTransfer',
children: [{
tagName: 'feFuncA',
type: 'linear',
slope: 0.3
}]
}, {
tagName: 'feMerge',
children: [{
tagName: 'feMergeNode'
}, {
tagName: 'feMergeNode',
in: 'SourceGraphic'
}]
}]
});
chart.renderer.definition({
tagName: 'style',
textContent: '.highcharts-tooltip-' + chart.index + '{' +
'filter:url(#drop-shadow-' + chart.index + ')' +
'}'
});
},
/**
* Creates the Tooltip label element if it does not exist, then returns it.
*
* @function Highcharts.Tooltip#getLabel
*
* @return {Highcharts.SVGElement}
*/
getLabel: function () {
var renderer = this.chart.renderer,
options = this.options,
container;
if (!this.label) {
if (this.outside) {
this.container = container = H.doc.createElement('div');
container.className = 'highcharts-tooltip-container';
H.css(container, {
position: 'absolute',
top: '1px',
pointerEvents: options.style && options.style.pointerEvents
});
H.doc.body.appendChild(container);
this.renderer = renderer = new H.Renderer(container, 0, 0);
}
// Create the label
if (this.split) {
this.label = renderer.g('tooltip');
} else {
this.label = renderer.label(
'',
0,
0,
options.shape || 'callout',
null,
null,
options.useHTML,
null,
'tooltip'
)
.attr({
padding: options.padding,
r: options.borderRadius
});
}
// Apply the drop-shadow filter
this.applyFilter();
this.label.addClass('highcharts-tooltip-' + this.chart.index);
if (this.outside) {
this.label.attr({
x: this.distance,
y: this.distance
});
this.label.xSetter = function (value) {
container.style.left = value + 'px';
};
this.label.ySetter = function (value) {
container.style.top = value + 'px';
};
}
this.label
.attr({
zIndex: 8
})
.add();
}
return this.label;
},
/**
* Updates the tooltip with the provided tooltip options.
*
* @function Highcharts.Tooltip#update
*
* @param {Highcharts.TooltipOptions} options
*/
update: function (options) {
this.destroy();
// Update user options (#6218)
merge(true, this.chart.options.tooltip.userOptions, options);
this.init(this.chart, merge(true, this.options, options));
},
/**
* Removes and destroys the tooltip and its elements.
*
* @function Highcharts.Tooltip#destroy
*/
destroy: function () {
// Destroy and clear local variables
if (this.label) {
this.label = this.label.destroy();
}
if (this.split && this.tt) {
this.cleanSplit(this.chart, true);
this.tt = this.tt.destroy();
}
if (this.renderer) {
this.renderer = this.renderer.destroy();
H.discardElement(this.container);
}
H.clearTimeout(this.hideTimer);
H.clearTimeout(this.tooltipTimeout);
},
/**
* Moves the tooltip with a soft animation to a new position.
*
* @function Highcharts.Tooltip#move
*
* @param {number} x
*
* @param {number} y
*
* @param {number} anchorX
*
* @param {number} anchorY
*/
move: function (x, y, anchorX, anchorY) {
var tooltip = this,
now = tooltip.now,
animate = tooltip.options.animation !== false &&
!tooltip.isHidden &&
// When we get close to the target position, abort animation and
// land on the right place (#3056)
(Math.abs(x - now.x) > 1 || Math.abs(y - now.y) > 1),
skipAnchor = tooltip.followPointer || tooltip.len > 1;
// Get intermediate values for animation
extend(now, {
x: animate ? (2 * now.x + x) / 3 : x,
y: animate ? (now.y + y) / 2 : y,
anchorX: skipAnchor ?
undefined :
animate ? (2 * now.anchorX + anchorX) / 3 : anchorX,
anchorY: skipAnchor ?
undefined :
animate ? (now.anchorY + anchorY) / 2 : anchorY
});
// Move to the intermediate value
tooltip.getLabel().attr(now);
// Run on next tick of the mouse tracker
if (animate) {
// Never allow two timeouts
H.clearTimeout(this.tooltipTimeout);
// Set the fixed interval ticking for the smooth tooltip
this.tooltipTimeout = setTimeout(function () {
// The interval function may still be running during destroy,
// so check that the chart is really there before calling.
if (tooltip) {
tooltip.move(x, y, anchorX, anchorY);
}
}, 32);
}
},
/**
* Hides the tooltip with a fade out animation.
*
* @function Highcharts.Tooltip#hide
*
* @param {number} [delay]
* The fade out in milliseconds. If no value is provided the value
* of the tooltip.hideDelay option is used. A value of 0 disables
* the fade out animation.
*/
hide: function (delay) {
var tooltip = this;
// disallow duplicate timers (#1728, #1766)
H.clearTimeout(this.hideTimer);
delay = pick(delay, this.options.hideDelay, 500);
if (!this.isHidden) {
this.hideTimer = syncTimeout(function () {
tooltip.getLabel()[delay ? 'fadeOut' : 'hide']();
tooltip.isHidden = true;
}, delay);
}
},
/**
* Extendable method to get the anchor position of the tooltip
* from a point or set of points
*
* @private
* @function Highcharts.Tooltip#getAnchor
*
* @param {Array<Highchart.Points>} points
*
* @param {global.Event} [mouseEvent]
*/
getAnchor: function (points, mouseEvent) {
var ret,
chart = this.chart,
pointer = chart.pointer,
inverted = chart.inverted,
plotTop = chart.plotTop,
plotLeft = chart.plotLeft,
plotX = 0,
plotY = 0,
yAxis,
xAxis;
points = splat(points);
// When tooltip follows mouse, relate the position to the mouse
if (this.followPointer && mouseEvent) {
if (mouseEvent.chartX === undefined) {
mouseEvent = pointer.normalize(mouseEvent);
}
ret = [
mouseEvent.chartX - chart.plotLeft,
mouseEvent.chartY - plotTop
];
// Pie uses a special tooltipPos
} else if (points[0].tooltipPos) {
ret = points[0].tooltipPos;
// When shared, use the average position
} else {
each(points, function (point) {
yAxis = point.series.yAxis;
xAxis = point.series.xAxis;
plotX += point.plotX +
(!inverted && xAxis ? xAxis.left - plotLeft : 0);
plotY +=
(
point.plotLow ?
(point.plotLow + point.plotHigh) / 2 :
point.plotY
) +
(!inverted && yAxis ? yAxis.top - plotTop : 0); // #1151
});
plotX /= points.length;
plotY /= points.length;
ret = [
inverted ? chart.plotWidth - plotY : plotX,
this.shared && !inverted && points.length > 1 && mouseEvent ?
// place shared tooltip next to the mouse (#424)
mouseEvent.chartY - plotTop :
inverted ? chart.plotHeight - plotX : plotY
];
}
return map(ret, Math.round);
},
/**
* Place the tooltip in a chart without spilling over
* and not covering the point it self.
*
* @private
* @function Highcharts.Tooltip#getPosition
*
* @param {number} boxWidth
*
* @param {number} boxHeight
*
* @param {Highcharts.Point} point
*
* @return {*}
*/
getPosition: function (boxWidth, boxHeight, point) {
var chart = this.chart,
distance = this.distance,
ret = {},
// Don't use h if chart isn't inverted (#7242)
h = (chart.inverted && point.h) || 0, // #4117
swapped,
outside = this.outside,
outerWidth = outside ?
// substract distance to prevent scrollbars
doc.documentElement.clientWidth - 2 * distance :
chart.chartWidth,
outerHeight = outside ?
Math.max(
doc.body.scrollHeight,
doc.documentElement.scrollHeight,
doc.body.offsetHeight,
doc.documentElement.offsetHeight,
doc.documentElement.clientHeight
) :
chart.chartHeight,
chartPosition = chart.pointer.chartPosition,
first = [
'y',
outerHeight,
boxHeight,
(outside ? chartPosition.top - distance : 0) +
point.plotY + chart.plotTop,
outside ? 0 : chart.plotTop,
outside ? outerHeight : chart.plotTop + chart.plotHeight
],
second = [
'x',
outerWidth,
boxWidth,
(outside ? chartPosition.left - distance : 0) +
point.plotX + chart.plotLeft,
outside ? 0 : chart.plotLeft,
outside ? outerWidth : chart.plotLeft + chart.plotWidth
],
// The far side is right or bottom
preferFarSide = !this.followPointer && pick(
point.ttBelow,
!chart.inverted === !!point.negative
), // #4984
/*
* Handle the preferred dimension. When the preferred dimension is
* tooltip on top or bottom of the point, it will look for space
* there.
*
* @private
*/
firstDimension = function (
dim,
outerSize,
innerSize,
point,
min,
max
) {
var roomLeft = innerSize < point - distance,
roomRight = point + distance + innerSize < outerSize,
alignedLeft = point - distance - innerSize,
alignedRight = point + distance;
if (preferFarSide && roomRight) {
ret[dim] = alignedRight;
} else if (!preferFarSide && roomLeft) {
ret[dim] = alignedLeft;
} else if (roomLeft) {
ret[dim] = Math.min(
max - innerSize,
alignedLeft - h < 0 ? alignedLeft : alignedLeft - h
);
} else if (roomRight) {
ret[dim] = Math.max(
min,
alignedRight + h + innerSize > outerSize ?
alignedRight :
alignedRight + h
);
} else {
return false;
}
},
/*
* Handle the secondary dimension. If the preferred dimension is
* tooltip on top or bottom of the point, the second dimension is to
* align the tooltip above the point, trying to align center but
* allowing left or right align within the chart box.
*
* @private
*/
secondDimension = function (dim, outerSize, innerSize, point) {
var retVal;
// Too close to the edge, return false and swap dimensions
if (point < distance || point > outerSize - distance) {
retVal = false;
// Align left/top
} else if (point < innerSize / 2) {
ret[dim] = 1;
// Align right/bottom
} else if (point > outerSize - innerSize / 2) {
ret[dim] = outerSize - innerSize - 2;
// Align center
} else {
ret[dim] = point - innerSize / 2;
}
return retVal;
},
/*
* Swap the dimensions
*/
swap = function (count) {
var temp = first;
first = second;
second = temp;
swapped = count;
},
run = function () {
if (firstDimension.apply(0, first) !== false) {
if (
secondDimension.apply(0, second) === false &&
!swapped
) {
swap(true);
run();
}
} else if (!swapped) {
swap(true);
run();
} else {
ret.x = ret.y = 0;
}
};
// Under these conditions, prefer the tooltip on the side of the point
if (chart.inverted || this.len > 1) {
swap();
}
run();
return ret;
},
/**
* In case no user defined formatter is given, this will be used. Note that
* the context here is an object holding point, series, x, y etc.
*
* @private
* @function Highcharts.Tooltip#defaultFormatter
*
* @param {Highcharts.Tooltip} tooltip
*
* @return {Array<string>}
*/
defaultFormatter: function (tooltip) {
var items = this.points || splat(this),
s;
// Build the header
s = [tooltip.tooltipFooterHeaderFormatter(items[0])];
// build the values
s = s.concat(tooltip.bodyFormatter(items));
// footer
s.push(tooltip.tooltipFooterHeaderFormatter(items[0], true));
return s;
},
/**
* Refresh the tooltip's text and position.
*
* @function Highcharts.Tooltip#refresh
*
* @param {Highcharts.Point|Array<Highcharts.Point>} pointOrPoints
* Either a point or an array of points.
*
* @param {global.Event} [mouseEvent]
* Mouse event, that is responsible for the refresh and should be
* used for the tooltip update.
*/
refresh: function (pointOrPoints, mouseEvent) {
var tooltip = this,
label,
options = tooltip.options,
x,
y,
point = pointOrPoints,
anchor,
textConfig = {},
text,
pointConfig = [],
formatter = options.formatter || tooltip.defaultFormatter,
shared = tooltip.shared,
currentSeries;
if (!options.enabled) {
return;
}
H.clearTimeout(this.hideTimer);
// get the reference point coordinates (pie charts use tooltipPos)
tooltip.followPointer = splat(point)[0].series.tooltipOptions
.followPointer;
anchor = tooltip.getAnchor(point, mouseEvent);
x = anchor[0];
y = anchor[1];
// shared tooltip, array is sent over
if (shared && !(point.series && point.series.noSharedTooltip)) {
each(point, function (item) {
item.setState('hover');
pointConfig.push(item.getLabelConfig());
});
textConfig = {
x: point[0].category,
y: point[0].y
};
textConfig.points = pointConfig;
point = point[0];
// single point tooltip
} else {
textConfig = point.getLabelConfig();
}
this.len = pointConfig.length; // #6128
text = formatter.call(textConfig, tooltip);
// register the current series
currentSeries = point.series;
this.distance = pick(currentSeries.tooltipOptions.distance, 16);
// update the inner HTML
if (text === false) {
this.hide();
} else {
label = tooltip.getLabel();
// show it
if (tooltip.isHidden) {
label.attr({
opacity: 1
}).show();
}
// update text
if (tooltip.split) {
this.renderSplit(text, splat(pointOrPoints));
} else {
// Prevent the tooltip from flowing over the chart box (#6659)
label.css({
width: this.chart.spacingBox.width
});
label.attr({
text: text && text.join ? text.join('') : text
});
// Set the stroke color of the box to reflect the point
label.removeClass(/highcharts-color-[\d]+/g)
.addClass(
'highcharts-color-' +
pick(point.colorIndex, currentSeries.colorIndex)
);
tooltip.updatePosition({
plotX: x,
plotY: y,
negative: point.negative,
ttBelow: point.ttBelow,
h: anchor[2] || 0
});
}
this.isHidden = false;
}
},
/**
* Render the split tooltip. Loops over each point's text and adds
* a label next to the point, then uses the distribute function to
* find best non-overlapping positions.
*
* @private
* @function Highcharts.Tooltip#renderSplit
*
* @param {Array<Highcharts.Label>} labels
*
* @param {Array<Highcharts.Point>} points
*/
renderSplit: function (labels, points) {
var tooltip = this,
boxes = [],
chart = this.chart,
ren = chart.renderer,
rightAligned = true,
options = this.options,
headerHeight = 0,
headerTop,
tooltipLabel = this.getLabel(),
distributionBoxTop = chart.plotTop;
// Graceful degradation for legacy formatters
if (H.isString(labels)) {
labels = [false, labels];
}
// Create the individual labels for header and points, ignore footer
each(labels.slice(0, points.length + 1), function (str, i) {
if (str !== false) {
var point = points[i - 1] ||
// Item 0 is the header. Instead of this, we could also
// use the crosshair label
{ isHeader: true, plotX: points[0].plotX },
owner = point.series || tooltip,
tt = owner.tt,
series = point.series || {},
colorClass = 'highcharts-color-' + pick(
point.colorIndex,
series.colorIndex,
'none'
),
target,
x,
bBox,
boxWidth;
// Store the tooltip referance on the series
if (!tt) {
owner.tt = tt = ren.label(
null,
null,
null,
'callout',
null,
null,
options.useHTML
)
.addClass(
'highcharts-tooltip-box ' + colorClass +
(point.isHeader ? ' highcharts-tooltip-header' : '')
)
.attr({
'padding': options.padding,
'r': options.borderRadius
})
.add(tooltipLabel);
}
tt.isActive = true;
tt.attr({
text: str
});
// Get X position now, so we can move all to the other side in
// case of overflow
bBox = tt.getBBox();
boxWidth = bBox.width + tt.strokeWidth();
if (point.isHeader) {
headerHeight = bBox.height;
if (chart.xAxis[0].opposite) {
headerTop = true;
distributionBoxTop -= headerHeight;
}
x = Math.max(
0, // No left overflow
Math.min(
point.plotX + chart.plotLeft - boxWidth / 2,
// No right overflow (#5794)
chart.chartWidth +
(
// Scrollable plot area
chart.scrollablePixels ?
chart.scrollablePixels - chart.marginRight :
0
) -
boxWidth
)
);
} else {
x = point.plotX + chart.plotLeft -
pick(options.distance, 16) - boxWidth;
}
// If overflow left, we don't use this x in the next loop
if (x < 0) {
rightAligned = false;
}
// Prepare for distribution
target = (point.series && point.series.yAxis &&
point.series.yAxis.pos) + (point.plotY || 0);
target -= distributionBoxTop;
if (point.isHeader) {
target = headerTop ?
-headerHeight :
chart.plotHeight + headerHeight;
}
boxes.push({
target: target,
rank: point.isHeader ? 1 : 0,
size: owner.tt.getBBox().height + 1,
point: point,
x: x,
tt: tt
});
}
});
// Clean previous run (for missing points)
this.cleanSplit();
// Distribute and put in place
H.distribute(boxes, chart.plotHeight + headerHeight);
each(boxes, function (box) {
var point = box.point,
series = point.series;
// Put the label in place
box.tt.attr({
visibility: box.pos === undefined ? 'hidden' : 'inherit',
x: (rightAligned || point.isHeader ?
box.x :
point.plotX + chart.plotLeft + pick(options.distance, 16)),
y: box.pos + distributionBoxTop,
anchorX: point.isHeader ?
point.plotX + chart.plotLeft :
point.plotX + series.xAxis.pos,
anchorY: point.isHeader ?
chart.plotTop + chart.plotHeight / 2 :
point.plotY + series.yAxis.pos
});
});
},
/**
* Find the new position and perform the move
*
* @private
* @function Highcharts.Tooltip#updatePosition
*
* @param {Highcharts.Point} point
*/
updatePosition: function (point) {
var chart = this.chart,
label = this.getLabel(),
pos = (this.options.positioner || this.getPosition).call(
this,
label.width,
label.height,
point
),
anchorX = point.plotX + chart.plotLeft,
anchorY = point.plotY + chart.plotTop,
pad;
// Set the renderer size dynamically to prevent document size to change
if (this.outside) {
pad = (this.options.borderWidth || 0) + 2 * this.distance;
this.renderer.setSize(
label.width + pad,
label.height + pad,
false
);
anchorX += chart.pointer.chartPosition.left - pos.x;
anchorY += chart.pointer.chartPosition.top - pos.y;
}
// do the move
this.move(
Math.round(pos.x),
Math.round(pos.y || 0), // can be undefined (#3977)
anchorX,
anchorY
);
},
/**
* Get the optimal date format for a point, based on a range.
*
* @private
* @function Highcharts.Tooltip#getDateFormat
*
* @param {number} range
* The time range
*
* @param {number|Date} date
* The date of the point in question
*
* @param {number} startOfWeek
* An integer representing the first day of the week, where 0 is
* Sunday.
*
* @param {Highcharts.Dictionary<string>} dateTimeLabelFormats
* A map of time units to formats.
*
* @return {string}
* The optimal date format for a point.
*/
getDateFormat: function (range, date, startOfWeek, dateTimeLabelFormats) {
var time = this.chart.time,
dateStr = time.dateFormat('%m-%d %H:%M:%S.%L', date),
format,
n,
blank = '01-01 00:00:00.000',
strpos = {
millisecond: 15,
second: 12,
minute: 9,
hour: 6,
day: 3
},
lastN = 'millisecond'; // for sub-millisecond data, #4223
for (n in timeUnits) {
// If the range is exactly one week and we're looking at a
// Sunday/Monday, go for the week format
if (
range === timeUnits.week &&
+time.dateFormat('%w', date) === startOfWeek &&
dateStr.substr(6) === blank.substr(6)
) {
n = 'week';
break;
}
// The first format that is too great for the range
if (timeUnits[n] > range) {
n = lastN;
break;
}
// If the point is placed every day at 23:59, we need to show
// the minutes as well. #2637.
if (
strpos[n] &&
dateStr.substr(strpos[n]) !== blank.substr(strpos[n])
) {
break;
}
// Weeks are outside the hierarchy, only apply them on
// Mondays/Sundays like in the first condition
if (n !== 'week') {
lastN = n;
}
}
if (n) {
format = time.resolveDTLFormat(dateTimeLabelFormats[n]).main;
}
return format;
},
/**
* Get the best X date format based on the closest point range on the axis.
*
* @private
* @function Highcharts.Tooltip#getXDateFormat
*
* @param {Highcharts.Point} point
*
* @param {Highcharts.TooltipOptions} options
*
* @param {Highcharts.Axis} xAxis
*
* @return {string}
*/
getXDateFormat: function (point, options, xAxis) {
var xDateFormat,
dateTimeLabelFormats = options.dateTimeLabelFormats,
closestPointRange = xAxis && xAxis.closestPointRange;
if (closestPointRange) {
xDateFormat = this.getDateFormat(
closestPointRange,
point.x,
xAxis.options.startOfWeek,
dateTimeLabelFormats
);
} else {
xDateFormat = dateTimeLabelFormats.day;
}
return xDateFormat || dateTimeLabelFormats.year; // #2546, 2581
},
/**
* Format the footer/header of the tooltip
* #3397: abstraction to enable formatting of footer and header
*
* @private
* @function Highcharts.Tooltip#tooltipFooterHeaderFormatter
*
* @param {*} labelConfig
*
* @param {boolean} isFooter
*
* @return {string}
*/
tooltipFooterHeaderFormatter: function (labelConfig, isFooter) {
var footOrHead = isFooter ? 'footer' : 'header',
series = labelConfig.series,
tooltipOptions = series.tooltipOptions,
xDateFormat = tooltipOptions.xDateFormat,
xAxis = series.xAxis,
isDateTime = (
xAxis &&
xAxis.options.type === 'datetime' &&
isNumber(labelConfig.key)
),
formatString = tooltipOptions[footOrHead + 'Format'];
// Guess the best date format based on the closest point distance (#568,
// #3418)
if (isDateTime && !xDateFormat) {
xDateFormat = this.getXDateFormat(
labelConfig,
tooltipOptions,
xAxis
);
}
// Insert the footer date format if any
if (isDateTime && xDateFormat) {
each(
(labelConfig.point && labelConfig.point.tooltipDateKeys) ||
['key'],
function (key) {
formatString = formatString.replace(
'{point.' + key + '}',
'{point.' + key + ':' + xDateFormat + '}'
);
}
);
}
return format(formatString, {
point: labelConfig,
series: series
}, this.chart.time);
},
/**
* Build the body (lines) of the tooltip by iterating over the items and
* returning one entry for each item, abstracting this functionality allows
* to easily overwrite and extend it.
*
* @private
* @function Highcharts.Tooltip#bodyFormatter
*
* @param {Array<Highcharts.Point>} items
*
* @return {string}
*/
bodyFormatter: function (items) {
return map(items, function (item) {
var tooltipOptions = item.series.tooltipOptions;
return (
tooltipOptions[
(item.point.formatPrefix || 'point') + 'Formatter'
] ||
item.point.tooltipFormatter
).call(
item.point,
tooltipOptions[(item.point.formatPrefix || 'point') + 'Format']
);
});
}
};