-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery.flot.originaxis.js
More file actions
200 lines (175 loc) · 6.61 KB
/
Copy pathjquery.flot.originaxis.js
File metadata and controls
200 lines (175 loc) · 6.61 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
/*
* The MIT License
Copyright (c) 2012 by Matt Burland
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
Normally flot draws the x axis at the bottom or top of the graph and the y axis at the left or right. This plugin will draw the axis through the origin
(i.e. 0,0) if that point is somewhere in the middle of the plot. Otherwise it will draw axis at the bottom and left as normal.
usage -
var options = {
crossOrigin: true
};
$.plot($("#placeholder), [{ data: [...] }}], options);
Individual axis can be turned off by setting crossOrigin to false for that particular axis -
var options = {
crossOrigin: true,
xaxis: {
crossOrigin: false
}
};
Now only the yaxis will be drawn as crossing the origin with the xaxis draw at the top or bottom as normal.
Note: this currently only effects the primary x and y axes. If you have multiple axes, they will not be affected.
*/
(function ($) {
function init(plot) {
// initalize needed hooks
plot.hooks.processOptions.push(processOptions);
function processOptions(plot, options) {
if (options.crossOrigin) {
if (options.xaxis.crossOrigin) {
options.xaxes[0].show = false;
options.xaxes[0].reserveSpace = true;
plot.hooks.drawBackground.push(drawXAxis);
}
if (options.yaxis.crossOrigin) {
options.yaxes[0].show = false;
options.yaxes[0].reserveSpace = true;
plot.hooks.drawBackground.push(drawYAxis);
}
}
}
}
function drawXAxis(plot, ctx) {
drawAxis(plot, ctx, plot.getAxes().xaxis);
}
function drawYAxis(plot, ctx) {
drawAxis(plot, ctx, plot.getAxes().yaxis);
}
function drawAxis(plot, ctx, axis) {
ctx.save();
var offset = plot.getPlotOffset();
var origin = plot.pointOffset({ x: 0, y: 0 });
var opt = plot.getOptions();
var height = plot.height();
var width = plot.width();
ctx.lineWidth = 1;
var yloc = yfrom = offset.top;
var xloc = xfrom = offset.left;
var yto = offset.top + height;
var xto = offset.left + width;
if (axis.direction == "x") {
yloc = Math.min(Math.max(origin.top, offset.top), plot.height() + offset.top);
yto = yloc;
}
else {
xloc = Math.min(Math.max(origin.left, offset.left), plot.width() + offset.left);
xto = xloc;
}
// draw axis
ctx.strokeStyle = opt.grid.borderColor;
ctx.beginPath();
ctx.moveTo(xloc, yloc);
ctx.lineTo(xto, yto);
ctx.stroke();
// draw ticks and labels
var f = axis.font;
ctx.font = f.style + " " + f.variant + " " + f.weight + " " + f.size + "px " + f.family;
ctx.textAlign = "start";
ctx.textBaseline = "middle";
ctx.fillStyle = axis.options.color;
ctx.strokeStyle = axis.options.tickColor || $.color.parse(axis.options.color).scale('a', 0.22).toString();
ctx.beginPath();
for (i = 0; i < axis.ticks.length; ++i) {
var tick = axis.ticks[i];
var v = tick.v;
if (v < axis.min || v > axis.max) {
continue;
}
yto = offset.top + height;
xto = offset.left + width;
aPos = axis.p2c(v); // + offset.top;
if (axis.direction == "x") {
xfrom = aPos + offset.left;
xto = xfrom;
}
else {
yfrom = aPos + offset.top;
yto = yfrom;
}
ctx.moveTo(xfrom, yfrom);
ctx.lineTo(xto, yto);
// draw labels
if (!tick.label)
continue;
var yOffset = 0;
for (k = 0; k < tick.lines.length; k++) {
var line = tick.lines[k];
var lPos = axis.p2c(tick.v);
if (opt.grid.borderWidth) {
if (lPos == 0) {
continue;
}
if (axis.direction == "x" && lPos == width) {
continue;
}
else if (lPos == height) {
continue;
}
}
var x = xloc - axis.box.padding - line.width;
var y = yloc + axis.box.padding;
if (axis.direction == "x") {
x = lPos + offset.left - line.width / 2;
}
else {
y = lPos + offset.top - tick.height / 2;
}
if (tick.v == 0) {
if (axis.direction == "x") {
x -= line.width;
}
else {
if (options.xaxis.crossOrigin && origin.left > offset.left && origin.left < (width + offset.left))
continue;
}
}
y += line.height / 2 + yOffset;
yOffset += line.height;
ctx.fillText(line.text, x, y);
}
}
ctx.stroke();
ctx.restore();
}
var options =
{
crossOrigin: false,
xaxis: {
crossOrigin: true
},
yaxis: {
crossOrigin: true
}
}; // any needed options
$.plot.plugins.push({
init: init,
options: options,
name: "originAxis",
version: "0.2"
});
})(jQuery);