OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / jquery / jqplot1.0.7 / plugins / jqplot.pyramidRenderer.js
1 /**
2  * jqPlot
3  * Pure JavaScript plotting plugin using jQuery
4  *
5  * Version: 1.0.7
6  * Revision: 1224
7  *
8  * Copyright (c) 2009-2013 Chris Leonello
9  * jqPlot is currently available for use in all personal or commercial projects 
10  * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL 
11  * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can 
12  * choose the license that best suits your project and use it accordingly. 
13  *
14  * Although not required, the author would appreciate an email letting him 
15  * know of any substantial use of jqPlot.  You can reach the author at: 
16  * chris at jqplot dot com or see http://www.jqplot.com/info.php .
17  *
18  * If you are feeling kind and generous, consider supporting the project by
19  * making a donation at: http://www.jqplot.com/donate.php .
20  *
21  * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
22  *
23  *     version 2007.04.27
24  *     author Ash Searle
25  *     http://hexmen.com/blog/2007/03/printf-sprintf/
26  *     http://hexmen.com/js/sprintf.js
27  *     The author (Ash Searle) has placed this code in the public domain:
28  *     "This code is unrestricted: you are free to use it however you like."
29  * 
30  */
31 (function($) {
32
33     // Need to ensure pyramid axis and grid renderers are loaded.
34     // You should load these with script tags in the html head, that is more efficient
35     // as the browser will cache the request.
36     // Note, have to block with synchronous request in order to execute bar renderer code.
37     if ($.jqplot.PyramidAxisRenderer === undefined) {
38         $.ajax({
39             url: $.jqplot.pluginLocation + 'jqplot.pyramidAxisRenderer.js',
40             dataType: "script",
41             async: false
42         });
43     }
44     
45     if ($.jqplot.PyramidGridRenderer === undefined) {
46         $.ajax({
47             url: $.jqplot.pluginLocation + 'jqplot.pyramidGridRenderer.js',
48             dataType: "script",
49             async: false
50         });
51     }
52
53     $.jqplot.PyramidRenderer = function(){
54         $.jqplot.LineRenderer.call(this);
55     };
56     
57     $.jqplot.PyramidRenderer.prototype = new $.jqplot.LineRenderer();
58     $.jqplot.PyramidRenderer.prototype.constructor = $.jqplot.PyramidRenderer;
59     
60     // called with scope of a series
61     $.jqplot.PyramidRenderer.prototype.init = function(options, plot) {
62         options = options || {};
63         this._type = 'pyramid';
64         // Group: Properties
65         //
66         // prop: barPadding
67         this.barPadding = 10;
68         this.barWidth = null;
69         // prop: fill
70         // True to fill the bars.
71         this.fill = true;
72         // prop: highlightMouseOver
73         // True to highlight slice when moused over.
74         // This must be false to enable highlightMouseDown to highlight when clicking on a slice.
75         this.highlightMouseOver = true;
76         // prop: highlightMouseDown
77         // True to highlight when a mouse button is pressed over a slice.
78         // This will be disabled if highlightMouseOver is true.
79         this.highlightMouseDown = false;
80         // prop: highlightColors
81         // an array of colors to use when highlighting a slice.
82         this.highlightColors = [];
83         // prop highlightThreshold
84         // Expand the highlightable region in the x direction.
85         // E.g. a value of 3 will highlight a bar when the mouse is
86         // within 3 pixels of the bar in the x direction.
87         this.highlightThreshold = 2;
88         // prop: synchronizeHighlight
89         // Index of another series to highlight when this series is highlighted.
90         // null or false to not synchronize.
91         this.synchronizeHighlight = false;
92         // prop: offsetBars
93         // False will center bars on their y value.
94         // True will push bars up by 1/2 bar width to fill between their y values.
95         // If true, there needs to be 1 more tick than there are bars.
96         this.offsetBars = false;
97         
98         // if user has passed in highlightMouseDown option and not set highlightMouseOver, disable highlightMouseOver
99         if (options.highlightMouseDown && options.highlightMouseOver == null) {
100             options.highlightMouseOver = false;
101         }
102
103         this.side = 'right';
104         
105         $.extend(true, this, options);
106
107         // if (this.fill === false) {
108         //     this.shadow = false;
109         // }
110
111         if (this.side === 'left') {
112             this._highlightThreshold = [[-this.highlightThreshold, 0], [-this.highlightThreshold, 0], [0,0], [0,0]];
113         }
114
115         else {
116             this._highlightThreshold = [[0,0], [0,0], [this.highlightThreshold, 0], [this.highlightThreshold, 0]];
117         }
118         
119         this.renderer.options = options;
120         // index of the currenty highlighted point, if any
121         this._highlightedPoint = null;
122         // Array of actual data colors used for each data point.
123         this._dataColors = [];
124         this._barPoints = [];
125         this.fillAxis = 'y';
126         this._primaryAxis = '_yaxis';
127         this._xnudge = 0;
128         
129         // set the shape renderer options
130         var opts = {lineJoin:'miter', lineCap:'butt', fill:this.fill, fillRect:this.fill, isarc:false, strokeStyle:this.color, fillStyle:this.color, closePath:this.fill, lineWidth: this.lineWidth};
131         this.renderer.shapeRenderer.init(opts);
132         // set the shadow renderer options
133         var shadow_offset = options.shadowOffset;
134         // set the shadow renderer options
135         if (shadow_offset == null) {
136             // scale the shadowOffset to the width of the line.
137             if (this.lineWidth > 2.5) {
138                 shadow_offset = 1.25 * (1 + (Math.atan((this.lineWidth/2.5))/0.785398163 - 1)*0.6);
139                 // var shadow_offset = this.shadowOffset;
140             }
141             // for skinny lines, don't make such a big shadow.
142             else {
143                 shadow_offset = 1.25 * Math.atan((this.lineWidth/2.5))/0.785398163;
144             }
145         }
146         var sopts = {lineJoin:'miter', lineCap:'butt', fill:this.fill, fillRect:this.fill, isarc:false, angle:this.shadowAngle, offset:shadow_offset, alpha:this.shadowAlpha, depth:this.shadowDepth, closePath:this.fill, lineWidth: this.lineWidth};
147         this.renderer.shadowRenderer.init(sopts);
148
149         plot.postDrawHooks.addOnce(postPlotDraw);
150         plot.eventListenerHooks.addOnce('jqplotMouseMove', handleMove);
151
152         // if this is the left side of pyramid, set y values to negative.
153         if (this.side === 'left') {
154             for (var i=0, l=this.data.length; i<l; i++) {
155                 this.data[i][1] = -Math.abs(this.data[i][1]);
156             }
157         }
158     };
159     
160     // setGridData
161     // converts the user data values to grid coordinates and stores them
162     // in the gridData array.
163     // Called with scope of a series.
164     $.jqplot.PyramidRenderer.prototype.setGridData = function(plot) {
165         // recalculate the grid data
166         var xp = this._xaxis.series_u2p;
167         var yp = this._yaxis.series_u2p;
168         var data = this._plotData;
169         var pdata = this._prevPlotData;
170         this.gridData = [];
171         this._prevGridData = [];
172         var l = data.length;
173         var adjust = false;
174         var i;
175
176         // if any data values are < 0,  consider this a negative series
177         for (i = 0; i < l; i++) {
178             if (data[i][1] < 0) {
179                 this.side = 'left';
180             }
181         }
182
183         if (this._yaxis.name === 'yMidAxis' && this.side === 'right') {
184             this._xnudge = this._xaxis.max/2000.0;
185             adjust = true;
186         }
187
188         for (i = 0; i < l; i++) {
189             // if not a line series or if no nulls in data, push the converted point onto the array.
190             if (data[i][0] != null && data[i][1] != null) {
191                 this.gridData.push([xp(data[i][1]), yp(data[i][0])]);
192             }
193             // else if there is a null, preserve it.
194             else if (data[i][0] == null) {
195                 this.gridData.push([xp(data[i][1]), null]);
196             }
197             else if (data[i][1] == null) {
198                 this.gridData.push(null, [yp(data[i][0])]);
199             }
200             // finally, adjust x grid data if have to
201             if (data[i][1] === 0 && adjust) {
202                 this.gridData[i][0] = xp(this._xnudge);
203             }
204         }
205     };
206     
207     // makeGridData
208     // converts any arbitrary data values to grid coordinates and
209     // returns them.  This method exists so that plugins can use a series'
210     // linerenderer to generate grid data points without overwriting the
211     // grid data associated with that series.
212     // Called with scope of a series.
213     $.jqplot.PyramidRenderer.prototype.makeGridData = function(data, plot) {
214         // recalculate the grid data
215         var xp = this._xaxis.series_u2p;
216         var yp = this._yaxis.series_u2p;
217         var gd = [];
218         var l = data.length;
219         var adjust = false;
220         var i;
221
222         // if any data values are < 0,  consider this a negative series
223         for (i = 0; i < l; i++) {
224             if (data[i][1] < 0) {
225                 this.side = 'left';
226             }
227         }
228
229         if (this._yaxis.name === 'yMidAxis' && this.side === 'right') {
230             this._xnudge = this._xaxis.max/2000.0;
231             adjust = true;
232         }
233
234         for (i = 0; i < l; i++) {
235             // if not a line series or if no nulls in data, push the converted point onto the array.
236             if (data[i][0] != null && data[i][1] != null) {
237                 gd.push([xp(data[i][1]), yp(data[i][0])]);
238             }
239             // else if there is a null, preserve it.
240             else if (data[i][0] == null) {
241                 gd.push([xp(data[i][1]), null]);
242             }
243             else if (data[i][1] == null) {
244                 gd.push([null, yp(data[i][0])]);
245             }
246             // finally, adjust x grid data if have to
247             if (data[i][1] === 0 && adjust) {
248                 gd[i][0] = xp(this._xnudge);
249             }
250         }
251
252         return gd;
253     };
254
255     $.jqplot.PyramidRenderer.prototype.setBarWidth = function() {
256         // need to know how many data values we have on the approprate axis and figure it out.
257         var i;
258         var nvals = 0;
259         var nseries = 0;
260         var paxis = this[this._primaryAxis];
261         var s, series, pos;
262         nvals = paxis.max - paxis.min;
263         var nticks = paxis.numberTicks;
264         var nbins = (nticks-1)/2;
265         // so, now we have total number of axis values.
266         var temp = (this.barPadding === 0) ? 1.0 : 0;
267         if (paxis.name == 'xaxis' || paxis.name == 'x2axis') {
268             this.barWidth = (paxis._offsets.max - paxis._offsets.min) / nvals - this.barPadding + temp;
269         }
270         else {
271             if (this.fill) {
272                 this.barWidth = (paxis._offsets.min - paxis._offsets.max) / nvals - this.barPadding + temp;
273             }
274             else {
275                 this.barWidth = (paxis._offsets.min - paxis._offsets.max) / nvals;
276             }
277         }
278     };
279     
280     $.jqplot.PyramidRenderer.prototype.draw = function(ctx, gridData, options) {
281         var i;
282         // Ughhh, have to make a copy of options b/c it may be modified later.
283         var opts = $.extend({}, options);
284         var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
285         var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
286         var fill = (opts.fill != undefined) ? opts.fill : this.fill;
287         var xp = this._xaxis.series_u2p;
288         var yp = this._yaxis.series_u2p;
289         var pointx, pointy;
290         // clear out data colors.
291         this._dataColors = [];
292         this._barPoints = [];
293         
294         if (this.renderer.options.barWidth == null) {
295             this.renderer.setBarWidth.call(this);
296         }
297         
298         // var temp = this._plotSeriesInfo = this.renderer.calcSeriesNumbers.call(this);
299         // var nvals = temp[0];
300         // var nseries = temp[1];
301         // var pos = temp[2];
302         var points = [],
303             w,
304             h;
305         
306         // this._barNudge = 0;
307
308         if (showLine) {
309             var negativeColors = new $.jqplot.ColorGenerator(this.negativeSeriesColors);
310             var positiveColors = new $.jqplot.ColorGenerator(this.seriesColors);
311             var negativeColor = negativeColors.get(this.index);
312             if (! this.useNegativeColors) {
313                 negativeColor = opts.fillStyle;
314             }
315             var positiveColor = opts.fillStyle;
316             var base;
317             var xstart = this._xaxis.series_u2p(this._xnudge);
318             var ystart = this._yaxis.series_u2p(this._yaxis.min);
319             var yend = this._yaxis.series_u2p(this._yaxis.max);
320             var bw = this.barWidth;
321             var bw2 = bw/2.0;
322             var points = [];
323             var yadj = this.offsetBars ? bw2 : 0;
324             
325             for (var i=0, l=gridData.length; i<l; i++) {
326                 if (this.data[i][0] == null) {
327                     continue;
328                 }
329                 base = gridData[i][1];
330                 // not stacked and first series in stack
331
332                 if (this._plotData[i][1] < 0) {
333                     if (this.varyBarColor && !this._stack) {
334                         if (this.useNegativeColors) {
335                             opts.fillStyle = negativeColors.next();
336                         }
337                         else {
338                             opts.fillStyle = positiveColors.next();
339                         }
340                     }
341                 }
342                 else {
343                     if (this.varyBarColor && !this._stack) {
344                         opts.fillStyle = positiveColors.next();
345                     }
346                     else {
347                         opts.fillStyle = positiveColor;
348                     }                    
349                 }
350                 
351                 if (this.fill) {
352
353                     if (this._plotData[i][1] >= 0) {
354                         // xstart = this._xaxis.series_u2p(this._xnudge);
355                         w = gridData[i][0] - xstart;
356                         h = this.barWidth;
357                         points = [xstart, base - bw2 - yadj, w, h];
358                     }
359                     else {
360                         // xstart = this._xaxis.series_u2p(0);
361                         w = xstart - gridData[i][0];
362                         h = this.barWidth;
363                         points = [gridData[i][0], base - bw2 - yadj, w, h];
364                     }
365
366                     this._barPoints.push([[points[0], points[1] + h], [points[0], points[1]], [points[0] + w, points[1]], [points[0] + w, points[1] + h]]);
367
368                     if (shadow) {
369                         this.renderer.shadowRenderer.draw(ctx, points);
370                     }
371                     var clr = opts.fillStyle || this.color;
372                     this._dataColors.push(clr);
373                     this.renderer.shapeRenderer.draw(ctx, points, opts); 
374                 }
375
376                 else {
377                     if (i === 0) {
378                         points =[[xstart, ystart], [gridData[i][0], ystart], [gridData[i][0], gridData[i][1] - bw2 - yadj]];
379                     }
380
381                     else if (i < l-1) {
382                         points = points.concat([[gridData[i-1][0], gridData[i-1][1] - bw2 - yadj], [gridData[i][0], gridData[i][1] + bw2 - yadj], [gridData[i][0], gridData[i][1] - bw2 - yadj]]);
383                     } 
384
385                     // finally, draw the line
386                     else {
387                         points = points.concat([[gridData[i-1][0], gridData[i-1][1] - bw2 - yadj], [gridData[i][0], gridData[i][1] + bw2 - yadj], [gridData[i][0], yend], [xstart, yend]]);
388                     
389                         if (shadow) {
390                             this.renderer.shadowRenderer.draw(ctx, points);
391                         }
392                         var clr = opts.fillStyle || this.color;
393                         this._dataColors.push(clr);
394                         this.renderer.shapeRenderer.draw(ctx, points, opts);
395                     }
396                 }
397             }  
398         }        
399         
400         if (this.highlightColors.length == 0) {
401             this.highlightColors = $.jqplot.computeHighlightColors(this._dataColors);
402         }
403         
404         else if (typeof(this.highlightColors) == 'string') {
405             this.highlightColors = [];
406             for (var i=0; i<this._dataColors.length; i++) {
407                 this.highlightColors.push(this.highlightColors);
408             }
409         }
410         
411     };
412
413         
414     // setup default renderers for axes and legend so user doesn't have to
415     // called with scope of plot
416     function preInit(target, data, options) {
417         options = options || {};
418         options.axesDefaults = options.axesDefaults || {};
419         options.grid = options.grid || {};
420         options.legend = options.legend || {};
421         options.seriesDefaults = options.seriesDefaults || {};
422         // only set these if there is a pie series
423         var setopts = false;
424         if (options.seriesDefaults.renderer === $.jqplot.PyramidRenderer) {
425             setopts = true;
426         }
427         else if (options.series) {
428             for (var i=0; i < options.series.length; i++) {
429                 if (options.series[i].renderer === $.jqplot.PyramidRenderer) {
430                     setopts = true;
431                 }
432             }
433         }
434         
435         if (setopts) {
436             options.axesDefaults.renderer = $.jqplot.PyramidAxisRenderer;
437             options.grid.renderer = $.jqplot.PyramidGridRenderer;
438             options.seriesDefaults.pointLabels = {show: false};
439         }
440     }
441     
442     // called within context of plot
443     // create a canvas which we can draw on.
444     // insert it before the eventCanvas, so eventCanvas will still capture events.
445     function postPlotDraw() {
446         // Memory Leaks patch    
447         if (this.plugins.pyramidRenderer && this.plugins.pyramidRenderer.highlightCanvas) {
448
449             this.plugins.pyramidRenderer.highlightCanvas.resetCanvas();
450             this.plugins.pyramidRenderer.highlightCanvas = null;
451         }
452          
453         this.plugins.pyramidRenderer = {highlightedSeriesIndex:null};
454         this.plugins.pyramidRenderer.highlightCanvas = new $.jqplot.GenericCanvas();
455         
456         this.eventCanvas._elem.before(this.plugins.pyramidRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-pyramidRenderer-highlight-canvas', this._plotDimensions, this));
457         this.plugins.pyramidRenderer.highlightCanvas.setContext();
458         this.eventCanvas._elem.bind('mouseleave', {plot:this}, function (ev) { unhighlight(ev.data.plot); });
459     }  
460     
461     function highlight (plot, sidx, pidx, points) {
462         var s = plot.series[sidx];
463         var canvas = plot.plugins.pyramidRenderer.highlightCanvas;
464         canvas._ctx.clearRect(0,0,canvas._ctx.canvas.width, canvas._ctx.canvas.height);
465         s._highlightedPoint = pidx;
466         plot.plugins.pyramidRenderer.highlightedSeriesIndex = sidx;
467         var opts = {fillStyle: s.highlightColors[pidx], fillRect: false};
468         s.renderer.shapeRenderer.draw(canvas._ctx, points, opts);
469         if (s.synchronizeHighlight !== false && plot.series.length >= s.synchronizeHighlight && s.synchronizeHighlight !== sidx) {
470             s = plot.series[s.synchronizeHighlight];
471             opts = {fillStyle: s.highlightColors[pidx], fillRect: false};
472             s.renderer.shapeRenderer.draw(canvas._ctx, s._barPoints[pidx], opts);
473         }
474         canvas = null;
475     }
476     
477     function unhighlight (plot) {
478         var canvas = plot.plugins.pyramidRenderer.highlightCanvas;
479         canvas._ctx.clearRect(0,0, canvas._ctx.canvas.width, canvas._ctx.canvas.height);
480         for (var i=0; i<plot.series.length; i++) {
481             plot.series[i]._highlightedPoint = null;
482         }
483         plot.plugins.pyramidRenderer.highlightedSeriesIndex = null;
484         plot.target.trigger('jqplotDataUnhighlight');
485         canvas =  null;
486     }
487     
488     
489     function handleMove(ev, gridpos, datapos, neighbor, plot) {
490         if (neighbor) {
491             var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
492             var evt1 = jQuery.Event('jqplotDataMouseOver');
493             evt1.pageX = ev.pageX;
494             evt1.pageY = ev.pageY;
495             plot.target.trigger(evt1, ins);
496             if (plot.series[ins[0]].highlightMouseOver && !(ins[0] == plot.plugins.pyramidRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
497                 var evt = jQuery.Event('jqplotDataHighlight');
498                 evt.which = ev.which;
499                 evt.pageX = ev.pageX;
500                 evt.pageY = ev.pageY;
501                 plot.target.trigger(evt, ins);
502                 highlight (plot, neighbor.seriesIndex, neighbor.pointIndex, neighbor.points);
503             }
504         }
505         else if (neighbor == null) {
506             unhighlight (plot);
507         }
508     }
509
510     // Have to add hook here, becuase it needs called before series is inited.
511     $.jqplot.preInitHooks.push(preInit);
512     
513
514 })(jQuery);