// o---------------------------------------------------------------------------------o // | This file is part of the RGraph package - you can learn more at: | // | | // | https://www.rgraph.net/license.html | // | | // | RGraph is dual-licensed under the Open Source GPL license. That means that it's | // | free to use and there are no restrictions on what you can use RGraph for! | // | If the GPL license does not suit you however, then there's an inexpensive | // | commercial license option available. See the URL above for more details. | // o---------------------------------------------------------------------------------o RGraph = window.RGraph || {isrgraph:true,isRGraph:true,rgraph:true}; // The progress bar constructor RGraph.HProgress = function (conf) { var id = conf.id, canvas = document.getElementById(id), min = conf.min, max = conf.max, value = conf.value; this.id = id; this.canvas = canvas; this.context = this.canvas.getContext('2d'); this.canvas.__object__ = this; this.min = RGraph.stringsToNumbers(min); this.max = RGraph.stringsToNumbers(max); this.value = RGraph.stringsToNumbers(value); this.type = 'hprogress'; 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.coords = []; this.coordsText = []; this.original_colors = []; this.firstDraw = true; // After the first draw this will be false this.stopAnimationRequested = false;// Used to control the animations this.properties = { colors: ['#0c0','red','green','yellow','pink','cyan','black','white','gray'], colorsStrokeInner: '#999', colorsStrokeOuter: '#999', tickmarksColor: '#999', tickmarksInnerCount: 0, tickmarksOuterCount: 0, backgroundColor: 'Gradient(#ccc:#eee:#efefef)', //backgroundColor: '#eee', marginLeft: 35, marginRight: 35, marginTop: 35, marginBottom: 35, shadow: false, shadowColor: 'rgba(0,0,0,0.5)', shadowBlur: 3, shadowOffsetx: 3, shadowOffsety: 3, title: '', titleX: null, titleY: null, titleHalign: null, titleValign: null, titleFont: null, titleSize: null, titleColor: null, titleBold: true, titleItalic: null, titleOffsetx: 0, titleOffsety: 0, titleSubtitle: '', titleSubtitleSize: null, titleSubtitleColor: '#aaa', titleSubtitleFont: null, titleSubtitleBold: null, titleSubtitleItalic: null, titleSubtitleOffsetx: 0, titleSubtitleOffsety: 0, textSize: 12, textColor: 'black', textFont: 'Arial, Verdana, sans-serif', textBold: false, textItalic: false, textAccessible: false, textAccessibleOverflow: 'visible', textAccessiblePointerevents: false, text: null, contextmenu: null, scaleUnitsPre: '', scaleUnitsPost: '', scaleDecimals: 0, scalePoint: '.', scaleThousand: ',', adjustable: false, tooltips: null, tooltipsEffect: 'slide', tooltipsCssClass: 'RGraph_tooltip', tooltipsCss: null, tooltipsHighlight: true, tooltipsPersistent: false, tooltipsEvent: 'onclick', tooltipsFormattedThousand: ',', tooltipsFormattedPoint: '.', tooltipsFormattedDecimals: 0, tooltipsFormattedUnitsPre: '', tooltipsFormattedUnitsPost: '', tooltipsFormattedKeyColors: null, tooltipsFormattedKeyColorsShape: 'square', tooltipsFormattedKeyLabels: [], tooltipsFormattedListType: 'ul', tooltipsFormattedListItems: null, tooltipsFormattedTableHeaders: null, tooltipsFormattedTableData: null, tooltipsPointer: true, tooltipsPointerOffsetx: 0, tooltipsPointerOffsety: 0, tooltipsPositionStatic: true, tooltipsHotspotIgnore: null, highlightLinewidth: 1, highlightStroke: 'rgba(0,0,0,0)', highlightFill: 'rgba(255,255,255,0.7)', annotatable: false, annotateColor: 'black', arrows: false, marginInner: 0, labelsPosition: 'bottom', labelsSpecific: null, labelsSpecificFormattedDecimals: 0, labelsSpecificFormattedPoint: '.', labelsSpecificFormattedThousand: ',', labelsSpecificFormattedUnitsPre: '', labelsSpecificFormattedUnitsPost: '', labelsCount: 10, labelsOffsetx: 0, labelsOffsety: 0, labelsFont: null, labelsSize: null, labelsColor: null, labelsBold: null, labelsItalic: null, labelsInner: false, labelsInnerFont: null, labelsInnerSize: null, labelsInnerColor: null, labelsInnerBold: null, labelsInnerItalic: null, labelsInnerOffsetx: 0, labelsInnerOffsety: 0, labelsInnerDecimals: 0, labelsInnerBackgroundFill: 'rgba(255,255,255,0.7)', labelsInnerBorder: true, labelsInnerBorderLinewidth: 1, labelsInnerBorderColor: '#ccc', labelsInnerScalePoint: null, labelsInnerScaleThousand: null, labelsInnerUnitsPre: '', labelsInnerUnitsPost: '', labelsInnerSpecific: null, key: null, keyBackground: 'white', keyPosition: 'margin', keyHalign: 'right', keyShadow: false, keyShadowColor: '#666', keyShadowBlur: 3, keyShadowOffsetx: 2, keyShadowOffsety: 2, keyPositionMarginBoxed: false, keyPositionMarginHSpace: 0, keyPositionX: null, keyPositionY: null, keyColorShape: 'square', keyRounded: true, keyLinewidth: 1, keyColors: null, keyColorShape: 'square', keyInteractive: false, keyInteractiveHighlightChartLinewidth: 2, keyInteractiveHighlightChartStroke: 'black', keyInteractiveHighlightChartFill: 'rgba(255,255,255,0.7)', keyInteractiveHighlightLabel: 'rgba(255,0,0,0.2)', keyLabelsColor: null, keyLabelsFont: null, keyLabelsSize: null, keyLabelsBold: null, keyLabelsItalic: null, keyLabelsOffsetx: 0, keyLabelsOffsety: 0, keyFormattedDecimals: 0, keyFormattedPoint: '.', keyFormattedThousand: ',', keyFormattedUnitsPre: '', keyFormattedUnitsPost: '', keyFormattedValueSpecific: null, keyFormattedItemsCount: null, borderInner: true, corners: 'square', // Can also be round cornersRoundRadius: 10, clearto: 'rgba(0,0,0,0)', events: {}, scale: true, scaleFactor: 2, antialiasTranslate: false }; // // These are the properties that get scaled up if the // scale option is enabled. // this.properties_scale = [ 'marginLeft', 'marginRight', 'marginTop', 'marginBottom', 'marginInner', 'shadowBlur', 'shadowOffsetx', 'shadowOffsety', 'titleX', 'titleY', 'titleSize', 'titleOffsetx', 'titleOffsety', 'titleSubtitleSize', 'titleSubtitleOffsetx', 'titleSubtitleOffsety', 'textSize', 'highlightLinewidth', 'labelsOffsetx', 'labelsOffsety', 'labelsSize', 'labelsInnerSize', 'labelsInnerOffsetx', 'labelsInnerOffsety', 'labelsInnerBorderLinewidth', 'keyShadowBlur', 'keyShadowOffsetx', 'keyShadowOffsety', 'keyPositionX', 'keyPositionY', 'keyLinewidth', 'keyInteractiveHighlightChartLinewidth', 'keyLabelsSize', 'keyLabelsOffsetx', 'keyLabelsOffsety', 'cornersRoundRadius' ]; // // 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('[HPROGRESS] No canvas support'); return; } // // Create the dollar objects so that functions can be added to them // var linear_data = RGraph.arrayLinearize(value); for (var i=0; i { // 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'); // Translate half a pixel for antialiasing purposes - but // only if it hasn't been done already // // The old style antialias fix // if ( !this.properties.scale && this.properties.antialiasTranslate && !this.canvas.__rgraph_aa_translated__) { this.context.translate(0.5,0.5); this.canvas.__rgraph_aa_translated__ = true; } // // Parse the colors. This allows for simple gradient syntax // if (!this.colorsParsed) { this.parseColors(); // Don't want to do this again this.colorsParsed = true; } // // Set the current value // this.currentValue = this.value; // // Make the margins easy ro access // this.marginLeft = properties.marginLeft; this.marginRight = properties.marginRight; this.marginTop = properties.marginTop; this.marginBottom = properties.marginBottom; // Figure out the width and height this.width = this.canvas.width - this.marginLeft - this.marginRight; this.height = this.canvas.height - this.marginTop - this.marginBottom; this.coords = []; this.coordsText = []; // // Install clipping // // MUST be the first thing that's done after the // beforedraw event // if (!RGraph.isNullish(this.properties.clip)) { RGraph.clipTo.start(this, this.properties.clip); } this.drawbar(); this.drawTickMarks(); this.drawLabels(); this.drawTitle(); // // Setup the context menu if required // if (properties.contextmenu) { RGraph.showContext(this); } // Draw the key if necessary if (properties.key && properties.key.length) { RGraph.drawKey( this, properties.key, properties.colors ); } // // Reset the strokestyle to black // this.context.strokeStyle = 'black'; // // Add custom text thats specified // RGraph.addCustomText(this); // // This installs the event listeners // RGraph.installEventListeners(this); // // End clipping // if (!RGraph.isNullish(this.properties.clip)) { RGraph.clipTo.end(); } // // Fire the onfirstdraw event // if (this.firstDraw) { this.firstDraw = false; RGraph.fireCustomEvent(this, 'onfirstdraw'); this.firstDrawFunc(); } // // Fire the RGraph draw event // RGraph.fireCustomEvent(this, 'ondraw'); // // Install any inline responsive configuration. This // should be last in the draw function - even after // the draw events. // RGraph.installInlineResponsive(this); return this; }; // // Used in chaining. Runs a function there and then - not waiting for // the events to fire (eg the onbeforedraw event) // // @param function func The function to execute // this.exec = function (func) { func(this); return this; }; // // Draws the bar // this.drawbar = function () { // // First get the scale // this.scale2 = RGraph.getScale({object: this, options: { 'scale.max': this.max, 'scale.min': this.min, 'scale.strict': true, 'scale.thousand': properties.scaleThousand, 'scale.point': properties.scalePoint, 'scale.decimals': properties.scaleDecimals, 'scale.labels.count': properties.labelsCount, 'scale.round': properties.scaleRound, 'scale.units.pre': properties.scaleUnitsPre, 'scale.units.post': properties.scaleUnitsPost }}); // Set a shadow if requested if (properties.shadow) { RGraph.setShadow({ object: this, prefix: 'shadow' }); } // // Draw the background // this.context.fillStyle = properties.backgroundColor; this.context.strokeStyle = properties.colorsStrokeOuter; if (properties.corners === 'round') { this.context.beginPath(); this.path( 'b rr % % % % % s % f %', this.marginLeft, this.marginTop, this.width, this.height, properties.cornersRoundRadius, properties.colorsStrokeOuter, properties.backgroundColor ); } else { this.context.fillStyle = properties.backgroundColor; this.context.strokeStyle = properties.colorsStrokeOuter; this.context.strokeRect(this.marginLeft, this.marginTop, this.width, this.height); this.context.fillRect(this.marginLeft, this.marginTop, this.width, this.height); } // // Turn off any shadow // RGraph.noShadow(this); this.context.fillStyle = properties.colors[0]; this.context.strokeStyle = properties.colorsStrokeOuter; var margin = properties.marginInner; // Draw the actual bar itself var barWidth = Math.min(this.width, ((RGraph.arraySum(this.value) - this.min) / (this.max - this.min) ) * this.width); if (properties.tickmarksInnerCount > 0) { var spacing = (this.canvas.width - this.marginLeft - this.marginRight) / properties.tickmarksInnerCount; this.context.lineWidth = 1; this.context.strokeStyle = properties.colorsStrokeOuter; this.context.beginPath(); for (var x = this.marginLeft; x 0) { this.context.beginPath(); // This is used by the label function below this.tickInterval = this.width / properties.tickmarksOuterCount; var start = 0; if (properties.labelsPosition === 'top') { for (var i=this.marginLeft + start; i<=(this.width + this.marginLeft + 0.1); i+=this.tickInterval) { this.context.moveTo(Math.round(i), this.marginTop); this.context.lineTo(Math.round(i), this.marginTop - 4); } } else { for (var i=this.marginLeft + start; i<=(this.width + this.marginLeft + 0.1); i+=this.tickInterval) { this.context.moveTo(Math.round(i), this.marginTop + this.height); this.context.lineTo(Math.round(i), this.marginTop + this.height + 4); } } this.context.stroke(); } }; // // The function that draws the labels // this.drawLabels = function () { var scaleFactor = RGraph.getScaleFactor(this); if (!RGraph.isNullish(properties.labelsSpecific)) { return this.drawSpecificLabels(); } if (properties.labelsCount === 0) { return; } this.context.fillStyle = properties.textColor; var xPoints = [], yPoints = [], bold = properties.textBold, italic = properties.textItalic, color = properties.textColor, font = properties.textFont, size = properties.textSize, offsetx = properties.labelsOffsetx, offsety = properties.labelsOffsety; for (i=0,len=this.scale2.labels.length; i (this.canvas.width - this.marginRight) ) { value = this.max } return value; }; // // Each object type has its own Highlight() function which // highlights the appropriate shape. // // @param object shape The shape to highlight // this.highlight = function (shape) { var isLast = shape.index === this.coords.length - 1; if (!properties.tooltipsHighlight) { return; } // Call a function to highlight the chart if (typeof properties.highlightStyle === 'function') { (properties.highlightStyle)(shape); // Highlight all of the rects except the selected one - essentially an inverted highlight } else if (typeof properties.highlightStyle === 'string' && properties.highlightStyle === 'invert') { for (var i=0; i this.marginLeft && mouseXY[0] < (this.canvas.width - this.marginRight) && mouseXY[1] > this.marginTop && mouseXY[1] < (this.canvas.height - this.marginBottom) ) { return this; } }; // // This method handles the adjusting calculation for when the mouse is moved // // @param object e The event object // this.adjusting_mousemove = function (e) { // // Handle adjusting for the HProgress // if (properties.adjustable && RGraph.Registry.get('adjusting') && RGraph.Registry.get('adjusting').uid == this.uid) { var mouseXY = RGraph.getMouseXY(e); var value = this.getValue(e); if (typeof value === 'number') { this.value = Number(value.toFixed(properties.scaleDecimals)); RGraph.redrawCanvas(this.canvas); // Fire the onadjust event RGraph.fireCustomEvent(this, 'onadjust'); } } }; // // Draws labelsSpecific // this.drawSpecificLabels = function () { // // If the xaxisLabels option is a string then turn it // into an array. // if (properties.labelsSpecific && properties.labelsSpecific.length) { if (typeof properties.labelsSpecific === 'string') { properties.labelsSpecific = RGraph.arrayPad({ array: [], length: properties.labelsCount, value: properties.labelsSpecific }); } // Label substitution // for (var i=0; i this.max) { return null; } var barWidth = this.canvas.width - this.marginLeft - this.marginRight; var coord = ((value - min) / (this.max - min)) * barWidth; coord = this.marginLeft + coord; return coord; }; // // This returns true/false as to whether the cursor is over the chart area. // The cursor does not necessarily have to be over the bar itself. // this.overChartArea = function (e) { var mouseXY = RGraph.getMouseXY(e); var mouseX = mouseXY[0]; var mouseY = mouseXY[1]; if ( mouseX >= this.marginLeft && mouseX <= (this.canvas.width - this.marginRight) && mouseY >= this.marginTop && mouseY <= (this.canvas.height - this.marginBottom) ) { return true; } return false; }; // // // 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.backgroundColor = RGraph.arrayClone(properties.backgroundColor, {structuredClone: false}); this.original_colors.colors = RGraph.arrayClone(properties.colors, {structuredClone: false}); this.original_colors.tickmarksColor = RGraph.arrayClone(properties.tickmarksColor, {structuredClone: false}); this.original_colors.colorsStrokeInner = RGraph.arrayClone(properties.colorsStrokeInner, {structuredClone: false}); this.original_colors.colorsStrokeOuter = RGraph.arrayClone(properties.colorsStrokeOuter, {structuredClone: false}); this.original_colors.highlightFill = RGraph.arrayClone(properties.highlightFill, {structuredClone: false}); this.original_colors.highlightStroke = RGraph.arrayClone(properties.highlightStroke, {structuredClone: false}); this.original_colors.highlightColor = RGraph.arrayClone(properties.highlightColor, {structuredClone: false}); } var colors = properties.colors; for (var i=0; i