forked from highcharts/draggable-points
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraggable-points.js
208 lines (171 loc) · 8.27 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
/**
* Draggable points plugin
* Author: Torstein Honsi
* License: MIT License
*
*/
(function (Highcharts) {
var addEvent = Highcharts.addEvent,
each = Highcharts.each,
pick = Highcharts.pick;
/**
* 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);
if (newY < dragMin) {
newY = dragMin;
} else if (newY > dragMax) {
newY = dragMax;
}
return newY;
}
Highcharts.dragdrop = {
mouseDown: function (e, chart) {
var hoverPoint = chart.hoverPoint,
options;
if (hoverPoint) {
options = hoverPoint.series.options;
if (options.draggableX) {
dragPoint = hoverPoint;
dragX = e.changedTouches ? e.changedTouches[0].pageX : e.pageX;
dragPlotX = dragPoint.plotX;
}
if (options.draggableY) {
dragPoint = hoverPoint;
dragY = e.changedTouches ? e.changedTouches[0].pageY : e.pageY;
dragPlotY = dragPoint.plotY + (chart.plotHeight - (dragPoint.yBottom || chart.plotHeight));
}
// Disable zooming when dragging
if (dragPoint) {
chart.mouseIsDown = false;
}
}
}
};
Highcharts.Chart.prototype.callbacks.push(function (chart) {
var container = chart.container,
dragPoint,
dragX,
dragY,
dragPlotX,
dragPlotY;
chart.redraw(); // kill animation (why was this again?)
addEvent(container, 'mousedown touchstart', function (e) {
var hoverPoint = chart.hoverPoint,
options;
if (hoverPoint) {
options = hoverPoint.series.options;
if (options.draggableX) {
dragPoint = hoverPoint;
dragX = e.originalEvent.changedTouches ? e.originalEvent.changedTouches[0].pageX : e.pageX;
dragPlotX = dragPoint.plotX;
}
if (options.draggableY) {
dragPoint = hoverPoint;
dragY = e.originalEvent.changedTouches ? e.originalEvent.changedTouches[0].pageY : e.pageY;
dragPlotY = dragPoint.plotY + (chart.plotHeight - (dragPoint.yBottom || chart.plotHeight));
}
// Disable zooming when dragging
if (dragPoint) {
chart.mouseIsDown = false;
}
}
});
addEvent(container, 'mousemove touchmove', function (e) {
e.preventDefault();
if (dragPoint) {
var pageX = e.originalEvent.changedTouches ? e.originalEvent.changedTouches[0].pageX : e.pageX,
pageY = e.originalEvent.changedTouches ? e.originalEvent.changedTouches[0].pageY : e.pageY,
deltaY = dragY - pageY,
deltaX = dragX - pageX,
draggableX = dragPoint.series.options.draggableX,
draggableY = dragPoint.series.options.draggableY,
series = dragPoint.series,
isScatter = series.type === 'bubble' || series.type === 'scatter',
newPlotX = isScatter ? dragPlotX - deltaX : dragPlotX - deltaX - dragPoint.series.xAxis.minPixelPadding,
newPlotY = chart.plotHeight - dragPlotY + deltaY,
newX = dragX === undefined ? dragPoint.x : dragPoint.series.xAxis.translate(newPlotX, true),
newY = dragY === undefined ? dragPoint.y : dragPoint.series.yAxis.translate(newPlotY, true),
proceed;
newX = filterRange(newX, series, 'X');
newY = filterRange(newY, series, 'Y');
// Fire the 'drag' event with a default action to move the point.
dragPoint.firePointEvent(
'drag', {
newX: draggableX ? newX : dragPoint.x,
newY: draggableY ? newY : dragPoint.y
},
function () {
proceed = true;
dragPoint.update({
x: draggableX ? newX : dragPoint.x,
y: draggableY ? newY : dragPoint.y
}, false);
if (chart.tooltip) {
chart.tooltip.refresh(chart.tooltip.shared ? [dragPoint] : dragPoint);
}
if (series.stackKey) {
chart.redraw();
} else {
series.redraw();
}
});
// The default handler has not run because of prevented default
if (!proceed) {
drop();
}
}
});
function drop(e) {
if (dragPoint) {
if (e) {
var pageX = e.originalEvent.changedTouches ? e.originalEvent.changedTouches[0].pageX : e.pageX,
pageY = e.originalEvent.changedTouches ? e.originalEvent.changedTouches[0].pageY : e.pageY,
draggableX = dragPoint.series.options.draggableX,
draggableY = dragPoint.series.options.draggableY,
deltaX = dragX - pageX,
deltaY = dragY - pageY,
series = dragPoint.series,
isScatter = series.type === 'bubble' || series.type === 'scatter',
newPlotX = isScatter ? dragPlotX - deltaX : dragPlotX - deltaX - dragPoint.series.xAxis.minPixelPadding,
newPlotY = chart.plotHeight - dragPlotY + deltaY,
newX = dragX === undefined ? dragPoint.x : dragPoint.series.xAxis.translate(newPlotX, true),
newY = dragY === undefined ? dragPoint.y : dragPoint.series.yAxis.translate(newPlotY, true);
newX = filterRange(newX, series, 'X');
newY = filterRange(newY, series, 'Y');
dragPoint.update({
x: draggableX ? newX : dragPoint.x,
y: draggableY ? newY : dragPoint.y
});
}
dragPoint.firePointEvent('drop');
}
dragPoint = dragX = dragY = undefined;
}
addEvent(document, 'mouseup touchend', drop);
addEvent(container, 'mouseleave', drop);
});
/**
* Extend the column chart tracker by visualizing the tracker object for small points
*/
Highcharts.wrap(Highcharts.seriesTypes.column.prototype, 'drawTracker', function (proceed) {
var series = this,
options = series.options;
proceed.apply(series);
if (options.draggableX || options.draggableY) {
each(series.points, function (point) {
point.graphic.attr(point.shapeArgs.height < 3 ? {
'stroke': 'black',
'stroke-width': 2,
'dashstyle': 'shortdot'
} : {
'stroke-width': series.options.borderWidth,
'dashstyle': series.options.dashStyle || 'solid'
});
});
}
});
})(Highcharts);