Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/packages/jqPlot.1.0.0/content/Scripts/jqPlot/plugins/jqplot.pyramidRenderer.js @ 9617

Last change on this file since 9617 was 9617, checked in by pfleck, 11 years ago

#2063:
Started integrating Hive statistics into statistics web project.
Added jqPlot library for charting.

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