// version: 2017-11-25 /** * o--------------------------------------------------------------------------------o * | This file is part of the RGraph package - you can learn more at: | * | | * | http://www.rgraph.net | * | | * | RGraph is licensed under the Open Source MIT license. That means that it's | * | totally free to use and there are no restrictions on what you can do with it! | * o--------------------------------------------------------------------------------o */ RGraph = window.RGraph || {isRGraph: true}; /** * The scatter graph constructor * * @param object canvas The cxanvas object * @param array data The chart data */ RGraph.Scatter = function (conf) { /** * Allow for object config style */ if ( typeof conf === 'object' && typeof conf.data === 'object' && typeof conf.id === 'string') { var parseConfObjectForOptions = true; // Set this so the config is parsed (at the end of the constructor) this.data = new Array(conf.data.length); // Store the data set(s) this.data = RGraph.arrayClone(conf.data); // Account for just one dataset being given if (typeof conf.data === 'object' && typeof conf.data[0] === 'object' && (typeof conf.data[0][0] === 'number' || typeof conf.data[0][0] === 'string')) { var tmp = RGraph.arrayClone(conf.data); conf.data = new Array(); conf.data[0] = RGraph.arrayClone(tmp); this.data = RGraph.arrayClone(conf.data); } } else { var conf = {id: conf}; conf.data = arguments[1]; this.data = []; // Handle multiple datasets being given as one argument if (arguments[1][0] && arguments[1][0][0] && typeof arguments[1][0][0] == 'object') { // Store the data set(s) for (var i=0; i 0) { if (typeof prop['chart.ymax'] === 'number') { this.max = prop['chart.ymax']; this.min = prop['chart.ymin'] ? prop['chart.ymin'] : 0; this.scale2 = RG.getScale2(this, { 'max':this.max, 'min':this.min, 'strict':true, 'scale.thousand':prop['chart.scale.thousand'], 'scale.point':prop['chart.scale.point'], 'scale.decimals':prop['chart.scale.decimals'], 'ylabels.count':prop['chart.ylabels.count'], 'scale.round':prop['chart.scale.round'], 'units.pre': prop['chart.units.pre'], 'units.post': prop['chart.units.post'] }); this.max = this.scale2.max; this.min = this.scale2.min; var decimals = prop['chart.scale.decimals']; } else { var i = 0; var j = 0; for (i=0,len=this.data.length; i 0 ? this.scale2.min : 0); co.moveTo(this.gutterLeft, y); co.lineTo(ca.width - this.gutterRight, y); } } // Draw the Y tickmarks if (prop['chart.noyaxis'] === false) { var numyticks = prop['chart.numyticks']; //for (y=this.gutterTop; y < ca.height - this.gutterBottom + (prop['chart.xaxispos'] == 'center' ? 1 : 0) ; y+=(graphHeight / numyticks)) { for (i=0; i 0) { if (prop['chart.xaxispos'] == 'center' && prop['chart.yaxispos'] == 'left') { co.moveTo(this.gutterLeft, ma.round(ca.height - this.gutterBottom)); co.lineTo(this.gutterLeft - 3, ma.round(ca.height - this.gutterBottom)); } else if (prop['chart.xaxispos'] == 'center') { co.moveTo(ca.width - this.gutterRight + 3, ma.round(ca.height - this.gutterBottom)); co.lineTo(ca.width - this.gutterRight, ma.round(ca.height - this.gutterBottom)); } } /** * Draw an extra tick if the X axis isn't being shown */ if (prop['chart.xaxis'] === false && prop['chart.yaxispos'] === 'left') { co.moveTo(this.gutterLeft, ma.round(ca.height - this.gutterBottom)); co.lineTo(this.gutterLeft - 3, ma.round(ca.height - this.gutterBottom)); } else if (prop['chart.xaxis'] === false && prop['chart.yaxispos'] === 'right') { co.moveTo(ca.width - this.gutterRight, ma.round(ca.height - this.gutterBottom)); co.lineTo(ca.width - this.gutterRight + 3, ma.round(ca.height - this.gutterBottom)); } // // If: the X axis is offset // the Y axis is being shown // there are Y tickmarks // if ( prop['chart.xaxispos'] === 'bottom' && prop['chart.numyticks'] > 0 ) { if (prop['chart.yaxispos'] == 'left') { co.moveTo(this.gutterLeft, ma.round(this.getYCoord(prop['chart.ymin']))); co.lineTo(this.gutterLeft - 3, ma.round(this.getYCoord(prop['chart.ymin']))); } else { co.moveTo(ca.width - this.gutterRight +3, ma.round(this.getYCoord(prop['chart.ymin']))); co.lineTo(ca.width - this.gutterRight, ma.round(this.getYCoord(prop['chart.ymin']))); } } } /** * Draw the X tickmarks */ if (prop['chart.numxticks'] > 0 && prop['chart.xaxis']) { var x = 0, y = this.getYCoord(prop['chart.ylabels.invert'] ? this.scale2.max : (this.scale2.max > 0 && this.scale2.min > 0 ? this.scale2.min : 0) ) - 3, size = 3; if (prop['chart.ymin'] === 0 && prop['chart.xaxispos'] === 'bottom') { y += 3; } if (this.scale2.max > 0 && this.scale2.min > 0) { y += 3; } this.xTickGap = (prop['chart.labels'] && prop['chart.labels'].length) ? ((ca.width - this.gutterLeft - this.gutterRight ) / prop['chart.labels'].length) : (ca.width - this.gutterLeft - this.gutterRight) / 10; /** * This allows the number of X tickmarks to be specified */ if (typeof(prop['chart.numxticks']) == 'number') { this.xTickGap = (ca.width - this.gutterLeft - this.gutterRight) / prop['chart.numxticks']; } for (x=(this.gutterLeft + (prop['chart.yaxispos'] == 'left' && prop['chart.noyaxis'] == false ? this.xTickGap : 0) ); x <= (ca.width - this.gutterRight - (prop['chart.yaxispos'] == 'left' || prop['chart.noyaxis'] == true ? -1 : 1)); x += this.xTickGap) { if (prop['chart.yaxispos'] == 'left' && prop['chart.noendxtick'] == true && x == (ca.width - this.gutterRight) ) { continue; } else if (prop['chart.yaxispos'] == 'right' && prop['chart.noendxtick'] == true && x == this.gutterLeft) { continue; } co.moveTo(ma.round(x), y); co.lineTo(ma.round(x), y + (prop['chart.xaxispos'] === 'center' || prop['chart.ymin'] < 0 ? size * 2 : size)); } } co.stroke(); /** * Reset the linewidth back to one */ co.lineWidth = 1; }; /** * Draws the labels on the scatter graph */ this.drawLabels = this.DrawLabels = function () { co.fillStyle = prop['chart.text.color']; var font = prop['chart.text.font'], xMin = prop['chart.xmin'], xMax = prop['chart.xmax'], yMax = this.scale2.max, yMin = prop['chart.ymin'] ? prop['chart.ymin'] : 0, text_size = prop['chart.text.size'], units_pre = prop['chart.units.pre'], units_post = prop['chart.units.post'], numYLabels = prop['chart.ylabels.count'], invert = prop['chart.ylabels.invert'], inside = prop['chart.ylabels.inside'], context = co, canvas = ca, boxed = false, offsetx = prop['chart.ylabels.offsetx'], offsety = prop['chart.ylabels.offsety'] this.halfTextHeight = text_size / 2; this.halfGraphHeight = (ca.height - this.gutterTop - this.gutterBottom) / 2; /** * Draw the Y yaxis labels, be it at the top or center */ if (prop['chart.ylabels']) { var xPos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5; var align = prop['chart.yaxispos'] == 'right' ? 'left' : 'right'; /** * Now change the two things above if chart.ylabels.inside is specified */ if (inside) { if (prop['chart.yaxispos'] == 'left') { xPos = prop['chart.gutter.left'] + 5; align = 'left'; boxed = true; } else { xPos = ca.width - prop['chart.gutter.right'] - 5; align = 'right'; boxed = true; } } if (prop['chart.xaxispos'] == 'center') { /** * Specific Y labels */ if (typeof(prop['chart.ylabels.specific']) == 'object' && prop['chart.ylabels.specific'] != null && prop['chart.ylabels.specific'].length) { var labels = prop['chart.ylabels.specific']; if (prop['chart.ymin'] > 0) { labels = []; for (var i=0; i<(prop['chart.ylabels.specific'].length - 1); ++i) { labels.push(prop['chart.ylabels.specific'][i]); } } for (var i=0; i 0 || prop['chart.scale.zerostart'])) { RG.text2(this, { font:font, size: text_size, x: xPos + offsetx, y: this.gutterTop + this.halfGraphHeight + offsety, valign: 'center', halign:align, bounding: boxed, boundingFill: 'white', text: RG.numberFormat(this, yMin.toFixed(this.scale2.min === 0 ? 0 : prop['chart.scale.decimals']), units_pre, units_post), tag: 'scale' }); } if (invert) { RG.text2(this, { 'font':font, 'size': text_size, 'x': xPos + offsetx, 'y': this.gutterTop + offsety, 'valign': 'center', 'halign':align, 'bounding': boxed, 'boundingFill': 'white', 'text': RG.number_format(this, yMin.toFixed(this.scale2.min === 0 ? 0 : prop['chart.scale.decimals']), units_pre, units_post), 'tag': 'scale' }); RG.text2(this, { 'font':font, 'size': text_size, 'x': xPos + offsetx, 'y': this.gutterTop + (this.halfGraphHeight * 2) + offsety, 'valign': 'center', 'halign':align, 'bounding': boxed, 'boundingFill': 'white', 'text': '-' + RG.numberFormat(this, yMin.toFixed(this.scale2.min === 0 ? 0 : prop['chart.scale.decimals']), units_pre, units_post), 'tag': 'scale' }); } } // X axis at the bottom } else { var xPos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5; var align = prop['chart.yaxispos'] == 'right' ? 'left' : 'right'; if (inside) { if (prop['chart.yaxispos'] == 'left') { xPos = prop['chart.gutter.left'] + 5; align = 'left'; boxed = true; } else { xPos = ca.width - this.gutterRight - 5; align = 'right'; boxed = true; } } /** * Specific Y labels */ if (typeof prop['chart.ylabels.specific'] == 'object' && prop['chart.ylabels.specific']) { var labels = prop['chart.ylabels.specific']; // Lose the last label if (prop['chart.ymin'] > 9999) { labels = []; for (var i=0; i<(prop['chart.ylabels.specific'].length - 1); ++i) { labels.push(prop['chart.ylabels.specific'][i]); } } for (var i=0,len=labels.length; i 0) { angle *= -1; valign = 'center'; halign = 'right'; y += 5; } else { valign = 'center'; halign = 'center'; } if (!prop['chart.xmax']) { var xmax = 0; var xmin = prop['chart.xmin']; for (var ds=0,len=this.data.length; ds 0) { angle = -1 * prop['chart.text.angle']; valign = 'center'; halign = 'right'; yPos += 10; } for (i=0; i= 2) { if (prop['chart.line.dash'] && typeof co.setLineDash === 'function') { co.setLineDash(prop['chart.line.dash']); } co.lineCap = 'round'; co.lineJoin = 'round'; co.lineWidth = this.getLineWidth(i);// i is the index of the set of coordinates co.strokeStyle = prop['chart.line.colors'][i]; co.beginPath(); var prevY = null; var currY = null; for (var j=0,len=this.coords[i].length; j 0) prevY = this.coords[i][j - 1][1]; currY = yPos; if (j == 0 || RG.is_null(prevY) || RG.is_null(currY)) { co.moveTo(xPos, yPos); } else { // Stepped? var stepped = prop['chart.line.stepped']; if ( (typeof stepped == 'boolean' && stepped) || (typeof stepped == 'object' && stepped[i]) ) { co.lineTo(this.coords[i][j][0], this.coords[i][j - 1][1]); } co.lineTo(xPos, yPos); } } co.stroke(); /** * Set the linedash back to the default */ if (prop['chart.line.dash'] && typeof co.setLineDash === 'function') { co.setLineDash([1,0]); } } /** * Set the linewidth back to 1 */ co.lineWidth = 1; }; /** * Returns the linewidth * * @param number i The index of the "line" (/set of coordinates) */ this.getLineWidth = this.GetLineWidth = function (i) { var linewidth = prop['chart.line.linewidth']; if (typeof linewidth == 'number') { return linewidth; } else if (typeof linewidth == 'object') { if (linewidth[i]) { return linewidth[i]; } else { return linewidth[0]; } alert('[SCATTER] Error! chart.linewidth should be a single number or an array of one or more numbers'); } }; /** * Draws vertical bars. Line chart doesn't use a horizontal scale, hence this function * is not common */ this.drawVBars = this.DrawVBars = function () { var vbars = prop['chart.background.vbars']; var graphWidth = ca.width - this.gutterLeft - this.gutterRight; if (vbars) { var xmax = prop['chart.xmax']; var xmin = prop['chart.xmin']; for (var i=0,len=vbars.length; i 0) { var i=0; for (var set=0; set= (x - offset) && mouseY <= (y + offset) && mouseY >= (y - offset)) { var tooltip = RG.parseTooltipText(this.data[set][i][3], 0); var index_adjusted = i; for (var ds=(set-1); ds >=0; --ds) { index_adjusted += this.data[ds].length; } return { 0: this, 1: x, 2: y, 3: set, 4: i, 5: this.data[set][i][3], 'object': this, 'x': x, 'y': y, 'dataset': set, 'index': i, 'tooltip': tooltip, 'index_adjusted': index_adjusted }; } } else if (RG.is_null(y)) { // Nothing to see here } else { var mark = this.data[set][i]; /** * Determine the width */ var width = prop['chart.boxplot.width']; if (typeof(mark[1][7]) == 'number') { width = mark[1][7]; } if ( typeof(x) == 'object' && mouseX > x[0] && mouseX < x[1] && mouseY < y[1] && mouseY > y[3] ) { var tooltip = RG.parseTooltipText(this.data[set][i][3], 0); return { 0: this, 1: x[0], 2: x[1] - x[0], 3: y[1], 4: y[3] - y[1], 5: set, 6: i, 7: this.data[set][i][3], 'object': this, 'x': x[0], 'y': y[1], 'width': x[1] - x[0], 'height': y[3] - y[1], 'dataset': set, 'index': i, 'tooltip': tooltip }; } } } } }; /** * Draws the above line labels */ this.drawAboveLabels = this.DrawAboveLabels = function () { var size = prop['chart.labels.above.size']; var font = prop['chart.text.font']; var units_pre = prop['chart.units.pre']; var units_post = prop['chart.units.post']; for (var set=0,len=this.coords.length; set (ca.height - this.gutterBottom) || mouseX < this.gutterLeft || mouseX > (ca.width - this.gutterRight) ) { return null; } if (prop['chart.xaxispos'] == 'center') { var value = (((this.grapharea / 2) - (mouseY - this.gutterTop)) / this.grapharea) * (this.max - this.min) value *= 2; // Account for each side of the X axis if (value >= 0) { value += this.min if (prop['chart.ylabels.invert']) { value -= this.min; value = this.max - value; } } else { value -= this.min; if (prop['chart.ylabels.invert']) { value += this.min; value = this.max + value; value *= -1; } } } else { var value = ((this.grapharea - (mouseY - this.gutterTop)) / this.grapharea) * (this.max - this.min) value += this.min; if (prop['chart.ylabels.invert']) { value -= this.min; value = this.max - value; } } return value; }; /** * When you click on the chart, this method can return the X value at that point. * * @param mixed arg This can either be an event object or the X coordinate * @param number If specifying the X coord as the first arg then this should be the Y coord */ this.getXValue = function (arg) { if (arg.length == 2) { var mouseX = arg[0]; var mouseY = arg[1]; } else { var mouseXY = RG.getMouseXY(arg); var mouseX = mouseXY[0]; var mouseY = mouseXY[1]; } var obj = this; if ( mouseY < this.gutterTop || mouseY > (ca.height - this.gutterBottom) || mouseX < this.gutterLeft || mouseX > (ca.width - this.gutterRight) ) { return null; } var width = (ca.width - this.gutterLeft - this.gutterRight); var value = ((mouseX - this.gutterLeft) / width) * (prop['chart.xmax'] - prop['chart.xmin']) value += prop['chart.xmin']; return value; }; /** * Each object type has its own Highlight() function which highlights the appropriate shape * * @param object shape The shape to highlight */ this.highlight = this.Highlight = function (shape) { if (typeof prop['chart.highlight.style'] === 'function') { (prop['chart.highlight.style'])(shape); } else { // Boxplot highlight if (shape['height']) { RG.Highlight.Rect(this, shape); // Point highlight } else { RG.Highlight.Point(this, shape); } } }; /** * The getObjectByXY() worker method. Don't call this call: * * RG.ObjectRegistry.getObjectByXY(e) * * @param object e The event object */ this.getObjectByXY = function (e) { var mouseXY = RG.getMouseXY(e); if ( mouseXY[0] > (this.gutterLeft - 3) && mouseXY[0] < (ca.width - this.gutterRight + 3) && mouseXY[1] > (this.gutterTop - 3) && mouseXY[1] < ((ca.height - this.gutterBottom) + 3) ) { return this; } }; /** * This function can be used when the canvas is clicked on (or similar - depending on the event) * to retrieve the relevant X coordinate for a particular value. * * @param int value The value to get the X coordinate for */ this.getXCoord = function (value) { if (typeof value != 'number' && typeof value != 'string') { return null; } // Allow for date strings to be passed if (typeof value === 'string') { value = RG.parseDate(value); } var xmin = prop['chart.xmin']; var xmax = prop['chart.xmax']; var x; if (value < xmin) return null; if (value > xmax) return null; var gutterRight = this.gutterRight; var gutterLeft = this.gutterLeft; if (prop['chart.yaxispos'] == 'right') { x = ((value - xmin) / (xmax - xmin)) * (ca.width - gutterLeft - gutterRight); x = (ca.width - gutterRight - x); } else { x = ((value - xmin) / (xmax - xmin)) * (ca.width - gutterLeft - gutterRight); x = x + gutterLeft; } return x; }; /** * This function positions a tooltip when it is displayed * * @param obj object The chart object * @param int x The X coordinate specified for the tooltip * @param int y The Y coordinate specified for the tooltip * @param objec tooltip The tooltips DIV element * this.positionTooltip = function (obj, x, y, tooltip, idx) { var shape = RG.Registry.Get('chart.tooltip.shape'); var dataset = shape['dataset']; var index = shape['index']; var coordX = obj.coords[dataset][index][0] var coordY = obj.coords[dataset][index][1] var canvasXY = RG.getCanvasXY(obj.canvas); var mouseXY = RG.getMouseXY(window.event); var gutterLeft = obj.gutterLeft; var gutterTop = obj.gutterTop; var width = tooltip.offsetWidth; var height = tooltip.offsetHeight; tooltip.style.left = 0; tooltip.style.top = 0; // Is the coord a boxplot //var isBoxplot = typeof(coordY) == 'object' ? true : false; // Show any overflow (ie the arrow) tooltip.style.overflow = ''; // Reposition the tooltip if at the edges: // LEFT edge ////////////////////////////////////////////////////////////////// if (canvasXY[0] + mouseXY[0] - (width / 2) < 0) { tooltip.style.left = canvasXY[0] + mouseXY[0] - (width * 0.1) + 'px'; tooltip.style.top = window.event.pageY - height - 5 + 'px'; // RIGHT edge ////////////////////////////////////////////////////////////////// } else if ((canvasXY[0] + (coordX[0] || coordX) + (width / 2)) > doc.body.offsetWidth) { tooltip.style.left = canvasXY[0] + mouseXY[0] - (width * 0.9) + 'px'; tooltip.style.top = window.event.pageY - height - 5 + 'px'; // Default positioning - CENTERED ////////////////////////////////////////////////////////////////// } else { tooltip.style.left = canvasXY[0] + mouseXY[0] - (width / 2) + 'px'; tooltip.style.top = window.event.pageY - height - 5 + 'px'; } };*/ /** * Returns the applicable Y COORDINATE when given a Y value * * @param int value The value to use * @return int The appropriate Y coordinate */ this.getYCoord = this.getYCoordFromValue = function (value) { if (typeof(value) != 'number') { return null; } var invert = prop['chart.ylabels.invert']; var xaxispos = prop['chart.xaxispos']; var graphHeight = ca.height - this.gutterTop - this.gutterBottom; var halfGraphHeight = graphHeight / 2; var ymax = this.max; var ymin = prop['chart.ymin']; var coord = 0; if (value > ymax || (prop['chart.xaxispos'] == 'bottom' && value < ymin) || (prop['chart.xaxispos'] == 'center' && ((value > 0 && value < ymin) || (value < 0 && value > (-1 * ymin))))) { return null; } /** * This calculates scale values if the X axis is in the center */ if (xaxispos == 'center') { coord = ((Math.abs(value) - ymin) / (ymax - ymin)) * halfGraphHeight; if (invert) { coord = halfGraphHeight - coord; } if (value < 0) { coord += this.gutterTop; coord += halfGraphHeight; } else { coord = halfGraphHeight - coord; coord += this.gutterTop; } /** * And this calculates scale values when the X axis is at the bottom */ } else { coord = ((value - ymin) / (ymax - ymin)) * graphHeight; if (invert) { coord = graphHeight - coord; } // Invert the coordinate because the Y scale starts at the top coord = graphHeight - coord; // And add on the top gutter coord = this.gutterTop + coord; } return coord; }; /** * A helper class that helps facilitatesbubble charts */ RG.Scatter.Bubble = function (scatter, min, max, width, data) { this.scatter = scatter; this.min = min; this.max = max; this.width = width; this.data = data; this.coords = []; this.type = 'scatter.bubble' /** * A setter for the Bubble chart class - it just acts as a "passthru" to the Scatter object */ this.set = this.Set = function (name, value) { this.scatter.set(name, value); return this; }; /** * A getter for the Bubble chart class - it just acts as a "passthru" to the Scatter object */ this.get = this.Get = function (name) { this.scatter.get(name); }; /** * Tha Bubble chart draw function */ this.draw = this.Draw = function () { var bubble_min = this.min, bubble_max = this.max, bubble_data = this.data, bubble_max_width = this.width; // Keep a record of the bubble chart object var obj_bubble = this, obj_scatter = this.scatter; // This custom ondraw event listener draws the bubbles this.scatter.ondraw = function (obj) { // Loop through all the points (first dataset) for (var i=0; i