3 * Pure JavaScript plotting plugin using jQuery
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.
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 .
18 * If you are feeling kind and generous, consider supporting the project by
19 * making a donation at: http://www.jqplot.com/donate.php .
21 * sprintf functions contained in jqplot.sprintf.js by 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."
33 * Class: $.jqplot.MeterGaugeRenderer
34 * Plugin renderer to draw a meter gauge chart.
36 * Data consists of a single series with 1 data point to position the gauge needle.
38 * To use this renderer, you need to include the
39 * meter gauge renderer plugin, for example:
41 * > <script type="text/javascript" src="plugins/jqplot.meterGaugeRenderer.js"></script>
43 * Properties described here are passed into the $.jqplot function
44 * as options on the series renderer. For example:
46 * > plot0 = $.jqplot('chart0',[[18]],{
47 * > title: 'Network Speed',
49 * > renderer: $.jqplot.MeterGaugeRenderer,
50 * > rendererOptions: {
56 * A meterGauge plot does not support events.
58 $.jqplot.MeterGaugeRenderer = function(){
59 $.jqplot.LineRenderer.call(this);
62 $.jqplot.MeterGaugeRenderer.prototype = new $.jqplot.LineRenderer();
63 $.jqplot.MeterGaugeRenderer.prototype.constructor = $.jqplot.MeterGaugeRenderer;
65 // called with scope of a series
66 $.jqplot.MeterGaugeRenderer.prototype.init = function(options) {
70 // Outer diameter of the meterGauge, auto computed by default
73 // padding between the meterGauge and plot edges, auto
74 // calculated by default.
77 // offset of the shadow from the gauge ring and offset of
78 // each succesive stroke of the shadow from the last.
79 this.shadowOffset = 2;
81 // transparency of the shadow (0 = transparent, 1 = opaque)
82 this.shadowAlpha = 0.07;
84 // number of strokes to apply to the shadow,
85 // each stroke offset shadowOffset from the last.
88 // background color of the inside of the gauge.
89 this.background = "#efefef";
91 // color of the outer ring, hub, and needle of the gauge.
92 this.ringColor = "#BBC6D0";
93 // needle color not implemented yet.
94 this.needleColor = "#C3D3E5";
96 // color of the tick marks around the gauge.
97 this.tickColor = "989898";
99 // width of the ring around the gauge. Auto computed by default.
100 this.ringWidth = null;
102 // Minimum value on the gauge. Auto computed by default
105 // Maximum value on the gauge. Auto computed by default
108 // Array of tick values. Auto computed by default.
111 // true to show ticks around gauge.
112 this.showTicks = true;
113 // prop: showTickLabels
114 // true to show tick labels next to ticks.
115 this.showTickLabels = true;
117 // A gauge label like 'kph' or 'Volts'
119 // prop: labelHeightAdjust
120 // Number of Pixels to offset the label up (-) or down (+) from its default position.
121 this.labelHeightAdjust = 0;
122 // prop: labelPosition
123 // Where to position the label, either 'inside' or 'bottom'.
124 this.labelPosition = 'inside';
126 // Array of ranges to be drawn around the gauge.
128 // > [value1, value2, ...]
129 // indicating the values for the first, second, ... intervals.
131 // prop: intervalColors
132 // Array of colors to use for the intervals.
133 this.intervalColors = [ "#4bb2c5", "#EAA228", "#c5b47f", "#579575", "#839557", "#958c12", "#953579", "#4b5de4", "#d8b83f", "#ff5800", "#0085cc", "#c747a3", "#cddf54", "#FBD178", "#26B4E3", "#bd70c7"];
134 // prop: intervalInnerRadius
135 // Radius of the inner circle of the interval ring.
136 this.intervalInnerRadius = null;
137 // prop: intervalOuterRadius
138 // Radius of the outer circle of the interval ring.
139 this.intervalOuterRadius = null;
140 this.tickRenderer = $.jqplot.MeterGaugeTickRenderer;
141 // ticks spaced every 1, 2, 2.5, 5, 10, 20, .1, .2, .25, .5, etc.
142 this.tickPositions = [1, 2, 2.5, 5, 10];
144 // Degrees between ticks. This is a target number, if
145 // incompatible span and ticks are supplied, a suitable
146 // spacing close to this value will be computed.
147 this.tickSpacing = 30;
148 this.numberMinorTicks = null;
150 // Radius of the hub at the bottom center of gauge which the needle attaches to.
151 // Auto computed by default
152 this.hubRadius = null;
154 // padding of the tick marks to the outer ring and the tick labels to marks.
155 // Auto computed by default.
156 this.tickPadding = null;
157 // prop: needleThickness
158 // Maximum thickness the needle. Auto computed by default.
159 this.needleThickness = null;
161 // Padding between needle and inner edge of the ring when the needle is at the min or max gauge value.
164 // True will stop needle just below/above the min/max values if data is below/above min/max,
165 // as if the meter is "pegged".
166 this.pegNeedle = true;
167 this._type = 'meterGauge';
169 $.extend(true, this, options);
171 this.numberTicks = null;
172 this.tickInterval = null;
173 // span, the sweep (in degrees) from min to max. This gauge is
176 // get rid of this nonsense
177 // this.innerSpan = this.span;
178 if (this.type == 'circular') {
179 this.semiCircular = false;
181 else if (this.type != 'circular') {
182 this.semiCircular = true;
185 this.semiCircular = (this.span <= 180) ? true : false;
187 this._tickPoints = [];
188 // reference to label element.
189 this._labelElem = null;
191 // start the gauge at the beginning of the span
192 this.startAngle = (90 + (360 - this.span)/2) * Math.PI/180;
193 this.endAngle = (90 - (360 - this.span)/2) * Math.PI/180;
195 this.setmin = !!(this.min == null);
196 this.setmax = !!(this.max == null);
198 // if given intervals and is an array of values, create labels and colors.
199 if (this.intervals.length) {
200 if (this.intervals[0].length == null || this.intervals.length == 1) {
201 for (var i=0; i<this.intervals.length; i++) {
202 this.intervals[i] = [this.intervals[i], this.intervals[i], this.intervalColors[i]];
205 else if (this.intervals[0].length == 2) {
206 for (i=0; i<this.intervals.length; i++) {
207 this.intervals[i] = [this.intervals[i][0], this.intervals[i][1], this.intervalColors[i]];
212 // compute min, max and ticks if not supplied:
213 if (this.ticks.length) {
214 if (this.ticks[0].length == null || this.ticks[0].length == 1) {
215 for (var i=0; i<this.ticks.length; i++) {
216 this.ticks[i] = [this.ticks[i], this.ticks[i]];
219 this.min = (this.min == null) ? this.ticks[0][0] : this.min;
220 this.max = (this.max == null) ? this.ticks[this.ticks.length-1][0] : this.max;
223 this.numberTicks = this.ticks.length;
224 this.tickInterval = this.ticks[1][0] - this.ticks[0][0];
225 this.tickFactor = Math.floor(parseFloat((Math.log(this.tickInterval)/Math.log(10)).toFixed(11)));
226 // use the first interal to calculate minor ticks;
227 this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor);
228 if (!this.numberMinorTicks) {
229 this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1);
231 if (!this.numberMinorTicks) {
232 this.numberMinorTicks = 1;
236 else if (this.intervals.length) {
237 this.min = (this.min == null) ? 0 : this.min;
239 if (this.max == null) {
240 if (this.intervals[this.intervals.length-1][0] >= this.data[0][1]) {
241 this.max = this.intervals[this.intervals.length-1][0];
251 // no ticks and no intervals supplied, put needle in middle
252 this.min = (this.min == null) ? 0 : this.min;
254 if (this.max == null) {
255 this.max = this.data[0][1] * 1.25;
264 $.jqplot.MeterGaugeRenderer.prototype.setGridData = function(plot) {
265 // set gridData property. This will hold angle in radians of each data point.
268 var sa = this.startAngle;
269 for (var i=0; i<this.data.length; i++){
270 stack.push(this.data[i][1]);
271 td.push([this.data[i][0]]);
273 stack[i] += stack[i-1];
276 var fact = Math.PI*2/stack[stack.length - 1];
278 for (var i=0; i<stack.length; i++) {
279 td[i][1] = stack[i] * fact;
284 $.jqplot.MeterGaugeRenderer.prototype.makeGridData = function(data, plot) {
287 var sa = this.startAngle;
288 for (var i=0; i<data.length; i++){
289 stack.push(data[i][1]);
290 td.push([data[i][0]]);
292 stack[i] += stack[i-1];
295 var fact = Math.PI*2/stack[stack.length - 1];
297 for (var i=0; i<stack.length; i++) {
298 td[i][1] = stack[i] * fact;
304 function getnmt(pos, interval, fact) {
306 for (var i=pos.length-1; i>=0; i--) {
307 temp = interval/(pos[i] * Math.pow(10, fact));
308 if (temp == 4 || temp == 5) {
315 // called with scope of series
316 $.jqplot.MeterGaugeRenderer.prototype.draw = function (ctx, gd, options) {
318 var opts = (options != undefined) ? options : {};
319 // offset and direction of offset due to legend placement
323 if (options.legendInfo && options.legendInfo.placement == 'inside') {
324 var li = options.legendInfo;
325 switch (li.location) {
327 offx = li.width + li.xoffset;
330 offx = li.width + li.xoffset;
333 offx = li.width + li.xoffset;
336 offx = li.width + li.xoffset;
340 offx = li.width + li.xoffset;
344 offx = li.width + li.xoffset;
348 offy = li.height + li.yoffset;
351 offy = li.height + li.yoffset;
361 // pre-draw so can get it's dimensions.
363 this._labelElem = $('<div class="jqplot-meterGauge-label" style="position:absolute;">'+this.label+'</div>');
364 this.canvas._elem.after(this._labelElem);
367 var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
368 var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
369 var fill = (opts.fill != undefined) ? opts.fill : this.fill;
370 var cw = ctx.canvas.width;
371 var ch = ctx.canvas.height;
372 if (this.padding == null) {
373 this.padding = Math.round(Math.min(cw, ch)/30);
375 var w = cw - offx - 2 * this.padding;
376 var h = ch - offy - 2 * this.padding;
377 if (this.labelPosition == 'bottom' && this.label) {
378 h -= this._labelElem.outerHeight(true);
380 var mindim = Math.min(w,h);
383 if (!this.diameter) {
384 if (this.semiCircular) {
386 if (!this.ringWidth) {
387 this.ringWidth = 2*h/35;
389 this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
390 this.innerPad = this.ringWidth/2 + this.needleThickness/2 + this.needlePad;
391 this.diameter = 2 * (h - 2*this.innerPad);
394 if (!this.ringWidth) {
395 this.ringWidth = w/35;
397 this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
398 this.innerPad = this.ringWidth/2 + this.needleThickness/2 + this.needlePad;
399 this.diameter = w - 2*this.innerPad - this.ringWidth - this.padding;
401 // center taking into account legend and over draw for gauge bottom below hub.
402 // this will be center of hub.
403 this._center = [(cw - trans * offx)/2 + trans * offx, (ch + trans*offy - this.padding - this.ringWidth - this.innerPad)];
406 if (!this.ringWidth) {
407 this.ringWidth = d/35;
409 this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
411 this.diameter = d - this.ringWidth;
412 // center in middle of canvas taking into account legend.
413 // will be center of hub.
414 this._center = [(cw-trans*offx)/2 + trans * offx, (ch-trans*offy)/2 + trans * offy];
419 if (this._labelElem && this.labelPosition == 'bottom') {
420 this._center[1] -= this._labelElem.outerHeight(true);
423 this._radius = this.diameter/2;
425 this.tickSpacing = 6000/this.diameter;
427 if (!this.hubRadius) {
428 this.hubRadius = this.diameter/18;
431 this.shadowOffset = 0.5 + this.ringWidth/9;
432 this.shadowWidth = this.ringWidth*1;
434 this.tickPadding = 3 + Math.pow(this.diameter/20, 0.7);
435 this.tickOuterRadius = this._radius - this.ringWidth/2 - this.tickPadding;
436 this.tickLength = (this.showTicks) ? this._radius/13 : 0;
438 if (this.ticks.length == 0) {
439 // no ticks, lets make some.
442 setmax = this.setmax,
443 setmin = this.setmin,
444 ti = (max - min) * this.tickSpacing / this.span;
445 var tf = Math.floor(parseFloat((Math.log(ti)/Math.log(10)).toFixed(11)));
446 var tp = (ti/Math.pow(10, tf));
447 (tp > 2 && tp <= 2.5) ? tp = 2.5 : tp = Math.ceil(tp);
448 var t = this.tickPositions;
451 for (i=0; i<t.length; i++) {
452 if (tp == t[i] || i && t[i-1] < tp && tp < t[i]) {
453 ti = t[i]*Math.pow(10, tf);
458 for (i=0; i<t.length; i++) {
459 if (tp == t[i] || i && t[i-1] < tp && tp < t[i]) {
460 ti = t[i]*Math.pow(10, tf);
461 nt = Math.ceil((max - min) / ti);
465 // both max and min are free
466 if (setmax && setmin) {
467 var tmin = (min > 0) ? min - min % ti : min - min % ti - ti;
468 if (!this.forceZero) {
469 var diff = Math.min(min - tmin, 0.8*ti);
470 var ntp = Math.floor(diff/t[tpindex]);
472 tmin = tmin + t[tpindex] * (ntp-1);
473 if (parseInt(tmin, 10) != tmin && parseInt(tmin-t[tpindex], 10) == tmin-t[tpindex]) {
474 tmin = tmin - t[tpindex];
482 // tmin should always be lower than dataMin
483 if (min - tmin > 0.23*ti) {
492 var tmax = min + (nt - 1) * ti;
497 // now tmax should always be mroe than dataMax
498 if (tmax - max < 0.23*ti) {
502 this.max = max = tmax;
505 this.tickInterval = ti;
506 this.numberTicks = nt;
508 for (i=0; i<nt; i++) {
509 it = parseFloat((min+i*ti).toFixed(11));
510 this.ticks.push([it, it]);
512 this.max = this.ticks[nt-1][1];
514 this.tickFactor = tf;
515 // determine number of minor ticks
517 this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor);
519 if (!this.numberMinorTicks) {
520 this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1);
523 // max is free, min is fixed
525 var tmax = min + (nt - 1) * ti;
534 this.tickInterval = this.tickInterval || ti;
535 this.numberTicks = this.numberTicks || nt;
537 for (i=0; i<this.numberTicks; i++) {
538 it = parseFloat((min+i*this.tickInterval).toFixed(11));
539 this.ticks.push([it, it]);
541 this.max = this.ticks[this.numberTicks-1][1];
543 this.tickFactor = tf;
544 // determine number of minor ticks
545 this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor);
547 if (!this.numberMinorTicks) {
548 this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1);
552 // not setting max or min
553 if (!setmax && !setmin) {
554 var range = this.max - this.min;
555 tf = Math.floor(parseFloat((Math.log(range)/Math.log(10)).toFixed(11))) - 1;
556 var nticks = [5,6,4,7,3,8,9,10,2], res, numticks, nonSigDigits=0, sigRange;
557 // check to see how many zeros are at the end of the range
559 var rstr = String(range);
560 if (rstr.search(/\./) == -1) {
561 var pos = rstr.search(/0+$/);
562 nonSigDigits = (pos > 0) ? rstr.length - pos - 1 : 0;
565 sigRange = range/Math.pow(10, nonSigDigits);
566 for (i=0; i<nticks.length; i++) {
567 res = sigRange/(nticks[i]-1);
568 if (res == parseInt(res, 10)) {
569 this.numberTicks = nticks[i];
570 this.tickInterval = range/(this.numberTicks-1);
571 this.tickFactor = tf+1;
576 for (i=0; i<this.numberTicks; i++) {
577 it = parseFloat((this.min+i*this.tickInterval).toFixed(11));
578 this.ticks.push([it, it]);
580 // determine number of minor ticks
581 this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor);
583 if (!this.numberMinorTicks) {
584 this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1);
587 if (!this.numberMinorTicks) {
588 this.numberMinorTicks = 1;
589 var nums = [4, 5, 3, 6, 2];
590 for (i=0; i<5; i++) {
591 var temp = this.tickInterval/nums[i];
592 if (temp == parseInt(temp, 10)) {
593 this.numberMinorTicks = nums[i]-1;
602 var r = this._radius,
603 sa = this.startAngle,
608 if (this.semiCircular) {
609 var overAngle = Math.atan(this.innerPad/r),
610 outersa = this.outerStartAngle = sa - overAngle,
611 outerea = this.outerEndAngle = ea + overAngle,
612 hubsa = this.hubStartAngle = sa - Math.atan(this.innerPad/this.hubRadius*2),
613 hubea = this.hubEndAngle = ea + Math.atan(this.innerPad/this.hubRadius*2);
617 ctx.translate(this._center[0], this._center[1]);
618 ctx.lineJoin = "round";
619 ctx.lineCap = "round";
621 // draw the innerbackground
624 ctx.fillStyle = this.background;
625 ctx.arc(0, 0, r, outersa, outerea, false);
632 var shadowColor = 'rgba(0,0,0,'+this.shadowAlpha+')';
634 for (var i=0; i<this.shadowDepth; i++) {
635 ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI));
637 ctx.strokeStyle = shadowColor;
638 ctx.lineWidth = this.shadowWidth;
639 ctx.arc(0 ,0, r, outersa, outerea, false);
647 var tempd = parseInt((this.shadowDepth+1)/2, 10);
648 for (var i=0; i<tempd; i++) {
649 ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI));
651 ctx.fillStyle = shadowColor;
652 ctx.arc(0 ,0, this.hubRadius, hubsa, hubea, false);
658 // draw the outer ring.
661 ctx.strokeStyle = this.ringColor;
662 ctx.lineWidth = this.ringWidth;
663 ctx.arc(0 ,0, r, outersa, outerea, false);
672 ctx.fillStyle = this.ringColor;
673 ctx.arc(0 ,0, this.hubRadius,hubsa, hubea, false);
679 if (this.showTicks) {
681 var orad = this.tickOuterRadius,
682 tl = this.tickLength,
684 nmt = this.numberMinorTicks,
685 ts = this.span * Math.PI / 180 / (this.ticks.length-1),
688 for (i = 0; i<this.ticks.length; i++) {
690 ctx.lineWidth = 1.5 + this.diameter/360;
691 ctx.strokeStyle = this.ringColor;
693 ctx.moveTo(-orad * Math.cos(ts*i+sa), orad * Math.sin(ts*i+sa));
694 ctx.lineTo(-(orad-tl) * Math.cos(ts*i+sa), (orad - tl) * Math.sin(ts*i+sa));
695 this._tickPoints.push([(orad-tl) * Math.cos(ts*i+sa) + this._center[0] + this.canvas._offsets.left, (orad - tl) * Math.sin(ts*i+sa) + this._center[1] + this.canvas._offsets.top, ts*i+sa]);
697 ctx.lineWidth = 1.0 + this.diameter/440;
698 if (i<this.ticks.length-1) {
699 for (var j=1; j<=nmt; j++) {
701 ctx.moveTo(-orad * Math.cos(ts*i+mts*j+sa), orad * Math.sin(ts*i+mts*j+sa));
702 ctx.lineTo(-(orad-mtl) * Math.cos(ts*i+mts*j+sa), (orad-mtl) * Math.sin(ts*i+mts*j+sa));
710 // draw the tick labels
711 if (this.showTickLabels) {
712 var elem, l, t, ew, eh, dim, maxdim=0;
713 var tp = this.tickPadding * (1 - 1/(this.diameter/80+1));
714 for (i=0; i<this.ticks.length; i++) {
715 elem = $('<div class="jqplot-meterGauge-tick" style="position:absolute;">'+this.ticks[i][1]+'</div>');
716 this.canvas._elem.after(elem);
717 ew = elem.outerWidth(true);
718 eh = elem.outerHeight(true);
719 l = this._tickPoints[i][0] - ew * (this._tickPoints[i][2]-Math.PI)/Math.PI - tp * Math.cos(this._tickPoints[i][2]);
720 t = this._tickPoints[i][1] - eh/2 + eh/2 * Math.pow(Math.abs((Math.sin(this._tickPoints[i][2]))), 0.5) + tp/3 * Math.pow(Math.abs((Math.sin(this._tickPoints[i][2]))), 0.5) ;
721 // t = this._tickPoints[i][1] - eh/2 - eh/2 * Math.sin(this._tickPoints[i][2]) - tp/2 * Math.sin(this._tickPoints[i][2]);
722 elem.css({left:l, top:t});
723 dim = ew*Math.cos(this._tickPoints[i][2]) + eh*Math.sin(Math.PI/2+this._tickPoints[i][2]/2);
724 maxdim = (dim > maxdim) ? dim : maxdim;
728 // draw the gauge label
729 if (this.label && this.labelPosition == 'inside') {
730 var l = this._center[0] + this.canvas._offsets.left;
731 var tp = this.tickPadding * (1 - 1/(this.diameter/80+1));
732 var t = 0.5*(this._center[1] + this.canvas._offsets.top - this.hubRadius) + 0.5*(this._center[1] + this.canvas._offsets.top - this.tickOuterRadius + this.tickLength + tp) + this.labelHeightAdjust;
733 // this._labelElem = $('<div class="jqplot-meterGauge-label" style="position:absolute;">'+this.label+'</div>');
734 // this.canvas._elem.after(this._labelElem);
735 l -= this._labelElem.outerWidth(true)/2;
736 t -= this._labelElem.outerHeight(true)/2;
737 this._labelElem.css({left:l, top:t});
740 else if (this.label && this.labelPosition == 'bottom') {
741 var l = this._center[0] + this.canvas._offsets.left - this._labelElem.outerWidth(true)/2;
742 var t = this._center[1] + this.canvas._offsets.top + this.innerPad + + this.ringWidth + this.padding + this.labelHeightAdjust;
743 this._labelElem.css({left:l, top:t});
747 // draw the intervals
750 var inner = this.intervalInnerRadius || this.hubRadius * 1.5;
751 if (this.intervalOuterRadius == null) {
752 if (this.showTickLabels) {
753 var outer = (this.tickOuterRadius - this.tickLength - this.tickPadding - this.diameter/8);
756 var outer = (this.tickOuterRadius - this.tickLength - this.diameter/16);
760 var outer = this.intervalOuterRadius;
762 var range = this.max - this.min;
763 var intrange = this.intervals[this.intervals.length-1] - this.min;
764 var start, end, span = this.span*Math.PI/180;
765 for (i=0; i<this.intervals.length; i++) {
766 start = (i == 0) ? sa : sa + (this.intervals[i-1][0] - this.min)*span/range;
770 end = sa + (this.intervals[i][0] - this.min)*span/range;
775 ctx.fillStyle = this.intervals[i][2];
776 ctx.arc(0, 0, inner, start, end, false);
777 ctx.lineTo(outer*Math.cos(end), outer*Math.sin(end));
778 ctx.arc(0, 0, outer, end, start, true);
779 ctx.lineTo(inner*Math.cos(start), inner*Math.sin(start));
786 var datapoint = this.data[0][1];
787 var dataspan = this.max - this.min;
788 if (this.pegNeedle) {
789 if (this.data[0][1] > this.max + dataspan*3/this.span) {
790 datapoint = this.max + dataspan*3/this.span;
792 if (this.data[0][1] < this.min - dataspan*3/this.span) {
793 datapoint = this.min - dataspan*3/this.span;
796 var dataang = (datapoint - this.min)/dataspan * this.span * Math.PI/180 + this.startAngle;
801 ctx.fillStyle = this.ringColor;
802 ctx.strokeStyle = this.ringColor;
803 this.needleLength = (this.tickOuterRadius - this.tickLength) * 0.85;
804 this.needleThickness = (this.needleThickness < 2) ? 2 : this.needleThickness;
805 var endwidth = this.needleThickness * 0.4;
808 var dl = this.needleLength/10;
809 var dt = (this.needleThickness - endwidth)/10;
811 for (var i=0; i<10; i++) {
812 templ = this.needleThickness - i*dt;
813 ctx.moveTo(dl*i*Math.cos(dataang), dl*i*Math.sin(dataang));
814 ctx.lineWidth = templ;
815 ctx.lineTo(dl*(i+1)*Math.cos(dataang), dl*(i+1)*Math.sin(dataang));
822 this._center = [(cw - trans * offx)/2 + trans * offx, (ch - trans*offy)/2 + trans * offy];
826 $.jqplot.MeterGaugeAxisRenderer = function() {
827 $.jqplot.LinearAxisRenderer.call(this);
830 $.jqplot.MeterGaugeAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
831 $.jqplot.MeterGaugeAxisRenderer.prototype.constructor = $.jqplot.MeterGaugeAxisRenderer;
834 // There are no traditional axes on a gauge chart. We just need to provide
835 // dummy objects with properties so the plot will render.
836 // called with scope of axis object.
837 $.jqplot.MeterGaugeAxisRenderer.prototype.init = function(options){
839 this.tickRenderer = $.jqplot.MeterGaugeTickRenderer;
840 $.extend(true, this, options);
841 // I don't think I'm going to need _dataBounds here.
842 // have to go Axis scaling in a way to fit chart onto plot area
843 // and provide u2p and p2u functionality for mouse cursor, etc.
844 // for convienence set _dataBounds to 0 and 100 and
845 // set min/max to 0 and 100.
846 this._dataBounds = {min:0, max:100};
849 this.showTicks = false;
851 this.showMark = false;
855 $.jqplot.MeterGaugeLegendRenderer = function(){
856 $.jqplot.TableLegendRenderer.call(this);
859 $.jqplot.MeterGaugeLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
860 $.jqplot.MeterGaugeLegendRenderer.prototype.constructor = $.jqplot.MeterGaugeLegendRenderer;
863 * Class: $.jqplot.MeterGaugeLegendRenderer
864 *Meter gauges don't typically have a legend, this overrides the default legend renderer.
866 $.jqplot.MeterGaugeLegendRenderer.prototype.init = function(options) {
867 // Maximum number of rows in the legend. 0 or null for unlimited.
868 this.numberRows = null;
869 // Maximum number of columns in the legend. 0 or null for unlimited.
870 this.numberColumns = null;
871 $.extend(true, this, options);
874 // called with context of legend
875 $.jqplot.MeterGaugeLegendRenderer.prototype.draw = function() {
877 var series = this._series;
878 var ss = 'position:absolute;';
879 ss += (this.background) ? 'background:'+this.background+';' : '';
880 ss += (this.border) ? 'border:'+this.border+';' : '';
881 ss += (this.fontSize) ? 'font-size:'+this.fontSize+';' : '';
882 ss += (this.fontFamily) ? 'font-family:'+this.fontFamily+';' : '';
883 ss += (this.textColor) ? 'color:'+this.textColor+';' : '';
884 ss += (this.marginTop != null) ? 'margin-top:'+this.marginTop+';' : '';
885 ss += (this.marginBottom != null) ? 'margin-bottom:'+this.marginBottom+';' : '';
886 ss += (this.marginLeft != null) ? 'margin-left:'+this.marginLeft+';' : '';
887 ss += (this.marginRight != null) ? 'margin-right:'+this.marginRight+';' : '';
888 this._elem = $('<table class="jqplot-table-legend" style="'+ss+'"></table>');
889 // MeterGauge charts legends don't go by number of series, but by number of data points
890 // in the series. Refactor things here for that.
899 if (this.numberRows) {
900 nr = this.numberRows;
901 if (!this.numberColumns){
902 nc = Math.ceil(pd.length/nr);
905 nc = this.numberColumns;
908 else if (this.numberColumns) {
909 nc = this.numberColumns;
910 nr = Math.ceil(pd.length/this.numberColumns);
917 var i, j, tr, td1, td2, lt, rs, color;
920 for (i=0; i<nr; i++) {
922 tr = $('<tr class="jqplot-table-legend"></tr>').prependTo(this._elem);
925 tr = $('<tr class="jqplot-table-legend"></tr>').appendTo(this._elem);
927 for (j=0; j<nc; j++) {
928 if (idx < pd.length){
930 lt = this.labels[idx] || pd[idx][0].toString();
948 rs = (pad) ? this.rowSpacing : '0';
950 td1 = $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+
951 '<div><div class="jqplot-table-legend-swatch" style="border-color:'+color+';"></div>'+
953 td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
954 if (this.escapeHtml){
979 // setup default renderers for axes and legend so user doesn't have to
980 // called with scope of plot
981 function preInit(target, data, options) {
983 options = options || {};
984 options.axesDefaults = options.axesDefaults || {};
985 options.legend = options.legend || {};
986 options.seriesDefaults = options.seriesDefaults || {};
987 options.grid = options.grid || {};
989 // only set these if there is a gauge series
991 if (options.seriesDefaults.renderer == $.jqplot.MeterGaugeRenderer) {
994 else if (options.series) {
995 for (var i=0; i < options.series.length; i++) {
996 if (options.series[i].renderer == $.jqplot.MeterGaugeRenderer) {
1003 options.axesDefaults.renderer = $.jqplot.MeterGaugeAxisRenderer;
1004 options.legend.renderer = $.jqplot.MeterGaugeLegendRenderer;
1005 options.legend.preDraw = true;
1006 options.grid.background = options.grid.background || 'white';
1007 options.grid.drawGridlines = false;
1008 options.grid.borderWidth = (options.grid.borderWidth != null) ? options.grid.borderWidth : 0;
1009 options.grid.shadow = (options.grid.shadow != null) ? options.grid.shadow : false;
1013 // called with scope of plot
1014 function postParseOptions(options) {
1018 $.jqplot.preInitHooks.push(preInit);
1019 $.jqplot.postParseOptionsHooks.push(postParseOptions);
1021 $.jqplot.MeterGaugeTickRenderer = function() {
1022 $.jqplot.AxisTickRenderer.call(this);
1025 $.jqplot.MeterGaugeTickRenderer.prototype = new $.jqplot.AxisTickRenderer();
1026 $.jqplot.MeterGaugeTickRenderer.prototype.constructor = $.jqplot.MeterGaugeTickRenderer;