// version: 2014-08-16 /** * o--------------------------------------------------------------------------------o * | This file is part of the RGraph package - you can learn more at: | * | | * | http://www.rgraph.net | * | | * | This package is licensed under the Creative Commons BY-NC license. That means | * | that for non-commercial purposes it's free to use and for business use there's | * | a 99 GBP per-company fee to pay. You can read the full license here: | * | | * | http://www.rgraph.net/license | * o--------------------------------------------------------------------------------o */ RGraph = window.RGraph || {isRGraph: true}; /** * The constructor * * @param object id The canvas tag ID * @param array min The minimum value * @param array max The maximum value * @param array value The indicated value */ RGraph.CornerGauge = function (conf) { /** * Allow for object config style */ if ( typeof conf === 'object' && typeof conf.min === 'number' && typeof conf.max === 'number' && typeof conf.value !== 'undefined' && typeof conf.id === 'string') { var id = conf.id var canvas = document.getElementById(id); var min = conf.min; var max = conf.max; var value = conf.value; var parseConfObjectForOptions = true; // Set this so the config is parsed (at the end of the constructor) } else { var id = conf; var canvas = document.getElementById(id); var min = arguments[1]; var max = arguments[2]; var value = arguments[3]; } // Get the canvas and context objects this.id = id; this.canvas = canvas; this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null; this.canvas.__object__ = this; this.type = 'cornergauge'; this.min = min; this.max = max; this.value = RGraph.stringsToNumbers(value); this.angles = {}; this.angles.needle = []; this.centerpin = {}; this.isRGraph = true; this.currentValue = null; this.uid = RGraph.CreateUID(); this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID(); this.coordsText = []; this.original_colors = []; this.firstDraw = true; // After the first draw this will be false /** * Range checking */ if (typeof(this.value) == 'object') { for (var i=0; i this.max) this.value[i] = max; if (this.value[i] < this.min) this.value[i] = min; } } else { if (this.value > this.max) this.value = max; if (this.value < this.min) this.value = min; } /** * Compatibility with older browsers */ //RGraph.OldBrowserCompat(this.context); // Various config type stuff this.properties = { 'chart.centerx': null, 'chart.centery': null, 'chart.radius': null, 'chart.gutter.left': 25, 'chart.gutter.right': 25, 'chart.gutter.top': 25, 'chart.gutter.bottom': 25, 'chart.strokestyle': 'black', 'chart.linewidth': 2, 'chart.title': '', 'chart.title.vpos': 0.5, 'chart.title.size': null, 'chart.title.x': null, 'chart.title.y': null, 'chart.title.bold': true, 'chart.text.font': 'Arial', 'chart.text.color': '#666', 'chart.text.size': 10, 'chart.background.gradient.color1': '#ddd', 'chart.background.gradient.color2': 'white', 'chart.shadow': true, 'chart.shadow.color': 'gray', 'chart.shadow.offsetx': 0, 'chart.shadow.offsety': 0, 'chart.shadow.blur': 15, 'chart.scale.decimals': 0, 'chart.scale.point': '.', 'chart.scale.thousand': ',', 'chart.units.pre': '', 'chart.units.post': '', 'chart.resizable': false, 'chart.chart.resize.handle.background': null, 'chart.adjustable': false, 'chart.annotatable': false, 'chart.annotate.color': 'black', 'chart.colors.ranges': null, 'chart.red.start': min + (0.9 * (this.max - min)), 'chart.green.end': min + (0.7 * (this.max - min)), 'chart.red.color': 'red', 'chart.yellow.color': 'yellow', 'chart.green.color': '#0f0', 'chart.value.text': true, 'chart.value.text.units.pre': '', 'chart.value.text.units.post': '', 'chart.value.text.boxed': true, 'chart.value.text.font': 'Arial', 'chart.value.text.size': 18, 'chart.value.text.bold': false, 'chart.value.text.decimals': 0, 'chart.centerpin.stroke': 'rgba(0,0,0,0)', 'chart.centerpin.fill': null, // Set in the DrawCenterpin function 'chart.centerpin.color': 'blue', 'chart.needle.colors': ['#ccc', '#D5604D', 'red', 'green', 'yellow'], 'chart.zoom.factor': 1.5, 'chart.zoom.fade.in': true, 'chart.zoom.fade.out': true, 'chart.zoom.hdir': 'right', 'chart.zoom.vdir': 'down', 'chart.zoom.frames': 25, 'chart.zoom.delay': 16.666, 'chart.zoom.shadow': true, 'chart.zoom.background': true } /* * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen * done already */ if (!this.canvas.__rgraph_aa_translated__) { this.context.translate(0.5,0.5); this.canvas.__rgraph_aa_translated__ = true; } // Short variable names var RG = RGraph; var ca = this.canvas; var co = ca.getContext('2d'); var prop = this.properties; var jq = jQuery; var pa = RG.Path; var win = window; var doc = document; var ma = Math; /** * An all encompassing accessor * * @param string name The name of the property * @param mixed value The value of the property */ this.set = this.Set = function (name, value) { /** * the number of arguments is only one and it's an * object - parse it for configuration data and return. */ if (arguments.length === 1 && typeof arguments[0] === 'object') { RG.parseObjectStyleConfig(this, arguments[0]); return this; } name = name.toLowerCase(); /** * This should be done first - prepend the property name with "chart." if necessary */ if (name.substr(0,6) != 'chart.') { name = 'chart.' + name; } prop[name] = value; return this; }; /** * An all encompassing accessor * * @param string name The name of the property */ this.get = this.Get = function (name) { /** * This should be done first - prepend the property name with "chart." if necessary */ if (name.substr(0,6) != 'chart.') { name = 'chart.' + name; } return prop[name]; }; /** * The function you call to draw the line chart */ this.draw = this.Draw = function () { /** * Fire the onbeforedraw event */ RG.FireCustomEvent(this, 'onbeforedraw'); /** * Store the value (for animation primarily */ this.currentValue = this.value; if (typeof this.gutterLeft == 'undefined') { this.gutterLeft = prop['chart.gutter.left']; this.gutterRight = prop['chart.gutter.right']; this.gutterTop = prop['chart.gutter.top']; this.gutterBottom = prop['chart.gutter.bottom']; } /** * Work out the radius first */ this.radius = Math.min( (ca.width - this.gutterLeft - this.gutterRight), (ca.height - this.gutterTop - this.gutterBottom) ); if (typeof(prop['chart.radius']) == 'number') this.radius = prop['chart.radius']; /** * Now use the radius in working out the centerX/Y */ this.centerx = (ca.width / 2) - (this.radius / 2) + Math.max(30, this.radius * 0.1); this.centery = (ca.height / 2) + (this.radius / 2) - (this.radius * 0.1); /** * Stop this growing uncontrollably */ this.coordsText = []; if (typeof prop['chart.centerx'] === 'number') this.centerx = prop['chart.centerx']; if (typeof prop['chart.centery'] === 'number') this.centery = prop['chart.centery']; /** * Parse the colors for gradients. Its down here so that the center X/Y can be used */ if (!this.colorsParsed) { this.parseColors(); // Don't want to do this again this.colorsParsed = true; } /** * Start with the background */ this.DrawBackGround(); /** * Draw the tickmarks */ this.DrawTickmarks(); /** * Draw the color bands */ this.DrawColorBands(); /** * Draw the label/value in text */ this.DrawLabel(); /** * Start with the labels/scale */ this.DrawLabels(); /** * Draw the needle(s) */ if (typeof this.value === 'object') { for (var i=0,len=this.value.length; i RG.TWOPI && angle < (RG.PI + RG.HALFPI)) { return null; } var value = ((angle - (RG.PI + RG.HALFPI)) / (RG.TWOPI - (RG.PI + RG.HALFPI))) * (this.max - this.min); value = value + this.min; if (value < this.min) { value = this.min } if (value > this.max) { value = this.max } // Special case for this chart if (mouseX > this.centerx && mouseY > this.centery) { value = this.max; } return value; }; /** * The getObjectByXY() worker method. Don't call this call: * * RGraph.ObjectRegistry.getObjectByXY(e) * * @param object e The event object */ this.getObjectByXY = function (e) { var mouseXY = RGraph.getMouseXY(e); if ( mouseXY[0] > (this.centerx - 5) && mouseXY[0] < (this.centerx + this.radius) && mouseXY[1] > (this.centery - this.radius) && mouseXY[1] < (this.centery + 5) && RG.getHypLength(this.centerx, this.centery, mouseXY[0], mouseXY[1]) <= this.radius ) { return this; } }; /** * This method handles the adjusting calculation for when the mouse is moved * * @param object e The event object */ this.adjusting_mousemove = this.Adjusting_mousemove = function (e) { /** * Handle adjusting for the Bar */ if (prop['chart.adjustable'] && RG.Registry.Get('chart.adjusting') && RG.Registry.Get('chart.adjusting').uid == this.uid) { this.value = this.getValue(e); RG.Clear(ca); RG.RedrawCanvas(ca); RG.FireCustomEvent(this, 'onadjust'); } }; /** * This method returns the appropriate angle for a value * * @param number value The value to get the angle for */ this.getAngle = function (value) { if (value < this.min || value > this.max) { return null; } var angle = ((value - this.min) / (this.max - this.min)) * RG.HALFPI angle += (RG.PI + RG.HALFPI); return angle; }; /** * This allows for easy specification of gradients */ this.parseColors = function () { // Save the original colors so that they can be restored when the canvas is reset if (this.original_colors.length === 0) { this.original_colors['chart.colors.ranges'] = RG.array_clone(prop['chart.colors.ranges']); this.original_colors['chart.green.color'] = RG.array_clone(prop['chart.green.color']); this.original_colors['chart.yellow.color'] = RG.array_clone(prop['chart.yellow.color']); this.original_colors['chart.red.color'] = RG.array_clone(prop['chart.red.color']); } if (!RG.is_null(prop['chart.colors.ranges'])) { for (var i=0; i obj.max) obj.value = obj.max; if (obj.value < obj.min) obj.value = obj.min; RGraph.Clear(obj.canvas); RGraph.RedrawCanvas(obj.canvas); if (frame < 30) { RGraph.Effects.updateCanvas(iterator); } else if (typeof callback === 'function') { callback(obj); } }; iterator(); // Multiple pointers } else { if (obj.currentValue == null) { obj.currentValue = []; for (var i=0,len=obj.value.length; i max) obj.value[i] = max; if (obj.value[i] < min) obj.value[i] = min; RG.clear(obj.canvas); RG.redrawCanvas(obj.canvas); } if (frame < 30) { RG.Effects.updateCanvas(iterator); } else if (typeof callback === 'function') { callback(obj); } }; iterator(); } return this; } /** * Register the object */ RG.Register(this); /** * This is the 'end' of the constructor so if the first argument * contains configuration dsta - handle that. */ if (parseConfObjectForOptions) { RG.parseObjectStyleConfig(this, conf.options); } };