draggable-points.src.js
66.8 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
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
/**
* (c) 2009-2018 Highsoft AS
* Authors: Øystein Moseng, Torstein Hønsi, Jon A. Nygård
*
* License: www.highcharts.com/license
*/
'use strict';
import H from '../parts/Globals.js';
import '../parts/Utilities.js';
var addEvent = H.addEvent,
each = H.each,
objectEach = H.objectEach,
pick = H.pick,
filter = H.grep,
merge = H.merge,
seriesTypes = H.seriesTypes;
/*
Add drag/drop support to specific data props for different series types.
The dragDrop.draggableX/Y user options on series enable/disable all of these
per direction unless they are specifically set in options using
dragDrop.{optionName}. If the prop does not specify an optionName here, it
can only be enabled/disabled by the user with draggableX/Y.
Supported options for each prop:
optionName: User option in series.dragDrop that enables/disables
dragging this prop.
axis: Can be 'x' or 'y'. Whether this prop is linked to x or y axis.
move: Whether or not this prop should be updated when moving points.
resize: Whether or not to draw a drag handle and allow user to drag and
update this prop by itself.
beforeResize: Hook to perform tasks before a resize is made. Gets
the guide box, the new points values, and the point as args.
resizeSide: Which side of the guide box to resize when dragging the
handle. Can be "left", "right", "top", "bottom". Chart.inverted is
handled automatically. Can also be a function, taking the new point
values as parameter, as well as the point, and returning a string
with the side.
propValidate: Function that takes the prop value and the point as
arguments, and returns true if the prop value is valid, false if
not. It is used to prevent e.g. resizing "low" above "high".
handlePositioner: For resizeable props, return 0,0 in SVG plot coords of
where to place the dragHandle. Gets point as argument. Should return
object with x and y properties.
handleFormatter: For resizeable props, return the path of the drag
handle as an SVG path array. Gets the point as argument. The handle
is translated according to handlePositioner.
handleOptions: Options to merge with the default handle options.
TODO:
- It makes sense to have support for resizing the size of bubbles and
e.g variwide columns. This requires us to support dragging along a
z-axis, somehow computing a relative value from old to new pixel
size.
- Moving maps could be useful, although we would have to compute new
point.path values in order to do it properly (using SVG translate
is easier, but won't update the data).
*/
// 90deg rotated column handle path, used in multiple series types
var horizHandleFormatter = function (point) {
var shapeArgs = point.shapeArgs || point.graphic.getBBox(),
top = shapeArgs.r || 0, // Rounding of bar corners
bottom = shapeArgs.height - top,
centerY = shapeArgs.height / 2;
return [
// Top wick
'M', 0, top,
'L', 0, centerY - 5,
// Circle
'A', 1, 1, 0, 0, 0, 0, centerY + 5,
'A', 1, 1, 0, 0, 0, 0, centerY - 5,
// Bottom wick
'M', 0, centerY + 5,
'L', 0, bottom
];
};
// Line series - only draggableX/Y, no drag handles
var lineDragDropProps = seriesTypes.line.prototype.dragDropProps = {
x: {
axis: 'x',
move: true
},
y: {
axis: 'y',
move: true
}
};
// Flag series - same as line/scatter
if (seriesTypes.flags) {
seriesTypes.flags.prototype.dragDropProps = lineDragDropProps;
}
// Column series - x can be moved, y can only be resized. Note extra
// functionality for handling upside down columns (below threshold).
var columnDragDropProps = seriesTypes.column.prototype.dragDropProps = {
x: {
axis: 'x',
move: true
},
y: {
axis: 'y',
move: false,
resize: true,
// Force guideBox start coordinates
beforeResize: function (guideBox, pointVals, point) {
// We need to ensure that guideBox always starts at threshold.
// We flip whether or not we update the top or bottom of the guide
// box at threshold, but if we drag the mouse fast, the top has not
// reached threshold before we cross over and update the bottom.
var threshold = point.series.translatedThreshold,
y = guideBox.attr('y'),
height,
diff;
if (pointVals.y >= point.series.options.threshold || 0) {
// Above threshold - always set height to hit the threshold
height = guideBox.attr('height');
diff = threshold ? threshold - (y + height) : 0;
guideBox.attr({
height: Math.max(0, Math.round(height + diff))
});
} else {
// Below - always set y to start at threshold
guideBox.attr({
y: Math.round(y + (threshold ? threshold - y : 0))
});
}
},
// Flip the side of the resize handle if column is below threshold
resizeSide: function (pointVals, point) {
return pointVals.y >= (point.series.options.threshold || 0) ?
'top' : 'bottom';
},
// Position handle at bottom if column is below threshold
handlePositioner: function (point) {
var bBox = point.shapeArgs || point.graphic.getBBox();
return {
x: bBox.x,
y: point.y >= (point.series.options.threshold || 0) ?
bBox.y : bBox.y + bBox.height
};
},
// Horizontal handle
handleFormatter: function (point) {
var shapeArgs = point.shapeArgs,
radius = shapeArgs.r || 0, // Rounding of bar corners
centerX = shapeArgs.width / 2;
return [
// Left wick
'M', radius, 0,
'L', centerX - 5, 0,
// Circle
'A', 1, 1, 0, 0, 0, centerX + 5, 0,
'A', 1, 1, 0, 0, 0, centerX - 5, 0,
// Right wick
'M', centerX + 5, 0,
'L', shapeArgs.width - radius, 0
];
}
}
};
// Bullet graph, x/y same as column, but also allow target to be dragged.
if (seriesTypes.bullet) {
seriesTypes.bullet.prototype.dragDropProps = {
x: columnDragDropProps.x,
y: columnDragDropProps.y,
target: {
/**
* Allow target value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.bullet.dragDrop.draggableTarget
*/
optionName: 'draggableTarget',
axis: 'y',
move: true,
resize: true,
resizeSide: 'top',
handlePositioner: function (point) {
var bBox = point.targetGraphic.getBBox();
return {
x: point.barX,
y: bBox.y + bBox.height / 2
};
},
handleFormatter: columnDragDropProps.y.handleFormatter
}
};
}
// Columnrange series - move x, resize or move low/high
if (seriesTypes.columnrange) {
seriesTypes.columnrange.prototype.dragDropProps = {
x: {
axis: 'x',
move: true
},
low: {
/**
* Allow low value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.columnrange.dragDrop.draggableLow
*/
optionName: 'draggableLow',
axis: 'y',
move: true,
resize: true,
resizeSide: 'bottom',
handlePositioner: function (point) {
var bBox = point.shapeArgs || point.graphic.getBBox();
return {
x: bBox.x,
y: bBox.y + bBox.height
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val <= point.high;
}
},
high: {
/**
* Allow high value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.columnrange.dragDrop.draggableHigh
*/
optionName: 'draggableHigh',
axis: 'y',
move: true,
resize: true,
resizeSide: 'top',
handlePositioner: function (point) {
var bBox = point.shapeArgs || point.graphic.getBBox();
return {
x: bBox.x,
y: bBox.y
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val >= point.low;
}
}
};
}
// Boxplot series - move x, resize or move low/q1/q3/high
if (seriesTypes.boxplot) {
seriesTypes.boxplot.prototype.dragDropProps = {
x: columnDragDropProps.x,
low: {
/**
* Allow low value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.boxplot.dragDrop.draggableLow
*/
optionName: 'draggableLow',
axis: 'y',
move: true,
resize: true,
resizeSide: 'bottom',
handlePositioner: function (point) {
return {
x: point.shapeArgs.x,
y: point.lowPlot
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val <= point.q1;
}
},
q1: {
/**
* Allow Q1 value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.boxplot.dragDrop.draggableQ1
*/
optionName: 'draggableQ1',
axis: 'y',
move: true,
resize: true,
resizeSide: 'bottom',
handlePositioner: function (point) {
return {
x: point.shapeArgs.x,
y: point.q1Plot
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val <= point.median && val >= point.low;
}
},
median: {
// Median can not be dragged individually, just move the whole
// point for this.
axis: 'y',
move: true
},
q3: {
/**
* Allow Q3 value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.boxplot.dragDrop.draggableQ3
*/
optionName: 'draggableQ3',
axis: 'y',
move: true,
resize: true,
resizeSide: 'top',
handlePositioner: function (point) {
return {
x: point.shapeArgs.x,
y: point.q3Plot
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val <= point.high && val >= point.median;
}
},
/**
* Allow high value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.boxplot.dragDrop.draggableHigh
*/
high: {
optionName: 'draggableHigh',
axis: 'y',
move: true,
resize: true,
resizeSide: 'top',
handlePositioner: function (point) {
return {
x: point.shapeArgs.x,
y: point.highPlot
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val >= point.q3;
}
}
};
}
// OHLC series - move x, resize or move open/high/low/close
if (seriesTypes.ohlc) {
seriesTypes.ohlc.prototype.dragDropProps = {
x: columnDragDropProps.x,
low: {
/**
* Allow low value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.ohlc.dragDrop.draggableLow
*/
optionName: 'draggableLow',
axis: 'y',
move: true,
resize: true,
resizeSide: 'bottom',
handlePositioner: function (point) {
return {
x: point.shapeArgs.x,
y: point.plotLow
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val <= point.open && val <= point.close;
}
},
high: {
/**
* Allow high value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.ohlc.dragDrop.draggableHigh
*/
optionName: 'draggableHigh',
axis: 'y',
move: true,
resize: true,
resizeSide: 'top',
handlePositioner: function (point) {
return {
x: point.shapeArgs.x,
y: point.plotHigh
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val >= point.open && val >= point.close;
}
},
open: {
/**
* Allow open value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.ohlc.dragDrop.draggableOpen
*/
optionName: 'draggableOpen',
axis: 'y',
move: true,
resize: true,
resizeSide: function (point) {
return point.open >= point.close ? 'top' : 'bottom';
},
handlePositioner: function (point) {
return {
x: point.shapeArgs.x,
y: point.plotOpen
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val <= point.high && val >= point.low;
}
},
close: {
/**
* Allow close value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.ohlc.dragDrop.draggableClose
*/
optionName: 'draggableClose',
axis: 'y',
move: true,
resize: true,
resizeSide: function (point) {
return point.open >= point.close ? 'bottom' : 'top';
},
handlePositioner: function (point) {
return {
x: point.shapeArgs.x,
y: point.plotClose
};
},
handleFormatter: columnDragDropProps.y.handleFormatter,
propValidate: function (val, point) {
return val <= point.high && val >= point.low;
}
}
};
}
// Arearange series - move x, resize or move low/high
if (seriesTypes.arearange) {
var columnrangeDragDropProps = seriesTypes.columnrange
.prototype.dragDropProps,
// Use a circle covering the marker as drag handle
arearangeHandleFormatter = function (point) {
var radius = point.graphic ?
point.graphic.getBBox().width / 2 + 1 :
4;
return [
'M', 0 - radius, 0,
'a', radius, radius, 0, 1, 0, radius * 2, 0,
'a', radius, radius, 0, 1, 0, radius * -2, 0
];
};
seriesTypes.arearange.prototype.dragDropProps = {
x: columnrangeDragDropProps.x,
low: {
/**
* Allow low value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.arearange.dragDrop.draggableLow
*/
optionName: 'draggableLow',
axis: 'y',
move: true,
resize: true,
resizeSide: 'bottom',
handlePositioner: function (point) {
var bBox = point.lowerGraphic && point.lowerGraphic.getBBox();
return bBox ? {
x: bBox.x + bBox.width / 2,
y: bBox.y + bBox.height / 2
} : { x: -999, y: -999 };
},
handleFormatter: arearangeHandleFormatter,
propValidate: columnrangeDragDropProps.low.propValidate
},
high: {
/**
* Allow high value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.arearange.dragDrop.draggableHigh
*/
optionName: 'draggableHigh',
axis: 'y',
move: true,
resize: true,
resizeSide: 'top',
handlePositioner: function (point) {
var bBox = point.upperGraphic && point.upperGraphic.getBBox();
return bBox ? {
x: bBox.x + bBox.width / 2,
y: bBox.y + bBox.height / 2
} : { x: -999, y: -999 };
},
handleFormatter: arearangeHandleFormatter,
propValidate: columnrangeDragDropProps.high.propValidate
}
};
}
// Waterfall - mostly as column, but don't show drag handles for sum points
if (seriesTypes.waterfall) {
seriesTypes.waterfall.prototype.dragDropProps = {
x: columnDragDropProps.x,
y: merge(columnDragDropProps.y, {
handleFormatter: function (point) {
return point.isSum || point.isIntermediateSum ? null :
columnDragDropProps.y.handleFormatter(point);
}
})
};
}
// Xrange - resize/move x/x2, and move y
if (seriesTypes.xrange) {
// Handle positioner logic is the same for x and x2 apart from the
// x value. shapeArgs does not take yAxis reversed etc into account, so we
// use axis.toPixels to handle positioning.
var xrangeHandlePositioner = function (point, xProp) {
var series = point.series,
xAxis = series.xAxis,
yAxis = series.yAxis,
inverted = series.chart.inverted,
// Using toPixels handles axis.reversed, but doesn't take
// chart.inverted into account.
newX = xAxis.toPixels(point[xProp], true),
newY = yAxis.toPixels(point.y, true);
// Handle chart inverted
if (inverted) {
newX = xAxis.len - newX;
newY = yAxis.len - newY -
point.shapeArgs.height / 2;
} else {
newY -= point.shapeArgs.height / 2;
}
return {
x: Math.round(newX),
y: Math.round(newY)
};
},
xrangeDragDropProps = seriesTypes.xrange.prototype.dragDropProps = {
y: {
axis: 'y',
move: true
},
x: {
/**
* Allow x value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.xrange.dragDrop.draggableX1
*/
optionName: 'draggableX1',
axis: 'x',
move: true,
resize: true,
resizeSide: 'left',
handlePositioner: function (point) {
return xrangeHandlePositioner(point, 'x');
},
handleFormatter: horizHandleFormatter,
propValidate: function (val, point) {
return val <= point.x2;
}
},
x2: {
/**
* Allow x2 value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.xrange.dragDrop.draggableX2
*/
optionName: 'draggableX2',
axis: 'x',
move: true,
resize: true,
resizeSide: 'right',
handlePositioner: function (point) {
return xrangeHandlePositioner(point, 'x2');
},
handleFormatter: horizHandleFormatter,
propValidate: function (val, point) {
return val >= point.x;
}
}
};
}
// Gantt - same as xrange, but with aliases
if (seriesTypes.gantt) {
seriesTypes.gantt.prototype.dragDropProps = {
y: xrangeDragDropProps.y,
start: merge(xrangeDragDropProps.x, {
/**
* Allow start value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.gantt.dragDrop.draggableStart
*/
optionName: 'draggableStart',
// Do not allow individual drag handles for milestones
validateIndividualDrag: function (point) {
return !point.milestone;
}
}),
end: merge(xrangeDragDropProps.x2, {
/**
* Allow end value to be dragged individually. Requires
* `draggable-points` module.
*
* @type {boolean}
* @default true
* @apioption plotOptions.gantt.dragDrop.draggableEnd
*/
optionName: 'draggableEnd',
// Do not allow individual drag handles for milestones
validateIndividualDrag: function (point) {
return !point.milestone;
}
})
};
}
// Don't support certain series types
each(
['gauge', 'pie', 'sunburst', 'wordcloud', 'sankey', 'histogram', 'pareto',
'vector', 'windbarb', 'treemap', 'bellcurve', 'sma', 'map', 'mapline'],
function (type) {
if (seriesTypes[type]) {
seriesTypes[type].prototype.dragDropProps = null;
}
}
);
/**
* The draggable-points module allows points to be moved around or modified
* in the chart. In addition to the options mentioned under the `dragDrop`
* API structure, the module fires three events,
* [point.dragStart](series.line.point.events.dragStart),
* [point.drag](series.line.point.events.drag) and
* [point.drop](series.line.point.events.drop).
*
* It requires the `modules/draggable-points.js` file to be loaded.
*
* @type {object}
* @since 6.2.0
*
* @sample highcharts/dragdrop/resize-column Draggable column and line series
*
* @sample highcharts/dragdrop/drag-bubble Draggable bubbles
*
* @sample highcharts/dragdrop/drag-xrang Draggable X range series
*
* @apioption plotOptions.series.dragDrop
*/
/**
* The amount of pixels to drag the pointer before it counts as a drag
* operation. This prevents drag/drop to fire when just clicking or selecting
* points.
*
* @type {number}
* @default 2
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragSensitivity
*/
var defaultDragSensitivity = 2;
/**
* Style options for the guide box. The guide box has one state by default,
* the "default" state.
*
* @type {object}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.guideBox
*/
var defaultGuideBoxOptions = {
/**
* Style options for the guide box default state.
*
* @type {object}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.guideBox.default
*/
default: {
/**
* CSS class name of the guide box in this state. Defaults to
* `highcharts-drag-box-default`.
*
* @type {String}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.guideBox.default.className
*/
className: 'highcharts-drag-box-default',
/**
* Width of the line around the guide box.
*
* @type {number}
* @default 1
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.guideBox.default.lineWidth
*/
lineWidth: 1,
/**
* Color of the border around the guide box.
*
* @type {Color}
* @since 6.2.0
* @default #888
* @apioption plotOptions.series.dragDrop.guideBox.default.lineColor
*/
lineColor: '#888',
/**
* Guide box fill color.
*
* @type {Color}
* @since 6.2.0
* @default rgba(0, 0, 0, 0.1)
* @apioption plotOptions.series.dragDrop.guideBox.default.color
*/
color: 'rgba(0, 0, 0, 0.1)',
/**
* Guide box cursor.
*
* @type {string}
* @default move
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.guideBox.default.cursor
*/
cursor: 'move',
/**
* Guide box zIndex.
*
* @type {number}
* @since 6.2.0
* @default 900
* @apioption plotOptions.series.dragDrop.guideBox.default.zIndex
*/
zIndex: 900
}
};
/**
* Options for the drag handles.
*
* @type {Object}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragHandle
*/
var defaultDragHandleOptions = {
/**
* Function to define the SVG path to use for the drag handles. Takes
* the point as argument. Should return an SVG path in array format. The
* SVG path is automatically positioned on the point.
*
* @type {Function}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragHandle.pathFormatter
*/
// pathFormatter: null,
/**
* The mouse cursor to use for the drag handles. By default this is
* intelligently switching between `ew-resize` and `ns-resize` depending
* on the direction the point is being dragged.
*
* @type {String}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragHandle.cursor
*/
// cursor: null,
/**
* The class name of the drag handles. Defaults to `highcharts-drag-handle`.
*
* @type {String}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragHandle.className
*/
className: 'highcharts-drag-handle',
/**
* The fill color of the drag handles.
*
* @type {Color}
* @since 6.2.0
* @default #fff
* @apioption plotOptions.series.dragDrop.dragHandle.color
*/
color: '#fff',
/**
* The line color of the drag handles.
*
* @type {Color}
* @since 6.2.0
* @default rgba(0, 0, 0, 0.6)
* @apioption plotOptions.series.dragDrop.dragHandle.lineColor
*/
lineColor: 'rgba(0, 0, 0, 0.6)',
/**
* The line width for the drag handles.
*
* @type {number}
* @default 2
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragHandle.lineWidth
*/
lineWidth: 1,
/**
* The z index for the drag handles.
*
* @type {number}
* @since 6.2.0
* @default 901
* @apioption plotOptions.series.dragDrop.dragHandle.zIndex
*/
zIndex: 901
};
/**
* Set the minimum X value the points can be moved to.
*
* @type {number}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragMinX
*/
/**
* Set the maximum X value the points can be moved to.
*
* @type {number}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragMaxX
*/
/**
* Set the minimum Y value the points can be moved to.
*
* @type {number}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragMinY
*/
/**
* Set the maximum Y value the points can be moved to.
*
* @type {number}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragMaxY
*/
/**
* The X precision value to drag to for this series. Set to 0 to disable. By
* default this is disabled, except for category axes, where the default is 1.
*
* @type {number}
* @default 0
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragPrecisionX
*/
/**
* The Y precision value to drag to for this series. Set to 0 to disable. By
* default this is disabled, except for category axes, where the default is 1.
*
* @type {number}
* @default 0
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.dragPrecisionY
*/
/**
* Enable dragging in the X dimension.
*
* @type {boolean}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.draggableX
*/
/**
* Enable dragging in the Y dimension. Note that this is not supported for
* TreeGrid axes (the default axis type in Gantt charts).
*
* @type {boolean}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.draggableY
*/
/**
* Group the points by a property. Points with the same property value will be
* grouped together when moving.
*
* @type {String}
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.groupBy
*/
/**
* Update points as they are dragged. If false, a guide box is drawn to
* illustrate the new point size.
*
* @type {boolean}
* @default true
* @since 6.2.0
* @apioption plotOptions.series.dragDrop.liveRedraw
*/
/**
* Set a key to hold when dragging to zoom the chart. Requires the
* draggable-points module. This is useful to avoid zooming while moving points.
* Should be set different than [chart.panKey](#chart.panKey).
*
* @type {String}
* @validvalue ["alt", "ctrl", "meta", "shift"]
* @since 6.2.0
* @apioption chart.zoomKey
*/
/**
* Callback that fires when starting to drag a point. The mouse event object is
* passed in as an argument. See [drag and drop options](series.line.dragDrop).
*
* Requires the draggable-points module.
*
* @type {Function}
* @since 6.2.0
* @apioption plotOptions.series.point.events.dragStart
*/
/**
* Callback that fires while dragging a point. The mouse event is passed in as
* parameter. The original data can be accessed from `e.origin`, and the new
* point values can be accessed from `e.newPoints`. If there is only a single
* point being updated, it can be accessed from `e.newPoint` for simplicity. To
* stop the default drag action, return `false`. See
* [drag and drop options](series.line.dragDrop).
*
* Requires the draggable-points module.
*
* @type {Function}
* @since 6.2.0
* @apioption plotOptions.series.point.events.drag
*/
/**
* Callback that fires when the point is dropped. The mouse event is passed in
* as parameter. The original data can be accessed from `e.origin`, and the new
* point values can be accessed from `e.newPoints`. If there is only a single
* point being updated, it can be accessed from `e.newPoint` for simplicity. To
* stop the default drop action, return `false`. See
* [drag and drop options](series.line.dragDrop).
*
* Requires the draggable-points module.
*
* @type {Function}
* @since 6.2.0
* @apioption plotOptions.series.point.events.drop
*/
/**
* Point specific options for the draggable-points module. Overrides options on
* `series.dragDrop`.
*
* Requires the draggable-points module.
*
* @extends plotOptions.series.dragDrop
* @since 6.2.0
* @apioption series.line.data.dragDrop
*/
/**
* Utility function to test if a series is using drag/drop, looking at its
* options.
*
* @private
* @param {Highcharts.Series} series
* The series to test.
*
* @return {boolean}
* True if the series is using drag/drop.
*/
function isSeriesDraggable(series) {
var props = ['draggableX', 'draggableY'],
i;
// Add optionNames from dragDropProps to the array of props to check for
objectEach(series.dragDropProps, function (val) {
if (val.optionName) {
props.push(val.optionName);
}
});
// Loop over all options we have that could enable dragDrop for this
// series. If any of them are truthy, this series is draggable.
i = props.length;
while (i--) {
if (series.options.dragDrop[props[i]]) {
return true;
}
}
}
/**
* Utility function to test if a chart should have drag/drop enabled, looking
* at its options.
*
* @private
* @param {Highcharts.Chart} chart
* The chart to test.
*
* @return {boolean}
* True if the chart is drag/droppable.
*/
function isChartDraggable(chart) {
var i = chart.series.length;
if (chart.hasCartesianSeries && !chart.polar) {
while (i--) {
if (
chart.series[i].options.dragDrop &&
isSeriesDraggable(chart.series[i])
) {
return true;
}
}
}
}
/**
* Utility function to test if a point is movable (any of its props can be
* dragged by a move, not just individually).
*
* @private
* @param {Highcharts.Point} point
* The point to test.
*
* @return {boolean}
* True if the point is movable.
*/
function isPointMovable(point) {
var series = point.series,
seriesDragDropOptions = series.options.dragDrop,
pointDragDropOptions = point.options && point.options.dragDrop,
updateProps = series.dragDropProps,
hasMovableX,
hasMovableY;
objectEach(updateProps, function (p) {
if (p.axis === 'x' && p.move) {
hasMovableX = true;
} else if (p.axis === 'y' && p.move) {
hasMovableY = true;
}
});
// We can only move the point if draggableX/Y is set, even if all the
// individual prop options are set.
return (
seriesDragDropOptions.draggableX && hasMovableX ||
seriesDragDropOptions.draggableY && hasMovableY
) &&
!(
pointDragDropOptions &&
pointDragDropOptions.draggableX === false &&
pointDragDropOptions.draggableY === false
) &&
series.yAxis &&
series.xAxis;
}
/**
* In mousemove events, check that we have dragged mouse further than the
* dragSensitivity before we call mouseMove handler.
*
* @private
* @param {global.Event} e
* Mouse move event to test.
*
* @param {Highcharts.Chart} chart
* Chart that has started dragging.
*
* @param {number} sensitivity
* Pixel sensitivity to test against.
*
* @return {boolean}
* True if the event is moved past sensitivity relative to the chart's
* drag origin.
*/
function hasDraggedPastSensitivity(e, chart, sensitivity) {
var orig = chart.dragDropData.origin,
oldX = orig.pageX,
oldY = orig.pageY,
newX = e.pageX,
newY = e.pageY,
distance = Math.sqrt(
(newX - oldX) * (newX - oldX) +
(newY - oldY) * (newY - oldY)
);
return distance > sensitivity;
}
/**
* Get a snapshot of points, mouse position, and guide box dimensions
*
* @private
* @param {global.Event} e
* Mouse event with mouse position to snapshot.
*
* @param {Array<Highcharts.Point>} points
* Points to take snapshot of. We store the value of the data properties
* defined in each series' dragDropProps.
*
* @param {Highcharts.SVGElement} [guideBox]
* The guide box to take snapshot of.
*
* @return {object}
* Snapshot object. Point properties are placed in a hashmap with IDs as
* keys.
*/
function getPositionSnapshot(e, points, guideBox) {
var res = {
pageX: e.pageX,
pageY: e.pageY,
guideBox: guideBox && {
x: guideBox.attr('x'),
y: guideBox.attr('y'),
width: guideBox.attr('width'),
height: guideBox.attr('height')
},
points: {}
};
// Loop over the points and add their props
each(points, function (point) {
var pointProps = {};
// Add all of the props defined in the series' dragDropProps to the
// snapshot
objectEach(point.series.dragDropProps, function (val, key) {
pointProps[key] = point[key];
});
pointProps.point = point; // Store reference to point
res.points[point.id] = pointProps;
});
return res;
}
/**
* Get a list of points that are grouped with this point. If only one point is
* in the group, that point is returned by itself in an array.
*
* @private
* @param {Highcharts.Point} point
* Point to find group from.
*
* @return {Array<Highcharts.Point>}
* Array of points in this group.
*/
function getGroupedPoints(point) {
var series = point.series,
groupKey = series.options.dragDrop.groupBy;
return point.options[groupKey] ?
// If we have a grouping option, filter the points by that
filter(series.points, function (comparePoint) {
return comparePoint.options[groupKey] === point.options[groupKey];
}) :
// Otherwise return the point by itself only
[point];
}
/**
* Resize a rect element on one side. The element is modified.
*
* @private
* @param {Highcharts.SVGElement} rect
* Rect element to resize.
*
* @param {string} updateSide
* Which side of the rect to update. Can be `left`, `right`, `top` or
* `bottom`.
*
* @param {object} update
* Object with x and y properties, detailing how much to resize each
* dimension.
*
* @return {Highcharts.SVGElement}
* The modified rect.
*/
function resizeRect(rect, updateSide, update) {
var resizeAttrs;
switch (updateSide) {
case 'left':
resizeAttrs = {
x: rect.attr('x') + update.x,
width: Math.max(1, rect.attr('width') - update.x)
};
break;
case 'right':
resizeAttrs = {
width: Math.max(1, rect.attr('width') + update.x)
};
break;
case 'top':
resizeAttrs = {
y: rect.attr('y') + update.y,
height: Math.max(1, rect.attr('height') - update.y)
};
break;
case 'bottom':
resizeAttrs = {
height: Math.max(1, rect.attr('height') + update.y)
};
break;
default:
}
rect.attr(resizeAttrs);
}
/**
* Flip a side property, used with resizeRect. If input side is "left", return
* "right" etc.
*
* @private
* @param {string} side
* Side prop to flip. Can be `left`, `right`, `top` or `bottom`.
*
* @return {string}
* The flipped side.
*/
function flipResizeSide(side) {
return {
left: 'right',
right: 'left',
top: 'bottom',
bottom: 'top'
}[side];
}
/**
* Prepare chart.dragDropData with origin info, and show the guide box.
*
* @private
* @param {global.Event} e
* Mouse event with original mouse position.
*
* @param {Highcharts.Point} point
* The point the dragging started on.
*/
function initDragDrop(e, point) {
var groupedPoints = getGroupedPoints(point),
series = point.series,
chart = series.chart,
guideBox;
// If liveRedraw is disabled, show the guide box with the default state
if (!pick(
series.options.dragDrop && series.options.dragDrop.liveRedraw,
true
)) {
chart.dragGuideBox = guideBox = series.getGuideBox(groupedPoints);
chart.setGuideBoxState('default', series.options.dragDrop.guideBox)
.add(series.group);
}
// Store some data on the chart to pick up later
chart.dragDropData = {
origin: getPositionSnapshot(e, groupedPoints, guideBox),
point: point,
groupedPoints: groupedPoints
};
// Set drag state
chart.isDragging = true;
}
/**
* Calculate new point options from points being dragged.
*
* @private
* @param {object} dragDropData
* A chart's dragDropData with drag/drop origin information, and info on
* which points are being dragged.
*
* @param {global.Event} newPos
* Event with the new position of the mouse (pageX/Y properties).
*
* @return {Array<object>}
* Hashmap with point.id mapped to an object with the original point
* reference, as well as the new data values.
*/
function getNewPoints(dragDropData, newPos) {
var point = dragDropData.point,
series = point.series,
options = merge(series.options.dragDrop, point.options.dragDrop),
updateProps = {},
resizeProp = dragDropData.updateProp,
hashmap = {};
// Go through the data props that can be updated on this series and find out
// which ones we want to update.
objectEach(point.series.dragDropProps, function (val, key) {
// If we are resizing, skip if this key is not the correct one or it
// is not resizable.
if (
resizeProp && (
resizeProp !== key ||
!val.resize ||
val.optionName && options[val.optionName] === false
)
) {
return;
}
// If we are resizing, we now know it is good. If we are moving, check
// that moving along this axis is enabled, and the prop is movable.
// If this prop is enabled, add it to be updated.
if (
resizeProp || (
val.move &&
(
val.axis === 'x' && options.draggableX ||
val.axis === 'y' && options.draggableY
)
)
) {
updateProps[key] = val;
}
});
// Go through the points to be updated and get new options for each of them
each(
resizeProp ? // If resizing, only update the point we are resizing
[point] :
dragDropData.groupedPoints,
function (p) {
hashmap[p.id] = {
point: p,
newValues: p.getDropValues(dragDropData.origin, newPos, updateProps)
};
});
return hashmap;
}
/**
* Update the points in a chart from dragDropData.newPoints.
*
* @private
* @param {Highcharts.Chart} chart
* A chart with dragDropData.newPoints.
*
* @param {boolean} [animate=true]
* Animate updating points?
*/
function updatePoints(chart, animate) {
var newPoints = chart.dragDropData.newPoints,
animOptions = animate === false ? false : merge({
duration: 400 // 400 is the default in H.animate
}, chart.options.animation);
chart.isDragDropAnimating = true;
// Update the points
objectEach(newPoints, function (newPoint) {
newPoint.point.update(newPoint.newValues, false);
});
chart.redraw(animOptions);
// Clear the isAnimating flag after animation duration is complete.
// The complete handler for animation seems to have bugs at this time, so
// we have to use a timeout instead.
setTimeout(function () {
delete chart.isDragDropAnimating;
if (chart.hoverPoint && !chart.dragHandles) {
chart.hoverPoint.showDragHandles();
}
}, animOptions.duration);
}
/**
* Resize the guide box according to point options and a difference in mouse
* positions. Handles reversed axes.
*
* @private
* @param {Highcharts.Point} point
* The point that is being resized.
*
* @param {number} dX
* Difference in X position.
*
* @param {number} dY
* Difference in Y position.
*/
function resizeGuideBox(point, dX, dY) {
var series = point.series,
chart = series.chart,
dragDropData = chart.dragDropData,
resizeSide,
newPoint,
resizeProp = series.dragDropProps[dragDropData.updateProp];
// dragDropProp.resizeSide holds info on which side to resize.
newPoint = dragDropData.newPoints[point.id].newValues;
resizeSide = typeof resizeProp.resizeSide === 'function' ?
resizeProp.resizeSide(newPoint, point) : resizeProp.resizeSide;
// Call resize hook if it is defined
if (resizeProp.beforeResize) {
resizeProp.beforeResize(chart.dragGuideBox, newPoint, point);
}
// Do the resize
resizeRect(
chart.dragGuideBox,
resizeProp.axis === 'x' && series.xAxis.reversed ||
resizeProp.axis === 'y' && series.yAxis.reversed ?
flipResizeSide(resizeSide) : resizeSide,
{
x: resizeProp.axis === 'x' ?
dX - (dragDropData.origin.prevdX || 0) : 0,
y: resizeProp.axis === 'y' ?
dY - (dragDropData.origin.prevdY || 0) : 0
}
);
}
/**
* Default mouse move handler while dragging. Handles updating points or guide
* box.
*
* @private
* @param {global.Event} e
* The mouse move event.
*
* @param {Highcharts.Point} point
* The point that is dragged.
*/
function dragMove(e, point) {
var series = point.series,
chart = series.chart,
data = chart.dragDropData,
options = merge(series.options.dragDrop, point.options.dragDrop),
draggableX = options.draggableX,
draggableY = options.draggableY,
origin = data.origin,
dX = e.pageX - origin.pageX,
dY = e.pageY - origin.pageY,
oldDx = dX,
updateProp = data.updateProp;
// Handle inverted
if (chart.inverted) {
dX = -dY;
dY = -oldDx;
}
// If we have liveRedraw enabled, update the points immediately. Otherwise
// update the guideBox.
if (pick(options.liveRedraw, true)) {
updatePoints(chart, false);
// Update drag handles
if (chart.dragHandles) {
chart.hideDragHandles();
}
point.showDragHandles();
} else {
// No live redraw, update guide box
if (updateProp) {
// We are resizing, so resize the guide box
resizeGuideBox(point, dX, dY);
} else {
// We are moving, so move the guide box
chart.dragGuideBox.translate(
draggableX ? dX : 0, draggableY ? dY : 0
);
}
}
// Update stored previous dX/Y
origin.prevdX = dX;
origin.prevdY = dY;
}
/**
* Set the state of the guide box.
*
* @private
* @param {string} state
* The state to set the guide box to.
*
* @param {object} options
* Additional overall guideBox options to consider.
*
* @return {Highcharts.SVGElement}
* The modified guide box.
*/
H.Chart.prototype.setGuideBoxState = function (state, options) {
var guideBox = this.dragGuideBox,
guideBoxOptions = merge(defaultGuideBoxOptions, options),
stateOptions = merge(guideBoxOptions.default, guideBoxOptions[state]);
return guideBox.attr({
className: stateOptions.className,
stroke: stateOptions.lineColor,
strokeWidth: stateOptions.lineWidth,
fill: stateOptions.color,
cursor: stateOptions.cursor,
zIndex: stateOptions.zIndex
});
};
/**
* Get updated point values when dragging a point.
*
* @private
* @param {object} origin Mouse position (pageX/Y) and point props at current
* data values. Point props should be organized per point.id in a hashmap.
*
* @param {global.Event} newPos New mouse position (pageX/Y).
*
* @param {object} updateProps Point props to modify. Map of prop objects where
* each key refers to the prop, and the value is an object with an axis
* property. Example:
* {
* x: {
* axis: 'x'
* },
* x2: {
* axis: 'x'
* }
* }
*
* @returns {object}
* An object with updated data values.
*/
H.Point.prototype.getDropValues = function (origin, newPos, updateProps) {
var point = this,
series = point.series,
options = merge(series.options.dragDrop, point.options.dragDrop),
yAxis = series.yAxis,
xAxis = series.xAxis,
dX = newPos.pageX - origin.pageX,
dY = newPos.pageY - origin.pageY,
oldX = pick(origin.x, point.x),
oldY = pick(origin.y, point.y),
dXValue = xAxis.toValue(
xAxis.toPixels(oldX, true) +
(xAxis.horiz ? dX : dY),
true
) - oldX,
dYValue = yAxis.toValue(
yAxis.toPixels(oldY, true) +
(yAxis.horiz ? dX : dY),
true
) - oldY,
result = {},
updateSingleProp,
pointOrigin = origin.points[point.id];
// Find out if we only have one prop to update
for (var key in updateProps) {
if (updateProps.hasOwnProperty(key)) {
if (updateSingleProp !== undefined) {
updateSingleProp = false;
break;
}
updateSingleProp = true;
}
}
// Utility function to apply precision and limit a value within the
// draggable range
function limitToRange(val, direction) {
var defaultPrecision = series[direction.toLowerCase() + 'Axis']
.categories ? 1 : 0,
precision = pick(
options['dragPrecision' + direction], defaultPrecision
),
min = pick(options['dragMin' + direction], -Infinity),
max = pick(options['dragMax' + direction], Infinity),
res = val;
if (precision) {
res = Math.round(res / precision) * precision;
}
return Math.max(min, Math.min(max, res));
}
// Assign new value to property. Adds dX/YValue to the old value, limiting
// it within min/max ranges.
objectEach(updateProps, function (val, key) {
var oldVal = pointOrigin[key],
newVal = limitToRange(
oldVal + (val.axis === 'x' ? dXValue : dYValue),
val.axis.toUpperCase()
);
// If we are updating a single prop, and it has a validation function
// for the prop, run it. If it fails, don't update the value.
if (
!(
updateSingleProp &&
val.propValidate &&
!val.propValidate(newVal, point)
) &&
oldVal !== undefined
) {
result[key] = newVal;
}
});
return result;
};
/**
* Returns an SVGElement to use as the guide box for a set of points.
*
* @private
* @param {Array<Highcharts.Point>} points
* The state to set the guide box to.
*
* @return {Highcharts.SVGElement}
* An SVG element for the guide box, not added to DOM.
*/
H.Series.prototype.getGuideBox = function (points) {
var chart = this.chart,
minX = Infinity,
maxX = -Infinity,
minY = Infinity,
maxY = -Infinity,
changed;
// Find bounding box of all points
each(points, function (point) {
var bBox = point.graphic && point.graphic.getBBox() || point.shapeArgs;
if (bBox && (bBox.width || bBox.height || bBox.x || bBox.y)) {
changed = true;
minX = Math.min(bBox.x, minX);
maxX = Math.max(bBox.x + bBox.width, maxX);
minY = Math.min(bBox.y, minY);
maxY = Math.max(bBox.y + bBox.height, maxY);
}
});
return changed ? chart.renderer.rect(
minX,
minY,
maxX - minX,
maxY - minY
) : chart.renderer.g();
};
/**
* On point mouse out. Hide drag handles, depending on state.
*
* @private
* @param {Highcharts.Point} point
* The point mousing out of.
*/
function mouseOut(point) {
var chart = point.series && point.series.chart;
if (
chart &&
chart.dragHandles &&
!(
chart.dragDropData &&
(
chart.dragDropData.isDragging ||
chart.dragDropData.isHoveringHandle === point.id
)
)
) {
chart.hideDragHandles();
}
}
/**
* Mouseout on resize handle. Handle states, and possibly run mouseOut on point.
*
* @private
* @param {Highcharts.Point} point
* The point mousing out of.
*/
function onResizeHandleMouseOut(point) {
var chart = point.series.chart;
if (
chart.dragDropData &&
point.id === chart.dragDropData.isHoveringHandle
) {
delete chart.dragDropData.isHoveringHandle;
}
if (!chart.hoverPoint) {
mouseOut(point);
}
}
/**
* Mousedown on resize handle. Init a drag if the conditions are right.
*
* @private
* @param {global.Event} e
* The mousedown event.
*
* @param {Highcharts.Point} point
* The point mousing down on.
*
* @param {string} updateProp
* The data property this resize handle is attached to for this point.
*/
function onResizeHandleMouseDown(e, point, updateProp) {
var chart = point.series.chart;
// Ignore if zoom/pan key is pressed
if (chart.zoomOrPanKeyPressed(e)) {
return;
}
// Prevent zooming
chart.mouseIsDown = false;
initDragDrop(e, point);
chart.dragDropData.isDragging = true;
chart.dragDropData.updateProp = e.updateProp = updateProp;
point.firePointEvent('dragStart', e);
// Prevent default to avoid point click for dragging too
e.stopPropagation();
e.preventDefault();
}
/**
* Render drag handles on a point - depending on which handles are enabled - and
* attach events to them.
* @private
*/
H.Point.prototype.showDragHandles = function () {
var point = this,
series = point.series,
chart = series.chart,
renderer = chart.renderer,
options = merge(series.options.dragDrop, point.options.dragDrop);
// Go through each updateProp and see if we are supposed to create a handle
// for it.
objectEach(series.dragDropProps, function (val, key) {
var handleOptions = merge(
defaultDragHandleOptions,
val.handleOptions,
options.dragHandle
),
handleAttrs = {
className: handleOptions.className,
'stroke-width': handleOptions.lineWidth,
fill: handleOptions.color,
stroke: handleOptions.lineColor
},
pathFormatter = handleOptions.pathFormatter || val.handleFormatter,
positioner = val.handlePositioner,
pos,
handle,
path,
// Run validation function on whether or not we allow individual
// updating of this prop.
validate = val.validateIndividualDrag ?
val.validateIndividualDrag(point) : true;
if (
val.resize &&
validate &&
val.resizeSide &&
pathFormatter &&
(
options['draggable' + val.axis.toUpperCase()] ||
options[val.optionName]
) &&
options[val.optionName] !== false
) {
// Create group if it doesn't exist
if (!chart.dragHandles) {
chart.dragHandles = {
group: renderer.g('drag-drop-handles')
.add(series.markerGroup || series.group)
};
}
// Find position and path of handle
pos = positioner(point);
path = pathFormatter(point);
if (!path || pos.x < 0 || pos.y < 0) {
return;
}
// If cursor is not set explicitly, use axis direction
handleAttrs.cursor = handleOptions.cursor ||
(val.axis === 'x') !== !!chart.inverted ?
'ew-resize' : 'ns-resize';
// Create and add the handle element
chart.dragHandles[
typeof val.resizeSide === 'function' ?
val.resizeSide(point.options, point) : val.resizeSide
] = handle = renderer
.path(path)
.translate(pos.x, pos.y)
.attr(handleAttrs)
.add(chart.dragHandles.group);
// Add events
addEvent(handle.element, 'mousedown', function (e) {
onResizeHandleMouseDown(e, point, key);
});
addEvent(chart.dragHandles.group.element, 'mouseover', function () {
chart.dragDropData = chart.dragDropData || {};
chart.dragDropData.isHoveringHandle = point.id;
});
addEvent(chart.dragHandles.group.element, 'mouseout', function () {
onResizeHandleMouseOut(point);
});
}
});
};
/**
* Remove the chart's drag handles if they exist.
* @private
*/
H.Chart.prototype.hideDragHandles = function () {
var chart = this;
if (chart.dragHandles) {
objectEach(chart.dragHandles, function (val, key) {
if (key !== 'group' && val.destroy) {
val.destroy();
}
});
if (chart.dragHandles.group && chart.dragHandles.group.destroy) {
chart.dragHandles.group.destroy();
}
delete chart.dragHandles;
}
};
/**
* Utility function to count the number of props in an object.
*
* @private
* @param {object} object
* The object to count.
*
* @return {number}
* Number of own properties on the object.
*/
function countProps(object) {
var count = 0;
for (var p in object) {
if (object.hasOwnProperty(p)) {
count++;
}
}
return count;
}
/**
* Utility function to get the value of the first prop of an object. (Note that
* the order of keys in an object is usually not guaranteed.)
*
* @private
* @param {object} object
* The object to count.
*
* @return {*}
* Value of the first prop in the object.
*/
function getFirstProp(object) {
for (var p in object) {
if (object.hasOwnProperty(p)) {
return object[p];
}
}
}
/**
* Mouseover on a point. Show drag handles if the conditions are right.
*
* @private
* @param {Highcharts.Point} point
* The point mousing over.
*/
function mouseOver(point) {
var series = point.series,
chart = series && series.chart;
if (
chart &&
!(chart.dragDropData && chart.dragDropData.isDragging) &&
!chart.isDragDropAnimating &&
series.options.dragDrop &&
!(
chart.options &&
chart.options.chart &&
chart.options.chart.options3d
)
) {
// Hide the handles if they exist on another point already
if (chart.dragHandles) {
chart.hideDragHandles();
}
point.showDragHandles();
}
}
/**
* On container mouse move. Handle drag sensitivity and fire drag event.
*
* @private
* @param {global.Event} e
* The mouse move event.
*
* @param {Highcharts.Chart} chart
* The chart we are moving across.
*/
function mouseMove(e, chart) {
// Ignore if zoom/pan key is pressed
if (chart.zoomOrPanKeyPressed(e)) {
return;
}
var dragDropData = chart.dragDropData,
point,
seriesDragDropOpts,
newPoints,
numNewPoints = 0,
newPoint;
if (dragDropData && dragDropData.isDragging) {
point = dragDropData.point;
seriesDragDropOpts = point.series.options.dragDrop;
// No tooltip for dragging
e.preventDefault();
// Update sensitivity test if not passed yet
if (!dragDropData.draggedPastSensitivity) {
dragDropData.draggedPastSensitivity = hasDraggedPastSensitivity(
e, chart, pick(
point.options.dragDrop &&
point.options.dragDrop.dragSensitivity,
seriesDragDropOpts &&
seriesDragDropOpts.dragSensitivity,
defaultDragSensitivity
)
);
}
// If we have dragged past dragSensitivity, run the mousemove handler
// for dragging
if (dragDropData.draggedPastSensitivity) {
// Find the new point values from the moving
dragDropData.newPoints = getNewPoints(dragDropData, e);
// If we are only dragging one point, add it to the event
newPoints = dragDropData.newPoints;
numNewPoints = countProps(newPoints);
newPoint = numNewPoints === 1 ?
getFirstProp(newPoints) :
null;
// Run the handler
point.firePointEvent('drag', {
origin: dragDropData.origin,
newPoints: dragDropData.newPoints,
newPoint: newPoint && newPoint.newValues,
newPointId: newPoint && newPoint.point.id,
numNewPoints: numNewPoints,
pageX: e.pageX,
pageY: e.pageY
}, function () {
dragMove(e, point);
});
}
}
}
/**
* On container mouse up. Fire drop event and reset state.
*
* @private
* @param {global.Event} e
* The mouse up event.
*
* @param {Highcharts.Chart} chart
* The chart we were dragging in.
*/
function mouseUp(e, chart) {
var dragDropData = chart.dragDropData;
if (
dragDropData &&
dragDropData.isDragging &&
dragDropData.draggedPastSensitivity
) {
var point = dragDropData.point,
newPoints = dragDropData.newPoints,
numNewPoints = countProps(newPoints),
newPoint = numNewPoints === 1 ?
getFirstProp(newPoints) :
null;
// Hide the drag handles
if (chart.dragHandles) {
chart.hideDragHandles();
}
// Prevent default action
e.preventDefault();
chart.cancelClick = true;
// Fire the event, with a default handler that updates the points
point.firePointEvent('drop', {
origin: dragDropData.origin,
pageX: e.pageX,
pageY: e.pageY,
newPoints: newPoints,
numNewPoints: numNewPoints,
newPoint: newPoint && newPoint.newValues,
newPointId: newPoint && newPoint.point.id
}, function () {
updatePoints(chart);
});
}
// Reset
delete chart.dragDropData;
// Clean up the drag guide box if it exists. This is always added on
// drag start, even if user is overriding events.
if (chart.dragGuideBox) {
chart.dragGuideBox.destroy();
delete chart.dragGuideBox;
}
}
/**
* On container mouse down. Init dragdrop if conditions are right.
*
* @private
* @param {global.Event} e
* The mouse down event.
*
* @param {Highcharts.Chart} chart
* The chart we are clicking.
*/
function mouseDown(e, chart) {
var dragPoint = chart.hoverPoint;
// Reset cancel click
chart.cancelClick = false;
// Ignore if zoom/pan key is pressed
if (chart.zoomOrPanKeyPressed(e)) {
return;
}
// Prevent zooming
chart.mouseIsDown = false;
// If we somehow get a mousedown event while we are dragging, cancel
if (chart.dragDropData && chart.dragDropData.isDragging) {
mouseUp(e, chart);
return;
}
// If this point is movable, start dragging it
if (dragPoint && isPointMovable(dragPoint)) {
initDragDrop(e, dragPoint);
chart.dragDropData.isDragging = true;
dragPoint.firePointEvent('dragStart', e);
}
}
// Point hover event. We use a short timeout due to issues with coordinating
// point mouseover/out events on dragHandles and points. Particularly arearange
// series are finicky since the markers are not individual points. This logic
// should preferably be improved in the future. Notice that the mouseOut event
// below must have a shorter timeout to ensure event order.
addEvent(H.Point, 'mouseOver', function () {
var point = this;
setTimeout(function () {
mouseOver(point);
}, 12);
});
// Point mouseleave event. See above function for explanation of the timeout.
addEvent(H.Point, 'mouseOut', function () {
var point = this;
setTimeout(function () {
mouseOut(point);
}, 10);
});
/**
* Check whether the zoomKey or panKey is pressed.
*
* @private
* @param {global.Event} e
* A mouse event.
*
* @return {boolean}
* True if the zoom or pan keys are pressed. False otherwise.
*/
H.Chart.prototype.zoomOrPanKeyPressed = function (e) {
// Check whether the panKey and zoomKey are set in chart.userOptions
var chartOptions = this.userOptions.chart || {},
panKey = chartOptions.panKey && chartOptions.panKey + 'Key',
zoomKey = chartOptions.zoomKey && chartOptions.zoomKey + 'Key';
return (e[zoomKey] || e[panKey]);
};
// Add events to document and chart after chart has been created
H.Chart.prototype.callbacks.push(function (chart) {
var container = chart.container,
doc = H.doc;
// Only enable if we have a draggable chart
if (isChartDraggable(chart)) {
addEvent(container, 'mousemove', function (e) {
mouseMove(e, chart);
});
addEvent(container, 'touchmove', function (e) {
mouseMove(e, chart);
});
addEvent(container, 'mousedown', function (e) {
mouseDown(e, chart);
});
addEvent(container, 'touchstart', function (e) {
mouseDown(e, chart);
});
addEvent(container, 'mouseleave', function (e) {
mouseUp(e, chart);
});
chart.unbindDragDropMouseUp = addEvent(doc, 'mouseup', function (e) {
mouseUp(e, chart);
});
chart.unbindDragDropTouchEnd = addEvent(doc, 'touchend', function (e) {
mouseUp(e, chart);
});
// Add cleanup to make sure we don't pollute document
addEvent(chart, 'destroy', function () {
if (chart.unbindDragDropMouseUp) {
chart.unbindDragDropMouseUp();
}
if (chart.unbindDragDropTouchEnd) {
chart.unbindDragDropTouchEnd();
}
});
}
});