drag-panes.src.js
18.2 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
/**
* @license Highcharts JS v6.2.0 (2018-10-17)
* Drag-panes module
*
* (c) 2010-2017 Highsoft AS
* Author: Kacper Madej
*
* License: www.highcharts.com/license
*/
'use strict';
(function (factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory;
} else if (typeof define === 'function' && define.amd) {
define(function () {
return factory;
});
} else {
factory(Highcharts);
}
}(function (Highcharts) {
(function (H) {
/**
* Plugin for resizing axes / panes in a chart.
*
* (c) 2010-2017 Highsoft AS
* Author: Kacper Madej
*
* License: www.highcharts.com/license
*/
var hasTouch = H.hasTouch,
merge = H.merge,
wrap = H.wrap,
each = H.each,
isNumber = H.isNumber,
addEvent = H.addEvent,
relativeLength = H.relativeLength,
objectEach = H.objectEach,
Axis = H.Axis,
Pointer = H.Pointer,
/**
* Default options for AxisResizer.
*/
resizerOptions = {
/**
* Minimal size of a resizable axis. Could be set as a percent
* of plot area or pixel size.
*
* This feature requires the `drag-panes.js` module.
*
* @type {Number|String}
* @product highstock
* @sample {highstock} stock/yaxis/resize-min-max-length
* minLength and maxLength
* @apioption yAxis.minLength
*/
minLength: '10%',
/**
* Maximal size of a resizable axis. Could be set as a percent
* of plot area or pixel size.
*
* This feature requires the `drag-panes.js` module.
*
* @type {String|Number}
* @product highstock
* @sample {highstock} stock/yaxis/resize-min-max-length
* minLength and maxLength
* @apioption yAxis.maxLength
*/
maxLength: '100%',
/**
* Options for axis resizing. This feature requires the
* `drag-panes.js` -
* [classic](http://code.highcharts.com/stock/modules/drag-panes.js) or
* [styled](http://code.highcharts.com/stock/js/modules/drag-panes.js)
* mode - module.
*
* @product highstock
* @sample {highstock} stock/demo/candlestick-and-volume
* Axis resizing enabled
* @optionparent yAxis.resize
*/
resize: {
/**
* Contains two arrays of axes that are controlled by control line
* of the axis.
*
* This feature requires the `drag-panes.js` module.
*/
controlledAxis: {
/**
* Array of axes that should move out of the way of resizing
* being done for the current axis. If not set, the next axis
* will be used.
*
* This feature requires the `drag-panes.js` module.
*
* @type {Array<String|Number>}
* @default []
* @sample {highstock} stock/yaxis/multiple-resizers
* Three panes with resizers
* @sample {highstock} stock/yaxis/resize-multiple-axes
* One resizer controlling multiple axes
*/
next: [],
/**
* Array of axes that should move with the current axis
* while resizing.
*
* This feature requires the `drag-panes.js` module.
*
* @type {Array<String|Number>}
* @sample {highstock} stock/yaxis/multiple-resizers
* Three panes with resizers
* @sample {highstock} stock/yaxis/resize-multiple-axes
* One resizer controlling multiple axes
*/
prev: []
},
/**
* Enable or disable resize by drag for the axis.
*
* This feature requires the `drag-panes.js` module.
*
* @sample {highstock} stock/demo/candlestick-and-volume
* Enabled resizer
*/
enabled: false,
/**
* Horizontal offset of the control line.
*
* This feature requires the `drag-panes.js` module.
*
* @sample {highstock} stock/yaxis/styled-resizer Styled resizer
*/
x: 0,
/**
* Vertical offset of the control line.
*
* This feature requires the `drag-panes.js` module.
*
* @sample {highstock} stock/yaxis/styled-resizer Styled resizer
*/
y: 0
}
};
merge(true, Axis.prototype.defaultYAxisOptions, resizerOptions);
/**
* The AxisResizer class.
* @param {Object} axis - main axis for the AxisResizer.
* @class
*/
H.AxisResizer = function (axis) {
this.init(axis);
};
H.AxisResizer.prototype = {
/**
* Initiate the AxisResizer object.
* @param {Object} axis - main axis for the AxisResizer.
*/
init: function (axis, update) {
this.axis = axis;
this.options = axis.options.resize;
this.render();
if (!update) {
// Add mouse events.
this.addMouseEvents();
}
},
/**
* Render the AxisResizer
*/
render: function () {
var resizer = this,
axis = resizer.axis,
chart = axis.chart,
options = resizer.options,
x = options.x,
y = options.y,
// Normalize control line position according to the plot area
pos = Math.min(
Math.max(
axis.top + axis.height + y,
chart.plotTop
),
chart.plotTop + chart.plotHeight
),
attr = {},
lineWidth;
// Register current position for future reference.
resizer.lastPos = pos - y;
if (!resizer.controlLine) {
resizer.controlLine = chart.renderer.path()
.addClass('highcharts-axis-resizer');
}
// Add to axisGroup after axis update, because the group is recreated
// Do .add() before path is calculated because strokeWidth() needs it.
resizer.controlLine.add(axis.axisGroup);
lineWidth = resizer.controlLine.strokeWidth();
attr.d = chart.renderer.crispLine(
[
'M', axis.left + x, pos,
'L', axis.left + axis.width + x, pos
],
lineWidth
);
resizer.controlLine.attr(attr);
},
/**
* Set up the mouse and touch events for the control line.
*/
addMouseEvents: function () {
var resizer = this,
ctrlLineElem = resizer.controlLine.element,
container = resizer.axis.chart.container,
eventsToUnbind = [],
mouseMoveHandler,
mouseUpHandler,
mouseDownHandler;
/**
* Create mouse events' handlers.
* Make them as separate functions to enable wrapping them:
*/
resizer.mouseMoveHandler = mouseMoveHandler = function (e) {
resizer.onMouseMove(e);
};
resizer.mouseUpHandler = mouseUpHandler = function (e) {
resizer.onMouseUp(e);
};
resizer.mouseDownHandler = mouseDownHandler = function (e) {
resizer.onMouseDown(e);
};
/**
* Add mouse move and mouseup events. These are bind to doc/container,
* because resizer.grabbed flag is stored in mousedown events.
*/
eventsToUnbind.push(
addEvent(container, 'mousemove', mouseMoveHandler),
addEvent(container.ownerDocument, 'mouseup', mouseUpHandler),
addEvent(ctrlLineElem, 'mousedown', mouseDownHandler)
);
// Touch events.
if (hasTouch) {
eventsToUnbind.push(
addEvent(container, 'touchmove', mouseMoveHandler),
addEvent(container.ownerDocument, 'touchend', mouseUpHandler),
addEvent(ctrlLineElem, 'touchstart', mouseDownHandler)
);
}
resizer.eventsToUnbind = eventsToUnbind;
},
/**
* Mouse move event based on x/y mouse position.
* @param {Object} e - mouse event.
*/
onMouseMove: function (e) {
/*
* In iOS, a mousemove event with e.pageX === 0 is fired when holding
* the finger down in the center of the scrollbar. This should
* be ignored. Borrowed from Navigator.
*/
if (!e.touches || e.touches[0].pageX !== 0) {
// Drag the control line
if (this.grabbed) {
this.hasDragged = true;
this.updateAxes(this.axis.chart.pointer.normalize(e).chartY -
this.options.y);
}
}
},
/**
* Mouse up event based on x/y mouse position.
* @param {Object} e - mouse event.
*/
onMouseUp: function (e) {
if (this.hasDragged) {
this.updateAxes(this.axis.chart.pointer.normalize(e).chartY -
this.options.y);
}
// Restore runPointActions.
this.grabbed = this.hasDragged = this.axis.chart.activeResizer = null;
},
/**
* Mousedown on a control line.
* Will store necessary information for drag&drop.
*/
onMouseDown: function () {
// Clear all hover effects.
this.axis.chart.pointer.reset(false, 0);
// Disable runPointActions.
this.grabbed = this.axis.chart.activeResizer = true;
},
/**
* Update all connected axes after a change of control line position
*/
updateAxes: function (chartY) {
var resizer = this,
chart = resizer.axis.chart,
axes = resizer.options.controlledAxis,
nextAxes = axes.next.length === 0 ?
[H.inArray(resizer.axis, chart.yAxis) + 1] : axes.next,
// Main axis is included in the prev array by default
prevAxes = [resizer.axis].concat(axes.prev),
axesConfigs = [], // prev and next configs
stopDrag = false,
plotTop = chart.plotTop,
plotHeight = chart.plotHeight,
plotBottom = plotTop + plotHeight,
yDelta,
normalize = function (val, min, max) {
return Math.round(Math.min(Math.max(val, min), max));
};
// Normalize chartY to plot area limits
chartY = Math.max(Math.min(chartY, plotBottom), plotTop);
yDelta = chartY - resizer.lastPos;
// Update on changes of at least 1 pixel in the desired direction
if (yDelta * yDelta < 1) {
return;
}
// First gather info how axes should behave
each([prevAxes, nextAxes], function (axesGroup, isNext) {
each(axesGroup, function (axisInfo, i) {
// Axes given as array index, axis object or axis id
var axis = isNumber(axisInfo) ?
// If it's a number - it's an index
chart.yAxis[axisInfo] :
(
// If it's first elem. in first group
(!isNext && !i) ?
// then it's an Axis object
axisInfo :
// else it should be an id
chart.get(axisInfo)
),
axisOptions = axis && axis.options,
optionsToUpdate = {},
hDelta = 0,
height, top,
minLength, maxLength;
// Skip if axis is not found
// or it is navigator's yAxis (#7732)
if (
!axisOptions ||
axisOptions.id === 'navigator-y-axis'
) {
return;
}
top = axis.top;
minLength = Math.round(
relativeLength(
axisOptions.minLength,
plotHeight
)
);
maxLength = Math.round(
relativeLength(
axisOptions.maxLength,
plotHeight
)
);
if (isNext) {
// Try to change height first. yDelta could had changed
yDelta = chartY - resizer.lastPos;
// Normalize height to option limits
height = normalize(axis.len - yDelta, minLength, maxLength);
// Adjust top, so the axis looks like shrinked from top
top = axis.top + yDelta;
// Check for plot area limits
if (top + height > plotBottom) {
hDelta = plotBottom - height - top;
chartY += hDelta;
top += hDelta;
}
// Fit to plot - when overflowing on top
if (top < plotTop) {
top = plotTop;
if (top + height > plotBottom) {
height = plotHeight;
}
}
// If next axis meets min length, stop dragging:
if (height === minLength) {
stopDrag = true;
}
axesConfigs.push({
axis: axis,
options: {
top: Math.round(top),
height: height
}
});
} else {
// Normalize height to option limits
height = normalize(chartY - top, minLength, maxLength);
// If prev axis meets max length, stop dragging:
if (height === maxLength) {
stopDrag = true;
}
// Check axis size limits
chartY = top + height;
axesConfigs.push({
axis: axis,
options: {
height: height
}
});
}
optionsToUpdate.height = height;
});
});
// If we hit the min/maxLength with dragging, don't do anything:
if (!stopDrag) {
// Now update axes:
each(axesConfigs, function (config) {
config.axis.update(config.options, false);
});
chart.redraw(false);
}
},
/**
* Destroy AxisResizer. Clear outside references, clear events,
* destroy elements, nullify properties.
*/
destroy: function () {
var resizer = this,
axis = resizer.axis;
// Clear resizer in axis
delete axis.resizer;
// Clear control line events
if (this.eventsToUnbind) {
each(this.eventsToUnbind, function (unbind) {
unbind();
});
}
// Destroy AxisResizer elements
resizer.controlLine.destroy();
// Nullify properties
objectEach(resizer, function (val, key) {
resizer[key] = null;
});
}
};
// Keep resizer reference on axis update
Axis.prototype.keepProps.push('resizer');
// Add new AxisResizer, update or remove it
addEvent(Axis, 'afterRender', function () {
var axis = this,
resizer = axis.resizer,
resizerOptions = axis.options.resize,
enabled;
if (resizerOptions) {
enabled = resizerOptions.enabled !== false;
if (resizer) {
// Resizer present and enabled
if (enabled) {
// Update options
resizer.init(axis, true);
// Resizer present, but disabled
} else {
// Destroy the resizer
resizer.destroy();
}
} else {
// Resizer not present and enabled
if (enabled) {
// Add new resizer
axis.resizer = new H.AxisResizer(axis);
}
// Resizer not present and disabled, so do nothing
}
}
});
// Clear resizer on axis remove.
addEvent(Axis, 'destroy', function (e) {
if (!e.keepEvents && this.resizer) {
this.resizer.destroy();
}
});
// Prevent any hover effects while dragging a control line of AxisResizer.
wrap(Pointer.prototype, 'runPointActions', function (proceed) {
if (!this.chart.activeResizer) {
proceed.apply(this, Array.prototype.slice.call(arguments, 1));
}
});
// Prevent default drag action detection while dragging a control line
// of AxisResizer. (#7563)
wrap(Pointer.prototype, 'drag', function (proceed) {
if (!this.chart.activeResizer) {
proceed.apply(this, Array.prototype.slice.call(arguments, 1));
}
});
}(Highcharts));
return (function () {
}());
}));