annotations.src.js
49.5 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
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
/**
* (c) 2009-2017 Highsoft, Black Label
*
* License: www.highcharts.com/license
*/
'use strict';
import H from '../parts/Globals.js';
import '../parts/Utilities.js';
import '../parts/Chart.js';
import '../parts/Series.js';
import '../parts/Tooltip.js';
var merge = H.merge,
addEvent = H.addEvent,
extend = H.extend,
each = H.each,
isString = H.isString,
isNumber = H.isNumber,
defined = H.defined,
isObject = H.isObject,
inArray = H.inArray,
erase = H.erase,
find = H.find,
format = H.format,
pick = H.pick,
objectEach = H.objectEach,
uniqueKey = H.uniqueKey,
doc = H.doc,
splat = H.splat,
destroyObjectProperties = H.destroyObjectProperties,
grep = H.grep,
tooltipPrototype = H.Tooltip.prototype,
seriesPrototype = H.Series.prototype,
chartPrototype = H.Chart.prototype;
/* ***************************************************************************
*
* MARKER SECTION
* Contains objects and functions for adding a marker element to a path element
*
**************************************************************************** */
/**
* Options for configuring markers for annotations.
*
* An example of the arrow marker:
* <pre>
* {
* arrow: {
* id: 'arrow',
* tagName: 'marker',
* refY: 5,
* refX: 5,
* markerWidth: 10,
* markerHeight: 10,
* children: [{
* tagName: 'path',
* attrs: {
* d: 'M 0 0 L 10 5 L 0 10 Z',
* strokeWidth: 0
* }
* }]
* }
* }
* </pre>
* @type {Object}
* @sample highcharts/annotations/custom-markers/
* Define a custom marker for annotations
* @sample highcharts/css/annotations-markers/
* Define markers in a styled mode
* @since 6.0.0
* @apioption defs
*/
var defaultMarkers = {
arrow: {
tagName: 'marker',
render: false,
id: 'arrow',
refY: 5,
refX: 5,
markerWidth: 10,
markerHeight: 10,
children: [{
tagName: 'path',
d: 'M 0 0 L 10 5 L 0 10 Z', // triangle (used as an arrow)
}]
}
};
var MarkerMixin = {
markerSetter: function (markerType) {
return function (value) {
this.attr(markerType, 'url(#' + value + ')');
};
}
};
extend(MarkerMixin, {
markerEndSetter: MarkerMixin.markerSetter('marker-end'),
markerStartSetter: MarkerMixin.markerSetter('marker-start')
});
H.SVGRenderer.prototype.addMarker = function (id, markerOptions) {
var options = { id: id };
var marker = this.definition(merge({
markerWidth: 20,
markerHeight: 20,
refX: 0,
refY: 0,
orient: 'auto'
}, markerOptions, options));
marker.id = id;
return marker;
};
/* ***************************************************************************
*
* MOCK POINT
*
**************************************************************************** */
/**
* A mock point configuration.
*
* @typedef {Object} MockPointOptions
* @property {Number} x - x value for the point in xAxis scale or pixels
* @property {Number} y - y value for the point in yAxis scale or pixels
* @property {String|Number} [xAxis] - xAxis index or id
* @property {String|Number} [yAxis] - yAxis index or id
*/
/**
* A trimmed point object which imitates {@link Highchart.Point} class.
* It is created when there is a need of pointing to some chart's position
* using axis values or pixel values
*
* @class MockPoint
* @memberof Highcharts
* @private
*
* @param {Highcharts.Chart} - the chart object
* @param {MockPointOptions} - the options object
*/
var MockPoint = H.MockPoint = function (chart, options) {
this.mock = true;
this.series = {
visible: true,
chart: chart,
getPlotBox: seriesPrototype.getPlotBox
};
// this.plotX
// this.plotY
/* Those might not exist if a specific axis was not found/defined */
// this.x?
// this.y?
this.init(chart, options);
};
/**
* A factory function for creating a mock point object
*
* @function #mockPoint
* @memberof Highcharts
*
* @param {MockPointOptions} mockPointOptions
* @return {MockPoint} a mock point
*/
var mockPoint = H.mockPoint = function (chart, mockPointOptions) {
return new MockPoint(chart, mockPointOptions);
};
MockPoint.prototype = {
/**
* Initialisation of the mock point
*
* @function init
* @memberof Highcharts.MockPoint#
*
* @param {Highcharts.Chart} chart - a chart object to which the mock point
* is attached
* @param {MockPointOptions} options - a config for the mock point
*/
init: function (chart, options) {
var xAxisId = options.xAxis,
xAxis = defined(xAxisId) ?
chart.xAxis[xAxisId] || chart.get(xAxisId) :
null,
yAxisId = options.yAxis,
yAxis = defined(yAxisId) ?
chart.yAxis[yAxisId] || chart.get(yAxisId) :
null;
if (xAxis) {
this.x = options.x;
this.series.xAxis = xAxis;
} else {
this.plotX = options.x;
}
if (yAxis) {
this.y = options.y;
this.series.yAxis = yAxis;
} else {
this.plotY = options.y;
}
},
/**
* Update of the point's coordinates (plotX/plotY)
*
* @function translate
* @memberof Highcharts.MockPoint#
*
* @return {undefined}
*/
translate: function () {
var series = this.series,
xAxis = series.xAxis,
yAxis = series.yAxis;
if (xAxis) {
this.plotX = xAxis.toPixels(this.x, true);
}
if (yAxis) {
this.plotY = yAxis.toPixels(this.y, true);
}
this.isInside = this.isInsidePane();
},
/**
* Returns a box to which an item can be aligned to
*
* @function #alignToBox
* @memberof Highcharts.MockPoint#
*
* @param {Boolean} [forceTranslate=false] - whether to update the point's
* coordinates
* @return {Array<Number>} A quadruple of numbers which denotes x, y,
* width and height of the box
**/
alignToBox: function (forceTranslate) {
if (forceTranslate) {
this.translate();
}
var x = this.plotX,
y = this.plotY,
temp;
if (this.series.chart.inverted) {
temp = x;
x = y;
y = temp;
}
return [x, y, 0, 0];
},
/**
* Returns a label config object -
* the same as Highcharts.Point.prototype.getLabelConfig
*
* @function getLabelConfig
* @memberof Highcharts.MockPoint#
*
* @return {Object} labelConfig - label config object
* @return {Number|undefined} labelConfig.x
* X value translated to x axis scale
* @return {Number|undefined} labelConfig.y
* Y value translated to y axis scale
* @return {MockPoint} labelConfig.point
* The instance of the point
*/
getLabelConfig: function () {
return {
x: this.x,
y: this.y,
point: this
};
},
isInsidePane: function () {
var plotX = this.plotX,
plotY = this.plotY,
xAxis = this.series.xAxis,
yAxis = this.series.yAxis,
isInside = true;
if (xAxis) {
isInside = defined(plotX) && plotX >= 0 && plotX <= xAxis.len;
}
if (yAxis) {
isInside =
isInside &&
defined(plotY) &&
plotY >= 0 && plotY <= yAxis.len;
}
return isInside;
}
};
/* ***************************************************************************
*
* ANNOTATION
*
**************************************************************************** */
H.defaultOptions.annotations = [];
/**
* An annotation class which serves as a container for items like labels or
* shapes. Created items are positioned on the chart either by linking them to
* existing points or created mock points
*
* @class Annotation
* @memberof Highcharts
*
* @param {Chart} - the chart object
* @param {AnnotationOptions} - the options object
*/
var Annotation = H.Annotation = function (chart, userOptions) {
/**
* The chart that the annotation belongs to.
*
* @name chart
* @memberof Highcharts.Annotation#
* @type {Chart}
*/
this.chart = chart;
/**
* The array of labels which belong to the annotation.
*
* @name labels
* @memberof Highcharts.Annotation#
* @type {Array<Highcharts.SVGElement>}
*/
this.labels = [];
/**
* The array of shapes which belong to the annotation.
*
* @name shapes
* @memberof Highcharts.Annotation#
* @type {Array<Highcharts.SVGElement>}
*/
this.shapes = [];
/**
* The user options for the annotations.
*
* @name options
* @memberof Highcharts.Annotation#
* @type {AnnotationOptions}
*/
this.userOptions = userOptions;
/**
* The options for the annotations. It contains user defined options
* merged with the default options.
*
* @name options
* @memberof Highcharts.Annotation#
* @type {AnnotationOptions}
*/
this.options = merge(this.defaultOptions, userOptions);
/**
* The callback that reports to the overlapping-labels module which
* labels it should account for.
*
* @name labelCollector
* @memberof Highcharts.Annotation#
* @type {Function}
* @private
*/
/**
* The group element of the annotation.
*
* @name group
* @memberof Highcharts.Annotation#
* @type {Highcharts.SVGElement}
* @private
*/
/**
* The group element of the annotation's shapes.
*
* @name shapesGroup
* @memberof Highcharts.Annotation#
* @type {Highcharts.SVGElement}
* @private
*/
/**
* The group element of the annotation's labels.
*
* @name labelsGroup
* @memberof Highcharts.Annotation#
* @type {Highcharts.SVGElement}
* @private
*/
this.init(chart, userOptions);
};
Annotation.prototype = /** @lends Highcharts.Annotation# */ {
/**
* Shapes which do not have background - the object is used for proper
* setting of the contrast color
*
* @type {Array<String>}
* @private
*/
shapesWithoutBackground: ['connector'],
/**
* A map object which allows to map options attributes to element
* attributes.
*
* @type {Object}
* @private
*/
attrsMap: {
zIndex: 'zIndex',
width: 'width',
height: 'height',
borderRadius: 'r',
r: 'r',
padding: 'padding'
},
/**
* Options for configuring annotations, for example labels, arrows or
* shapes. Annotations can be tied to points, axis coordinates or chart
* pixel coordinates.
*
* @private
* @type {Array<Object>}
* @sample highcharts/annotations/basic/
* Basic annotations
* @sample highcharts/demo/annotations/
* Advanced annotations
* @sample highcharts/css/annotations
* Styled mode
* @sample {highstock} stock/annotations/fibonacci-retracements
* Custom annotation, Fibonacci retracement
* @since 6.0.0
* @optionparent annotations
*/
defaultOptions: {
/**
* Whether the annotation is visible.
*
* @sample highcharts/annotations/visible/
* Set annotation visibility
*/
visible: true,
/**
* Options for annotation's labels. Each label inherits options
* from the labelOptions object. An option from the labelOptions can be
* overwritten by config for a specific label.
*/
labelOptions: {
/**
* The alignment of the annotation's label. If right,
* the right side of the label should be touching the point.
*
* @validvalue ["left", "center", "right"]
* @sample highcharts/annotations/label-position/
* Set labels position
*/
align: 'center',
/**
* Whether to allow the annotation's labels to overlap.
* To make the labels less sensitive for overlapping,
* the can be set to 0.
*
* @sample highcharts/annotations/tooltip-like/
* Hide overlapping labels
*/
allowOverlap: false,
/**
* The background color or gradient for the annotation's label.
*
* @type {Color}
* @sample highcharts/annotations/label-presentation/
* Set labels graphic options
*/
backgroundColor: 'rgba(0, 0, 0, 0.75)',
/**
* The border color for the annotation's label.
*
* @type {Color}
* @sample highcharts/annotations/label-presentation/
* Set labels graphic options
*/
borderColor: 'black',
/**
* The border radius in pixels for the annotaiton's label.
*
* @sample highcharts/annotations/label-presentation/
* Set labels graphic options
*/
borderRadius: 3,
/**
* The border width in pixels for the annotation's label
*
* @sample highcharts/annotations/label-presentation/
* Set labels graphic options
*/
borderWidth: 1,
/**
* A class name for styling by CSS.
*
* @sample highcharts/css/annotations
* Styled mode annotations
* @since 6.0.5
*/
className: '',
/**
* Whether to hide the annotation's label that is outside the plot
* area.
*
* @sample highcharts/annotations/label-crop-overflow/
* Crop or justify labels
*/
crop: false,
/**
* The label's pixel distance from the point.
*
* @type {Number}
* @sample highcharts/annotations/label-position/
* Set labels position
* @default undefined
* @apioption annotations.labelOptions.distance
*/
/**
* A [format](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting) string for the data label.
*
* @type {String}
* @see [plotOptions.series.dataLabels.format](
* plotOptions.series.dataLabels.format.html)
* @sample highcharts/annotations/label-text/
* Set labels text
* @default undefined
* @apioption annotations.labelOptions.format
*/
/**
* Alias for the format option.
*
* @type {String}
* @see [format](annotations.labelOptions.format.html)
* @sample highcharts/annotations/label-text/
* Set labels text
* @default undefined
* @apioption annotations.labelOptions.text
*/
/**
* Callback JavaScript function to format the annotation's label.
* Note that if a `format` or `text` are defined, the format or text
* take precedence and the formatter is ignored. `This` refers to a
* point object.
*
* @type {Function}
* @sample highcharts/annotations/label-text/
* Set labels text
* @default function () {
* return defined(this.y) ? this.y : 'Annotation label';
* }
*/
formatter: function () {
return defined(this.y) ? this.y : 'Annotation label';
},
/**
* How to handle the annotation's label that flow outside the plot
* area. The justify option aligns the label inside the plot area.
*
* @validvalue ["allow", "justify"]
* @sample highcharts/annotations/label-crop-overflow/
* Crop or justify labels
**/
overflow: 'justify',
/**
* When either the borderWidth or the backgroundColor is set,
* this is the padding within the box.
*
* @sample highcharts/annotations/label-presentation/
* Set labels graphic options
*/
padding: 5,
/**
* The shadow of the box. The shadow can be an object configuration
* containing `color`, `offsetX`, `offsetY`, `opacity` and `width`.
*
* @type {Boolean|Object}
* @sample highcharts/annotations/label-presentation/
* Set labels graphic options
*/
shadow: false,
/**
* The name of a symbol to use for the border around the label.
* Symbols are predefined functions on the Renderer object.
*
* @type {String}
* @sample highcharts/annotations/shapes/
* Available shapes for labels
*/
shape: 'callout',
/**
* Styles for the annotation's label.
*
* @type {CSSObject}
* @sample highcharts/annotations/label-presentation/
* Set labels graphic options
* @see [plotOptions.series.dataLabels.style](
* plotOptions.series.dataLabels.style.html)
*/
style: {
fontSize: '11px',
fontWeight: 'normal',
color: 'contrast'
},
/**
* Whether to [use HTML](https://www.highcharts.com/docs/chart-concepts/labels-and-string-formatting#html)
* to render the annotation's label.
*
* @type {Boolean}
* @default false
*/
useHTML: false,
/**
* The vertical alignment of the annotation's label.
*
* @type {String}
* @validvalue ["top", "middle", "bottom"]
* @sample highcharts/annotations/label-position/
* Set labels position
*/
verticalAlign: 'bottom',
/**
* The x position offset of the label relative to the point.
* Note that if a `distance` is defined, the distance takes
* precedence over `x` and `y` options.
*
* @sample highcharts/annotations/label-position/
* Set labels position
*/
x: 0,
/**
* The y position offset of the label relative to the point.
* Note that if a `distance` is defined, the distance takes
* precedence over `x` and `y` options.
*
* @sample highcharts/annotations/label-position/
* Set labels position
*/
y: -16
},
/**
* An array of labels for the annotation. For options that apply to
* multiple labels, they can be added to the
* [labelOptions](annotations.labelOptions.html).
*
* @type {Array<Object>}
* @extends annotations.labelOptions
* @apioption annotations.labels
*/
/**
* This option defines the point to which the label will be connected.
* It can be either the point which exists in the series - it is
* referenced by the point's id - or a new point with defined x, y
* properies and optionally axes.
*
* @type {String|Object}
* @sample highcharts/annotations/mock-point/
* Attach annotation to a mock point
* @apioption annotations.labels.point
*/
/**
* The x position of the point. Units can be either in axis
* or chart pixel coordinates.
*
* @type {Number}
* @apioption annotations.labels.point.x
*/
/**
* The y position of the point. Units can be either in axis
* or chart pixel coordinates.
*
* @type {Number}
* @apioption annotations.labels.point.y
*/
/**
* This number defines which xAxis the point is connected to. It refers
* to either the axis id or the index of the axis in the xAxis array.
* If the option is not configured or the axis is not found the point's
* x coordinate refers to the chart pixels.
*
* @type {Number|String}
* @apioption annotations.labels.point.xAxis
*/
/**
* This number defines which yAxis the point is connected to. It refers
* to either the axis id or the index of the axis in the yAxis array.
* If the option is not configured or the axis is not found the point's
* y coordinate refers to the chart pixels.
*
* @type {Number|String}
* @apioption annotations.labels.point.yAxis
*/
/**
* An array of shapes for the annotation. For options that apply to
* multiple shapes, then can be added to the
* [shapeOptions](annotations.shapeOptions.html).
*
* @type {Array<Object>}
* @extends annotations.shapeOptions
* @apioption annotations.shapes
*/
/**
* This option defines the point to which the shape will be connected.
* It can be either the point which exists in the series - it is
* referenced by the point's id - or a new point with defined x, y
* properties and optionally axes.
*
* @type {String|Object}
* @extends annotations.labels.point
* @apioption annotations.shapes.point
*/
/**
* An array of points for the shape. This option is available for shapes
* which can use multiple points such as path. A point can be either
* a point object or a point's id.
*
* @type {Array}
* @see [annotations.shapes.point](annotations.shapes.point.html)
* @apioption annotations.shapes.points
*/
/**
* Id of the marker which will be drawn at the final vertex of the path.
* Custom markers can be defined in defs property.
*
* @type {String}
* @see [defs.markers](defs.markers.html)
* @sample highcharts/annotations/custom-markers/
* Define a custom marker for annotations
* @apioption annotations.shapes.markerEnd
*/
/**
* Id of the marker which will be drawn at the first vertex of the path.
* Custom markers can be defined in defs property.
*
* @type {String}
* @see [defs.markers](defs.markers.html)
* @sample {highcharts} highcharts/annotations/custom-markers/
* Define a custom marker for annotations
* @apioption annotations.shapes.markerStart
*/
/**
* Options for annotation's shapes. Each shape inherits options
* from the shapeOptions object. An option from the shapeOptions can be
* overwritten by config for a specific shape.
*
* @type {Object}
*/
shapeOptions: {
/**
* The width of the shape.
*
* @type {Number}
* @sample highcharts/annotations/shape/
* Basic shape annotation
* @apioption annotations.shapeOptions.width
**/
/**
* The height of the shape.
*
* @type {Number}
* @sample highcharts/annotations/shape/
* Basic shape annotation
* @apioption annotations.shapeOptions.height
*/
/**
* The color of the shape's stroke.
*
* @type {Color}
* @sample highcharts/annotations/shape/
* Basic shape annotation
*/
stroke: 'rgba(0, 0, 0, 0.75)',
/**
* The pixel stroke width of the shape.
*
* @sample highcharts/annotations/shape/
* Basic shape annotation
*/
strokeWidth: 1,
/**
* The color of the shape's fill.
*
* @type {Color}
* @sample highcharts/annotations/shape/
* Basic shape annotation
*/
fill: 'rgba(0, 0, 0, 0.75)',
/**
* The type of the shape, e.g. circle or rectangle.
*
* @type {String}
* @sample highcharts/annotations/shape/
* Basic shape annotation
* @default 'rect'
* @apioption annotations.shapeOptions.type
*/
/**
* The radius of the shape.
*
* @sample highcharts/annotations/shape/
* Basic shape annotation
*/
r: 0
},
/**
* The Z index of the annotation.
*
* @type {Number}
* @default 6
*/
zIndex: 6
},
/**
* Initialize the annotation.
*
* @param {Chart} - the chart
* @param {AnnotationOptions} - the user options for the annotation
*/
init: function () {
var anno = this;
each(this.options.labels || [], this.initLabel, this);
each(this.options.shapes || [], this.initShape, this);
this.labelCollector = function () {
return grep(anno.labels, function (label) {
return !label.options.allowOverlap;
});
};
this.chart.labelCollectors.push(this.labelCollector);
},
/**
* Main method for drawing an annotation.
**/
redraw: function () {
if (!this.group) {
this.render();
}
this.redrawItems(this.shapes);
this.redrawItems(this.labels);
},
/**
* @private
* @param {Array<Object>} items
**/
redrawItems: function (items) {
var i = items.length;
// needs a backward loop
// labels/shapes array might be modified due to destruction of the item
while (i--) {
this.redrawItem(items[i]);
}
},
/**
* Render the annotation.
**/
render: function () {
var renderer = this.chart.renderer;
var group = this.group = renderer.g('annotation')
.attr({
zIndex: this.options.zIndex,
visibility: this.options.visible ? 'visible' : 'hidden'
})
.add();
this.shapesGroup = renderer.g('annotation-shapes').add(group);
this.labelsGroup = renderer.g('annotation-labels').attr({
// hideOverlappingLabels requires translation
translateX: 0,
translateY: 0
}).add(group);
this.shapesGroup.clip(this.chart.plotBoxClip);
},
/**
* Set the annotation's visibility.
*
* @param {Boolean} [visibility] - Whether to show or hide an annotation.
* If the param is omitted, the annotation's visibility is toggled.
**/
setVisible: function (visibility) {
var options = this.options,
visible = pick(visibility, !options.visible);
this.group.attr({
visibility: visible ? 'visible' : 'hidden'
});
options.visible = visible;
},
/**
* Destroy the annotation. This function does not touch the chart
* that the annotation belongs to (all annotations are kept in
* the chart.annotations array) - it is recommended to use
* {@link Highcharts.Chart#removeAnnotation} instead.
**/
destroy: function () {
var chart = this.chart,
destroyItem = function (item) {
item.destroy();
};
erase(this.chart.labelCollectors, this.labelCollector);
each(this.labels, destroyItem);
each(this.shapes, destroyItem);
destroyObjectProperties(this, chart);
},
/* ***********************************************************************
* ITEM SECTION
* Contains methods for handling a single item in an annotation
*********************************************************************** */
/**
* Initialisation of a single shape
*
* @private
* @param {Object} shapeOptions - a confg object for a single shape
**/
initShape: function (shapeOptions) {
var renderer = this.chart.renderer,
options = merge(this.options.shapeOptions, shapeOptions),
attr = this.attrsFromOptions(options),
type = renderer[options.type] ? options.type : 'rect',
shape = renderer[type](0, -9e9, 0, 0);
shape.points = [];
shape.type = type;
shape.options = options;
shape.itemType = 'shape';
if (type === 'path') {
extend(shape, {
markerStartSetter: MarkerMixin.markerStartSetter,
markerEndSetter: MarkerMixin.markerEndSetter,
markerStart: MarkerMixin.markerStart,
markerEnd: MarkerMixin.markerEnd
});
}
shape.attr(attr);
if (options.className) {
shape.addClass(options.className);
}
this.shapes.push(shape);
},
/**
* Initialisation of a single label
*
* @private
* @param {Object} labelOptions
**/
initLabel: function (labelOptions) {
var options = merge(this.options.labelOptions, labelOptions),
attr = this.attrsFromOptions(options),
label = this.chart.renderer.label(
'',
0, -9e9,
options.shape,
null,
null,
options.useHTML,
null,
'annotation-label'
);
label.points = [];
label.options = options;
label.itemType = 'label';
// Labelrank required for hideOverlappingLabels()
label.labelrank = options.labelrank;
label.annotation = this;
label.attr(attr);
if (options.className) {
label.addClass(options.className);
}
this.labels.push(label);
},
/**
* Redrawing a single item
*
* @private
* @param {SVGElement} item
*/
redrawItem: function (item) {
var points = this.linkPoints(item),
itemOptions = item.options,
text,
time = this.chart.time;
if (!points.length) {
this.destroyItem(item);
} else {
if (!item.parentGroup) {
this.renderItem(item);
}
if (item.itemType === 'label') {
text = itemOptions.format || itemOptions.text;
item.attr({
text: text ?
format(text, points[0].getLabelConfig(), time) :
itemOptions.formatter.call(points[0])
});
}
if (item.type === 'path') {
this.redrawPath(item);
} else {
this.alignItem(item, !item.placed);
}
}
},
/**
* Destroing a single item
*
* @private
* @param {SVGElement} item
*/
destroyItem: function (item) {
// erase from shapes or labels array
erase(this[item.itemType + 's'], item);
item.destroy();
},
/**
* Returns a point object
*
* @private
* @param {Object} pointOptions
* @param {Highcharts.MockPoint|Highcharts.Point} point
* @return {Highcharts.MockPoint|Highcharts.Point|null} if the point is
* found/exists returns this point, otherwise null
*/
pointItem: function (pointOptions, point) {
if (!point || point.series === null) {
if (isObject(pointOptions)) {
point = mockPoint(this.chart, pointOptions);
} else if (isString(pointOptions)) {
point = this.chart.get(pointOptions) || null;
}
}
return point;
},
/**
* Linking item with the point or points and returning an array of linked
* points.
*
* @private
* @param {SVGElement} item
* @return {
* Highcharts.Point|
* Highcharts.MockPoint|
* Array<Highcharts.Point|Highcharts.MockPoint>
* }
*/
linkPoints: function (item) {
var pointsOptions = (
item.options.points ||
(item.options.point && H.splat(item.options.point))
),
points = item.points,
len = pointsOptions && pointsOptions.length,
i,
point;
for (i = 0; i < len; i++) {
point = this.pointItem(pointsOptions[i], points[i]);
if (!point) {
return (item.points = []);
}
points[i] = point;
}
return points;
},
/**
* Aligning the item and setting its anchor
*
* @private
* @param {SVGElement} item
* @param {Boolean} isNew
* If the label is re-positioned (is not new) it is animated
* @return {undefined}
*/
alignItem: function (item, isNew) {
var anchor = this.itemAnchor(item, item.points[0]),
attrs = this.itemPosition(item, anchor);
if (attrs) {
item.alignAttr = attrs;
item.placed = true;
attrs.anchorX = anchor.absolutePosition.x;
attrs.anchorY = anchor.absolutePosition.y;
item[isNew ? 'attr' : 'animate'](attrs);
} else {
item.placed = false;
item.attr({
x: 0,
y: -9e9
});
}
},
/**
* @private
*/
redrawPath: function (pathItem, isNew) {
var points = pathItem.points,
strokeWidth = pathItem['stroke-width'] || 1,
d = ['M'],
pointIndex = 0,
dIndex = 0,
len = points && points.length,
crispSegmentIndex,
anchor,
point,
showPath;
if (len) {
do {
point = points[pointIndex];
anchor = this.itemAnchor(pathItem, point).absolutePosition;
d[++dIndex] = anchor.x;
d[++dIndex] = anchor.y;
// Crisping line, it might be replaced with
// Renderer.prototype.crispLine but it requires creating many
// temporary arrays
crispSegmentIndex = dIndex % 5;
if (crispSegmentIndex === 0) {
if (d[crispSegmentIndex + 1] === d[crispSegmentIndex + 4]) {
d[crispSegmentIndex + 1] = d[crispSegmentIndex + 4] =
Math.round(d[crispSegmentIndex + 1]) -
(strokeWidth % 2 / 2);
}
if (d[crispSegmentIndex + 2] === d[crispSegmentIndex + 5]) {
d[crispSegmentIndex + 2] = d[crispSegmentIndex + 5] =
Math.round(d[crispSegmentIndex + 2]) +
(strokeWidth % 2 / 2);
}
}
if (pointIndex < len - 1) {
d[++dIndex] = 'L';
}
showPath = point.series.visible;
} while (++pointIndex < len && showPath);
}
if (showPath) {
pathItem[isNew ? 'attr' : 'animate']({
d: d
});
} else {
pathItem.attr({
d: 'M 0 ' + -9e9
});
}
pathItem.placed = showPath;
},
/*
* @private
*/
renderItem: function (item) {
item.add(
item.itemType === 'label' ?
this.labelsGroup :
this.shapesGroup
);
this.setItemMarkers(item);
},
/*
* @private
*/
setItemMarkers: function (item) {
var itemOptions = item.options,
chart = this.chart,
defs = chart.options.defs,
fill = itemOptions.fill,
color = defined(fill) && fill !== 'none' ?
fill :
itemOptions.stroke,
setMarker = function (markerType) {
var markerId = itemOptions[markerType],
def,
predefinedMarker,
key,
marker;
if (markerId) {
for (key in defs) {
def = defs[key];
if (markerId === def.id && def.tagName === 'marker') {
predefinedMarker = def;
break;
}
}
if (predefinedMarker) {
marker = item[markerType] = chart.renderer.addMarker(
(itemOptions.id || uniqueKey()) + '-' +
predefinedMarker.id,
merge(predefinedMarker, { color: color })
);
item.attr(markerType, marker.attr('id'));
}
}
};
each(['markerStart', 'markerEnd'], setMarker);
},
/**
* An object which denotes an anchor position
*
* @typedef {Object} AnchorPosition
* @property {Number} AnchorPosition.x
* @property {Number} AnchorPosition.y
* @property {Number} AnchorPosition.height
* @property {Number} AnchorPosition.width
*/
/**
* Returns object which denotes anchor position - relative and absolute
*
* @private
* @param {SVGElement} item
* @param {Highcharts.Point|Highcharts.MockPoint} point
* @return {Object} anchor
* @return {AnchorPosition} anchor.relativePosition
* Relative to the plot area position
* @return {AnchorPosition} anchor.absolutePosition
* Absolute position
*/
itemAnchor: function (item, point) {
var plotBox = point.series.getPlotBox(),
box = point.mock ?
point.alignToBox(true) :
tooltipPrototype.getAnchor.call({
chart: this.chart
}, point),
anchor = {
x: box[0],
y: box[1],
height: box[2] || 0,
width: box[3] || 0
};
return {
relativePosition: anchor,
absolutePosition: merge(anchor, {
x: anchor.x + plotBox.translateX,
y: anchor.y + plotBox.translateY
})
};
},
/**
* Returns the item position
*
* @private
* @param {SVGElement} item
* @param {AnchorPosition} anchor
* @return {Object|null} position
* @return {Number} position.x
* @return {Number} position.y
*/
itemPosition: function (item, anchor) {
var chart = this.chart,
point = item.points[0],
itemOptions = item.options,
anchorAbsolutePosition = anchor.absolutePosition,
anchorRelativePosition = anchor.relativePosition,
itemPosition,
alignTo,
itemPosRelativeX,
itemPosRelativeY,
showItem =
point.series.visible &&
MockPoint.prototype.isInsidePane.call(point);
if (showItem) {
if (defined(itemOptions.distance) || itemOptions.positioner) {
itemPosition = (
itemOptions.positioner ||
tooltipPrototype.getPosition
).call(
{
chart: chart,
distance: pick(itemOptions.distance, 16)
},
item.width,
item.height,
{
plotX: anchorRelativePosition.x,
plotY: anchorRelativePosition.y,
negative: point.negative,
ttBelow: point.ttBelow,
h: anchorRelativePosition.height ||
anchorRelativePosition.width
}
);
} else {
alignTo = {
x: anchorAbsolutePosition.x,
y: anchorAbsolutePosition.y,
width: 0,
height: 0
};
itemPosition = this.alignedPosition(
extend(itemOptions, {
width: item.width,
height: item.height
}),
alignTo
);
if (item.options.overflow === 'justify') {
itemPosition = this.alignedPosition(
this.justifiedOptions(item, itemOptions, itemPosition),
alignTo
);
}
}
if (itemOptions.crop) {
itemPosRelativeX = itemPosition.x - chart.plotLeft;
itemPosRelativeY = itemPosition.y - chart.plotTop;
showItem =
chart.isInsidePlot(itemPosRelativeX, itemPosRelativeY) &&
chart.isInsidePlot(
itemPosRelativeX + item.width,
itemPosRelativeY + item.height
);
}
}
return showItem ? itemPosition : null;
},
/**
* Returns new aligned position based alignment options and box to align to.
* It is almost a one-to-one copy from SVGElement.prototype.align
* except it does not use and mutate an element
*
* @private
* @param {Object} alignOptions
* @param {Object} box
* @return {Object} aligned position
**/
alignedPosition: function (alignOptions, box) {
var align = alignOptions.align,
vAlign = alignOptions.verticalAlign,
x = (box.x || 0) + (alignOptions.x || 0),
y = (box.y || 0) + (alignOptions.y || 0),
alignFactor,
vAlignFactor;
if (align === 'right') {
alignFactor = 1;
} else if (align === 'center') {
alignFactor = 2;
}
if (alignFactor) {
x += (box.width - (alignOptions.width || 0)) / alignFactor;
}
if (vAlign === 'bottom') {
vAlignFactor = 1;
} else if (vAlign === 'middle') {
vAlignFactor = 2;
}
if (vAlignFactor) {
y += (box.height - (alignOptions.height || 0)) / vAlignFactor;
}
return {
x: Math.round(x),
y: Math.round(y)
};
},
/**
* Returns new alignment options for a label if the label is outside the
* plot area. It is almost a one-to-one copy from
* Series.prototype.justifyDataLabel except it does not mutate the label and
* it works with absolute instead of relative position.
*
* @private
* @param {Object} label
* @param {Object} alignOptions
* @param {Object} alignAttr
* @return {Object} justified options
**/
justifiedOptions: function (label, alignOptions, alignAttr) {
var chart = this.chart,
align = alignOptions.align,
verticalAlign = alignOptions.verticalAlign,
padding = label.box ? 0 : (label.padding || 0),
bBox = label.getBBox(),
off,
options = {
align: align,
verticalAlign: verticalAlign,
x: alignOptions.x,
y: alignOptions.y,
width: label.width,
height: label.height
},
x = alignAttr.x - chart.plotLeft,
y = alignAttr.y - chart.plotTop;
// Off left
off = x + padding;
if (off < 0) {
if (align === 'right') {
options.align = 'left';
} else {
options.x = -off;
}
}
// Off right
off = x + bBox.width - padding;
if (off > chart.plotWidth) {
if (align === 'left') {
options.align = 'right';
} else {
options.x = chart.plotWidth - off;
}
}
// Off top
off = y + padding;
if (off < 0) {
if (verticalAlign === 'bottom') {
options.verticalAlign = 'top';
} else {
options.y = -off;
}
}
// Off bottom
off = y + bBox.height - padding;
if (off > chart.plotHeight) {
if (verticalAlign === 'top') {
options.verticalAlign = 'bottom';
} else {
options.y = chart.plotHeight - off;
}
}
return options;
},
/**
* Utility function for mapping item's options to element's attribute
*
* @private
* @param {Object} options
* @return {Object} mapped options
**/
attrsFromOptions: function (options) {
var map = this.attrsMap,
attrs = {},
key,
mappedKey;
for (key in options) {
mappedKey = map[key];
if (mappedKey) {
attrs[mappedKey] = options[key];
}
}
return attrs;
}
};
/* ***************************************************************************
*
* EXTENDING CHART PROTOTYPE
*
**************************************************************************** */
H.extend(chartPrototype, /** @lends Chart# */ {
/**
* Add an annotation to the chart after render time.
*
* @param {AnnotationOptions} options
* The series options for the new, detailed series.
*
* @return {Highcharts.Annotation} - The newly generated annotation.
*/
addAnnotation: function (userOptions, redraw) {
var annotation = new Annotation(this, userOptions);
this.annotations.push(annotation);
this.options.annotations.push(userOptions);
if (pick(redraw, true)) {
annotation.redraw();
}
return annotation;
},
/**
* Remove an annotation from the chart.
*
* @param {String} id - The annotation's id.
*/
removeAnnotation: function (id) {
var annotations = this.annotations,
annotation = find(annotations, function (annotation) {
return annotation.options.id === id;
});
if (annotation) {
erase(this.options.annotations, annotation.userOptions);
erase(annotations, annotation);
annotation.destroy();
}
},
/**
* @private
* @memberof Highcharts.Chart#
* @function drawAnnotations
*/
drawAnnotations: function () {
var clip = this.plotBoxClip,
plotBox = this.plotBox;
if (clip) {
clip.attr(plotBox);
} else {
this.plotBoxClip = this.renderer.clipRect(plotBox);
}
each(this.annotations, function (annotation) {
annotation.redraw();
});
}
});
chartPrototype.callbacks.push(function (chart) {
chart.annotations = [];
each(chart.options.annotations, function (annotationOptions) {
chart.annotations.push(
new Annotation(chart, annotationOptions)
);
});
chart.drawAnnotations();
addEvent(chart, 'redraw', chart.drawAnnotations);
addEvent(chart, 'destroy', function () {
var plotBoxClip = chart.plotBoxClip;
if (plotBoxClip && plotBoxClip.destroy) {
plotBoxClip.destroy();
}
});
});
addEvent(H.Chart, 'afterGetContainer', function () {
this.options.defs = merge(defaultMarkers, this.options.defs || {});
});
/* ************************************************************************* */
/**
* General symbol definition for labels with connector
*/
H.SVGRenderer.prototype.symbols.connector = function (x, y, w, h, options) {
var anchorX = options && options.anchorX,
anchorY = options && options.anchorY,
path,
yOffset,
lateral = w / 2;
if (isNumber(anchorX) && isNumber(anchorY)) {
path = ['M', anchorX, anchorY];
// Prefer 45 deg connectors
yOffset = y - anchorY;
if (yOffset < 0) {
yOffset = -h - yOffset;
}
if (yOffset < w) {
lateral = anchorX < x + (w / 2) ? yOffset : w - yOffset;
}
// Anchor below label
if (anchorY > y + h) {
path.push('L', x + lateral, y + h);
// Anchor above label
} else if (anchorY < y) {
path.push('L', x + lateral, y);
// Anchor left of label
} else if (anchorX < x) {
path.push('L', x, y + h / 2);
// Anchor right of label
} else if (anchorX > x + w) {
path.push('L', x + w, y + h / 2);
}
}
return path || [];
};