-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdraggable-points.js
345 lines (288 loc) · 12 KB
/
draggable-points.js
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
/**
* Draggable points plugin for Highcharts JS
* Author: Torstein Honsi
* License: MIT License
* Version: 2.0.10 (2018-11-09)
*/
/*global document, Highcharts */
(function (factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory;
} else {
factory(Highcharts);
}
}(function (Highcharts) {
'use strict';
var addEvent = Highcharts.addEvent,
each = Highcharts.each,
extend = Highcharts.extend,
pick = Highcharts.pick,
columnProto = Highcharts.seriesTypes.column.prototype;
/**
* Filter by dragMin and dragMax
*/
function filterRange(newY, series, XOrY) {
var options = series.options,
dragMin = pick(options['dragMin' + XOrY], undefined),
dragMax = pick(options['dragMax' + XOrY], undefined),
precision = pick(options['dragPrecision' + XOrY], undefined);
if (!isNaN(precision)) {
newY = Math.round(newY / precision) * precision;
}
if (newY < dragMin) {
newY = dragMin;
} else if (newY > dragMax) {
newY = dragMax;
}
return newY;
}
Highcharts.Chart.prototype.callbacks.push(function (chart) {
var container = chart.container,
chartOptions = chart.userOptions.chart || {},
dragPoint,
dragStart,
dragX,
dragY,
dragPlotX,
dragPlotY,
dragPlotHigh,
dragPlotLow,
changeLow,
newHigh,
newLow,
// Check whether the panKey and zoomKey are set in chart.userOptions
panKey = chartOptions.panKey && chartOptions.panKey + 'Key',
zoomKey = chartOptions.zoomKey && chartOptions.zoomKey + 'Key';
/**
* Check, whether the zoomKey or panKey is pressed
**/
function zoomOrPanKeyPressed(e) {
return (e[zoomKey] || e[panKey]);
}
/**
* Get the new values based on the drag event
*/
function getNewPos(e) {
var originalEvent = e.originalEvent || e,
pageX = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageX : e.pageX,
pageY = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageY : e.pageY,
series = dragPoint.series,
draggableX = series.options.draggableX && dragPoint.draggableX !== false,
draggableY = series.options.draggableY && dragPoint.draggableY !== false,
dragSensitivity = pick(series.options.dragSensitivity, 1),
deltaX = draggableX ? dragX - pageX : 0,
deltaY = draggableY ? dragY - pageY : 0,
newPlotX = dragPlotX - deltaX,
newPlotY = dragPlotY - deltaY,
newX = dragX === undefined ? dragPoint.x : series.xAxis.toValue(newPlotX, true),
newY = dragY === undefined ? dragPoint.y : series.yAxis.toValue(newPlotY, true),
ret;
newX = filterRange(newX, series, 'X');
newY = filterRange(newY, series, 'Y');
if (dragPoint.low) {
var newPlotHigh = dragPlotHigh - deltaY,
newPlotLow = dragPlotLow - deltaY;
newHigh = dragY === undefined ? dragPoint.high : series.yAxis.toValue(newPlotHigh, true);
newLow = dragY === undefined ? dragPoint.low : series.yAxis.toValue(newPlotLow, true);
newHigh = filterRange(newHigh, series, 'Y');
newLow = filterRange(newLow, series, 'Y');
}
if (Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)) > dragSensitivity) {
return {
x: draggableX ? newX : dragPoint.x,
y: draggableY ? newY : dragPoint.y,
high: (draggableY && !changeLow) ? newHigh : dragPoint.high,
low: (draggableY && changeLow) ? newLow : dragPoint.low,
dragStart: dragStart,
originalEvent: e
};
} else {
return null;
}
}
/**
* Handler for mouseup
*/
function drop(e) {
var newPos = dragPoint && e && getNewPos(e);
function reset() {
dragPoint = dragX = dragY = undefined;
}
if (newPos) {
dragPoint.firePointEvent('drop', newPos, function () {
dragPoint.update(newPos);
// Update k-d-tree immediately to prevent tooltip jump (#43)
dragPoint.series.options.kdNow = true;
dragPoint.series.buildKDTree();
reset();
});
} else {
reset();
}
}
/**
* Handler for mousedown
*/
function mouseDown(e) {
// Check whether the panKey or zoomKey isn't pressed
if (!zoomOrPanKeyPressed(e)) {
var options,
originalEvent = e.originalEvent || e,
hoverPoint,
series,
bottom;
if ((originalEvent.target.getAttribute('class') || '').indexOf('highcharts-handle') !== -1) {
hoverPoint = originalEvent.target.point;
}
series = chart.hoverPoint && chart.hoverPoint.series;
if (!hoverPoint && chart.hoverPoint && (!series.useDragHandle || !series.useDragHandle())) {
hoverPoint = chart.hoverPoint;
}
if (hoverPoint) {
options = hoverPoint.series.options;
dragStart = {};
if (options.draggableX && hoverPoint.draggableX !== false) {
dragPoint = hoverPoint;
dragX = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageX : e.pageX;
dragPlotX = dragPoint.plotX;
dragStart.x = dragPoint.x;
}
if (options.draggableY && hoverPoint.draggableY !== false) {
dragPoint = hoverPoint;
// Added support for normal stacking (#78)
bottom = pick(series.translatedThreshold, chart.plotHeight);
dragY = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageY : e.pageY;
dragPlotY = dragPoint.plotY + (bottom - (dragPoint.yBottom || bottom));
dragStart.y = dragPoint.y;
if (dragPoint.plotHigh) {
dragPlotHigh = dragPoint.plotHigh;
dragPlotLow = dragPoint.plotLow;
changeLow = (Math.abs(dragPlotLow - (dragY - 60)) < Math.abs(dragPlotHigh - (dragY - 60))) ? true : false;
}
}
// Disable zooming when dragging
if (dragPoint) {
chart.mouseIsDown = false;
}
}
}
}
/**
* Handler for mousemove. If the mouse button is pressed, drag the element.
*/
function mouseMove(e) {
// Check whether the zoomKey or panKey isn't pressed
if (dragPoint && !zoomOrPanKeyPressed(e)) {
e.preventDefault();
var evtArgs = getNewPos(e), // Gets x and y
proceed;
// Fire the 'drag' event with a default action to move the point.
if (evtArgs) {
dragPoint.firePointEvent(
'drag',
evtArgs,
function () {
var kdTree,
series = dragPoint.series;
proceed = true;
dragPoint.update(evtArgs, false);
// Hide halo while dragging (#14)
if (series.halo) {
series.halo = series.halo.destroy();
}
// Let the tooltip follow and reflect the drag point
chart.pointer.reset(true);
// Stacking requires full redraw
if (series.stackKey) {
chart.redraw();
// Do a series redraw only. Let the k-d-tree survive the redraw in order to
// prevent tooltip flickering (#43).
} else {
kdTree = series.kdTree;
series.redraw();
series.kdTree = kdTree;
}
}
);
// The default handler has not run because of prevented default
if (!proceed) {
drop();
}
}
}
}
// Kill animation on first drag when chart.animation is set to false.
chart.redraw();
// Add'em
addEvent(container, 'mousemove', mouseMove);
addEvent(container, 'touchmove', mouseMove);
addEvent(container, 'mousedown', mouseDown);
addEvent(container, 'touchstart', mouseDown);
addEvent(document, 'mouseup', drop);
addEvent(document, 'touchend', drop);
addEvent(container, 'mouseleave', drop);
});
/**
* Extend the column chart tracker by visualizing the tracker object for
* small points
*/
columnProto.useDragHandle = function () {
var is3d = this.chart.is3d && this.chart.is3d();
return !is3d;
};
columnProto.dragHandlePath = function (shapeArgs, strokeW, isNegative) {
var h = 6,
h1 = h / 3,
h2 = h1 * 2,
x1 = shapeArgs.x,
y = (isNegative ? shapeArgs.height - h : 0) + shapeArgs.y,
x2 = shapeArgs.x + shapeArgs.width;
return [
'M', x1, y + h * strokeW,
'L', x1, y,
'L', x2, y,
'L', x2, y + h1 * strokeW,
'L', x1, y + h1 * strokeW,
'L', x2, y + h1 * strokeW,
'L', x2, y + h2 * strokeW,
'L', x1, y + h2 * strokeW,
'L', x2, y + h2 * strokeW,
'L', x2, y + h * strokeW
];
};
Highcharts.wrap(columnProto, 'drawTracker', function (proceed) {
var series = this,
options = series.options,
strokeW = series.borderWidth || 0;
proceed.apply(series);
if (
this.useDragHandle() &&
(options.draggableX || options.draggableY)
) {
each(series.points, function (point) {
var path = (
options.dragHandlePath ||
series.dragHandlePath
)(point.shapeArgs, strokeW, point.negative);
if (!point.handle) {
point.handle = series.chart.renderer.path(path)
.attr({
fill: options.dragHandleFill || 'rgba(0,0,0,0.5)',
'class': 'highcharts-handle',
'stroke-width': strokeW,
'stroke': options.dragHandleStroke ||
options.borderColor ||
1
})
.css({
cursor: 'ns-resize'
})
.add(series.group);
point.handle.element.point = point;
} else {
point.handle.attr({d: path});
}
});
}
});
}));