-
Notifications
You must be signed in to change notification settings - Fork 5
/
zoomout-selection.js
72 lines (58 loc) · 2.15 KB
/
zoomout-selection.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
/**
* Highstock plugin for zooming out X-Axis by dragging from right to left.
*
* Author: Roland Banguiran
* Email: banguiran@gmail.com
*
* Usage: Set zoomType to 'x'.
*/
// JSLint options:
/*global Highcharts */
(function (H) {
H.wrap(H.Chart.prototype, 'init', function (proceed) {
// Run the original proceed method
proceed.apply(this, Array.prototype.slice.call(arguments, 1));
var chart = this,
options = chart.options,
zoomType = options.chart.zoomType,
container = chart.container,
xAxis = chart.xAxis[0],
extremes,
dataMin,
dataMax,
min,
max,
selectFromPixels,
selectToPixels,
pixelDiff,
valueDiff,
newMin,
newMax;
if (zoomType === 'x') {
H.addEvent(container, 'mousedown', function (e) {
selectFromPixels = chart.pointer.normalize(e).chartX;
});
H.addEvent(container, 'mouseup', function (e) {
selectToPixels = chart.pointer.normalize(e).chartX;
pixelDiff = selectToPixels - selectFromPixels;
});
H.addEvent(chart, 'selection', function (e) {
console.log(e);
if (pixelDiff < 0) {
extremes = xAxis.getExtremes();
dataMin = extremes.dataMin;
dataMax = extremes.dataMax;
min = extremes.min;
max = extremes.max;
valueDiff = Math.abs(xAxis.toValue(selectToPixels) - xAxis.toValue(selectFromPixels));
newMin = min - valueDiff;
newMax = max + valueDiff;
newMin = (newMin > dataMin) ? newMin : dataMin;
newMax = (newMax < dataMax) ? newMax : dataMax;
xAxis.setExtremes(newMin, newMax);
e.preventDefault();
}
});
}
});
}(Highcharts));