// 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 bi-polar/age frequency constructor. * * @param string id The id of the canvas * @param array left The left set of data points * @param array right The right set of data points * * REMEMBER If ymin is implemented you need to update the .getValue() method */ RGraph.Bipolar = function (conf) { /** * Allow for object config style */ if ( typeof conf === 'object' && typeof conf.left === 'object' && typeof conf.right === 'object' && typeof conf.id === 'string') { var id = conf.id var canvas = document.getElementById(id); var left = conf.left; var right = conf.right; 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 left = arguments[1]; var right = arguments[2]; } // Get the canvas and context objects this.id = id; this.canvas = canvas; this.context = this.canvas.getContext('2d'); this.canvas.__object__ = this; this.type = 'bipolar'; this.coords = []; this.coordsLeft = []; this.coordsRight = []; this.max = 0; this.isRGraph = true; 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 /** * Compatibility with older browsers */ //RGraph.OldBrowserCompat(this.context); // The left and right data respectively this.left = left; this.right = right; this.data = [left, right]; this.properties = { 'chart.margin': 2, 'chart.xtickinterval': null, 'chart.labels': [], 'chart.labels.above': false, 'chart.text.size': 10, 'chart.text.color': 'black', // (Simple) gradients are not supported 'chart.text.font': 'Arial', 'chart.title.left': '', 'chart.title.right': '', 'chart.gutter.center': 60, 'chart.gutter.left': 25, 'chart.gutter.right': 25, 'chart.gutter.top': 25, 'chart.gutter.bottom': 25, 'chart.title': '', 'chart.title.background': null, 'chart.title.hpos': null, 'chart.title.vpos': null, 'chart.title.bold': true, 'chart.title.font': null, 'chart.title.x': null, 'chart.title.y': null, 'chart.title.halign': null, 'chart.title.valign': null, 'chart.colors': ['#0f0'], 'chart.contextmenu': null, 'chart.tooltips': null, 'chart.tooltips.effect': 'fade', 'chart.tooltips.css.class': 'RGraph_tooltip', 'chart.tooltips.highlight': true, 'chart.tooltips.event': 'onclick', 'chart.highlight.stroke': 'rgba(0,0,0,0)', 'chart.highlight.fill': 'rgba(255,255,255,0.7)', 'chart.units.pre': '', 'chart.units.post': '', 'chart.shadow': false, 'chart.shadow.color': '#666', 'chart.shadow.offsetx': 3, 'chart.shadow.offsety': 3, 'chart.shadow.blur': 3, 'chart.annotatable': false, 'chart.annotate.color': 'black', 'chart.xmax': null, 'chart.xmin': 0, 'chart.scale.decimals': null, 'chart.scale.point': '.', 'chart.scale.thousand': ',', 'chart.axis.color': 'black', '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, 'chart.zoom.action': 'zoom', 'chart.resizable': false, 'chart.resize.handle.background': null, 'chart.strokestyle': 'rgba(0,0,0,0)', 'chart.events.mousemove': null, 'chart.events.click': null, 'chart.linewidth': 1, 'chart.noaxes': false, 'chart.xlabels': true, 'chart.numyticks': null, 'chart.numxticks': 5, 'chart.axis.linewidth': 1, 'chart.labels.count': 5 } // Pad the arrays so they're the same size while (this.left.length < this.right.length) this.left.push(0); while (this.left.length > this.right.length) this.right.push(0); /** * Set the default for the number of Y tickmarks */ this.properties['chart.numyticks'] = this.left.length; /** * Create the dollar objects so that functions can be added to them */ var linear_data = RGraph.array_linearize(this.left, this.right); for (var i=0; i 0) { co.beginPath(); for (var i=0; i 0) { var xInterval = this.axisWidth / prop['chart.numxticks']; // Is chart.xtickinterval specified ? If so, use that. if (typeof(prop['chart.xtickinterval']) == 'number') { xInterval = prop['chart.xtickinterval']; } // Draw the left sides X tick marks for (i=this.gutterLeft; i<(this.gutterLeft + this.axisWidth); i+=xInterval) { co.beginPath(); co.moveTo(Math.round( i), ca.height - this.gutterBottom); co.lineTo(Math.round( i), (ca.height - this.gutterBottom) + 4); co.closePath(); co.stroke(); } // Draw the right sides X tick marks var stoppingPoint = ca.width - this.gutterRight; for (i=(this.gutterLeft + this.axisWidth + prop['chart.gutter.center'] + xInterval); i<=stoppingPoint; i+=xInterval) { co.beginPath(); co.moveTo(Math.round(i), ca.height - this.gutterBottom); co.lineTo(Math.round(i), (ca.height - this.gutterBottom) + 4); co.closePath(); co.stroke(); } } }; /** * Figures out the maximum value, or if defined, uses xmax */ this.getMax = this.GetMax = function() { var dec = prop['chart.scale.decimals']; // chart.xmax defined if (prop['chart.xmax']) { var max = prop['chart.xmax']; var min = prop['chart.xmin']; this.scale2 = RG.getScale2(this, { 'max':max, 'min':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.labels.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; /** * Generate the scale ourselves */ } else { var max = Math.max(RG.array_max(this.left), RG.array_max(this.right)); this.scale2 = RG.getScale2(this, { 'max':max, //'strict': true, 'min':prop['chart.xmin'], 'scale.thousand':prop['chart.scale.thousand'], 'scale.point':prop['chart.scale.point'], 'scale.decimals':prop['chart.scale.decimals'], 'ylabels.count':prop['chart.labels.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; } // Don't need to return it as it is stored in this.max }; /** * Function to draw the left hand bars */ this.drawLeftBars = this.DrawLeftBars = function () { // Set the stroke colour co.strokeStyle = prop['chart.strokestyle']; // Set the linewidth co.lineWidth = prop['chart.linewidth']; for (i=0; i= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height) ) { var tooltip = RG.parseTooltipText(prop['chart.tooltips'], i); return { 0: this,1: left,2: top,3: width,4: height,5: i, 'object': this, 'x': left, 'y': top, 'width': width, 'height': height, 'index': i, 'tooltip': tooltip }; } } return null; }; /** * 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) { // Add the new highlight RG.Highlight.Rect(this, shape); }; /** * When you click on the canvas, this will return the relevant value (if any) * * REMEMBER This function will need updating if the Bipolar ever gets chart.ymin * * @param object e The event object */ this.getValue = function (e) { var obj = e.target.__object__; var mouseXY = RG.getMouseXY(e); var mouseX = mouseXY[0]; /** * Left hand side */ if (mouseX > this.gutterLeft && mouseX < ( (ca.width / 2) - (prop['chart.gutter.center'] / 2) )) { var value = (mouseX - prop['chart.gutter.left']) / this.axisWidth; value = this.max - (value * this.max); } /** * Right hand side */ if (mouseX < (ca.width - this.gutterRight) && mouseX > ( (ca.width / 2) + (prop['chart.gutter.center'] / 2) )) { var value = (mouseX - prop['chart.gutter.left'] - this.axisWidth - prop['chart.gutter.center']) / this.axisWidth; value = (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 = RG.getMouseXY(e); if ( mouseXY[0] > prop['chart.gutter.left'] && mouseXY[0] < (ca.width - prop['chart.gutter.right']) && mouseXY[1] > prop['chart.gutter.top'] && mouseXY[1] < (ca.height - prop['chart.gutter.bottom']) ) { return this; } }; /** * 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 coordX = obj.coords[tooltip.__index__][0]; var coordY = obj.coords[tooltip.__index__][1]; var coordW = obj.coords[tooltip.__index__][2]; var coordH = obj.coords[tooltip.__index__][3]; var canvasXY = RG.getCanvasXY(obj.canvas); var gutterLeft = obj.Get('chart.gutter.left'); var gutterTop = obj.Get('chart.gutter.top'); var width = tooltip.offsetWidth; var height = tooltip.offsetHeight; // Set the top position tooltip.style.left = 0; tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px'; // By default any overflow is hidden tooltip.style.overflow = ''; // The arrow var img = new Image(); img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC'; img.style.position = 'absolute'; img.id = '__rgraph_tooltip_pointer__'; img.style.top = (tooltip.offsetHeight - 2) + 'px'; tooltip.appendChild(img); // Reposition the tooltip if at the edges: // LEFT edge if ((canvasXY[0] + coordX + (coordW / 2)- (width / 2)) < 0) { tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px'; img.style.left = ((width * 0.1) - 8.5) + 'px'; // RIGHT edge } else if ((canvasXY[0] + coordX + width) > doc.body.offsetWidth) { tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px'; img.style.left = ((width * 0.9) - 8.5) + 'px'; // Default positioning - CENTERED } else { tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px'; img.style.left = ((width * 0.5) - 8.5) + 'px'; } }; /** * Redraw the bar so that the shadow is NOT on top */ this.redrawBars = this.RedrawBars = function () { var coords = this.coords; var len = coords.length; // Reset the sequentail color index this.sequentialColorIndex = 0; co.beginPath(); // Turn off shadow RG.NoShadow(this); // Set the stroke color co.strokeStyle = prop['chart.strokestyle']; // Set the linewidth co.lineWidth = prop['chart.linewidth']; for (var i=0; i 0) { if (prop['chart.colors.sequential']) { co.fillStyle = prop['chart.colors'][this.sequentialColorIndex++]; } else { co.fillStyle = prop['chart.colors'][0]; } // Draw the bar itself co.strokeRect(coords[i][0], coords[i][1], coords[i][2], coords[i][3]); co.fillRect(coords[i][0], coords[i][1], coords[i][2], coords[i][3]); } else { // Even if there's no redrawing - the color index needs incrementing this.sequentialColorIndex++ } } co.stroke(); co.fill(); }; /** * Returns the X coords for a value. Returns two coords because there are... two scales. * * @param number value The value to get the coord for */ this.getXCoord = function (value) { if (value > this.max || value < 0) { return null; } var ret = []; // The offset into the graph area var offset = ((value / this.max) * this.axisWidth); // Get the coords (one fo each side) ret[0] = (this.gutterLeft + this.axisWidth) - offset; ret[1] = (ca.width - this.gutterRight - this.axisWidth) + offset; return ret; }; /** * 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'] = RG.array_clone(prop['chart.colors']); this.original_colors['chart.highlight.stroke'] = RG.array_clone(prop['chart.highlight.fill']); this.original_colors['chart.highlight.fill'] = RG.array_clone(prop['chart.highlight.fill']); this.original_colors['chart.axis.color'] = RG.array_clone(prop['chart.axis.color']); this.original_colors['chart.strokestyle'] = RG.array_clone(prop['chart.strokestyle']); } var props = this.properties; var colors = props['chart.colors']; for (var i=0; i