report-resources.jqplot.1.0.8.plugins.jqplot.meterGaugeRenderer.js Maven / Gradle / Ivy
/**
* jqPlot
* Pure JavaScript plotting plugin using jQuery
*
* Version: 1.0.8
* Revision: 1250
*
* Copyright (c) 2009-2013 Chris Leonello
* jqPlot is currently available for use in all personal or commercial projects
* under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
* version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
* choose the license that best suits your project and use it accordingly.
*
* Although not required, the author would appreciate an email letting him
* know of any substantial use of jqPlot. You can reach the author at:
* chris at jqplot dot com or see http://www.jqplot.com/info.php .
*
* If you are feeling kind and generous, consider supporting the project by
* making a donation at: http://www.jqplot.com/donate.php .
*
* sprintf functions contained in jqplot.sprintf.js by Ash Searle:
*
* version 2007.04.27
* author Ash Searle
* http://hexmen.com/blog/2007/03/printf-sprintf/
* http://hexmen.com/js/sprintf.js
* The author (Ash Searle) has placed this code in the public domain:
* "This code is unrestricted: you are free to use it however you like."
*
*/
(function($) {
/**
* Class: $.jqplot.MeterGaugeRenderer
* Plugin renderer to draw a meter gauge chart.
*
* Data consists of a single series with 1 data point to position the gauge needle.
*
* To use this renderer, you need to include the
* meter gauge renderer plugin, for example:
*
* >
*
* Properties described here are passed into the $.jqplot function
* as options on the series renderer. For example:
*
* > plot0 = $.jqplot('chart0',[[18]],{
* > title: 'Network Speed',
* > seriesDefaults: {
* > renderer: $.jqplot.MeterGaugeRenderer,
* > rendererOptions: {
* > label: 'MB/s'
* > }
* > }
* > });
*
* A meterGauge plot does not support events.
*/
$.jqplot.MeterGaugeRenderer = function(){
$.jqplot.LineRenderer.call(this);
};
$.jqplot.MeterGaugeRenderer.prototype = new $.jqplot.LineRenderer();
$.jqplot.MeterGaugeRenderer.prototype.constructor = $.jqplot.MeterGaugeRenderer;
// called with scope of a series
$.jqplot.MeterGaugeRenderer.prototype.init = function(options) {
// Group: Properties
//
// prop: diameter
// Outer diameter of the meterGauge, auto computed by default
this.diameter = null;
// prop: padding
// padding between the meterGauge and plot edges, auto
// calculated by default.
this.padding = null;
// prop: shadowOffset
// offset of the shadow from the gauge ring and offset of
// each succesive stroke of the shadow from the last.
this.shadowOffset = 2;
// prop: shadowAlpha
// transparency of the shadow (0 = transparent, 1 = opaque)
this.shadowAlpha = 0.07;
// prop: shadowDepth
// number of strokes to apply to the shadow,
// each stroke offset shadowOffset from the last.
this.shadowDepth = 4;
// prop: background
// background color of the inside of the gauge.
this.background = "#efefef";
// prop: ringColor
// color of the outer ring, hub, and needle of the gauge.
this.ringColor = "#BBC6D0";
// needle color not implemented yet.
this.needleColor = "#C3D3E5";
// prop: tickColor
// color of the tick marks around the gauge.
this.tickColor = "#989898";
// prop: ringWidth
// width of the ring around the gauge. Auto computed by default.
this.ringWidth = null;
// prop: min
// Minimum value on the gauge. Auto computed by default
this.min;
// prop: max
// Maximum value on the gauge. Auto computed by default
this.max;
// prop: ticks
// Array of tick values. Auto computed by default.
this.ticks = [];
// prop: showTicks
// true to show ticks around gauge.
this.showTicks = true;
// prop: showTickLabels
// true to show tick labels next to ticks.
this.showTickLabels = true;
// prop: label
// A gauge label like 'kph' or 'Volts'
this.label = null;
// prop: labelHeightAdjust
// Number of Pixels to offset the label up (-) or down (+) from its default position.
this.labelHeightAdjust = 0;
// prop: labelPosition
// Where to position the label, either 'inside' or 'bottom'.
this.labelPosition = 'inside';
// prop: intervals
// Array of ranges to be drawn around the gauge.
// Array of form:
// > [value1, value2, ...]
// indicating the values for the first, second, ... intervals.
this.intervals = [];
// prop: intervalColors
// Array of colors to use for the intervals.
this.intervalColors = [ "#4bb2c5", "#EAA228", "#c5b47f", "#579575", "#839557", "#958c12", "#953579", "#4b5de4", "#d8b83f", "#ff5800", "#0085cc", "#c747a3", "#cddf54", "#FBD178", "#26B4E3", "#bd70c7"];
// prop: intervalInnerRadius
// Radius of the inner circle of the interval ring.
this.intervalInnerRadius = null;
// prop: intervalOuterRadius
// Radius of the outer circle of the interval ring.
this.intervalOuterRadius = null;
this.tickRenderer = $.jqplot.MeterGaugeTickRenderer;
// ticks spaced every 1, 2, 2.5, 5, 10, 20, .1, .2, .25, .5, etc.
this.tickPositions = [1, 2, 2.5, 5, 10];
// prop: tickSpacing
// Degrees between ticks. This is a target number, if
// incompatible span and ticks are supplied, a suitable
// spacing close to this value will be computed.
this.tickSpacing = 30;
this.numberMinorTicks = null;
// prop: hubRadius
// Radius of the hub at the bottom center of gauge which the needle attaches to.
// Auto computed by default
this.hubRadius = null;
// prop: tickPadding
// padding of the tick marks to the outer ring and the tick labels to marks.
// Auto computed by default.
this.tickPadding = null;
// prop: needleThickness
// Maximum thickness the needle. Auto computed by default.
this.needleThickness = null;
// prop: needlePad
// Padding between needle and inner edge of the ring when the needle is at the min or max gauge value.
this.needlePad = 6;
// prop: pegNeedle
// True will stop needle just below/above the min/max values if data is below/above min/max,
// as if the meter is "pegged".
this.pegNeedle = true;
this._type = 'meterGauge';
$.extend(true, this, options);
this.type = null;
this.numberTicks = null;
this.tickInterval = null;
// span, the sweep (in degrees) from min to max. This gauge is
// a semi-circle.
this.span = 180;
// get rid of this nonsense
// this.innerSpan = this.span;
if (this.type == 'circular') {
this.semiCircular = false;
}
else if (this.type != 'circular') {
this.semiCircular = true;
}
else {
this.semiCircular = (this.span <= 180) ? true : false;
}
this._tickPoints = [];
// reference to label element.
this._labelElem = null;
// start the gauge at the beginning of the span
this.startAngle = (90 + (360 - this.span)/2) * Math.PI/180;
this.endAngle = (90 - (360 - this.span)/2) * Math.PI/180;
this.setmin = !!(this.min == null);
this.setmax = !!(this.max == null);
// if given intervals and is an array of values, create labels and colors.
if (this.intervals.length) {
if (this.intervals[0].length == null || this.intervals.length == 1) {
for (var i=0; i= this.data[0][1]) {
this.max = this.intervals[this.intervals.length-1][0];
this.setmax = false;
}
}
else {
this.setmax = false;
}
}
else {
// no ticks and no intervals supplied, put needle in middle
this.min = (this.min == null) ? 0 : this.min;
this.setmin = false;
if (this.max == null) {
this.max = this.data[0][1] * 1.25;
this.setmax = true;
}
else {
this.setmax = false;
}
}
};
$.jqplot.MeterGaugeRenderer.prototype.setGridData = function(plot) {
// set gridData property. This will hold angle in radians of each data point.
var stack = [];
var td = [];
var sa = this.startAngle;
for (var i=0; i0) {
stack[i] += stack[i-1];
}
}
var fact = Math.PI*2/stack[stack.length - 1];
for (var i=0; i0) {
stack[i] += stack[i-1];
}
}
var fact = Math.PI*2/stack[stack.length - 1];
for (var i=0; i=0; i--) {
temp = interval/(pos[i] * Math.pow(10, fact));
if (temp == 4 || temp == 5) {
return temp - 1;
}
}
return null;
}
// called with scope of series
$.jqplot.MeterGaugeRenderer.prototype.draw = function (ctx, gd, options) {
var i;
var opts = (options != undefined) ? options : {};
// offset and direction of offset due to legend placement
var offx = 0;
var offy = 0;
var trans = 1;
if (options.legendInfo && options.legendInfo.placement == 'inside') {
var li = options.legendInfo;
switch (li.location) {
case 'nw':
offx = li.width + li.xoffset;
break;
case 'w':
offx = li.width + li.xoffset;
break;
case 'sw':
offx = li.width + li.xoffset;
break;
case 'ne':
offx = li.width + li.xoffset;
trans = -1;
break;
case 'e':
offx = li.width + li.xoffset;
trans = -1;
break;
case 'se':
offx = li.width + li.xoffset;
trans = -1;
break;
case 'n':
offy = li.height + li.yoffset;
break;
case 's':
offy = li.height + li.yoffset;
trans = -1;
break;
default:
break;
}
}
// pre-draw so can get its dimensions.
if (this.label) {
this._labelElem = $(''+this.label+'');
this.canvas._elem.after(this._labelElem);
}
var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
var fill = (opts.fill != undefined) ? opts.fill : this.fill;
var cw = ctx.canvas.width;
var ch = ctx.canvas.height;
if (this.padding == null) {
this.padding = Math.round(Math.min(cw, ch)/30);
}
var w = cw - offx - 2 * this.padding;
var h = ch - offy - 2 * this.padding;
if (this.labelPosition == 'bottom' && this.label) {
h -= this._labelElem.outerHeight(true);
}
var mindim = Math.min(w,h);
var d = mindim;
if (!this.diameter) {
if (this.semiCircular) {
if ( w >= 2*h) {
if (!this.ringWidth) {
this.ringWidth = 2*h/35;
}
this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
this.innerPad = this.ringWidth/2 + this.needleThickness/2 + this.needlePad;
this.diameter = 2 * (h - 2*this.innerPad);
}
else {
if (!this.ringWidth) {
this.ringWidth = w/35;
}
this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
this.innerPad = this.ringWidth/2 + this.needleThickness/2 + this.needlePad;
this.diameter = w - 2*this.innerPad - this.ringWidth - this.padding;
}
// center taking into account legend and over draw for gauge bottom below hub.
// this will be center of hub.
this._center = [(cw - trans * offx)/2 + trans * offx, (ch + trans*offy - this.padding - this.ringWidth - this.innerPad)];
}
else {
if (!this.ringWidth) {
this.ringWidth = d/35;
}
this.needleThickness = this.needleThickness || 2+Math.pow(this.ringWidth, 0.8);
this.innerPad = 0;
this.diameter = d - this.ringWidth;
// center in middle of canvas taking into account legend.
// will be center of hub.
this._center = [(cw-trans*offx)/2 + trans * offx, (ch-trans*offy)/2 + trans * offy];
}
if (this._labelElem && this.labelPosition == 'bottom') {
this._center[1] -= this._labelElem.outerHeight(true);
}
}
this._radius = this.diameter/2;
this.tickSpacing = 6000/this.diameter;
if (!this.hubRadius) {
this.hubRadius = this.diameter/18;
}
this.shadowOffset = 0.5 + this.ringWidth/9;
this.shadowWidth = this.ringWidth*1;
this.tickPadding = 3 + Math.pow(this.diameter/20, 0.7);
this.tickOuterRadius = this._radius - this.ringWidth/2 - this.tickPadding;
this.tickLength = (this.showTicks) ? this._radius/13 : 0;
if (this.ticks.length == 0) {
// no ticks, lets make some.
var max = this.max,
min = this.min,
setmax = this.setmax,
setmin = this.setmin,
ti = (max - min) * this.tickSpacing / this.span;
var tf = Math.floor(parseFloat((Math.log(ti)/Math.log(10)).toFixed(11)));
var tp = (ti/Math.pow(10, tf));
(tp > 2 && tp <= 2.5) ? tp = 2.5 : tp = Math.ceil(tp);
var t = this.tickPositions;
var tpindex, nt;
for (i=0; i 0) ? min - min % ti : min - min % ti - ti;
if (!this.forceZero) {
var diff = Math.min(min - tmin, 0.8*ti);
var ntp = Math.floor(diff/t[tpindex]);
if (ntp > 1) {
tmin = tmin + t[tpindex] * (ntp-1);
if (parseInt(tmin, 10) != tmin && parseInt(tmin-t[tpindex], 10) == tmin-t[tpindex]) {
tmin = tmin - t[tpindex];
}
}
}
if (min == tmin) {
min -= ti;
}
else {
// tmin should always be lower than dataMin
if (min - tmin > 0.23*ti) {
min = tmin;
}
else {
min = tmin -ti;
nt += 1;
}
}
nt += 1;
var tmax = min + (nt - 1) * ti;
if (max >= tmax) {
tmax += ti;
nt += 1;
}
// now tmax should always be mroe than dataMax
if (tmax - max < 0.23*ti) {
tmax += ti;
nt += 1;
}
this.max = max = tmax;
this.min = min;
this.tickInterval = ti;
this.numberTicks = nt;
var it;
for (i=0; i= tmax) {
max = tmax + ti;
nt += 1;
}
else {
max = tmax;
}
this.tickInterval = this.tickInterval || ti;
this.numberTicks = this.numberTicks || nt;
var it;
for (i=0; i 1) {
var rstr = String(range);
if (rstr.search(/\./) == -1) {
var pos = rstr.search(/0+$/);
nonSigDigits = (pos > 0) ? rstr.length - pos - 1 : 0;
}
}
sigRange = range/Math.pow(10, nonSigDigits);
for (i=0; i'+this.ticks[i][1]+'
'+this.label+'
');
// this.canvas._elem.after(this._labelElem);
l -= this._labelElem.outerWidth(true)/2;
t -= this._labelElem.outerHeight(true)/2;
this._labelElem.css({left:l, top:t});
}
else if (this.label && this.labelPosition == 'bottom') {
var l = this._center[0] + this.canvas._offsets.left - this._labelElem.outerWidth(true)/2;
var t = this._center[1] + this.canvas._offsets.top + this.innerPad + this.ringWidth + this.padding + this.labelHeightAdjust;
this._labelElem.css({left:l, top:t});
}
// draw the intervals
ctx.save();
var inner = this.intervalInnerRadius || this.hubRadius * 1.5;
if (this.intervalOuterRadius == null) {
if (this.showTickLabels) {
var outer = (this.tickOuterRadius - this.tickLength - this.tickPadding - this.diameter/8);
}
else {
var outer = (this.tickOuterRadius - this.tickLength - this.diameter/16);
}
}
else {
var outer = this.intervalOuterRadius;
}
var range = this.max - this.min;
var intrange = this.intervals[this.intervals.length-1] - this.min;
var start, end, span = this.span*Math.PI/180;
for (i=0; i'+
'