1 | /** |
---|
2 | * jqPlot |
---|
3 | * Pure JavaScript plotting plugin using jQuery |
---|
4 | * |
---|
5 | * Version: @VERSION |
---|
6 | * |
---|
7 | * Copyright (c) 2009-2011 Chris Leonello |
---|
8 | * jqPlot is currently available for use in all personal or commercial projects |
---|
9 | * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL |
---|
10 | * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can |
---|
11 | * choose the license that best suits your project and use it accordingly. |
---|
12 | * |
---|
13 | * Although not required, the author would appreciate an email letting him |
---|
14 | * know of any substantial use of jqPlot. You can reach the author at: |
---|
15 | * chris at jqplot dot com or see http://www.jqplot.com/info.php . |
---|
16 | * |
---|
17 | * If you are feeling kind and generous, consider supporting the project by |
---|
18 | * making a donation at: http://www.jqplot.com/donate.php . |
---|
19 | * |
---|
20 | * sprintf functions contained in jqplot.sprintf.js by Ash Searle: |
---|
21 | * |
---|
22 | * version 2007.04.27 |
---|
23 | * author Ash Searle |
---|
24 | * http://hexmen.com/blog/2007/03/printf-sprintf/ |
---|
25 | * http://hexmen.com/js/sprintf.js |
---|
26 | * The author (Ash Searle) has placed this code in the public domain: |
---|
27 | * "This code is unrestricted: you are free to use it however you like." |
---|
28 | * |
---|
29 | */ |
---|
30 | (function($) { |
---|
31 | // Class: $.jqplot.BezierCurveRenderer.js |
---|
32 | // Renderer which draws lines as stacked bezier curves. |
---|
33 | // Data for the line will not be specified as an array of |
---|
34 | // [x, y] data point values, but as a an array of [start piont, bezier curve] |
---|
35 | // So, the line is specified as: [[xstart, ystart], [cp1x, cp1y, cp2x, cp2y, xend, yend]]. |
---|
36 | $.jqplot.BezierCurveRenderer = function(){ |
---|
37 | $.jqplot.LineRenderer.call(this); |
---|
38 | }; |
---|
39 | |
---|
40 | $.jqplot.BezierCurveRenderer.prototype = new $.jqplot.LineRenderer(); |
---|
41 | $.jqplot.BezierCurveRenderer.prototype.constructor = $.jqplot.BezierCurveRenderer; |
---|
42 | |
---|
43 | |
---|
44 | // Method: setGridData |
---|
45 | // converts the user data values to grid coordinates and stores them |
---|
46 | // in the gridData array. |
---|
47 | // Called with scope of a series. |
---|
48 | $.jqplot.BezierCurveRenderer.prototype.setGridData = function(plot) { |
---|
49 | // recalculate the grid data |
---|
50 | var xp = this._xaxis.series_u2p; |
---|
51 | var yp = this._yaxis.series_u2p; |
---|
52 | // this._plotData should be same as this.data |
---|
53 | var data = this.data; |
---|
54 | this.gridData = []; |
---|
55 | this._prevGridData = []; |
---|
56 | // if seriesIndex = 0, fill to x axis. |
---|
57 | // if seriesIndex > 0, fill to previous series data. |
---|
58 | var idx = this.index; |
---|
59 | if (data.length == 2) { |
---|
60 | if (idx == 0) { |
---|
61 | this.gridData = [ |
---|
62 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])], |
---|
63 | [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]), |
---|
64 | xp.call(this._xaxis, data[1][2]), yp.call(this._yaxis, data[1][3]), |
---|
65 | xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, data[1][5])], |
---|
66 | [xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, this._yaxis.min)], |
---|
67 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, this._yaxis.min)] |
---|
68 | ]; |
---|
69 | } |
---|
70 | else { |
---|
71 | var psd = plot.series[idx-1].data; |
---|
72 | this.gridData = [ |
---|
73 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])], |
---|
74 | [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]), |
---|
75 | xp.call(this._xaxis, data[1][2]), yp.call(this._yaxis, data[1][3]), |
---|
76 | xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, data[1][5])], |
---|
77 | [xp.call(this._xaxis, psd[1][4]), yp.call(this._yaxis, psd[1][5])], |
---|
78 | [xp.call(this._xaxis, psd[1][2]), yp.call(this._yaxis, psd[1][3]), |
---|
79 | xp.call(this._xaxis, psd[1][0]), yp.call(this._yaxis, psd[1][1]), |
---|
80 | xp.call(this._xaxis, psd[0][0]), yp.call(this._yaxis, psd[0][1])] |
---|
81 | ]; |
---|
82 | } |
---|
83 | } |
---|
84 | else { |
---|
85 | if (idx == 0) { |
---|
86 | this.gridData = [ |
---|
87 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])], |
---|
88 | [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]), |
---|
89 | xp.call(this._xaxis, data[2][0]), yp.call(this._yaxis, data[2][1]), |
---|
90 | xp.call(this._xaxis, data[3][0]), yp.call(this._yaxis, data[3][1])], |
---|
91 | [xp.call(this._xaxis, data[3][1]), yp.call(this._yaxis, this._yaxis.min)], |
---|
92 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, this._yaxis.min)] |
---|
93 | ]; |
---|
94 | } |
---|
95 | else { |
---|
96 | var psd = plot.series[idx-1].data; |
---|
97 | this.gridData = [ |
---|
98 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])], |
---|
99 | [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]), |
---|
100 | xp.call(this._xaxis, data[2][0]), yp.call(this._yaxis, data[2][1]), |
---|
101 | xp.call(this._xaxis, data[3][0]), yp.call(this._yaxis, data[3][1])], |
---|
102 | [xp.call(this._xaxis, psd[3][0]), yp.call(this._yaxis, psd[3][1])], |
---|
103 | [xp.call(this._xaxis, psd[2][0]), yp.call(this._yaxis, psd[2][1]), |
---|
104 | xp.call(this._xaxis, psd[1][0]), yp.call(this._yaxis, psd[1][1]), |
---|
105 | xp.call(this._xaxis, psd[0][0]), yp.call(this._yaxis, psd[0][1])] |
---|
106 | ]; |
---|
107 | } |
---|
108 | } |
---|
109 | }; |
---|
110 | |
---|
111 | // Method: makeGridData |
---|
112 | // converts any arbitrary data values to grid coordinates and |
---|
113 | // returns them. This method exists so that plugins can use a series' |
---|
114 | // linerenderer to generate grid data points without overwriting the |
---|
115 | // grid data associated with that series. |
---|
116 | // Called with scope of a series. |
---|
117 | $.jqplot.BezierCurveRenderer.prototype.makeGridData = function(data, plot) { |
---|
118 | // recalculate the grid data |
---|
119 | var xp = this._xaxis.series_u2p; |
---|
120 | var yp = this._yaxis.series_u2p; |
---|
121 | var gd = []; |
---|
122 | var pgd = []; |
---|
123 | // if seriesIndex = 0, fill to x axis. |
---|
124 | // if seriesIndex > 0, fill to previous series data. |
---|
125 | var idx = this.index; |
---|
126 | if (data.length == 2) { |
---|
127 | if (idx == 0) { |
---|
128 | gd = [ |
---|
129 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])], |
---|
130 | [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]), |
---|
131 | xp.call(this._xaxis, data[1][2]), yp.call(this._yaxis, data[1][3]), |
---|
132 | xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, data[1][5])], |
---|
133 | [xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, this._yaxis.min)], |
---|
134 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, this._yaxis.min)] |
---|
135 | ]; |
---|
136 | } |
---|
137 | else { |
---|
138 | var psd = plot.series[idx-1].data; |
---|
139 | gd = [ |
---|
140 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])], |
---|
141 | [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]), |
---|
142 | xp.call(this._xaxis, data[1][2]), yp.call(this._yaxis, data[1][3]), |
---|
143 | xp.call(this._xaxis, data[1][4]), yp.call(this._yaxis, data[1][5])], |
---|
144 | [xp.call(this._xaxis, psd[1][4]), yp.call(this._yaxis, psd[1][5])], |
---|
145 | [xp.call(this._xaxis, psd[1][2]), yp.call(this._yaxis, psd[1][3]), |
---|
146 | xp.call(this._xaxis, psd[1][0]), yp.call(this._yaxis, psd[1][1]), |
---|
147 | xp.call(this._xaxis, psd[0][0]), yp.call(this._yaxis, psd[0][1])] |
---|
148 | ]; |
---|
149 | } |
---|
150 | } |
---|
151 | else { |
---|
152 | if (idx == 0) { |
---|
153 | gd = [ |
---|
154 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])], |
---|
155 | [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]), |
---|
156 | xp.call(this._xaxis, data[2][0]), yp.call(this._yaxis, data[2][1]), |
---|
157 | xp.call(this._xaxis, data[3][0]), yp.call(this._yaxis, data[3][1])], |
---|
158 | [xp.call(this._xaxis, data[3][1]), yp.call(this._yaxis, this._yaxis.min)], |
---|
159 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, this._yaxis.min)] |
---|
160 | ]; |
---|
161 | } |
---|
162 | else { |
---|
163 | var psd = plot.series[idx-1].data; |
---|
164 | gd = [ |
---|
165 | [xp.call(this._xaxis, data[0][0]), yp.call(this._yaxis, data[0][1])], |
---|
166 | [xp.call(this._xaxis, data[1][0]), yp.call(this._yaxis, data[1][1]), |
---|
167 | xp.call(this._xaxis, data[2][0]), yp.call(this._yaxis, data[2][1]), |
---|
168 | xp.call(this._xaxis, data[3][0]), yp.call(this._yaxis, data[3][1])], |
---|
169 | [xp.call(this._xaxis, psd[3][0]), yp.call(this._yaxis, psd[3][1])], |
---|
170 | [xp.call(this._xaxis, psd[2][0]), yp.call(this._yaxis, psd[2][1]), |
---|
171 | xp.call(this._xaxis, psd[1][0]), yp.call(this._yaxis, psd[1][1]), |
---|
172 | xp.call(this._xaxis, psd[0][0]), yp.call(this._yaxis, psd[0][1])] |
---|
173 | ]; |
---|
174 | } |
---|
175 | } |
---|
176 | return gd; |
---|
177 | }; |
---|
178 | |
---|
179 | |
---|
180 | // called within scope of series. |
---|
181 | $.jqplot.BezierCurveRenderer.prototype.draw = function(ctx, gd, options) { |
---|
182 | var i; |
---|
183 | ctx.save(); |
---|
184 | if (gd.length) { |
---|
185 | if (this.showLine) { |
---|
186 | ctx.save(); |
---|
187 | var opts = (options != null) ? options : {}; |
---|
188 | ctx.fillStyle = opts.fillStyle || this.color; |
---|
189 | ctx.beginPath(); |
---|
190 | ctx.moveTo(gd[0][0], gd[0][1]); |
---|
191 | ctx.bezierCurveTo(gd[1][0], gd[1][1], gd[1][2], gd[1][3], gd[1][4], gd[1][5]); |
---|
192 | ctx.lineTo(gd[2][0], gd[2][1]); |
---|
193 | if (gd[3].length == 2) { |
---|
194 | ctx.lineTo(gd[3][0], gd[3][1]); |
---|
195 | } |
---|
196 | else { |
---|
197 | ctx.bezierCurveTo(gd[3][0], gd[3][1], gd[3][2], gd[3][3], gd[3][4], gd[3][5]); |
---|
198 | } |
---|
199 | ctx.closePath(); |
---|
200 | ctx.fill(); |
---|
201 | ctx.restore(); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | ctx.restore(); |
---|
206 | }; |
---|
207 | |
---|
208 | $.jqplot.BezierCurveRenderer.prototype.drawShadow = function(ctx, gd, options) { |
---|
209 | // This is a no-op, shadows drawn with lines. |
---|
210 | }; |
---|
211 | |
---|
212 | $.jqplot.BezierAxisRenderer = function() { |
---|
213 | $.jqplot.LinearAxisRenderer.call(this); |
---|
214 | }; |
---|
215 | |
---|
216 | $.jqplot.BezierAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer(); |
---|
217 | $.jqplot.BezierAxisRenderer.prototype.constructor = $.jqplot.BezierAxisRenderer; |
---|
218 | |
---|
219 | |
---|
220 | // Axes on a plot with Bezier Curves |
---|
221 | $.jqplot.BezierAxisRenderer.prototype.init = function(options){ |
---|
222 | $.extend(true, this, options); |
---|
223 | var db = this._dataBounds; |
---|
224 | // Go through all the series attached to this axis and find |
---|
225 | // the min/max bounds for this axis. |
---|
226 | for (var i=0; i<this._series.length; i++) { |
---|
227 | var s = this._series[i]; |
---|
228 | var d = s.data; |
---|
229 | if (d.length == 4) { |
---|
230 | for (var j=0; j<d.length; j++) { |
---|
231 | if (this.name == 'xaxis' || this.name == 'x2axis') { |
---|
232 | if (d[j][0] < db.min || db.min == null) { |
---|
233 | db.min = d[j][0]; |
---|
234 | } |
---|
235 | if (d[j][0] > db.max || db.max == null) { |
---|
236 | db.max = d[j][0]; |
---|
237 | } |
---|
238 | } |
---|
239 | else { |
---|
240 | if (d[j][1] < db.min || db.min == null) { |
---|
241 | db.min = d[j][1]; |
---|
242 | } |
---|
243 | if (d[j][1] > db.max || db.max == null) { |
---|
244 | db.max = d[j][1]; |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | else { |
---|
250 | if (this.name == 'xaxis' || this.name == 'x2axis') { |
---|
251 | if (d[0][0] < db.min || db.min == null) { |
---|
252 | db.min = d[0][0]; |
---|
253 | } |
---|
254 | if (d[0][0] > db.max || db.max == null) { |
---|
255 | db.max = d[0][0]; |
---|
256 | } |
---|
257 | for (var j=0; j<5; j+=2) { |
---|
258 | if (d[1][j] < db.min || db.min == null) { |
---|
259 | db.min = d[1][j]; |
---|
260 | } |
---|
261 | if (d[1][j] > db.max || db.max == null) { |
---|
262 | db.max = d[1][j]; |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | else { |
---|
267 | if (d[0][1] < db.min || db.min == null) { |
---|
268 | db.min = d[0][1]; |
---|
269 | } |
---|
270 | if (d[0][1] > db.max || db.max == null) { |
---|
271 | db.max = d[0][1]; |
---|
272 | } |
---|
273 | for (var j=1; j<6; j+=2) { |
---|
274 | if (d[1][j] < db.min || db.min == null) { |
---|
275 | db.min = d[1][j]; |
---|
276 | } |
---|
277 | if (d[1][j] > db.max || db.max == null) { |
---|
278 | db.max = d[1][j]; |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | }; |
---|
285 | |
---|
286 | // setup default renderers for axes and legend so user doesn't have to |
---|
287 | // called with scope of plot |
---|
288 | function preInit(target, data, options) { |
---|
289 | options = options || {}; |
---|
290 | options.axesDefaults = $.extend(true, {pad:0}, options.axesDefaults); |
---|
291 | options.legend = $.extend(true, {placement:'outside'}, options.legend); |
---|
292 | // only set these if there is a pie series |
---|
293 | var setopts = false; |
---|
294 | if (options.seriesDefaults.renderer == $.jqplot.BezierCurveRenderer) { |
---|
295 | setopts = true; |
---|
296 | } |
---|
297 | else if (options.series) { |
---|
298 | for (var i=0; i < options.series.length; i++) { |
---|
299 | if (options.series[i].renderer == $.jqplot.BezierCurveRenderer) { |
---|
300 | setopts = true; |
---|
301 | } |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | if (setopts) { |
---|
306 | options.axesDefaults.renderer = $.jqplot.BezierAxisRenderer; |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | $.jqplot.preInitHooks.push(preInit); |
---|
311 | |
---|
312 | })(jQuery); |
---|