-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscalable-yaxis.js
122 lines (101 loc) · 4.47 KB
/
scalable-yaxis.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
/**
* Highcharts plugin for manually scaling Y-Axis range.
*
* Author: Roland Banguiran
* Email: [email protected]
*
* Usage: Set scalable:false in the yAxis options to disable.
* Default: true
*/
// JSLint options:
/*global Highcharts, document */
(function (H) {
'use strict';
var addEvent = H.addEvent,
each = H.each,
doc = document,
body = doc.body;
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,
renderer = chart.renderer,
yAxis = chart.yAxis;
each(yAxis, function (yAxis) {
var options = yAxis.options,
scalable = options.scalable === undefined ? true : options.scalable,
labels = options.labels,
pointer = chart.pointer,
labelGroupBBox,
bBoxX,
bBoxY,
bBoxWidth,
bBoxHeight,
isDragging = false,
downYValue;
if (scalable) {
bBoxWidth = 40;
bBoxHeight = chart.containerHeight - yAxis.top - yAxis.bottom;
bBoxX = yAxis.opposite ? (labels.align === 'left' ? chart.containerWidth - yAxis.right : chart.containerWidth - (yAxis.right + bBoxWidth)) : (labels.align === 'left' ? yAxis.left : yAxis.left - bBoxWidth);
bBoxY = yAxis.top;
// Render an invisible bounding box around the y-axis label group
// This is where we add mousedown event to start dragging
labelGroupBBox = renderer.rect(bBoxX, bBoxY, bBoxWidth, bBoxHeight)
.attr({
fill: '#fff',
opacity: 0,
zIndex: 8
})
.css({
cursor: 'ns-resize'
})
.add();
labels.style.cursor = 'ns-resize';
addEvent(labelGroupBBox.element, 'mousedown', function (e) {
var downYPixels = pointer.normalize(e).chartY;
downYValue = yAxis.toValue(downYPixels);
isDragging = true;
});
addEvent(chart.container, 'mousemove', function (e) {
if (isDragging) {
body.style.cursor = 'ns-resize';
var dragYPixels = chart.pointer.normalize(e).chartY,
dragYValue = yAxis.toValue(dragYPixels),
extremes = yAxis.getExtremes(),
userMin = extremes.userMin,
userMax = extremes.userMax,
dataMin = extremes.dataMin,
dataMax = extremes.dataMax,
min = userMin !== undefined ? userMin : dataMin,
max = userMax !== undefined ? userMax : dataMax,
newMin,
newMax;
// update max extreme only if dragged from upper portion
// update min extreme only if dragged from lower portion
if (downYValue > (dataMin + dataMax) / 2) {
newMin = min;
newMax = max - (dragYValue - downYValue);
newMax = newMax > dataMax ? newMax : dataMax; //limit
} else {
newMin = min - (dragYValue - downYValue);
newMin = newMin < dataMin ? newMin : dataMin; //limit
newMax = max;
}
yAxis.setExtremes(newMin, newMax, true, false);
}
});
addEvent(document, 'mouseup', function () {
body.style.cursor = 'default';
isDragging = false;
});
// double-click to go back to default range
addEvent(labelGroupBBox.element, 'dblclick', function () {
var extremes = yAxis.getExtremes(),
dataMin = extremes.dataMin,
dataMax = extremes.dataMax;
yAxis.setExtremes(dataMin, dataMax, true, false);
});
}
});
});
}(Highcharts));