// o---------------------------------------------------------------------------o // | This file is part of the RGraph package - you can learn more at: | // | | // | https://www.rgraph.net | // | | // | RGraph is dual-licensed under the Open Source GPL license. This means | // | that it's free to use for any purpose. The GPL license does have | // | consequences on the license of the software that you include it in, | // | however. If this is not desirable, then there's an inexpensive commercial | // | license option available. See the RGraph website for more details. | // o---------------------------------------------------------------------------o RGraph = window.RGraph || {isrgraph:true,isRGraph:true,rgraph:true}; // // The bar chart constructor // RGraph.Meter = function (conf) { var id = conf.id var canvas = document.getElementById(id); var min = conf.min; var max = conf.max; var value = conf.value; // id, min, max, value // 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 = 'meter'; this.min = RGraph.stringsToNumbers(min); this.max = RGraph.stringsToNumbers(max); this.value = RGraph.stringsToNumbers(value); this.centerx = null; this.centery = null; this.radius = null; this.isRGraph = true; this.isrgraph = true; this.rgraph = true; this.currentValue = null; this.uid = RGraph.createUID(); this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.createUID(); this.colorsParsed = false; this.coordsText = []; this.original_colors = []; this.firstDraw = true; // After the first draw this will be false this.stopAnimationRequested = false;// Used to control the animations // Various config type stuff this.properties = { backgroundImageUrl: null, backgroundImageOffsetx: 0, backgroundImageOffsety: 0, backgroundImageStretch: true, backgroundColor: 'white', marginLeft: 35, marginRight: 35, marginTop: 35, marginBottom: 35, linewidth: 1, linewidthSegments: 0, colorsStroke: null, border: true, borderColor: 'black', textFont: 'Arial, Verdana, sans-serif', textSize: 12, textColor: 'black', textBold: false, textItalic: false, textValign: 'center', textAccessible: false, textAccessibleOverflow: 'visible', textAccessiblePointerevents: false, text: null, labels: true, labelsCount: 10, labelsSpecific: null, labelsRadiusOffset: 0, labelsOffsetRadius: null, labelsFont: null, labelsSize: null, labelsColor: null, labelsBold: null, labelsItalic: null, labelsValue: false, labelsValueFont: null, labelsValueSize: null, labelsValueBold: null, labelsValueItalic: null, labelsValueColor: null, labelsValueDecimals: 0, labelsValuePoint: '.', labelsValueThousand: ',', labelsValueUnitsPre: '', labelsValueUnitsPost: '', labelsValueBackground: true, labelsValueBackgroundFill: 'rgba(255,255,255,0.75)', labelsValueBackgroundStroke: 'transparent', labelsValueSpecific: null, labelsValueAccessible: false, labelsValueOffsetx: 0, labelsValueOffsety: 0, title: '', titleColor: null, titleBold: true, titleFont: null, titleItalic: null, titleSize: null, titleX: null, titleY: null, titleHalign: null, titleValign: null, titleOffsetx: 0, titleOffsety: 0, titleSubtitle: '', titleSubtitleSize: null, titleSubtitleColor: '#aaa', titleSubtitleFont: null, titleSubtitleBold: null, titleSubtitleItalic: null, titleSubtitleOffsetx: 0, titleSubtitleOffsety: 0, colorsGreenStart: ((this.max - this.min) * 0.35) + this.min, colorsGreenEnd: this.max, colorsGreenColor: '#00cc0088', colorsYellowStart: ((this.max - this.min) * 0.1) + this.min, colorsYellowEnd: ((this.max - this.min) * 0.35) + this.min, colorsYellowColor: '#ffff0088', colorsRedStart: this.min, colorsRedEnd: ((this.max - this.min) * 0.1) + this.min, colorsRedColor: '#ff000088', colorsRanges: null, contextmenu: null, annotatable: false, annotatableColor: 'black', shadow: false, shadowColor: 'rgba(0,0,0,0.5)', shadowBlur: 3, shadowOffsetx: 3, shadowOffsety: 3, tickmarksSmallCount: 100, tickmarksSmallColor: '#bbb', tickmarksLargeCount: 10, tickmarksLargeColor: 'black', scaleUnitsPre: '', scaleUnitsPost: '', scaleDecimals: 0, scalePoint: '.', scaleThousand: ',', radius: null, centerx: null, centery: null, segmentsRadiusStart: 0, needleRadius: null, needleType: 'normal', needleTail: false, needleHead: true, needleHeadLength: 30, needleHeadWidth: 0.088, needleColor: 'black', needleLinewidth: null, needleImageUrl: null, needleImageOffsetx: 0, needleImageOffsety: 0, adjustable: false, anglesStart: RGraph.PI, anglesEnd: RGraph.TWOPI, centerpinStroke: 'black', centerpinFill: 'white', clearto: 'rgba(0,0,0,0)', events: {}, clip: null, responsive: null, scale: true, scaleFactor: 2, antialiasTranslate: false, style: [] }; // // These are the properties that get scaled up if the // scale option is enabled. // this.properties_scale = [ 'backgroundImageOffsetx', 'backgroundImageOffsety', 'marginLeft', 'marginRight', 'marginTop', 'marginBottom', 'linewidth', 'linewidthSegments', 'textSize', 'labelsSize', 'labelsValueSize', 'labelsValueOffsetx', 'labelsValueOffsety', 'titleSize', 'titleX', 'titleY', 'titleOffsetx', 'titleOffsety', 'titleSubtitleSize', 'titleSubtitleOffsetx', 'titleSubtitleOffsety', 'annotatableLinewidth', 'shadowBlur', 'shadowOffsetx', 'shadowOffsety', 'radius', 'centerx', 'centery', 'segmentsRadiusStart', 'needleRadius', 'needleHeadLength', 'needleImageOffsetx', 'needleImageOffsety' ]; // // Add the reverse look-up table for property names // so that property names can be specified in any case. // this.properties_lowercase_map = []; for (var i in this.properties) { if (typeof i === 'string') { this.properties_lowercase_map[i.toLowerCase()] = i; } } // Check for support if (!this.canvas) { alert('[METER] No canvas support'); return; } // Easy access to properties and the path function var properties = this.properties; this.path = RGraph.pathObjectFunction; // // "Decorate" the object with the generic effects if the effects library has been included // if (RGraph.Effects && typeof RGraph.Effects.decorate === 'function') { RGraph.Effects.decorate(this); } // Add the responsive method. This method resides in the common file. this.responsive = RGraph.responsive; // // A setter // this.set = function (name) { var value = typeof arguments[1] === 'undefined' ? null : arguments[1]; // Go through all of the properties and make sure // that they're using the correct capitalisation if (typeof name === 'string') { name = this.properties_lowercase_map[name.toLowerCase()] || name; } // BC for the labeslsValueText properties if (name.substr(0, 15) === 'labelsValueText') { name = name.replace(/^labelsValueText/, 'labelsValue'); } if ( name === 'colorsRedColor' || name === 'colorsYellowColor' || name === 'colorsGreenColor' || name === 'colorsRanges' ) { this.colorsParsed = false; } // 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') { for (i in arguments[0]) { if (typeof i === 'string') { this.set(i, arguments[0][i]); } } return this; } properties[name] = value; return this; }; // // A getter // // @param name string The name of the property to get // this.get = function (name) { // Go through all of the properties and make sure // that they're using the correct capitalisation name = this.properties_lowercase_map[name.toLowerCase()] || name; return properties[name]; }; // // The function you call to draw the bar chart // this.draw = function () { // MUST be the first thing that's done - but only // once!! RGraph.runOnce(`scale-up-the-canvas-once-in-the-draw-function-${this.id}-${this.uid}`, () => { // Note that we're in an arrow function so the // 'this' variable is OK to be used and refers // to the RGraph Line chart object. RGraph.scale(this); }); // // Fire the onbeforedraw event // RGraph.fireCustomEvent(this, 'onbeforedraw'); // // Add any CSS that has been specified to the document. // This is general CSS and does not necessarily have to // pertain to the canvas tag. It only gets added once // to the document no matter how many times this draw // function is called. // // Add the CSS to a