OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / jquery / jqplot1.0 / plugins / jqplot.donutRenderer.js
1 /**
2  * jqPlot
3  * Pure JavaScript plotting plugin using jQuery
4  *
5  * Version: 1.0.4
6  * Revision: 1120
7  *
8  * Copyright (c) 2009-2012 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      * Class: $.jqplot.DonutRenderer
34      * Plugin renderer to draw a donut chart.
35      * x values, if present, will be used as slice labels.
36      * y values give slice size.
37      * 
38      * To use this renderer, you need to include the 
39      * donut renderer plugin, for example:
40      * 
41      * > <script type="text/javascript" src="plugins/jqplot.donutRenderer.js"></script>
42      * 
43      * Properties described here are passed into the $.jqplot function
44      * as options on the series renderer.  For example:
45      * 
46      * > plot2 = $.jqplot('chart2', [s1, s2], {
47      * >     seriesDefaults: {
48      * >         renderer:$.jqplot.DonutRenderer,
49      * >         rendererOptions:{
50      * >              sliceMargin: 2,
51      * >              innerDiameter: 110,
52      * >              startAngle: -90
53      * >          }
54      * >      }
55      * > });
56      * 
57      * A donut plot will trigger events on the plot target
58      * according to user interaction.  All events return the event object,
59      * the series index, the point (slice) index, and the point data for 
60      * the appropriate slice.
61      * 
62      * 'jqplotDataMouseOver' - triggered when user mouseing over a slice.
63      * 'jqplotDataHighlight' - triggered the first time user mouses over a slice,
64      * if highlighting is enabled.
65      * 'jqplotDataUnhighlight' - triggered when a user moves the mouse out of
66      * a highlighted slice.
67      * 'jqplotDataClick' - triggered when the user clicks on a slice.
68      * 'jqplotDataRightClick' - tiggered when the user right clicks on a slice if
69      * the "captureRightClick" option is set to true on the plot.
70      */
71     $.jqplot.DonutRenderer = function(){
72         $.jqplot.LineRenderer.call(this);
73     };
74     
75     $.jqplot.DonutRenderer.prototype = new $.jqplot.LineRenderer();
76     $.jqplot.DonutRenderer.prototype.constructor = $.jqplot.DonutRenderer;
77     
78     // called with scope of a series
79     $.jqplot.DonutRenderer.prototype.init = function(options, plot) {
80         // Group: Properties
81         //
82         // prop: diameter
83         // Outer diameter of the donut, auto computed by default
84         this.diameter = null;
85         // prop: innerDiameter
86         // Inner diameter of the donut, auto calculated by default.
87         // If specified will override thickness value.
88         this.innerDiameter = null;
89         // prop: thickness
90         // thickness of the donut, auto computed by default
91         // Overridden by if innerDiameter is specified.
92         this.thickness = null;
93         // prop: padding
94         // padding between the donut and plot edges, legend, etc.
95         this.padding = 20;
96         // prop: sliceMargin
97         // angular spacing between donut slices in degrees.
98         this.sliceMargin = 0;
99         // prop: ringMargin
100         // pixel distance between rings, or multiple series in a donut plot.
101         // null will compute ringMargin based on sliceMargin.
102         this.ringMargin = null;
103         // prop: fill
104         // true or false, wether to fil the slices.
105         this.fill = true;
106         // prop: shadowOffset
107         // offset of the shadow from the slice and offset of 
108         // each succesive stroke of the shadow from the last.
109         this.shadowOffset = 2;
110         // prop: shadowAlpha
111         // transparency of the shadow (0 = transparent, 1 = opaque)
112         this.shadowAlpha = 0.07;
113         // prop: shadowDepth
114         // number of strokes to apply to the shadow, 
115         // each stroke offset shadowOffset from the last.
116         this.shadowDepth = 5;
117         // prop: highlightMouseOver
118         // True to highlight slice when moused over.
119         // This must be false to enable highlightMouseDown to highlight when clicking on a slice.
120         this.highlightMouseOver = true;
121         // prop: highlightMouseDown
122         // True to highlight when a mouse button is pressed over a slice.
123         // This will be disabled if highlightMouseOver is true.
124         this.highlightMouseDown = false;
125         // prop: highlightColors
126         // an array of colors to use when highlighting a slice.
127         this.highlightColors = [];
128         // prop: dataLabels
129         // Either 'label', 'value', 'percent' or an array of labels to place on the pie slices.
130         // Defaults to percentage of each pie slice.
131         this.dataLabels = 'percent';
132         // prop: showDataLabels
133         // true to show data labels on slices.
134         this.showDataLabels = false;
135         // prop: dataLabelFormatString
136         // Format string for data labels.  If none, '%s' is used for "label" and for arrays, '%d' for value and '%d%%' for percentage.
137         this.dataLabelFormatString = null;
138         // prop: dataLabelThreshold
139         // Threshhold in percentage (0 - 100) of pie area, below which no label will be displayed.
140         // This applies to all label types, not just to percentage labels.
141         this.dataLabelThreshold = 3;
142         // prop: dataLabelPositionFactor
143         // A Multiplier (0-1) of the pie radius which controls position of label on slice.
144         // Increasing will slide label toward edge of pie, decreasing will slide label toward center of pie.
145         this.dataLabelPositionFactor = 0.4;
146         // prop: dataLabelNudge
147         // Number of pixels to slide the label away from (+) or toward (-) the center of the pie.
148         this.dataLabelNudge = 0;
149         // prop: startAngle
150         // Angle to start drawing donut in degrees.  
151         // According to orientation of canvas coordinate system:
152         // 0 = on the positive x axis
153         // -90 = on the positive y axis.
154         // 90 = on the negaive y axis.
155         // 180 or - 180 = on the negative x axis.
156         this.startAngle = 0;
157         this.tickRenderer = $.jqplot.DonutTickRenderer;
158         // Used as check for conditions where donut shouldn't be drawn.
159         this._drawData = true;
160         this._type = 'donut';
161         
162         // if user has passed in highlightMouseDown option and not set highlightMouseOver, disable highlightMouseOver
163         if (options.highlightMouseDown && options.highlightMouseOver == null) {
164             options.highlightMouseOver = false;
165         }
166         
167         $.extend(true, this, options);
168         if (this.diameter != null) {
169             this.diameter = this.diameter - this.sliceMargin;
170         }
171         this._diameter = null;
172         this._innerDiameter = null;
173         this._radius = null;
174         this._innerRadius = null;
175         this._thickness = null;
176         // references to the previous series in the plot to properly calculate diameters
177         // and thicknesses of nested rings.
178         this._previousSeries = [];
179         this._numberSeries = 1;
180         // array of [start,end] angles arrays, one for each slice.  In radians.
181         this._sliceAngles = [];
182         // index of the currenty highlighted point, if any
183         this._highlightedPoint = null;
184         
185         // set highlight colors if none provided
186         if (this.highlightColors.length == 0) {
187             for (var i=0; i<this.seriesColors.length; i++){
188                 var rgba = $.jqplot.getColorComponents(this.seriesColors[i]);
189                 var newrgb = [rgba[0], rgba[1], rgba[2]];
190                 var sum = newrgb[0] + newrgb[1] + newrgb[2];
191                 for (var j=0; j<3; j++) {
192                     // when darkening, lowest color component can be is 60.
193                     newrgb[j] = (sum > 570) ?  newrgb[j] * 0.8 : newrgb[j] + 0.3 * (255 - newrgb[j]);
194                     newrgb[j] = parseInt(newrgb[j], 10);
195                 }
196                 this.highlightColors.push('rgb('+newrgb[0]+','+newrgb[1]+','+newrgb[2]+')');
197             }
198         }
199         
200         plot.postParseOptionsHooks.addOnce(postParseOptions);
201         plot.postInitHooks.addOnce(postInit);
202         plot.eventListenerHooks.addOnce('jqplotMouseMove', handleMove);
203         plot.eventListenerHooks.addOnce('jqplotMouseDown', handleMouseDown);
204         plot.eventListenerHooks.addOnce('jqplotMouseUp', handleMouseUp);
205         plot.eventListenerHooks.addOnce('jqplotClick', handleClick);
206         plot.eventListenerHooks.addOnce('jqplotRightClick', handleRightClick);
207         plot.postDrawHooks.addOnce(postPlotDraw);
208         
209         
210     };
211     
212     $.jqplot.DonutRenderer.prototype.setGridData = function(plot) {
213         // set gridData property.  This will hold angle in radians of each data point.
214         var stack = [];
215         var td = [];
216         var sa = this.startAngle/180*Math.PI;
217         var tot = 0;
218         // don't know if we have any valid data yet, so set plot to not draw.
219         this._drawData = false;
220         for (var i=0; i<this.data.length; i++){
221             if (this.data[i][1] != 0) {
222                 // we have data, O.K. to draw.
223                 this._drawData = true;
224             }
225             stack.push(this.data[i][1]);
226             td.push([this.data[i][0]]);
227             if (i>0) {
228                 stack[i] += stack[i-1];
229             }
230             tot += this.data[i][1];
231         }
232         var fact = Math.PI*2/stack[stack.length - 1];
233         
234         for (var i=0; i<stack.length; i++) {
235             td[i][1] = stack[i] * fact;
236             td[i][2] = this.data[i][1]/tot;
237         }
238         this.gridData = td;
239     };
240     
241     $.jqplot.DonutRenderer.prototype.makeGridData = function(data, plot) {
242         var stack = [];
243         var td = [];
244         var tot = 0;
245         var sa = this.startAngle/180*Math.PI;
246         // don't know if we have any valid data yet, so set plot to not draw.
247         this._drawData = false;
248         for (var i=0; i<data.length; i++){
249             if (this.data[i][1] != 0) {
250                 // we have data, O.K. to draw.
251                 this._drawData = true;
252             }
253             stack.push(data[i][1]);
254             td.push([data[i][0]]);
255             if (i>0) {
256                 stack[i] += stack[i-1];
257             }
258             tot += data[i][1];
259         }
260         var fact = Math.PI*2/stack[stack.length - 1];
261         
262         for (var i=0; i<stack.length; i++) {
263             td[i][1] = stack[i] * fact;
264             td[i][2] = data[i][1]/tot;
265         }
266         return td;
267     };
268     
269     $.jqplot.DonutRenderer.prototype.drawSlice = function (ctx, ang1, ang2, color, isShadow) {
270         var r = this._diameter / 2;
271         var ri = r - this._thickness;
272         var fill = this.fill;
273         // var lineWidth = this.lineWidth;
274         ctx.save();
275         ctx.translate(this._center[0], this._center[1]);
276         // ctx.translate(this.sliceMargin*Math.cos((ang1+ang2)/2), this.sliceMargin*Math.sin((ang1+ang2)/2));
277         
278         if (isShadow) {
279             for (var i=0; i<this.shadowDepth; i++) {
280                 ctx.save();
281                 ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI));
282                 doDraw();
283             }
284         }
285         
286         else {
287             doDraw();
288         }
289         
290         function doDraw () {
291             // Fix for IE and Chrome that can't seem to draw circles correctly.
292             // ang2 should always be <= 2 pi since that is the way the data is converted.
293              if (ang2 > 6.282 + this.startAngle) {
294                 ang2 = 6.282 + this.startAngle;
295                 if (ang1 > ang2) {
296                     ang1 = 6.281 + this.startAngle;
297                 }
298             }
299             // Fix for IE, where it can't seem to handle 0 degree angles.  Also avoids
300             // ugly line on unfilled donuts.
301             if (ang1 >= ang2) {
302                 return;
303             }
304             ctx.beginPath();  
305             ctx.fillStyle = color;
306             ctx.strokeStyle = color;
307             // ctx.lineWidth = lineWidth;
308             ctx.arc(0, 0, r, ang1, ang2, false);
309             ctx.lineTo(ri*Math.cos(ang2), ri*Math.sin(ang2));
310             ctx.arc(0,0, ri, ang2, ang1, true);
311             ctx.closePath();
312             if (fill) {
313                 ctx.fill();
314             }
315             else {
316                 ctx.stroke();
317             }
318         }
319         
320         if (isShadow) {
321             for (var i=0; i<this.shadowDepth; i++) {
322                 ctx.restore();
323             }
324         }
325         
326         ctx.restore();
327     };
328     
329     // called with scope of series
330     $.jqplot.DonutRenderer.prototype.draw = function (ctx, gd, options, plot) {
331         var i;
332         var opts = (options != undefined) ? options : {};
333         // offset and direction of offset due to legend placement
334         var offx = 0;
335         var offy = 0;
336         var trans = 1;
337         // var colorGenerator = new this.colorGenerator(this.seriesColors);
338         if (options.legendInfo && options.legendInfo.placement == 'insideGrid') {
339             var li = options.legendInfo;
340             switch (li.location) {
341                 case 'nw':
342                     offx = li.width + li.xoffset;
343                     break;
344                 case 'w':
345                     offx = li.width + li.xoffset;
346                     break;
347                 case 'sw':
348                     offx = li.width + li.xoffset;
349                     break;
350                 case 'ne':
351                     offx = li.width + li.xoffset;
352                     trans = -1;
353                     break;
354                 case 'e':
355                     offx = li.width + li.xoffset;
356                     trans = -1;
357                     break;
358                 case 'se':
359                     offx = li.width + li.xoffset;
360                     trans = -1;
361                     break;
362                 case 'n':
363                     offy = li.height + li.yoffset;
364                     break;
365                 case 's':
366                     offy = li.height + li.yoffset;
367                     trans = -1;
368                     break;
369                 default:
370                     break;
371             }
372         }
373         
374         var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
375         var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
376         var fill = (opts.fill != undefined) ? opts.fill : this.fill;
377         var cw = ctx.canvas.width;
378         var ch = ctx.canvas.height;
379         var w = cw - offx - 2 * this.padding;
380         var h = ch - offy - 2 * this.padding;
381         var mindim = Math.min(w,h);
382         var d = mindim;
383         var ringmargin =  (this.ringMargin == null) ? this.sliceMargin * 2.0 : this.ringMargin;
384         
385         for (var i=0; i<this._previousSeries.length; i++) {
386             d -= 2.0 * this._previousSeries[i]._thickness + 2.0 * ringmargin;
387         }
388         this._diameter = this.diameter || d;
389         if (this.innerDiameter != null) {
390             var od = (this._numberSeries > 1 && this.index > 0) ? this._previousSeries[0]._diameter : this._diameter;
391             this._thickness = this.thickness || (od - this.innerDiameter - 2.0*ringmargin*this._numberSeries) / this._numberSeries/2.0;
392         }
393         else {
394             this._thickness = this.thickness || mindim / 2 / (this._numberSeries + 1) * 0.85;
395         }
396
397         var r = this._radius = this._diameter/2;
398         this._innerRadius = this._radius - this._thickness;
399         var sa = this.startAngle / 180 * Math.PI;
400         this._center = [(cw - trans * offx)/2 + trans * offx, (ch - trans*offy)/2 + trans * offy];
401         
402         if (this.shadow) {
403             var shadowColor = 'rgba(0,0,0,'+this.shadowAlpha+')';
404             for (var i=0; i<gd.length; i++) {
405                 var ang1 = (i == 0) ? sa : gd[i-1][1] + sa;
406                 // Adjust ang1 and ang2 for sliceMargin
407                 ang1 += this.sliceMargin/180*Math.PI;
408                 this.renderer.drawSlice.call (this, ctx, ang1, gd[i][1]+sa, shadowColor, true);
409             }
410             
411         }
412         for (var i=0; i<gd.length; i++) {
413             var ang1 = (i == 0) ? sa : gd[i-1][1] + sa;
414             // Adjust ang1 and ang2 for sliceMargin
415             ang1 += this.sliceMargin/180*Math.PI;
416             var ang2 = gd[i][1] + sa;
417             this._sliceAngles.push([ang1, ang2]);
418             this.renderer.drawSlice.call (this, ctx, ang1, ang2, this.seriesColors[i], false);
419             
420             if (this.showDataLabels && gd[i][2]*100 >= this.dataLabelThreshold) {
421                 var fstr, avgang = (ang1+ang2)/2, label;
422                 
423                 if (this.dataLabels == 'label') {
424                     fstr = this.dataLabelFormatString || '%s';
425                     label = $.jqplot.sprintf(fstr, gd[i][0]);
426                 }
427                 else if (this.dataLabels == 'value') {
428                     fstr = this.dataLabelFormatString || '%d';
429                     label = $.jqplot.sprintf(fstr, this.data[i][1]);
430                 }
431                 else if (this.dataLabels == 'percent') {
432                     fstr = this.dataLabelFormatString || '%d%%';
433                     label = $.jqplot.sprintf(fstr, gd[i][2]*100);
434                 }
435                 else if (this.dataLabels.constructor == Array) {
436                     fstr = this.dataLabelFormatString || '%s';
437                     label = $.jqplot.sprintf(fstr, this.dataLabels[i]);
438                 }
439                 
440                 var fact = this._innerRadius + this._thickness * this.dataLabelPositionFactor + this.sliceMargin + this.dataLabelNudge;
441                 
442                 var x = this._center[0] + Math.cos(avgang) * fact + this.canvas._offsets.left;
443                 var y = this._center[1] + Math.sin(avgang) * fact + this.canvas._offsets.top;
444                 
445                 var labelelem = $('<span class="jqplot-donut-series jqplot-data-label" style="position:absolute;">' + label + '</span>').insertBefore(plot.eventCanvas._elem);
446                 x -= labelelem.width()/2;
447                 y -= labelelem.height()/2;
448                 x = Math.round(x);
449                 y = Math.round(y);
450                 labelelem.css({left: x, top: y});
451             }
452         }
453                
454     };
455     
456     $.jqplot.DonutAxisRenderer = function() {
457         $.jqplot.LinearAxisRenderer.call(this);
458     };
459     
460     $.jqplot.DonutAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
461     $.jqplot.DonutAxisRenderer.prototype.constructor = $.jqplot.DonutAxisRenderer;
462         
463     
464     // There are no traditional axes on a donut chart.  We just need to provide
465     // dummy objects with properties so the plot will render.
466     // called with scope of axis object.
467     $.jqplot.DonutAxisRenderer.prototype.init = function(options){
468         //
469         this.tickRenderer = $.jqplot.DonutTickRenderer;
470         $.extend(true, this, options);
471         // I don't think I'm going to need _dataBounds here.
472         // have to go Axis scaling in a way to fit chart onto plot area
473         // and provide u2p and p2u functionality for mouse cursor, etc.
474         // for convienence set _dataBounds to 0 and 100 and
475         // set min/max to 0 and 100.
476         this._dataBounds = {min:0, max:100};
477         this.min = 0;
478         this.max = 100;
479         this.showTicks = false;
480         this.ticks = [];
481         this.showMark = false;
482         this.show = false; 
483     };
484     
485     
486     
487     
488     $.jqplot.DonutLegendRenderer = function(){
489         $.jqplot.TableLegendRenderer.call(this);
490     };
491     
492     $.jqplot.DonutLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
493     $.jqplot.DonutLegendRenderer.prototype.constructor = $.jqplot.DonutLegendRenderer;
494     
495     /**
496      * Class: $.jqplot.DonutLegendRenderer
497      * Legend Renderer specific to donut plots.  Set by default
498      * when user creates a donut plot.
499      */
500     $.jqplot.DonutLegendRenderer.prototype.init = function(options) {
501         // Group: Properties
502         //
503         // prop: numberRows
504         // Maximum number of rows in the legend.  0 or null for unlimited.
505         this.numberRows = null;
506         // prop: numberColumns
507         // Maximum number of columns in the legend.  0 or null for unlimited.
508         this.numberColumns = null;
509         $.extend(true, this, options);
510     };
511     
512     // called with context of legend
513     $.jqplot.DonutLegendRenderer.prototype.draw = function() {
514         var legend = this;
515         if (this.show) {
516             var series = this._series;
517             var ss = 'position:absolute;';
518             ss += (this.background) ? 'background:'+this.background+';' : '';
519             ss += (this.border) ? 'border:'+this.border+';' : '';
520             ss += (this.fontSize) ? 'font-size:'+this.fontSize+';' : '';
521             ss += (this.fontFamily) ? 'font-family:'+this.fontFamily+';' : '';
522             ss += (this.textColor) ? 'color:'+this.textColor+';' : '';
523             ss += (this.marginTop != null) ? 'margin-top:'+this.marginTop+';' : '';
524             ss += (this.marginBottom != null) ? 'margin-bottom:'+this.marginBottom+';' : '';
525             ss += (this.marginLeft != null) ? 'margin-left:'+this.marginLeft+';' : '';
526             ss += (this.marginRight != null) ? 'margin-right:'+this.marginRight+';' : '';
527             this._elem = $('<table class="jqplot-table-legend" style="'+ss+'"></table>');
528             // Donut charts legends don't go by number of series, but by number of data points
529             // in the series.  Refactor things here for that.
530             
531             var pad = false, 
532                 reverse = false,
533                 nr, nc;
534             var s = series[0];
535             var colorGenerator = new $.jqplot.ColorGenerator(s.seriesColors);
536             
537             if (s.show) {
538                 var pd = s.data;
539                 if (this.numberRows) {
540                     nr = this.numberRows;
541                     if (!this.numberColumns){
542                         nc = Math.ceil(pd.length/nr);
543                     }
544                     else{
545                         nc = this.numberColumns;
546                     }
547                 }
548                 else if (this.numberColumns) {
549                     nc = this.numberColumns;
550                     nr = Math.ceil(pd.length/this.numberColumns);
551                 }
552                 else {
553                     nr = pd.length;
554                     nc = 1;
555                 }
556                 
557                 var i, j, tr, td1, td2, lt, rs, color;
558                 var idx = 0;    
559                 
560                 for (i=0; i<nr; i++) {
561                     if (reverse){
562                         tr = $('<tr class="jqplot-table-legend"></tr>').prependTo(this._elem);
563                     }
564                     else{
565                         tr = $('<tr class="jqplot-table-legend"></tr>').appendTo(this._elem);
566                     }
567                     for (j=0; j<nc; j++) {
568                         if (idx < pd.length){
569                             lt = this.labels[idx] || pd[idx][0].toString();
570                             color = colorGenerator.next();
571                             if (!reverse){
572                                 if (i>0){
573                                     pad = true;
574                                 }
575                                 else{
576                                     pad = false;
577                                 }
578                             }
579                             else{
580                                 if (i == nr -1){
581                                     pad = false;
582                                 }
583                                 else{
584                                     pad = true;
585                                 }
586                             }
587                             rs = (pad) ? this.rowSpacing : '0';
588                 
589                             td1 = $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+
590                                 '<div><div class="jqplot-table-legend-swatch" style="border-color:'+color+';"></div>'+
591                                 '</div></td>');
592                             td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
593                             if (this.escapeHtml){
594                                 td2.text(lt);
595                             }
596                             else {
597                                 td2.html(lt);
598                             }
599                             if (reverse) {
600                                 td2.prependTo(tr);
601                                 td1.prependTo(tr);
602                             }
603                             else {
604                                 td1.appendTo(tr);
605                                 td2.appendTo(tr);
606                             }
607                             pad = true;
608                         }
609                         idx++;
610                     }   
611                 }
612             }
613         }
614         return this._elem;                
615     };
616     
617     // setup default renderers for axes and legend so user doesn't have to
618     // called with scope of plot
619     function preInit(target, data, options) {
620         options = options || {};
621         options.axesDefaults = options.axesDefaults || {};
622         options.legend = options.legend || {};
623         options.seriesDefaults = options.seriesDefaults || {};
624         // only set these if there is a donut series
625         var setopts = false;
626         if (options.seriesDefaults.renderer == $.jqplot.DonutRenderer) {
627             setopts = true;
628         }
629         else if (options.series) {
630             for (var i=0; i < options.series.length; i++) {
631                 if (options.series[i].renderer == $.jqplot.DonutRenderer) {
632                     setopts = true;
633                 }
634             }
635         }
636         
637         if (setopts) {
638             options.axesDefaults.renderer = $.jqplot.DonutAxisRenderer;
639             options.legend.renderer = $.jqplot.DonutLegendRenderer;
640             options.legend.preDraw = true;
641             options.seriesDefaults.pointLabels = {show: false};
642         }
643     }
644     
645     // called with scope of plot.
646     function postInit(target, data, options) {
647         // if multiple series, add a reference to the previous one so that
648         // donut rings can nest.
649         for (var i=1; i<this.series.length; i++) {
650             if (!this.series[i]._previousSeries.length){
651                 for (var j=0; j<i; j++) {
652                     if (this.series[i].renderer.constructor == $.jqplot.DonutRenderer && this.series[j].renderer.constructor == $.jqplot.DonutRenderer) {
653                         this.series[i]._previousSeries.push(this.series[j]);
654                     }
655                 }
656             }
657         }
658         for (i=0; i<this.series.length; i++) {
659             if (this.series[i].renderer.constructor == $.jqplot.DonutRenderer) {
660                 this.series[i]._numberSeries = this.series.length;
661                 // don't allow mouseover and mousedown at same time.
662                 if (this.series[i].highlightMouseOver) {
663                     this.series[i].highlightMouseDown = false;
664                 }
665             }
666         }
667     }
668     
669     var postParseOptionsRun = false;
670     // called with scope of plot
671     function postParseOptions(options) {
672         for (var i=0; i<this.series.length; i++) {
673             this.series[i].seriesColors = this.seriesColors;
674             this.series[i].colorGenerator = $.jqplot.colorGenerator;
675         }
676     }
677     
678     function highlight (plot, sidx, pidx) {
679         var s = plot.series[sidx];
680         var canvas = plot.plugins.donutRenderer.highlightCanvas;
681         canvas._ctx.clearRect(0,0,canvas._ctx.canvas.width, canvas._ctx.canvas.height);
682         s._highlightedPoint = pidx;
683         plot.plugins.donutRenderer.highlightedSeriesIndex = sidx;
684         s.renderer.drawSlice.call(s, canvas._ctx, s._sliceAngles[pidx][0], s._sliceAngles[pidx][1], s.highlightColors[pidx], false);
685     }
686     
687     function unhighlight (plot) {
688         var canvas = plot.plugins.donutRenderer.highlightCanvas;
689         canvas._ctx.clearRect(0,0, canvas._ctx.canvas.width, canvas._ctx.canvas.height);
690         for (var i=0; i<plot.series.length; i++) {
691             plot.series[i]._highlightedPoint = null;
692         }
693         plot.plugins.donutRenderer.highlightedSeriesIndex = null;
694         plot.target.trigger('jqplotDataUnhighlight');
695     }
696  
697     function handleMove(ev, gridpos, datapos, neighbor, plot) {
698         if (neighbor) {
699             var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
700             var evt1 = jQuery.Event('jqplotDataMouseOver');
701             evt1.pageX = ev.pageX;
702             evt1.pageY = ev.pageY;
703             plot.target.trigger(evt1, ins);
704             if (plot.series[ins[0]].highlightMouseOver && !(ins[0] == plot.plugins.donutRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
705                 var evt = jQuery.Event('jqplotDataHighlight');
706                 evt.which = ev.which;
707                 evt.pageX = ev.pageX;
708                 evt.pageY = ev.pageY;
709                 plot.target.trigger(evt, ins);
710                 highlight (plot, ins[0], ins[1]);
711             }
712         }
713         else if (neighbor == null) {
714             unhighlight (plot);
715         }
716     } 
717     
718     function handleMouseDown(ev, gridpos, datapos, neighbor, plot) {
719         if (neighbor) {
720             var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
721             if (plot.series[ins[0]].highlightMouseDown && !(ins[0] == plot.plugins.donutRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
722                 var evt = jQuery.Event('jqplotDataHighlight');
723                 evt.which = ev.which;
724                 evt.pageX = ev.pageX;
725                 evt.pageY = ev.pageY;
726                 plot.target.trigger(evt, ins);
727                 highlight (plot, ins[0], ins[1]);
728             }
729         }
730         else if (neighbor == null) {
731             unhighlight (plot);
732         }
733     }
734     
735     function handleMouseUp(ev, gridpos, datapos, neighbor, plot) {
736         var idx = plot.plugins.donutRenderer.highlightedSeriesIndex;
737         if (idx != null && plot.series[idx].highlightMouseDown) {
738             unhighlight(plot);
739         }
740     }
741     
742     function handleClick(ev, gridpos, datapos, neighbor, plot) {
743         if (neighbor) {
744             var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
745             var evt = jQuery.Event('jqplotDataClick');
746             evt.which = ev.which;
747             evt.pageX = ev.pageX;
748             evt.pageY = ev.pageY;
749             plot.target.trigger(evt, ins);
750         }
751     }
752     
753     function handleRightClick(ev, gridpos, datapos, neighbor, plot) {
754         if (neighbor) {
755             var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
756             var idx = plot.plugins.donutRenderer.highlightedSeriesIndex;
757             if (idx != null && plot.series[idx].highlightMouseDown) {
758                 unhighlight(plot);
759             }
760             var evt = jQuery.Event('jqplotDataRightClick');
761             evt.which = ev.which;
762             evt.pageX = ev.pageX;
763             evt.pageY = ev.pageY;
764             plot.target.trigger(evt, ins);
765         }
766     }    
767     
768     // called within context of plot
769     // create a canvas which we can draw on.
770     // insert it before the eventCanvas, so eventCanvas will still capture events.
771     function postPlotDraw() {
772         // Memory Leaks patch    
773         if (this.plugins.donutRenderer && this.plugins.donutRenderer.highlightCanvas) {
774             this.plugins.donutRenderer.highlightCanvas.resetCanvas();
775             this.plugins.donutRenderer.highlightCanvas = null;
776         }
777
778         this.plugins.donutRenderer = {highlightedSeriesIndex:null};
779         this.plugins.donutRenderer.highlightCanvas = new $.jqplot.GenericCanvas();
780         // do we have any data labels?  if so, put highlight canvas before those
781         // Fix for broken jquery :first selector with canvas (VML) elements.
782         var labels = $(this.targetId+' .jqplot-data-label');
783         if (labels.length) {
784             $(labels[0]).before(this.plugins.donutRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-donutRenderer-highlight-canvas', this._plotDimensions, this));
785         }
786         // else put highlight canvas before event canvas.
787         else {
788             this.eventCanvas._elem.before(this.plugins.donutRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-donutRenderer-highlight-canvas', this._plotDimensions, this));
789         }
790         var hctx = this.plugins.donutRenderer.highlightCanvas.setContext();
791         this.eventCanvas._elem.bind('mouseleave', {plot:this}, function (ev) { unhighlight(ev.data.plot); });
792     }
793     
794     $.jqplot.preInitHooks.push(preInit);
795     
796     $.jqplot.DonutTickRenderer = function() {
797         $.jqplot.AxisTickRenderer.call(this);
798     };
799     
800     $.jqplot.DonutTickRenderer.prototype = new $.jqplot.AxisTickRenderer();
801     $.jqplot.DonutTickRenderer.prototype.constructor = $.jqplot.DonutTickRenderer;
802     
803 })(jQuery);
804     
805