[9062] | 1 | /** |
---|
| 2 | * jqPlot |
---|
| 3 | * Pure JavaScript plotting plugin using jQuery |
---|
| 4 | * |
---|
| 5 | * Version: @VERSION |
---|
| 6 | * |
---|
| 7 | * Copyright (c) 2009-2011 Chris Leonello |
---|
| 8 | * jqPlot is currently available for use in all personal or commercial projects |
---|
| 9 | * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL |
---|
| 10 | * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can |
---|
| 11 | * choose the license that best suits your project and use it accordingly. |
---|
| 12 | * |
---|
| 13 | * Although not required, the author would appreciate an email letting him |
---|
| 14 | * know of any substantial use of jqPlot. You can reach the author at: |
---|
| 15 | * chris at jqplot dot com or see http://www.jqplot.com/info.php . |
---|
| 16 | * |
---|
| 17 | * If you are feeling kind and generous, consider supporting the project by |
---|
| 18 | * making a donation at: http://www.jqplot.com/donate.php . |
---|
| 19 | * |
---|
| 20 | * sprintf functions contained in jqplot.sprintf.js by Ash Searle: |
---|
| 21 | * |
---|
| 22 | * version 2007.04.27 |
---|
| 23 | * author Ash Searle |
---|
| 24 | * http://hexmen.com/blog/2007/03/printf-sprintf/ |
---|
| 25 | * http://hexmen.com/js/sprintf.js |
---|
| 26 | * The author (Ash Searle) has placed this code in the public domain: |
---|
| 27 | * "This code is unrestricted: you are free to use it however you like." |
---|
| 28 | * |
---|
| 29 | */ |
---|
| 30 | (function($) { |
---|
| 31 | /** |
---|
| 32 | * Class: $.jqplot.MeterGaugeRenderer |
---|
| 33 | * Plugin renderer to draw a meter gauge chart. |
---|
| 34 | * |
---|
| 35 | * Data consists of a single series with 1 data point to position the gauge needle. |
---|
| 36 | * |
---|
| 37 | * To use this renderer, you need to include the |
---|
| 38 | * meter gauge renderer plugin, for example: |
---|
| 39 | * |
---|
| 40 | * > <script type="text/javascript" src="plugins/jqplot.meterGaugeRenderer.js"></script> |
---|
| 41 | * |
---|
| 42 | * Properties described here are passed into the $.jqplot function |
---|
| 43 | * as options on the series renderer. For example: |
---|
| 44 | * |
---|
| 45 | * > plot0 = $.jqplot('chart0',[[18]],{ |
---|
| 46 | * > title: 'Network Speed', |
---|
| 47 | * > seriesDefaults: { |
---|
| 48 | * > renderer: $.jqplot.MeterGaugeRenderer, |
---|
| 49 | * > rendererOptions: { |
---|
| 50 | * > label: 'MB/s' |
---|
| 51 | * > } |
---|
| 52 | * > } |
---|
| 53 | * > }); |
---|
| 54 | * |
---|
| 55 | * A meterGauge plot does not support events. |
---|
| 56 | */ |
---|
| 57 | $.jqplot.MeterGaugeRenderer = function(){ |
---|
| 58 | $.jqplot.LineRenderer.call(this); |
---|
| 59 | }; |
---|
| 60 | |
---|
| 61 | $.jqplot.MeterGaugeRenderer.prototype = new $.jqplot.LineRenderer(); |
---|
| 62 | $.jqplot.MeterGaugeRenderer.prototype.constructor = $.jqplot.MeterGaugeRenderer; |
---|
| 63 | |
---|
| 64 | // called with scope of a series |
---|
| 65 | $.jqplot.MeterGaugeRenderer.prototype.init = function(options) { |
---|
| 66 | // Group: Properties |
---|
| 67 | // |
---|
| 68 | // prop: diameter |
---|
| 69 | // Outer diameter of the meterGauge, auto computed by default |
---|
| 70 | this.diameter = null; |
---|
| 71 | // prop: padding |
---|
| 72 | // padding between the meterGauge and plot edges, auto |
---|
| 73 | // calculated by default. |
---|
| 74 | this.padding = null; |
---|
| 75 | // prop: shadowOffset |
---|
| 76 | // offset of the shadow from the gauge ring and offset of |
---|
| 77 | // each succesive stroke of the shadow from the last. |
---|
| 78 | this.shadowOffset = 2; |
---|
| 79 | // prop: shadowAlpha |
---|
| 80 | // transparency of the shadow (0 = transparent, 1 = opaque) |
---|
| 81 | this.shadowAlpha = 0.07; |
---|
| 82 | // prop: shadowDepth |
---|
| 83 | // number of strokes to apply to the shadow, |
---|
| 84 | // each stroke offset shadowOffset from the last. |
---|
| 85 | this.shadowDepth = 4; |
---|
| 86 | // prop: background |
---|
| 87 | // background color of the inside of the gauge. |
---|
| 88 | this.background = "#efefef"; |
---|
| 89 | // prop: ringColor |
---|
| 90 | // color of the outer ring, hub, and needle of the gauge. |
---|
| 91 | this.ringColor = "#BBC6D0"; |
---|
| 92 | // needle color not implemented yet. |
---|
| 93 | this.needleColor = "#C3D3E5"; |
---|
| 94 | // prop: tickColor |
---|
| 95 | // color of the tick marks around the gauge. |
---|
| 96 | this.tickColor = "989898"; |
---|
| 97 | // prop: ringWidth |
---|
| 98 | // width of the ring around the gauge. Auto computed by default. |
---|
| 99 | this.ringWidth = null; |
---|
| 100 | // prop: min |
---|
| 101 | // Minimum value on the gauge. Auto computed by default |
---|
| 102 | this.min; |
---|
| 103 | // prop: max |
---|
| 104 | // Maximum value on the gauge. Auto computed by default |
---|
| 105 | this.max; |
---|
| 106 | // prop: ticks |
---|
| 107 | // Array of tick values. Auto computed by default. |
---|
| 108 | this.ticks = []; |
---|
| 109 | // prop: showTicks |
---|
| 110 | // true to show ticks around gauge. |
---|
| 111 | this.showTicks = true; |
---|
| 112 | // prop: showTickLabels |
---|
| 113 | // true to show tick labels next to ticks. |
---|
| 114 | this.showTickLabels = true; |
---|
| 115 | // prop: label |
---|
| 116 | // A gauge label like 'kph' or 'Volts' |
---|
| 117 | this.label = null; |
---|
| 118 | // prop: labelHeightAdjust |
---|
| 119 | // Number of Pixels to offset the label up (-) or down (+) from its default position. |
---|
| 120 | this.labelHeightAdjust = 0; |
---|
| 121 | // prop: labelPosition |
---|
| 122 | // Where to position the label, either 'inside' or 'bottom'. |
---|
| 123 | this.labelPosition = 'inside'; |
---|
| 124 | // prop: intervals |
---|
| 125 | // Array of ranges to be drawn around the gauge. |
---|
| 126 | // Array of form: |
---|
| 127 | // > [value1, value2, ...] |
---|
| 128 | // indicating the values for the first, second, ... intervals. |
---|
| 129 | this.intervals = []; |
---|
| 130 | // prop: intervalColors |
---|
| 131 | // Array of colors to use for the intervals. |
---|
| 132 | this.intervalColors = [ "#4bb2c5", "#EAA228", "#c5b47f", "#579575", "#839557", "#958c12", "#953579", "#4b5de4", "#d8b83f", "#ff5800", "#0085cc", "#c747a3", "#cddf54", "#FBD178", "#26B4E3", "#bd70c7"]; |
---|
| 133 | // prop: intervalInnerRadius |
---|
| 134 | // Radius of the inner circle of the interval ring. |
---|
| 135 | this.intervalInnerRadius = null; |
---|
| 136 | // prop: intervalOuterRadius |
---|
| 137 | // Radius of the outer circle of the interval ring. |
---|
| 138 | this.intervalOuterRadius = null; |
---|
| 139 | this.tickRenderer = $.jqplot.MeterGaugeTickRenderer; |
---|
| 140 | // ticks spaced every 1, 2, 2.5, 5, 10, 20, .1, .2, .25, .5, etc. |
---|
| 141 | this.tickPositions = [1, 2, 2.5, 5, 10]; |
---|
| 142 | // prop: tickSpacing |
---|
| 143 | // Degrees between ticks. This is a target number, if |
---|
| 144 | // incompatible span and ticks are supplied, a suitable |
---|
| 145 | // spacing close to this value will be computed. |
---|
| 146 | this.tickSpacing = 30; |
---|
| 147 | this.numberMinorTicks = null; |
---|
| 148 | // prop: hubRadius |
---|
| 149 | // Radius of the hub at the bottom center of gauge which the needle attaches to. |
---|
| 150 | // Auto computed by default |
---|
| 151 | this.hubRadius = null; |
---|
| 152 | // prop: tickPadding |
---|
| 153 | // padding of the tick marks to the outer ring and the tick labels to marks. |
---|
| 154 | // Auto computed by default. |
---|
| 155 | this.tickPadding = null; |
---|
| 156 | // prop: needleThickness |
---|
| 157 | // Maximum thickness the needle. Auto computed by default. |
---|
| 158 | this.needleThickness = null; |
---|
| 159 | // prop: needlePad |
---|
| 160 | // Padding between needle and inner edge of the ring when the needle is at the min or max gauge value. |
---|
| 161 | this.needlePad = 6; |
---|
| 162 | // prop: pegNeedle |
---|
| 163 | // True will stop needle just below/above the min/max values if data is below/above min/max, |
---|
| 164 | // as if the meter is "pegged". |
---|
| 165 | this.pegNeedle = true; |
---|
| 166 | this._type = 'meterGauge'; |
---|
| 167 | |
---|
| 168 | $.extend(true, this, options); |
---|
| 169 | this.type = null; |
---|
| 170 | this.numberTicks = null; |
---|
| 171 | this.tickInterval = null; |
---|
| 172 | // span, the sweep (in degrees) from min to max. This gauge is |
---|
| 173 | // a semi-circle. |
---|
| 174 | this.span = 180; |
---|
| 175 | // get rid of this nonsense |
---|
| 176 | // this.innerSpan = this.span; |
---|
| 177 | if (this.type == 'circular') { |
---|
| 178 | this.semiCircular = false; |
---|
| 179 | } |
---|
| 180 | else if (this.type != 'circular') { |
---|
| 181 | this.semiCircular = true; |
---|
| 182 | } |
---|
| 183 | else { |
---|
| 184 | this.semiCircular = (this.span <= 180) ? true : false; |
---|
| 185 | } |
---|
| 186 | this._tickPoints = []; |
---|
| 187 | // reference to label element. |
---|
| 188 | this._labelElm = null; |
---|
| 189 | |
---|
| 190 | // start the gauge at the beginning of the span |
---|
| 191 | this.startAngle = (90 + (360 - this.span)/2) * Math.PI/180; |
---|
| 192 | this.endAngle = (90 - (360 - this.span)/2) * Math.PI/180; |
---|
| 193 | |
---|
| 194 | this.setmin = !!(this.min == null); |
---|
| 195 | this.setmax = !!(this.max == null); |
---|
| 196 | |
---|
| 197 | // if given intervals and is an array of values, create labels and colors. |
---|
| 198 | if (this.intervals.length) { |
---|
| 199 | if (this.intervals[0].length == null || this.intervals.length == 1) { |
---|
| 200 | for (var i=0; i<this.intervals.length; i++) { |
---|
| 201 | this.intervals[i] = [this.intervals[i], this.intervals[i], this.intervalColors[i]]; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | else if (this.intervals[0].length == 2) { |
---|
| 205 | for (i=0; i<this.intervals.length; i++) { |
---|
| 206 | this.intervals[i] = [this.intervals[i][0], this.intervals[i][1], this.intervalColors[i]]; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | // compute min, max and ticks if not supplied: |
---|
| 212 | if (this.ticks.length) { |
---|
| 213 | if (this.ticks[0].length == null || this.ticks[0].length == 1) { |
---|
| 214 | for (var i=0; i<this.ticks.length; i++) { |
---|
| 215 | this.ticks[i] = [this.ticks[i], this.ticks[i]]; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | this.min = (this.min == null) ? this.ticks[0][0] : this.min; |
---|
| 219 | this.max = (this.max == null) ? this.ticks[this.ticks.length-1][0] : this.max; |
---|
| 220 | this.setmin = false; |
---|
| 221 | this.setmax = false; |
---|
| 222 | this.numberTicks = this.ticks.length; |
---|
| 223 | this.tickInterval = this.ticks[1][0] - this.ticks[0][0]; |
---|
| 224 | this.tickFactor = Math.floor(parseFloat((Math.log(this.tickInterval)/Math.log(10)).toFixed(11))); |
---|
| 225 | // use the first interal to calculate minor ticks; |
---|
| 226 | this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor); |
---|
| 227 | if (!this.numberMinorTicks) { |
---|
| 228 | this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1); |
---|
| 229 | } |
---|
| 230 | if (!this.numberMinorTicks) { |
---|
| 231 | this.numberMinorTicks = 1; |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | else if (this.intervals.length) { |
---|
| 236 | this.min = (this.min == null) ? 0 : this.min; |
---|
| 237 | this.setmin = false; |
---|
| 238 | if (this.max == null) { |
---|
| 239 | if (this.intervals[this.intervals.length-1][0] >= this.data[0][1]) { |
---|
| 240 | this.max = this.intervals[this.intervals.length-1][0]; |
---|
| 241 | this.setmax = false; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | else { |
---|
| 245 | this.setmax = false; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | else { |
---|
| 250 | // no ticks and no intervals supplied, put needle in middle |
---|
| 251 | this.min = (this.min == null) ? 0 : this.min; |
---|
| 252 | this.setmin = false; |
---|
| 253 | if (this.max == null) { |
---|
| 254 | this.max = this.data[0][1] * 1.25; |
---|
| 255 | this.setmax = true; |
---|
| 256 | } |
---|
| 257 | else { |
---|
| 258 | this.setmax = false; |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | }; |
---|
| 262 | |
---|
| 263 | $.jqplot.MeterGaugeRenderer.prototype.setGridData = function(plot) { |
---|
| 264 | // set gridData property. This will hold angle in radians of each data point. |
---|
| 265 | var stack = []; |
---|
| 266 | var td = []; |
---|
| 267 | var sa = this.startAngle; |
---|
| 268 | for (var i=0; i<this.data.length; i++){ |
---|
| 269 | stack.push(this.data[i][1]); |
---|
| 270 | td.push([this.data[i][0]]); |
---|
| 271 | if (i>0) { |
---|
| 272 | stack[i] += stack[i-1]; |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | var fact = Math.PI*2/stack[stack.length - 1]; |
---|
| 276 | |
---|
| 277 | for (var i=0; i<stack.length; i++) { |
---|
| 278 | td[i][1] = stack[i] * fact; |
---|
| 279 | } |
---|
| 280 | this.gridData = td; |
---|
| 281 | }; |
---|
| 282 | |
---|
| 283 | $.jqplot.MeterGaugeRenderer.prototype.makeGridData = function(data, plot) { |
---|
| 284 | var stack = []; |
---|
| 285 | var td = []; |
---|
| 286 | var sa = this.startAngle; |
---|
| 287 | for (var i=0; i<data.length; i++){ |
---|
| 288 | stack.push(data[i][1]); |
---|
| 289 | td.push([data[i][0]]); |
---|
| 290 | if (i>0) { |
---|
| 291 | stack[i] += stack[i-1]; |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | var fact = Math.PI*2/stack[stack.length - 1]; |
---|
| 295 | |
---|
| 296 | for (var i=0; i<stack.length; i++) { |
---|
| 297 | td[i][1] = stack[i] * fact; |
---|
| 298 | } |
---|
| 299 | return td; |
---|
| 300 | }; |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | function getnmt(pos, interval, fact) { |
---|
| 304 | var temp; |
---|
| 305 | for (var i=pos.length-1; i>=0; i--) { |
---|
| 306 | temp = interval/(pos[i] * Math.pow(10, fact)); |
---|
| 307 | if (temp == 4 || temp == 5) { |
---|
| 308 | return temp - 1; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | return null; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | // called with scope of series |
---|
| 315 | $.jqplot.MeterGaugeRenderer.prototype.draw = function (ctx, gd, options) { |
---|
| 316 | var i; |
---|
| 317 | var opts = (options != undefined) ? options : {}; |
---|
| 318 | // offset and direction of offset due to legend placement |
---|
| 319 | var offx = 0; |
---|
| 320 | var offy = 0; |
---|
| 321 | var trans = 1; |
---|
| 322 | if (options.legendInfo && options.legendInfo.placement == 'inside') { |
---|
| 323 | var li = options.legendInfo; |
---|
| 324 | switch (li.location) { |
---|
| 325 | case 'nw': |
---|
| 326 | offx = li.width + li.xoffset; |
---|
| 327 | break; |
---|
| 328 | case 'w': |
---|
| 329 | offx = li.width + li.xoffset; |
---|
| 330 | break; |
---|
| 331 | case 'sw': |
---|
| 332 | offx = li.width + li.xoffset; |
---|
| 333 | break; |
---|
| 334 | case 'ne': |
---|
| 335 | offx = li.width + li.xoffset; |
---|
| 336 | trans = -1; |
---|
| 337 | break; |
---|
| 338 | case 'e': |
---|
| 339 | offx = li.width + li.xoffset; |
---|
| 340 | trans = -1; |
---|
| 341 | break; |
---|
| 342 | case 'se': |
---|
| 343 | offx = li.width + li.xoffset; |
---|
| 344 | trans = -1; |
---|
| 345 | break; |
---|
| 346 | case 'n': |
---|
| 347 | offy = li.height + li.yoffset; |
---|
| 348 | break; |
---|
| 349 | case 's': |
---|
| 350 | offy = li.height + li.yoffset; |
---|
| 351 | trans = -1; |
---|
| 352 | break; |
---|
| 353 | default: |
---|
| 354 | break; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | |
---|
| 359 | |
---|
| 360 | // pre-draw so can get it's dimensions. |
---|
| 361 | if (this.label) { |
---|
| 362 | this._labelElem = $('<div class="jqplot-meterGauge-label" style="position:absolute;">'+this.label+'</div>'); |
---|
| 363 | this.canvas._elem.after(this._labelElem); |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow; |
---|
| 367 | var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine; |
---|
| 368 | var fill = (opts.fill != undefined) ? opts.fill : this.fill; |
---|
| 369 | var cw = ctx.canvas.width; |
---|
| 370 | var ch = ctx.canvas.height; |
---|
| 371 | if (this.padding == null) { |
---|
| 372 | this.padding = Math.round(Math.min(cw, ch)/30); |
---|
| 373 | } |
---|
| 374 | var w = cw - offx - 2 * this.padding; |
---|
| 375 | var h = ch - offy - 2 * this.padding; |
---|
| 376 | if (this.labelPosition == 'bottom' && this.label) { |
---|
| 377 | h -= this._labelElem.outerHeight(true); |
---|
| 378 | } |
---|
| 379 | var mindim = Math.min(w,h); |
---|
| 380 | var d = mindim; |
---|
| 381 | |
---|
| 382 | if (!this.diameter) { |
---|
| 383 | if (this.semiCircular) { |
---|
| 384 | if ( w >= 2*h) { |
---|
| 385 | if (!this.ringWidth) { |
---|
| 386 | this.ringWidth = 2*h/35; |
---|
| 387 | } |
---|
| 388 | this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8); |
---|
| 389 | this.innerPad = this.ringWidth/2 + this.needleThickness/2 + this.needlePad; |
---|
| 390 | this.diameter = 2 * (h - 2*this.innerPad); |
---|
| 391 | } |
---|
| 392 | else { |
---|
| 393 | if (!this.ringWidth) { |
---|
| 394 | this.ringWidth = w/35; |
---|
| 395 | } |
---|
| 396 | this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8); |
---|
| 397 | this.innerPad = this.ringWidth/2 + this.needleThickness/2 + this.needlePad; |
---|
| 398 | this.diameter = w - 2*this.innerPad; |
---|
| 399 | } |
---|
| 400 | // center taking into account legend and over draw for gauge bottom below hub. |
---|
| 401 | // this will be center of hub. |
---|
| 402 | this._center = [(cw - trans * offx)/2 + trans * offx, (ch + trans*offy - this.padding - this.ringWidth - this.innerPad)]; |
---|
| 403 | } |
---|
| 404 | else { |
---|
| 405 | if (!this.ringWidth) { |
---|
| 406 | this.ringWidth = d/35; |
---|
| 407 | } |
---|
| 408 | this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8); |
---|
| 409 | this.innerPad = 0; |
---|
| 410 | this.diameter = d - this.ringWidth; |
---|
| 411 | // center in middle of canvas taking into account legend. |
---|
| 412 | // will be center of hub. |
---|
| 413 | this._center = [(cw-trans*offx)/2 + trans * offx, (ch-trans*offy)/2 + trans * offy]; |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | if (this._labelElem && this.labelPosition == 'bottom') { |
---|
| 418 | this._center[1] -= this._labelElem.outerHeight(true); |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | this._radius = this.diameter/2; |
---|
| 422 | |
---|
| 423 | this.tickSpacing = 6000/this.diameter; |
---|
| 424 | |
---|
| 425 | if (!this.hubRadius) { |
---|
| 426 | this.hubRadius = this.diameter/18; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | this.shadowOffset = 0.5 + this.ringWidth/9; |
---|
| 430 | this.shadowWidth = this.ringWidth*1; |
---|
| 431 | |
---|
| 432 | this.tickPadding = 3 + Math.pow(this.diameter/20, 0.7); |
---|
| 433 | this.tickOuterRadius = this._radius - this.ringWidth/2 - this.tickPadding; |
---|
| 434 | this.tickLength = (this.showTicks) ? this._radius/13 : 0; |
---|
| 435 | |
---|
| 436 | if (this.ticks.length == 0) { |
---|
| 437 | // no ticks, lets make some. |
---|
| 438 | var max = this.max, |
---|
| 439 | min = this.min, |
---|
| 440 | setmax = this.setmax, |
---|
| 441 | setmin = this.setmin, |
---|
| 442 | ti = (max - min) * this.tickSpacing / this.span; |
---|
| 443 | var tf = Math.floor(parseFloat((Math.log(ti)/Math.log(10)).toFixed(11))); |
---|
| 444 | var tp = (ti/Math.pow(10, tf)); |
---|
| 445 | (tp > 2 && tp <= 2.5) ? tp = 2.5 : tp = Math.ceil(tp); |
---|
| 446 | var t = this.tickPositions; |
---|
| 447 | var tpindex, nt; |
---|
| 448 | |
---|
| 449 | for (i=0; i<t.length; i++) { |
---|
| 450 | if (tp == t[i] || i && t[i-1] < tp && tp < t[i]) { |
---|
| 451 | ti = t[i]*Math.pow(10, tf); |
---|
| 452 | tpindex = i; |
---|
| 453 | } |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | for (i=0; i<t.length; i++) { |
---|
| 457 | if (tp == t[i] || i && t[i-1] < tp && tp < t[i]) { |
---|
| 458 | ti = t[i]*Math.pow(10, tf); |
---|
| 459 | nt = Math.ceil((max - min) / ti); |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | // both max and min are free |
---|
| 464 | if (setmax && setmin) { |
---|
| 465 | var tmin = (min > 0) ? min - min % ti : min - min % ti - ti; |
---|
| 466 | if (!this.forceZero) { |
---|
| 467 | var diff = Math.min(min - tmin, 0.8*ti); |
---|
| 468 | var ntp = Math.floor(diff/t[tpindex]); |
---|
| 469 | if (ntp > 1) { |
---|
| 470 | tmin = tmin + t[tpindex] * (ntp-1); |
---|
| 471 | if (parseInt(tmin, 10) != tmin && parseInt(tmin-t[tpindex], 10) == tmin-t[tpindex]) { |
---|
| 472 | tmin = tmin - t[tpindex]; |
---|
| 473 | } |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | if (min == tmin) { |
---|
| 477 | min -= ti; |
---|
| 478 | } |
---|
| 479 | else { |
---|
| 480 | // tmin should always be lower than dataMin |
---|
| 481 | if (min - tmin > 0.23*ti) { |
---|
| 482 | min = tmin; |
---|
| 483 | } |
---|
| 484 | else { |
---|
| 485 | min = tmin -ti; |
---|
| 486 | nt += 1; |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | nt += 1; |
---|
| 490 | var tmax = min + (nt - 1) * ti; |
---|
| 491 | if (max >= tmax) { |
---|
| 492 | tmax += ti; |
---|
| 493 | nt += 1; |
---|
| 494 | } |
---|
| 495 | // now tmax should always be mroe than dataMax |
---|
| 496 | if (tmax - max < 0.23*ti) { |
---|
| 497 | tmax += ti; |
---|
| 498 | nt += 1; |
---|
| 499 | } |
---|
| 500 | this.max = max = tmax; |
---|
| 501 | this.min = min; |
---|
| 502 | |
---|
| 503 | this.tickInterval = ti; |
---|
| 504 | this.numberTicks = nt; |
---|
| 505 | var it; |
---|
| 506 | for (i=0; i<nt; i++) { |
---|
| 507 | it = parseFloat((min+i*ti).toFixed(11)); |
---|
| 508 | this.ticks.push([it, it]); |
---|
| 509 | } |
---|
| 510 | this.max = this.ticks[nt-1][1]; |
---|
| 511 | |
---|
| 512 | this.tickFactor = tf; |
---|
| 513 | // determine number of minor ticks |
---|
| 514 | |
---|
| 515 | this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor); |
---|
| 516 | |
---|
| 517 | if (!this.numberMinorTicks) { |
---|
| 518 | this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1); |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | // max is free, min is fixed |
---|
| 522 | else if (setmax) { |
---|
| 523 | var tmax = min + (nt - 1) * ti; |
---|
| 524 | if (max >= tmax) { |
---|
| 525 | max = tmax + ti; |
---|
| 526 | nt += 1; |
---|
| 527 | } |
---|
| 528 | else { |
---|
| 529 | max = tmax; |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | this.tickInterval = this.tickInterval || ti; |
---|
| 533 | this.numberTicks = this.numberTicks || nt; |
---|
| 534 | var it; |
---|
| 535 | for (i=0; i<this.numberTicks; i++) { |
---|
| 536 | it = parseFloat((min+i*this.tickInterval).toFixed(11)); |
---|
| 537 | this.ticks.push([it, it]); |
---|
| 538 | } |
---|
| 539 | this.max = this.ticks[this.numberTicks-1][1]; |
---|
| 540 | |
---|
| 541 | this.tickFactor = tf; |
---|
| 542 | // determine number of minor ticks |
---|
| 543 | this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor); |
---|
| 544 | |
---|
| 545 | if (!this.numberMinorTicks) { |
---|
| 546 | this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1); |
---|
| 547 | } |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | // not setting max or min |
---|
| 551 | if (!setmax && !setmin) { |
---|
| 552 | var range = this.max - this.min; |
---|
| 553 | tf = Math.floor(parseFloat((Math.log(range)/Math.log(10)).toFixed(11))) - 1; |
---|
| 554 | var nticks = [5,6,4,7,3,8,9,10,2], res, numticks, nonSigDigits=0, sigRange; |
---|
| 555 | // check to see how many zeros are at the end of the range |
---|
| 556 | if (range > 1) { |
---|
| 557 | var rstr = String(range); |
---|
| 558 | if (rstr.search(/\./) == -1) { |
---|
| 559 | var pos = rstr.search(/0+$/); |
---|
| 560 | nonSigDigits = (pos > 0) ? rstr.length - pos - 1 : 0; |
---|
| 561 | } |
---|
| 562 | } |
---|
| 563 | sigRange = range/Math.pow(10, nonSigDigits); |
---|
| 564 | for (i=0; i<nticks.length; i++) { |
---|
| 565 | res = sigRange/(nticks[i]-1); |
---|
| 566 | if (res == parseInt(res, 10)) { |
---|
| 567 | this.numberTicks = nticks[i]; |
---|
| 568 | this.tickInterval = range/(this.numberTicks-1); |
---|
| 569 | this.tickFactor = tf+1; |
---|
| 570 | break; |
---|
| 571 | } |
---|
| 572 | } |
---|
| 573 | var it; |
---|
| 574 | for (i=0; i<this.numberTicks; i++) { |
---|
| 575 | it = parseFloat((this.min+i*this.tickInterval).toFixed(11)); |
---|
| 576 | this.ticks.push([it, it]); |
---|
| 577 | } |
---|
| 578 | // determine number of minor ticks |
---|
| 579 | this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor); |
---|
| 580 | |
---|
| 581 | if (!this.numberMinorTicks) { |
---|
| 582 | this.numberMinorTicks = getnmt(this.tickPositions, this.tickInterval, this.tickFactor-1); |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | if (!this.numberMinorTicks) { |
---|
| 586 | this.numberMinorTicks = 1; |
---|
| 587 | var nums = [4, 5, 3, 6, 2]; |
---|
| 588 | for (i=0; i<5; i++) { |
---|
| 589 | var temp = this.tickInterval/nums[i]; |
---|
| 590 | if (temp == parseInt(temp, 10)) { |
---|
| 591 | this.numberMinorTicks = nums[i]-1; |
---|
| 592 | break; |
---|
| 593 | } |
---|
| 594 | } |
---|
| 595 | } |
---|
| 596 | } |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | |
---|
| 600 | var r = this._radius, |
---|
| 601 | sa = this.startAngle, |
---|
| 602 | ea = this.endAngle, |
---|
| 603 | pi = Math.PI, |
---|
| 604 | hpi = Math.PI/2; |
---|
| 605 | |
---|
| 606 | if (this.semiCircular) { |
---|
| 607 | var overAngle = Math.atan(this.innerPad/r), |
---|
| 608 | outersa = this.outerStartAngle = sa - overAngle, |
---|
| 609 | outerea = this.outerEndAngle = ea + overAngle, |
---|
| 610 | hubsa = this.hubStartAngle = sa - Math.atan(this.innerPad/this.hubRadius*2), |
---|
| 611 | hubea = this.hubEndAngle = ea + Math.atan(this.innerPad/this.hubRadius*2); |
---|
| 612 | |
---|
| 613 | ctx.save(); |
---|
| 614 | |
---|
| 615 | ctx.translate(this._center[0], this._center[1]); |
---|
| 616 | ctx.lineJoin = "round"; |
---|
| 617 | ctx.lineCap = "round"; |
---|
| 618 | |
---|
| 619 | // draw the innerbackground |
---|
| 620 | ctx.save(); |
---|
| 621 | ctx.beginPath(); |
---|
| 622 | ctx.fillStyle = this.background; |
---|
| 623 | ctx.arc(0, 0, r, outersa, outerea, false); |
---|
| 624 | ctx.closePath(); |
---|
| 625 | ctx.fill(); |
---|
| 626 | ctx.restore(); |
---|
| 627 | |
---|
| 628 | // draw the shadow |
---|
| 629 | // the outer ring. |
---|
| 630 | var shadowColor = 'rgba(0,0,0,'+this.shadowAlpha+')'; |
---|
| 631 | ctx.save(); |
---|
| 632 | for (var i=0; i<this.shadowDepth; i++) { |
---|
| 633 | ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI)); |
---|
| 634 | ctx.beginPath(); |
---|
| 635 | ctx.strokeStyle = shadowColor; |
---|
| 636 | ctx.lineWidth = this.shadowWidth; |
---|
| 637 | ctx.arc(0 ,0, r, outersa, outerea, false); |
---|
| 638 | ctx.closePath(); |
---|
| 639 | ctx.stroke(); |
---|
| 640 | } |
---|
| 641 | ctx.restore(); |
---|
| 642 | |
---|
| 643 | // the inner hub. |
---|
| 644 | ctx.save(); |
---|
| 645 | var tempd = parseInt((this.shadowDepth+1)/2, 10); |
---|
| 646 | for (var i=0; i<tempd; i++) { |
---|
| 647 | ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI)); |
---|
| 648 | ctx.beginPath(); |
---|
| 649 | ctx.fillStyle = shadowColor; |
---|
| 650 | ctx.arc(0 ,0, this.hubRadius, hubsa, hubea, false); |
---|
| 651 | ctx.closePath(); |
---|
| 652 | ctx.fill(); |
---|
| 653 | } |
---|
| 654 | ctx.restore(); |
---|
| 655 | |
---|
| 656 | // draw the outer ring. |
---|
| 657 | ctx.save(); |
---|
| 658 | ctx.beginPath(); |
---|
| 659 | ctx.strokeStyle = this.ringColor; |
---|
| 660 | ctx.lineWidth = this.ringWidth; |
---|
| 661 | ctx.arc(0 ,0, r, outersa, outerea, false); |
---|
| 662 | ctx.closePath(); |
---|
| 663 | ctx.stroke(); |
---|
| 664 | ctx.restore(); |
---|
| 665 | |
---|
| 666 | // draw the hub |
---|
| 667 | |
---|
| 668 | ctx.save(); |
---|
| 669 | ctx.beginPath(); |
---|
| 670 | ctx.fillStyle = this.ringColor; |
---|
| 671 | ctx.arc(0 ,0, this.hubRadius,hubsa, hubea, false); |
---|
| 672 | ctx.closePath(); |
---|
| 673 | ctx.fill(); |
---|
| 674 | ctx.restore(); |
---|
| 675 | |
---|
| 676 | // draw the ticks |
---|
| 677 | if (this.showTicks) { |
---|
| 678 | ctx.save(); |
---|
| 679 | var orad = this.tickOuterRadius, |
---|
| 680 | tl = this.tickLength, |
---|
| 681 | mtl = tl/2, |
---|
| 682 | nmt = this.numberMinorTicks, |
---|
| 683 | ts = this.span * Math.PI / 180 / (this.ticks.length-1), |
---|
| 684 | mts = ts/(nmt + 1); |
---|
| 685 | |
---|
| 686 | for (i = 0; i<this.ticks.length; i++) { |
---|
| 687 | ctx.beginPath(); |
---|
| 688 | ctx.lineWidth = 1.5 + this.diameter/360; |
---|
| 689 | ctx.strokeStyle = this.ringColor; |
---|
| 690 | var wps = ts*i+sa; |
---|
| 691 | ctx.moveTo(-orad * Math.cos(ts*i+sa), orad * Math.sin(ts*i+sa)); |
---|
| 692 | ctx.lineTo(-(orad-tl) * Math.cos(ts*i+sa), (orad - tl) * Math.sin(ts*i+sa)); |
---|
| 693 | 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]); |
---|
| 694 | ctx.stroke(); |
---|
| 695 | ctx.lineWidth = 1.0 + this.diameter/440; |
---|
| 696 | if (i<this.ticks.length-1) { |
---|
| 697 | for (var j=1; j<=nmt; j++) { |
---|
| 698 | ctx.beginPath(); |
---|
| 699 | ctx.moveTo(-orad * Math.cos(ts*i+mts*j+sa), orad * Math.sin(ts*i+mts*j+sa)); |
---|
| 700 | ctx.lineTo(-(orad-mtl) * Math.cos(ts*i+mts*j+sa), (orad-mtl) * Math.sin(ts*i+mts*j+sa)); |
---|
| 701 | ctx.stroke(); |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | } |
---|
| 705 | ctx.restore(); |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | // draw the tick labels |
---|
| 709 | if (this.showTickLabels) { |
---|
| 710 | var elem, l, t, ew, eh, dim, maxdim=0; |
---|
| 711 | var tp = this.tickPadding * (1 - 1/(this.diameter/80+1)); |
---|
| 712 | for (i=0; i<this.ticks.length; i++) { |
---|
| 713 | elem = $('<div class="jqplot-meterGauge-tick" style="position:absolute;">'+this.ticks[i][1]+'</div>'); |
---|
| 714 | this.canvas._elem.after(elem); |
---|
| 715 | ew = elem.outerWidth(true); |
---|
| 716 | eh = elem.outerHeight(true); |
---|
| 717 | l = this._tickPoints[i][0] - ew * (this._tickPoints[i][2]-Math.PI)/Math.PI - tp * Math.cos(this._tickPoints[i][2]); |
---|
| 718 | 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) ; |
---|
| 719 | // t = this._tickPoints[i][1] - eh/2 - eh/2 * Math.sin(this._tickPoints[i][2]) - tp/2 * Math.sin(this._tickPoints[i][2]); |
---|
| 720 | elem.css({left:l, top:t}); |
---|
| 721 | dim = ew*Math.cos(this._tickPoints[i][2]) + eh*Math.sin(Math.PI/2+this._tickPoints[i][2]/2); |
---|
| 722 | maxdim = (dim > maxdim) ? dim : maxdim; |
---|
| 723 | } |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | // draw the gauge label |
---|
| 727 | if (this.label && this.labelPosition == 'inside') { |
---|
| 728 | var l = this._center[0] + this.canvas._offsets.left; |
---|
| 729 | var tp = this.tickPadding * (1 - 1/(this.diameter/80+1)); |
---|
| 730 | 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; |
---|
| 731 | // this._labelElem = $('<div class="jqplot-meterGauge-label" style="position:absolute;">'+this.label+'</div>'); |
---|
| 732 | // this.canvas._elem.after(this._labelElem); |
---|
| 733 | l -= this._labelElem.outerWidth(true)/2; |
---|
| 734 | t -= this._labelElem.outerHeight(true)/2; |
---|
| 735 | this._labelElem.css({left:l, top:t}); |
---|
| 736 | } |
---|
| 737 | |
---|
| 738 | else if (this.label && this.labelPosition == 'bottom') { |
---|
| 739 | var l = this._center[0] + this.canvas._offsets.left - this._labelElem.outerWidth(true)/2; |
---|
| 740 | var t = this._center[1] + this.canvas._offsets.top + this.innerPad + + this.ringWidth + this.padding + this.labelHeightAdjust; |
---|
| 741 | this._labelElem.css({left:l, top:t}); |
---|
| 742 | |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | // draw the intervals |
---|
| 746 | |
---|
| 747 | ctx.save(); |
---|
| 748 | var inner = this.intervalInnerRadius || this.hubRadius * 1.5; |
---|
| 749 | if (this.intervalOuterRadius == null) { |
---|
| 750 | if (this.showTickLabels) { |
---|
| 751 | var outer = (this.tickOuterRadius - this.tickLength - this.tickPadding - this.diameter/8); |
---|
| 752 | } |
---|
| 753 | else { |
---|
| 754 | var outer = (this.tickOuterRadius - this.tickLength - this.diameter/16); |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | else { |
---|
| 758 | var outer = this.intervalOuterRadius; |
---|
| 759 | } |
---|
| 760 | var range = this.max - this.min; |
---|
| 761 | var intrange = this.intervals[this.intervals.length-1] - this.min; |
---|
| 762 | var start, end, span = this.span*Math.PI/180; |
---|
| 763 | for (i=0; i<this.intervals.length; i++) { |
---|
| 764 | start = (i == 0) ? sa : sa + (this.intervals[i-1][0] - this.min)*span/range; |
---|
| 765 | if (start < 0) { |
---|
| 766 | start = 0; |
---|
| 767 | } |
---|
| 768 | end = sa + (this.intervals[i][0] - this.min)*span/range; |
---|
| 769 | if (end < 0) { |
---|
| 770 | end = 0; |
---|
| 771 | } |
---|
| 772 | ctx.beginPath(); |
---|
| 773 | ctx.fillStyle = this.intervals[i][2]; |
---|
| 774 | ctx.arc(0, 0, inner, start, end, false); |
---|
| 775 | ctx.lineTo(outer*Math.cos(end), outer*Math.sin(end)); |
---|
| 776 | ctx.arc(0, 0, outer, end, start, true); |
---|
| 777 | ctx.lineTo(inner*Math.cos(start), inner*Math.sin(start)); |
---|
| 778 | ctx.closePath(); |
---|
| 779 | ctx.fill(); |
---|
| 780 | } |
---|
| 781 | ctx.restore(); |
---|
| 782 | |
---|
| 783 | // draw the needle |
---|
| 784 | var datapoint = this.data[0][1]; |
---|
| 785 | var dataspan = this.max - this.min; |
---|
| 786 | if (this.pegNeedle) { |
---|
| 787 | if (this.data[0][1] > this.max + dataspan*3/this.span) { |
---|
| 788 | datapoint = this.max + dataspan*3/this.span; |
---|
| 789 | } |
---|
| 790 | if (this.data[0][1] < this.min - dataspan*3/this.span) { |
---|
| 791 | datapoint = this.min - dataspan*3/this.span; |
---|
| 792 | } |
---|
| 793 | } |
---|
| 794 | var dataang = (datapoint - this.min)/dataspan * this.span * Math.PI/180 + this.startAngle; |
---|
| 795 | |
---|
| 796 | |
---|
| 797 | ctx.save(); |
---|
| 798 | ctx.beginPath(); |
---|
| 799 | ctx.fillStyle = this.ringColor; |
---|
| 800 | ctx.strokeStyle = this.ringColor; |
---|
| 801 | this.needleLength = (this.tickOuterRadius - this.tickLength) * 0.85; |
---|
| 802 | this.needleThickness = (this.needleThickness < 2) ? 2 : this.needleThickness; |
---|
| 803 | var endwidth = this.needleThickness * 0.4; |
---|
| 804 | |
---|
| 805 | |
---|
| 806 | var dl = this.needleLength/10; |
---|
| 807 | var dt = (this.needleThickness - endwidth)/10; |
---|
| 808 | var templ; |
---|
| 809 | for (var i=0; i<10; i++) { |
---|
| 810 | templ = this.needleThickness - i*dt; |
---|
| 811 | ctx.moveTo(dl*i*Math.cos(dataang), dl*i*Math.sin(dataang)); |
---|
| 812 | ctx.lineWidth = templ; |
---|
| 813 | ctx.lineTo(dl*(i+1)*Math.cos(dataang), dl*(i+1)*Math.sin(dataang)); |
---|
| 814 | ctx.stroke(); |
---|
| 815 | } |
---|
| 816 | |
---|
| 817 | ctx.restore(); |
---|
| 818 | } |
---|
| 819 | else { |
---|
| 820 | this._center = [(cw - trans * offx)/2 + trans * offx, (ch - trans*offy)/2 + trans * offy]; |
---|
| 821 | } |
---|
| 822 | }; |
---|
| 823 | |
---|
| 824 | $.jqplot.MeterGaugeAxisRenderer = function() { |
---|
| 825 | $.jqplot.LinearAxisRenderer.call(this); |
---|
| 826 | }; |
---|
| 827 | |
---|
| 828 | $.jqplot.MeterGaugeAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer(); |
---|
| 829 | $.jqplot.MeterGaugeAxisRenderer.prototype.constructor = $.jqplot.MeterGaugeAxisRenderer; |
---|
| 830 | |
---|
| 831 | |
---|
| 832 | // There are no traditional axes on a gauge chart. We just need to provide |
---|
| 833 | // dummy objects with properties so the plot will render. |
---|
| 834 | // called with scope of axis object. |
---|
| 835 | $.jqplot.MeterGaugeAxisRenderer.prototype.init = function(options){ |
---|
| 836 | // |
---|
| 837 | this.tickRenderer = $.jqplot.MeterGaugeTickRenderer; |
---|
| 838 | $.extend(true, this, options); |
---|
| 839 | // I don't think I'm going to need _dataBounds here. |
---|
| 840 | // have to go Axis scaling in a way to fit chart onto plot area |
---|
| 841 | // and provide u2p and p2u functionality for mouse cursor, etc. |
---|
| 842 | // for convienence set _dataBounds to 0 and 100 and |
---|
| 843 | // set min/max to 0 and 100. |
---|
| 844 | this._dataBounds = {min:0, max:100}; |
---|
| 845 | this.min = 0; |
---|
| 846 | this.max = 100; |
---|
| 847 | this.showTicks = false; |
---|
| 848 | this.ticks = []; |
---|
| 849 | this.showMark = false; |
---|
| 850 | this.show = false; |
---|
| 851 | }; |
---|
| 852 | |
---|
| 853 | $.jqplot.MeterGaugeLegendRenderer = function(){ |
---|
| 854 | $.jqplot.TableLegendRenderer.call(this); |
---|
| 855 | }; |
---|
| 856 | |
---|
| 857 | $.jqplot.MeterGaugeLegendRenderer.prototype = new $.jqplot.TableLegendRenderer(); |
---|
| 858 | $.jqplot.MeterGaugeLegendRenderer.prototype.constructor = $.jqplot.MeterGaugeLegendRenderer; |
---|
| 859 | |
---|
| 860 | /** |
---|
| 861 | * Class: $.jqplot.MeterGaugeLegendRenderer |
---|
| 862 | *Meter gauges don't typically have a legend, this overrides the default legend renderer. |
---|
| 863 | */ |
---|
| 864 | $.jqplot.MeterGaugeLegendRenderer.prototype.init = function(options) { |
---|
| 865 | // Maximum number of rows in the legend. 0 or null for unlimited. |
---|
| 866 | this.numberRows = null; |
---|
| 867 | // Maximum number of columns in the legend. 0 or null for unlimited. |
---|
| 868 | this.numberColumns = null; |
---|
| 869 | $.extend(true, this, options); |
---|
| 870 | }; |
---|
| 871 | |
---|
| 872 | // called with context of legend |
---|
| 873 | $.jqplot.MeterGaugeLegendRenderer.prototype.draw = function() { |
---|
| 874 | if (this.show) { |
---|
| 875 | var series = this._series; |
---|
| 876 | var ss = 'position:absolute;'; |
---|
| 877 | ss += (this.background) ? 'background:'+this.background+';' : ''; |
---|
| 878 | ss += (this.border) ? 'border:'+this.border+';' : ''; |
---|
| 879 | ss += (this.fontSize) ? 'font-size:'+this.fontSize+';' : ''; |
---|
| 880 | ss += (this.fontFamily) ? 'font-family:'+this.fontFamily+';' : ''; |
---|
| 881 | ss += (this.textColor) ? 'color:'+this.textColor+';' : ''; |
---|
| 882 | ss += (this.marginTop != null) ? 'margin-top:'+this.marginTop+';' : ''; |
---|
| 883 | ss += (this.marginBottom != null) ? 'margin-bottom:'+this.marginBottom+';' : ''; |
---|
| 884 | ss += (this.marginLeft != null) ? 'margin-left:'+this.marginLeft+';' : ''; |
---|
| 885 | ss += (this.marginRight != null) ? 'margin-right:'+this.marginRight+';' : ''; |
---|
| 886 | this._elem = $('<table class="jqplot-table-legend" style="'+ss+'"></table>'); |
---|
| 887 | // MeterGauge charts legends don't go by number of series, but by number of data points |
---|
| 888 | // in the series. Refactor things here for that. |
---|
| 889 | |
---|
| 890 | var pad = false, |
---|
| 891 | reverse = false, |
---|
| 892 | nr, nc; |
---|
| 893 | var s = series[0]; |
---|
| 894 | |
---|
| 895 | if (s.show) { |
---|
| 896 | var pd = s.data; |
---|
| 897 | if (this.numberRows) { |
---|
| 898 | nr = this.numberRows; |
---|
| 899 | if (!this.numberColumns){ |
---|
| 900 | nc = Math.ceil(pd.length/nr); |
---|
| 901 | } |
---|
| 902 | else{ |
---|
| 903 | nc = this.numberColumns; |
---|
| 904 | } |
---|
| 905 | } |
---|
| 906 | else if (this.numberColumns) { |
---|
| 907 | nc = this.numberColumns; |
---|
| 908 | nr = Math.ceil(pd.length/this.numberColumns); |
---|
| 909 | } |
---|
| 910 | else { |
---|
| 911 | nr = pd.length; |
---|
| 912 | nc = 1; |
---|
| 913 | } |
---|
| 914 | |
---|
| 915 | var i, j, tr, td1, td2, lt, rs, color; |
---|
| 916 | var idx = 0; |
---|
| 917 | |
---|
| 918 | for (i=0; i<nr; i++) { |
---|
| 919 | if (reverse){ |
---|
| 920 | tr = $('<tr class="jqplot-table-legend"></tr>').prependTo(this._elem); |
---|
| 921 | } |
---|
| 922 | else{ |
---|
| 923 | tr = $('<tr class="jqplot-table-legend"></tr>').appendTo(this._elem); |
---|
| 924 | } |
---|
| 925 | for (j=0; j<nc; j++) { |
---|
| 926 | if (idx < pd.length){ |
---|
| 927 | // debugger |
---|
| 928 | lt = this.labels[idx] || pd[idx][0].toString(); |
---|
| 929 | color = s.color; |
---|
| 930 | if (!reverse){ |
---|
| 931 | if (i>0){ |
---|
| 932 | pad = true; |
---|
| 933 | } |
---|
| 934 | else{ |
---|
| 935 | pad = false; |
---|
| 936 | } |
---|
| 937 | } |
---|
| 938 | else{ |
---|
| 939 | if (i == nr -1){ |
---|
| 940 | pad = false; |
---|
| 941 | } |
---|
| 942 | else{ |
---|
| 943 | pad = true; |
---|
| 944 | } |
---|
| 945 | } |
---|
| 946 | rs = (pad) ? this.rowSpacing : '0'; |
---|
| 947 | |
---|
| 948 | td1 = $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+ |
---|
| 949 | '<div><div class="jqplot-table-legend-swatch" style="border-color:'+color+';"></div>'+ |
---|
| 950 | '</div></td>'); |
---|
| 951 | td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>'); |
---|
| 952 | if (this.escapeHtml){ |
---|
| 953 | td2.text(lt); |
---|
| 954 | } |
---|
| 955 | else { |
---|
| 956 | td2.html(lt); |
---|
| 957 | } |
---|
| 958 | if (reverse) { |
---|
| 959 | td2.prependTo(tr); |
---|
| 960 | td1.prependTo(tr); |
---|
| 961 | } |
---|
| 962 | else { |
---|
| 963 | td1.appendTo(tr); |
---|
| 964 | td2.appendTo(tr); |
---|
| 965 | } |
---|
| 966 | pad = true; |
---|
| 967 | } |
---|
| 968 | idx++; |
---|
| 969 | } |
---|
| 970 | } |
---|
| 971 | } |
---|
| 972 | } |
---|
| 973 | return this._elem; |
---|
| 974 | }; |
---|
| 975 | |
---|
| 976 | |
---|
| 977 | // setup default renderers for axes and legend so user doesn't have to |
---|
| 978 | // called with scope of plot |
---|
| 979 | function preInit(target, data, options) { |
---|
| 980 | // debugger |
---|
| 981 | options = options || {}; |
---|
| 982 | options.axesDefaults = options.axesDefaults || {}; |
---|
| 983 | options.legend = options.legend || {}; |
---|
| 984 | options.seriesDefaults = options.seriesDefaults || {}; |
---|
| 985 | options.grid = options.grid || {}; |
---|
| 986 | options.gridPadding = options.gridPadding || {}; |
---|
| 987 | |
---|
| 988 | // only set these if there is a gauge series |
---|
| 989 | var setopts = false; |
---|
| 990 | if (options.seriesDefaults.renderer == $.jqplot.MeterGaugeRenderer) { |
---|
| 991 | setopts = true; |
---|
| 992 | } |
---|
| 993 | else if (options.series) { |
---|
| 994 | for (var i=0; i < options.series.length; i++) { |
---|
| 995 | if (options.series[i].renderer == $.jqplot.MeterGaugeRenderer) { |
---|
| 996 | setopts = true; |
---|
| 997 | } |
---|
| 998 | } |
---|
| 999 | } |
---|
| 1000 | |
---|
| 1001 | if (setopts) { |
---|
| 1002 | options.axesDefaults.renderer = $.jqplot.MeterGaugeAxisRenderer; |
---|
| 1003 | options.legend.renderer = $.jqplot.MeterGaugeLegendRenderer; |
---|
| 1004 | options.legend.preDraw = true; |
---|
| 1005 | options.grid.background = options.grid.background || 'white'; |
---|
| 1006 | options.grid.drawGridlines = false; |
---|
| 1007 | options.grid.borderWidth = (options.grid.borderWidth != null) ? options.grid.borderWidth : 0; |
---|
| 1008 | options.grid.shadow = (options.grid.shadow != null) ? options.grid.shadow : false; |
---|
| 1009 | options.gridPadding.top = (options.gridPadding.top != null) ? options.gridPadding.top : 0; |
---|
| 1010 | options.gridPadding.bottom = (options.gridPadding.bottom != null) ? options.gridPadding.bottom : 0; |
---|
| 1011 | options.gridPadding.left = (options.gridPadding.left != null) ? options.gridPadding.left : 0; |
---|
| 1012 | options.gridPadding.right = (options.gridPadding.right != null) ? options.gridPadding.right : 0; |
---|
| 1013 | } |
---|
| 1014 | } |
---|
| 1015 | |
---|
| 1016 | // called with scope of plot |
---|
| 1017 | function postParseOptions(options) { |
---|
| 1018 | // |
---|
| 1019 | } |
---|
| 1020 | |
---|
| 1021 | $.jqplot.preInitHooks.push(preInit); |
---|
| 1022 | $.jqplot.postParseOptionsHooks.push(postParseOptions); |
---|
| 1023 | |
---|
| 1024 | $.jqplot.MeterGaugeTickRenderer = function() { |
---|
| 1025 | $.jqplot.AxisTickRenderer.call(this); |
---|
| 1026 | }; |
---|
| 1027 | |
---|
| 1028 | $.jqplot.MeterGaugeTickRenderer.prototype = new $.jqplot.AxisTickRenderer(); |
---|
| 1029 | $.jqplot.MeterGaugeTickRenderer.prototype.constructor = $.jqplot.MeterGaugeTickRenderer; |
---|
| 1030 | |
---|
| 1031 | })(jQuery); |
---|
| 1032 | |
---|
| 1033 | |
---|