// 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}; // Module pattern (function (win, doc, undefined) { var RG = RGraph, ua = navigator.userAgent, ma = Math; /** * Initialise the various objects */ RG.Highlight = {}; RG.Registry = {}; RG.Registry.store = []; RG.Registry.store['chart.event.handlers'] = []; RG.Registry.store['__rgraph_event_listeners__'] = []; // Used in the new system for tooltips RG.Background = {}; RG.background = {}; RG.objects = []; RG.Resizing = {}; RG.events = []; RG.cursor = []; RG.Effects = RG.Effects || {}; RG.cache = []; RG.ObjectRegistry = {}; RG.ObjectRegistry.objects = {}; RG.ObjectRegistry.objects.byUID = []; RG.ObjectRegistry.objects.byCanvasID = []; /** * Some "constants". The ua variable is navigator.userAgent (definedabove) */ RG.PI = ma.PI; RG.HALFPI = RG.PI / 2; RG.TWOPI = RG.PI * 2; RG.ISFF = ua.indexOf('Firefox') != -1; RG.ISOPERA = ua.indexOf('Opera') != -1; RG.ISCHROME = ua.indexOf('Chrome') != -1; RG.ISSAFARI = ua.indexOf('Safari') != -1 && !RG.ISCHROME; RG.ISWEBKIT = ua.indexOf('WebKit') != -1; RG.ISIE = ua.indexOf('Trident') > 0 || navigator.userAgent.indexOf('MSIE') > 0; RG.ISIE6 = ua.indexOf('MSIE 6') > 0; RG.ISIE7 = ua.indexOf('MSIE 7') > 0; RG.ISIE8 = ua.indexOf('MSIE 8') > 0; RG.ISIE9 = ua.indexOf('MSIE 9') > 0; RG.ISIE10 = ua.indexOf('MSIE 10') > 0; RG.ISOLD = RGraph.ISIE6 || RGraph.ISIE7 || RGraph.ISIE8; // MUST be here RG.ISIE11UP = ua.indexOf('MSIE') == -1 && ua.indexOf('Trident') > 0; RG.ISIE10UP = RG.ISIE10 || RG.ISIE11UP; RG.ISIE9UP = RG.ISIE9 || RG.ISIE10UP; /** * Returns five values which are used as a nice scale * * @param max int The maximum value of the graph * @param obj object The graph object * @return array An appropriate scale */ RG.getScale = function (max, obj) { /** * Special case for 0 */ if (max == 0) { return ['0.2', '0.4', '0.6', '0.8', '1.0']; } var original_max = max; /** * Manually do decimals */ if (max <= 1) { if (max > 0.5) { return [0.2,0.4,0.6,0.8, Number(1).toFixed(1)]; } else if (max >= 0.1) { return obj.Get('chart.scale.round') ? [0.2,0.4,0.6,0.8,1] : [0.1,0.2,0.3,0.4,0.5]; } else { var tmp = max; var exp = 0; while (tmp < 1.01) { exp += 1; tmp *= 10; } var ret = ['2e-' + exp, '4e-' + exp, '6e-' + exp, '8e-' + exp, '10e-' + exp]; if (max <= ('5e-' + exp)) { ret = ['1e-' + exp, '2e-' + exp, '3e-' + exp, '4e-' + exp, '5e-' + exp]; } return ret; } } // Take off any decimals if (String(max).indexOf('.') > 0) { max = String(max).replace(/\.\d+$/, ''); } var interval = ma.pow(10, Number(String(Number(max)).length - 1)); var topValue = interval; while (topValue < max) { topValue += (interval / 2); } // Handles cases where the max is (for example) 50.5 if (Number(original_max) > Number(topValue)) { topValue += (interval / 2); } // Custom if the max is greater than 5 and less than 10 if (max < 10) { topValue = (Number(original_max) <= 5 ? 5 : 10); } /** * Added 02/11/2010 to create "nicer" scales */ if (obj && typeof(obj.Get('chart.scale.round')) == 'boolean' && obj.Get('chart.scale.round')) { topValue = 10 * interval; } return [topValue * 0.2, topValue * 0.4, topValue * 0.6, topValue * 0.8, topValue]; }; /** * Returns an appropriate scale. The return value is actualy an object consisting of: * scale.max * scale.min * scale.scale * * @param obj object The graph object * @param prop object An object consisting of configuration properties * @return object An object containg scale information */ RG.getScale2 = function (obj, opt) { var ca = obj.canvas; var co = obj.context; var prop = obj.properties; var numlabels = typeof(opt['ylabels.count']) == 'number' ? opt['ylabels.count'] : 5; var units_pre = typeof(opt['units.pre']) == 'string' ? opt['units.pre'] : ''; var units_post = typeof(opt['units.post']) == 'string' ? opt['units.post'] : ''; var max = Number(opt['max']); var min = typeof(opt['min']) == 'number' ? opt['min'] : 0; var strict = opt['strict']; var decimals = Number(opt['scale.decimals']); // Sometimes the default is null var point = opt['scale.point']; // Default is a string in all chart libraries so no need to cast it var thousand = opt['scale.thousand']; // Default is a string in all chart libraries so no need to cast it var original_max = max; var round = opt['scale.round']; var scale = {'max':1,'labels':[]}; /** * Special case for 0 * * ** Must be first ** */ if (!max) { var max = 1; var scale = {max:1,min:0,labels:[]}; for (var i=0; i 0.5) { max = 1; min = min; scale.min = min; for (var i=0; i= 0.1) { max = 0.5; min = min; scale = {'max': 0.5, 'min':min,'labels':[]} for (var i=0; i 0) { var numdecimals = ma.abs(max_str.substring(max_str.indexOf('e') + 1)); } else { var numdecimals = String(max).length - 2; } var max = 1 / ma.pow(10,numdecimals - 1); for (var i=0; i Number(topValue)) { topValue += (interval / 2); } // Custom if the max is greater than 5 and less than 10 if (max <= 10) { topValue = (Number(original_max) <= 5 ? 5 : 10); } // Added 02/11/2010 to create "nicer" scales if (obj && typeof(round) == 'boolean' && round) { topValue = 10 * interval; } scale.max = topValue; // Now generate the scale. Temporarily set the objects chart.scale.decimal and chart.scale.point to those //that we've been given as the number_format functuion looks at those instead of using argumrnts. var tmp_point = prop['chart.scale.point']; var tmp_thousand = prop['chart.scale.thousand']; obj.Set('chart.scale.thousand', thousand); obj.Set('chart.scale.point', point); for (var i=0; i=0; i-=1) { newarr.push(arr[i]); } return newarr; }; /** * Clears the canvas by setting the width. You can specify a colour if you wish. * * @param object canvas The canvas to clear * @param mixed Usually a color string to use to clear the canvas * with - could also be a gradient object */ RG.clear = RG.Clear = function (ca) { var obj = ca.__object__; var co = ca.getContext('2d'); var color = arguments[1] || (obj && obj.get('clearto')); if (!ca) { return; } RG.FireCustomEvent(obj, 'onbeforeclear'); if (RG.ISIE8 && !color) { color = 'white'; } /** * Can now clear the canvas back to fully transparent */ if (!color || (color && color === 'rgba(0,0,0,0)' || color === 'transparent')) { co.clearRect(0,0,ca.width, ca.height); // Reset the globalCompositeOperation co.globalCompositeOperation = 'source-over'; } else { co.fillStyle = color; co.beginPath(); if (RG.ISIE8) { co.fillRect(0,0,ca.width,ca.height); } else { co.fillRect(-10,-10,ca.width + 20,ca.height + 20); } co.fill(); } //if (RG.ClearAnnotations) { //RG.ClearAnnotations(ca.id); //} /** * This removes any background image that may be present */ if (RG.Registry.Get('chart.background.image.' + ca.id)) { var img = RG.Registry.Get('chart.background.image.' + ca.id); img.style.position = 'absolute'; img.style.left = '-10000px'; img.style.top = '-10000px'; } /** * This hides the tooltip that is showing IF it has the same canvas ID as * that which is being cleared */ if (RG.Registry.Get('chart.tooltip')) { RG.HideTooltip(ca); //RG.Redraw(); } /** * Set the cursor to default */ ca.style.cursor = 'default'; RG.FireCustomEvent(obj, 'onclear'); }; /** * Draws the title of the graph * * @param object canvas The canvas object * @param string text The title to write * @param integer gutter The size of the gutter * @param integer The center X point (optional - if not given it will be generated from the canvas width) * @param integer Size of the text. If not given it will be 14 * @param object An optional object which has canvas and context properties to use instead of those on * the obj argument (so as to enable caching) */ RG.drawTitle = RG.DrawTitle = function (obj, text, gutterTop) { var ca = canvas = obj.canvas; var co = context = obj.context; var prop = obj.properties; if (arguments[5]) { var ca = canvas = arguments[5].canvas; var co = context = arguments[5].context; } var gutterLeft = prop['chart.gutter.left']; var gutterRight = prop['chart.gutter.right']; var gutterTop = gutterTop; var gutterBottom = prop['chart.gutter.bottom']; var size = arguments[4] ? arguments[4] : 12; var bold = prop['chart.title.bold']; var centerx = (arguments[3] ? arguments[3] : ((ca.width - gutterLeft - gutterRight) / 2) + gutterLeft); var keypos = prop['chart.key.position']; var vpos = prop['chart.title.vpos']; var hpos = prop['chart.title.hpos']; var bgcolor = prop['chart.title.background']; var x = prop['chart.title.x']; var y = prop['chart.title.y']; var halign = 'center'; var valign = 'center'; // Account for 3D effect by faking the key position if (obj.type == 'bar' && prop['chart.variant'] == '3d') { keypos = 'gutter'; } co.beginPath(); co.fillStyle = prop['chart.text.color'] ? prop['chart.text.color'] : 'black'; /** * Vertically center the text if the key is not present */ if (keypos && keypos != 'gutter') { var valign = 'center'; } else if (!keypos) { var valign = 'center'; } else { var valign = 'bottom'; } // if chart.title.vpos is a number, use that if (typeof prop['chart.title.vpos'] === 'number') { vpos = prop['chart.title.vpos'] * gutterTop; if (prop['chart.xaxispos'] === 'top') { vpos = prop['chart.title.vpos'] * gutterBottom + gutterTop + (ca.height - gutterTop - gutterBottom); } } else { vpos = gutterTop - size - 5; if (prop['chart.xaxispos'] === 'top') { vpos = ca.height - gutterBottom + size + 5; } } // if chart.title.hpos is a number, use that. It's multiplied with the (entire) canvas width if (typeof hpos === 'number') { centerx = hpos * ca.width; } /** * Now the chart.title.x and chart.title.y settings override (is set) the above */ if (typeof x === 'number') centerx = x; if (typeof y === 'number') vpos = y; /** * Horizontal alignment can now (Jan 2013) be specified */ if (typeof prop['chart.title.halign'] === 'string') { halign = prop['chart.title.halign']; } /** * Vertical alignment can now (Jan 2013) be specified */ if (typeof prop['chart.title.valign'] === 'string') { valign = prop['chart.title.valign']; } // Set the colour if (typeof prop['chart.title.color'] !== null) { var oldColor = co.fillStyle var newColor = prop['chart.title.color']; co.fillStyle = newColor ? newColor : 'black'; } /** * Default font is Arial */ var font = prop['chart.text.font']; /** * Override the default font with chart.title.font */ if (typeof prop['chart.title.font'] === 'string') { font = prop['chart.title.font']; } /** * Draw the title */ RG.Text2(co, {'font':font, 'size':size, 'x':centerx, 'y':vpos, 'text':text, 'valign':valign, 'halign':halign, 'bounding':bgcolor != null, 'bounding.fill':bgcolor, 'bold':bold, 'tag':'title' }); // Reset the fill colour co.fillStyle = oldColor; }; /** * Gets the mouse X/Y coordinates relative to the canvas * * @param object e The event object. As such this method should be used in an event listener. */ RG.getMouseXY = function(e) { var el = e.target; var ca = el; var caStyle = ca.style; var offsetX = 0; var offsetY = 0; var x; var y; var ISFIXED = (ca.style.position == 'fixed'); var borderLeft = parseInt(caStyle.borderLeftWidth) || 0; var borderTop = parseInt(caStyle.borderTopWidth) || 0; var paddingLeft = parseInt(caStyle.paddingLeft) || 0 var paddingTop = parseInt(caStyle.paddingTop) || 0 var additionalX = borderLeft + paddingLeft; var additionalY = borderTop + paddingTop; if (typeof e.offsetX === 'number' && typeof e.offsetY === 'number') { if (ISFIXED) { if (RG.ISOPERA) { x = e.offsetX; y = e.offsetY; } else if (RG.ISWEBKIT) { x = e.offsetX - paddingLeft - borderLeft; y = e.offsetY - paddingTop - borderTop; } else if (RG.ISIE) { x = e.offsetX - paddingLeft; y = e.offsetY - paddingTop; } else { x = e.offsetX; y = e.offsetY; } } else { if (!RG.ISIE && !RG.ISOPERA) { x = e.offsetX - borderLeft - paddingLeft; y = e.offsetY - borderTop - paddingTop; } else if (RG.ISIE) { x = e. offsetX - paddingLeft; y = e.offsetY - paddingTop; } else { x = e.offsetX; y = e.offsetY; } } } else { if (typeof el.offsetParent !== 'undefined') { do { offsetX += el.offsetLeft; offsetY += el.offsetTop; } while ((el = el.offsetParent)); } x = e.pageX - offsetX - additionalX; y = e.pageY - offsetY - additionalY; x -= (2 * (parseInt(document.body.style.borderLeftWidth) || 0)); y -= (2 * (parseInt(document.body.style.borderTopWidth) || 0)); //x += (parseInt(caStyle.borderLeftWidth) || 0); //y += (parseInt(caStyle.borderTopWidth) || 0); } // We return a javascript array with x and y defined return [x, y]; }; /** * This function returns a two element array of the canvas x/y position in * relation to the page * * @param object canvas */ RG.getCanvasXY = function (canvas) { var x = 0; var y = 0; var el = canvas; // !!! do { x += el.offsetLeft; y += el.offsetTop; // ACCOUNT FOR TABLES IN wEBkIT if (el.tagName.toLowerCase() == 'table' && (RG.ISCHROME || RG.ISSAFARI)) { x += parseInt(el.border) || 0; y += parseInt(el.border) || 0; } el = el.offsetParent; } while (el && el.tagName.toLowerCase() != 'body'); var paddingLeft = canvas.style.paddingLeft ? parseInt(canvas.style.paddingLeft) : 0; var paddingTop = canvas.style.paddingTop ? parseInt(canvas.style.paddingTop) : 0; var borderLeft = canvas.style.borderLeftWidth ? parseInt(canvas.style.borderLeftWidth) : 0; var borderTop = canvas.style.borderTopWidth ? parseInt(canvas.style.borderTopWidth) : 0; if (navigator.userAgent.indexOf('Firefox') > 0) { x += parseInt(document.body.style.borderLeftWidth) || 0; y += parseInt(document.body.style.borderTopWidth) || 0; } return [x + paddingLeft + borderLeft, y + paddingTop + borderTop]; }; /** * This function determines whther a canvas is fixed (CSS positioning) or not. If not it returns * false. If it is then the element that is fixed is returned (it may be a parent of the canvas). * * @return Either false or the fixed positioned element */ RG.isFixed = function (canvas) { var obj = canvas; var i = 0; while (obj && obj.tagName.toLowerCase() != 'body' && i < 99) { if (obj.style.position == 'fixed') { return obj; } obj = obj.offsetParent; } return false; }; /** * Registers a graph object (used when the canvas is redrawn) * * @param object obj The object to be registered */ RG.register = RG.Register = function (obj) { // Checking this property ensures the object is only registered once if (!obj.Get('chart.noregister')) { // As of 21st/1/2012 the object registry is now used RGraph.ObjectRegistry.Add(obj); obj.Set('chart.noregister', true); } }; /** * Causes all registered objects to be redrawn * * @param string An optional color to use to clear the canvas */ RG.redraw = RG.Redraw = function () { var objectRegistry = RGraph.ObjectRegistry.objects.byCanvasID; // Get all of the canvas tags on the page var tags = document.getElementsByTagName('canvas'); for (var i=0,len=tags.length; i 0) { return String(prepend + String(num) + append); } // We need then number as a string num = String(num); // Take off the decimal part - we re-append it later if (num.indexOf('.') > 0) { var tmp = num; num = num.replace(/\.(.*)/, ''); // The front part of the number decimal = tmp.replace(/(.*)\.(.*)/, '$2'); // The decimal part of the number } // Thousand seperator //var seperator = arguments[1] ? String(arguments[1]) : ','; var seperator = thousand_seperator; /** * Work backwards adding the thousand seperators */ var foundPoint; for (i=(num.length - 1),j=0; i>=0; j++,i--) { var character = num.charAt(i); if ( j % 3 == 0 && j != 0) { output += seperator; } /** * Build the output */ output += character; } /** * Now need to reverse the string */ var rev = output; output = ''; for (i=(rev.length - 1); i>=0; i--) { output += rev.charAt(i); } // Tidy up //output = output.replace(/^-,/, '-'); if (output.indexOf('-' + prop['chart.scale.thousand']) == 0) { output = '-' + output.substr(('-' + prop['chart.scale.thousand']).length); } // Reappend the decimal if (decimal.length) { output = output + decimal_seperator + decimal; decimal = ''; RegExp.$1 = ''; } // Minor bugette if (output.charAt(0) == '-') { output = output.replace(/-/, ''); prepend = '-' + prepend; } return prepend + output + append; }; /** * Draws horizontal coloured bars on something like the bar, line or scatter */ RG.drawBars = RG.DrawBars = function (obj) { var prop = obj.properties; var co = obj.context; var ca = obj.canvas; var hbars = prop['chart.background.hbars']; if (hbars === null) { return; } /** * Draws a horizontal bar */ co.beginPath(); for (i=0,len=hbars.length; i obj.scale2.max) start = obj.scale2.max; if (RG.is_null(length)) length = obj.scale2.max - start; if (start + length > obj.scale2.max) length = obj.scale2.max - start; if (start + length < (-1 * obj.scale2.max) ) length = (-1 * obj.scale2.max) - start; if (prop['chart.xaxispos'] == 'center' && start == obj.scale2.max && length < (obj.scale2.max * -2)) { length = obj.scale2.max * -2; } /** * Draw the bar */ var x = prop['chart.gutter.left']; var y = obj.getYCoord(start); var w = ca.width - prop['chart.gutter.left'] - prop['chart.gutter.right']; var h = obj.getYCoord(start + length) - y; // Accommodate Opera :-/ if (RG.ISOPERA != -1 && prop['chart.xaxispos'] == 'center' && h < 0) { h *= -1; y = y - h; } /** * Account for X axis at the top */ if (prop['chart.xaxispos'] == 'top') { y = ca.height - y; h *= -1; } co.fillStyle = color; co.fillRect(x, y, w, h); } /* // If the X axis is at the bottom, and a negative max is given, warn the user if (obj.Get('chart.xaxispos') == 'bottom' && (hbars[i][0] < 0 || (hbars[i][1] + hbars[i][1] < 0)) ) { alert('[' + obj.type.toUpperCase() + ' (ID: ' + obj.id + ') BACKGROUND HBARS] You have a negative value in one of your background hbars values, whilst the X axis is in the center'); } var ystart = (obj.grapharea - (((hbars[i][0] - obj.scale2.min) / (obj.scale2.max - obj.scale2.min)) * obj.grapharea)); //var height = (Math.min(hbars[i][1], obj.max - hbars[i][0]) / (obj.scale2.max - obj.scale2.min)) * obj.grapharea; var height = obj.getYCoord(hbars[i][0]) - obj.getYCoord(hbars[i][1]); // Account for the X axis being in the center if (obj.Get('chart.xaxispos') == 'center') { ystart /= 2; //height /= 2; } ystart += obj.Get('chart.gutter.top') var x = obj.Get('chart.gutter.left'); var y = ystart - height; var w = obj.canvas.width - obj.Get('chart.gutter.left') - obj.Get('chart.gutter.right'); var h = height; // Accommodate Opera :-/ if (navigator.userAgent.indexOf('Opera') != -1 && obj.Get('chart.xaxispos') == 'center' && h < 0) { h *= -1; y = y - h; } /** * Account for X axis at the top */ //if (obj.Get('chart.xaxispos') == 'top') { // y = obj.canvas.height - y; // h *= -1; //} //obj.context.fillStyle = hbars[i][2]; //obj.context.fillRect(x, y, w, h); //} }; /** * Draws in-graph labels. * * @param object obj The graph object */ RG.drawInGraphLabels = RG.DrawInGraphLabels = function (obj) { var ca = obj.canvas; var co = obj.context; var prop = obj.properties; var labels = prop['chart.labels.ingraph']; var labels_processed = []; // Defaults var fgcolor = 'black'; var bgcolor = 'white'; var direction = 1; if (!labels) { return; } /** * Preprocess the labels array. Numbers are expanded */ for (var i=0,len=labels.length; i 0) { for (var i=0,len=labels_processed.length; i 0) { var x = (obj.type == 'bar' ? coords[0] + (coords[2] / 2) : coords[0]); var y = (obj.type == 'bar' ? coords[1] + (coords[3] / 2) : coords[1]); var length = typeof labels_processed[i][4] === 'number' ? labels_processed[i][4] : 25; co.beginPath(); co.fillStyle = 'black'; co.strokeStyle = 'black'; if (obj.type === 'bar') { /** * X axis at the top */ if (obj.Get('chart.xaxispos') == 'top') { length *= -1; } if (prop['chart.variant'] == 'dot') { co.moveTo(ma.round(x), obj.coords[i][1] - 5); co.lineTo(ma.round(x), obj.coords[i][1] - 5 - length); var text_x = ma.round(x); var text_y = obj.coords[i][1] - 5 - length; } else if (prop['chart.variant'] == 'arrow') { co.moveTo(ma.round(x), obj.coords[i][1] - 5); co.lineTo(ma.round(x), obj.coords[i][1] - 5 - length); var text_x = ma.round(x); var text_y = obj.coords[i][1] - 5 - length; } else { co.arc(ma.round(x), y, 2.5, 0, 6.28, 0); co.moveTo(ma.round(x), y); co.lineTo(ma.round(x), y - length); var text_x = ma.round(x); var text_y = y - length; } co.stroke(); co.fill(); } else if (obj.type == 'line') { if ( typeof labels_processed[i] == 'object' && typeof labels_processed[i][3] == 'number' && labels_processed[i][3] == -1 ) { co.moveTo(ma.round(x), y + 5); co.lineTo(ma.round(x), y + 5 + length); co.stroke(); co.beginPath(); // This draws the arrow co.moveTo(ma.round(x), y + 5); co.lineTo(ma.round(x) - 3, y + 10); co.lineTo(ma.round(x) + 3, y + 10); co.closePath(); var text_x = x; var text_y = y + 5 + length; } else { var text_x = x; var text_y = y - 5 - length; co.moveTo(ma.round(x), y - 5); co.lineTo(ma.round(x), y - 5 - length); co.stroke(); co.beginPath(); // This draws the arrow co.moveTo(ma.round(x), y - 5); co.lineTo(ma.round(x) - 3, y - 10); co.lineTo(ma.round(x) + 3, y - 10); co.closePath(); } co.fill(); } // Taken out on the 10th Nov 2010 - unnecessary //var width = context.measureText(labels[i]).width; co.beginPath(); // Fore ground color co.fillStyle = (typeof labels_processed[i] === 'object' && typeof labels_processed[i][1] === 'string') ? labels_processed[i][1] : 'black'; RG.Text2(obj,{'font':prop['chart.text.font'], 'size':prop['chart.text.size'], 'x':text_x, 'y':text_y, 'text': (typeof labels_processed[i] === 'object' && typeof labels_processed[i][0] === 'string') ? labels_processed[i][0] : labels_processed[i], 'valign': 'bottom', 'halign':'center', 'bounding':true, 'bounding.fill': (typeof labels_processed[i] === 'object' && typeof labels_processed[i][2] === 'string') ? labels_processed[i][2] : 'white', 'tag':'labels ingraph' }); co.fill(); } } } } }; /** * This function "fills in" key missing properties that various implementations lack * * @param object e The event object */ RG.fixEventObject = RG.FixEventObject = function (e) { if (RG.ISOLD) { var e = event; e.pageX = (event.clientX + doc.body.scrollLeft); e.pageY = (event.clientY + doc.body.scrollTop); e.target = event.srcElement; if (!doc.body.scrollTop && doc.documentElement.scrollTop) { e.pageX += parseInt(doc.documentElement.scrollLeft); e.pageY += parseInt(doc.documentElement.scrollTop); } } // Any browser that doesn't implement stopPropagation() (MSIE) if (!e.stopPropagation) { e.stopPropagation = function () {window.event.cancelBubble = true;} } return e; }; /** * Thisz function hides the crosshairs coordinates */ RG.hideCrosshairCoords = RG.HideCrosshairCoords = function () { var div = RG.Registry.Get('chart.coordinates.coords.div'); if ( div && div.style.opacity == 1 && div.__object__.Get('chart.crosshairs.coords.fadeout') ) { var style = RG.Registry.Get('chart.coordinates.coords.div').style; setTimeout(function() {style.opacity = 0.9;}, 25); setTimeout(function() {style.opacity = 0.8;}, 50); setTimeout(function() {style.opacity = 0.7;}, 75); setTimeout(function() {style.opacity = 0.6;}, 100); setTimeout(function() {style.opacity = 0.5;}, 125); setTimeout(function() {style.opacity = 0.4;}, 150); setTimeout(function() {style.opacity = 0.3;}, 175); setTimeout(function() {style.opacity = 0.2;}, 200); setTimeout(function() {style.opacity = 0.1;}, 225); setTimeout(function() {style.opacity = 0;}, 250); setTimeout(function() {style.display = 'none';}, 275); } }; /** * Draws the3D axes/background */ RG.draw3DAxes = RG.Draw3DAxes = function (obj) { var prop = obj.properties; var co = obj.context; var ca = obj.canvas; var gutterLeft = prop['chart.gutter.left']; var gutterRight = prop['chart.gutter.right']; var gutterTop = prop['chart.gutter.top']; var gutterBottom = prop['chart.gutter.bottom']; co.strokeStyle = '#aaa'; co.fillStyle = '#ddd'; // Draw the vertical left side co.beginPath(); co.moveTo(gutterLeft, gutterTop); co.lineTo(gutterLeft + 10, gutterTop - 5); co.lineTo(gutterLeft + 10, ca.height - gutterBottom - 5); co.lineTo(gutterLeft, ca.height - gutterBottom); // Draw the bottom floor co.moveTo(gutterLeft, ca.height - gutterBottom); co.lineTo(gutterLeft + 10, ca.height - gutterBottom - 5); co.lineTo(ca.width - gutterRight + 10, ca.height - gutterBottom - 5); co.lineTo(ca.width - gutterRight, ca.height - gutterBottom); co.closePath(); co.stroke(); co.fill(); }; /** * Draws a rectangle with curvy corners * * @param co object The context * @param x number The X coordinate (top left of the square) * @param y number The Y coordinate (top left of the square) * @param w number The width of the rectangle * @param h number The height of the rectangle * @param number The radius of the curved corners * @param boolean Whether the top left corner is curvy * @param boolean Whether the top right corner is curvy * @param boolean Whether the bottom right corner is curvy * @param boolean Whether the bottom left corner is curvy */ RG.strokedCurvyRect = function (co, x, y, w, h) { // The corner radius var r = arguments[5] ? arguments[5] : 3; // The corners var corner_tl = (arguments[6] || arguments[6] == null) ? true : false; var corner_tr = (arguments[7] || arguments[7] == null) ? true : false; var corner_br = (arguments[8] || arguments[8] == null) ? true : false; var corner_bl = (arguments[9] || arguments[9] == null) ? true : false; co.beginPath(); // Top left side co.moveTo(x + (corner_tl ? r : 0), y); co.lineTo(x + w - (corner_tr ? r : 0), y); // Top right corner if (corner_tr) { co.arc(x + w - r, y + r, r, RG.PI + RG.HALFPI, RG.TWOPI, false); } // Top right side co.lineTo(x + w, y + h - (corner_br ? r : 0) ); // Bottom right corner if (corner_br) { co.arc(x + w - r, y - r + h, r, RG.TWOPI, RG.HALFPI, false); } // Bottom right side co.lineTo(x + (corner_bl ? r : 0), y + h); // Bottom left corner if (corner_bl) { co.arc(x + r, y - r + h, r, RG.HALFPI, RG.PI, false); } // Bottom left side co.lineTo(x, y + (corner_tl ? r : 0) ); // Top left corner if (corner_tl) { co.arc(x + r, y + r, r, RG.PI, RG.PI + RG.HALFPI, false); } co.stroke(); }; /** * Draws a filled rectangle with curvy corners * * @param context object The context * @param x number The X coordinate (top left of the square) * @param y number The Y coordinate (top left of the square) * @param w number The width of the rectangle * @param h number The height of the rectangle * @param number The radius of the curved corners * @param boolean Whether the top left corner is curvy * @param boolean Whether the top right corner is curvy * @param boolean Whether the bottom right corner is curvy * @param boolean Whether the bottom left corner is curvy */ RG.filledCurvyRect = function (co, x, y, w, h) { // The corner radius var r = arguments[5] ? arguments[5] : 3; // The corners var corner_tl = (arguments[6] || arguments[6] == null) ? true : false; var corner_tr = (arguments[7] || arguments[7] == null) ? true : false; var corner_br = (arguments[8] || arguments[8] == null) ? true : false; var corner_bl = (arguments[9] || arguments[9] == null) ? true : false; co.beginPath(); // First draw the corners // Top left corner if (corner_tl) { co.moveTo(x + r, y + r); co.arc(x + r, y + r, r, RG.PI, RG.PI + RG.HALFPI, false); } else { co.fillRect(x, y, r, r); } // Top right corner if (corner_tr) { co.moveTo(x + w - r, y + r); co.arc(x + w - r, y + r, r, RG.PI + RG.HALFPI, 0, false); } else { co.moveTo(x + w - r, y); co.fillRect(x + w - r, y, r, r); } // Bottom right corner if (corner_br) { co.moveTo(x + w - r, y + h - r); co.arc(x + w - r, y - r + h, r, 0, RG.HALFPI, false); } else { co.moveTo(x + w - r, y + h - r); co.fillRect(x + w - r, y + h - r, r, r); } // Bottom left corner if (corner_bl) { co.moveTo(x + r, y + h - r); co.arc(x + r, y - r + h, r, RG.HALFPI, RG.PI, false); } else { co.moveTo(x, y + h - r); co.fillRect(x, y + h - r, r, r); } // Now fill it in co.fillRect(x + r, y, w - r - r, h); co.fillRect(x, y + r, r + 1, h - r - r); co.fillRect(x + w - r - 1, y + r, r + 1, h - r - r); co.fill(); }; /** * Hides the zoomed canvas */ RG.hideZoomedCanvas = RG.HideZoomedCanvas = function () { var interval = 10; var frames = 15; if (typeof RG.zoom_image === 'object') { var obj = RG.zoom_image.obj; var prop = obj.properties; } else { return; } if (prop['chart.zoom.fade.out']) { for (var i=frames,j=1; i>=0; --i, ++j) { if (typeof RG.zoom_image === 'object') { setTimeout("RGraph.zoom_image.style.opacity = " + String(i / 10), j * interval); } } if (typeof RG.zoom_background === 'object') { setTimeout("RGraph.zoom_background.style.opacity = " + String(i / frames), j * interval); } } if (typeof RG.zoom_image === 'object') { setTimeout("RGraph.zoom_image.style.display = 'none'", prop['chart.zoom.fade.out'] ? (frames * interval) + 10 : 0); } if (typeof RG.zoom_background === 'object') { setTimeout("RGraph.zoom_background.style.display = 'none'", prop['chart.zoom.fade.out'] ? (frames * interval) + 10 : 0); } }; /** * Adds an event handler * * @param object obj The graph object * @param string event The name of the event, eg ontooltip * @param object func The callback function */ RG.addCustomEventListener = RG.AddCustomEventListener = function (obj, name, func) { var RG = RGraph; if (typeof RG.events[obj.uid] === 'undefined') { RG.events[obj.uid] = []; } RG.events[obj.uid].push([obj, name, func]); return RG.events[obj.uid].length - 1; }; /** * Used to fire one of the RGraph custom events * * @param object obj The graph object that fires the event * @param string event The name of the event to fire */ RG.fireCustomEvent = RG.FireCustomEvent = function (obj, name) { if (obj && obj.isRGraph) { // New style of adding custom events if (obj[name]) { (obj[name])(obj); } var uid = obj.uid; if ( typeof uid === 'string' && typeof RG.events === 'object' && typeof RG.events[uid] === 'object' && RG.events[uid].length > 0) { for(var j=0; j=0; --i) { var obj = objects[i].getObjectByXY(e); if (obj) { return obj; } } }; /** * Retrieves the relevant objects based on the X/Y position. * NOTE This function returns an array of objects * * @param object e The event object * @return An array of pertinent objects. Note the there may be only one object */ RG.ObjectRegistry.getObjectsByXY = function (e) { var canvas = e.target; var ret = []; var objects = RG.ObjectRegistry.getObjectsByCanvasID(canvas.id); // Retrieve objects "front to back" for (var i=(objects.length - 1); i>=0; --i) { var obj = objects[i].getObjectByXY(e); if (obj) { ret.push(obj); } } return ret; }; /** * Retrieves the object with the corresponding UID * * @param string uid The UID to get the relevant object for */ RG.ObjectRegistry.getObjectByUID = function (uid) { var objects = RG.ObjectRegistry.objects.byUID; for (var i=0,len=objects.length; i= cx && y >= cy) { angle += RG.TWOPI; } else if (x >= cx && y < cy) { angle = (RG.HALFPI - angle) + (RG.PI + RG.HALFPI); } else if (x < cx && y < cy) { angle += RG.PI; } else { angle = RG.PI - angle; } /** * Upper and lower limit checking */ if (angle > RG.TWOPI) { angle -= RG.TWOPI; } return angle; }; /** * This function returns the distance between two points. In effect the * radius of an imaginary circle that is centered on x1 and y1. The name * of this function is derived from the word "Hypoteneuse", which in * trigonmetry is the longest side of a triangle * * @param number x1 The original X coordinate * @param number y1 The original Y coordinate * @param number x2 The target X coordinate * @param number y2 The target Y coordinate */ RG.getHypLength = function (x1, y1, x2, y2) { var ret = ma.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))); return ret; }; /** * This function gets the end point (X/Y coordinates) of a given radius. * You pass it the center X/Y and the radius and this function will return * the endpoint X/Y coordinates. * * @param number cx The center X coord * @param number cy The center Y coord * @param number r The lrngth of the radius */ RG.getRadiusEndPoint = function (cx, cy, angle, radius) { var x = cx + (ma.cos(angle) * radius); var y = cy + (ma.sin(angle) * radius); return [x, y]; }; /** * This installs all of the event listeners * * @param object obj The chart object */ RG.installEventListeners = RG.InstallEventListeners = function (obj) { var prop = obj.properties; /** * Don't attempt to install event listeners for older versions of MSIE */ if (RG.ISOLD) { return; } /** * If this function exists, then the dynamic file has been included. */ if (RG.installCanvasClickListener) { RG.installWindowMousedownListener(obj); RG.installWindowMouseupListener(obj); RG.installCanvasMousemoveListener(obj); RG.installCanvasMouseupListener(obj); RG.installCanvasMousedownListener(obj); RG.installCanvasClickListener(obj); } else if ( RG.hasTooltips(obj) || prop['chart.adjustable'] || prop['chart.annotatable'] || prop['chart.contextmenu'] || prop['chart.resizable'] || prop['chart.key.interactive'] || prop['chart.events.click'] || prop['chart.events.mousemove'] || typeof obj.onclick === 'function' || typeof obj.onmousemove === 'function' ) { alert('[RGRAPH] You appear to have used dynamic features but not included the file: RGraph.common.dynamic.js'); } }; /** * Loosly mimicks the PHP function print_r(); */ RG.pr = function (obj) { var indent = (arguments[2] ? arguments[2] : ' '); var str = ''; var counter = typeof arguments[3] == 'number' ? arguments[3] : 0; if (counter >= 5) { return ''; } switch (typeof obj) { case 'string': str += obj + ' (' + (typeof obj) + ', ' + obj.length + ')'; break; case 'number': str += obj + ' (' + (typeof obj) + ')'; break; case 'boolean': str += obj + ' (' + (typeof obj) + ')'; break; case 'function': str += 'function () {}'; break; case 'undefined': str += 'undefined'; break; case 'null': str += 'null'; break; case 'object': // In case of null if (RGraph.is_null(obj)) { str += indent + 'null\n'; } else { str += indent + 'Object {' + '\n' for (j in obj) { str += indent + ' ' + j + ' => ' + RGraph.pr(obj[j], true, indent + ' ', counter + 1) + '\n'; } str += indent + '}'; } break; default: str += 'Unknown type: ' + typeof obj + ''; break; } /** * Finished, now either return if we're in a recursed call, or alert() * if we're not. */ if (!arguments[1]) { alert(str); } return str; }; /** * Produces a dashed line * * @param object co The 2D context * @param number x1 The start X coordinate * @param number y1 The start Y coordinate * @param number x2 The end X coordinate * @param number y2 The end Y coordinate */ RG.dashedLine = RG.DashedLine = function(co, x1, y1, x2, y2) { /** * This is the size of the dashes */ var size = 5; /** * The optional fifth argument can be the size of the dashes */ if (typeof arguments[5] === 'number') { size = arguments[5]; } var dx = x2 - x1; var dy = y2 - y1; var num = ma.floor(ma.sqrt((dx * dx) + (dy * dy)) / size); var xLen = dx / num; var yLen = dy / num; var count = 0; do { (count % 2 == 0 && count > 0) ? co.lineTo(x1, y1) : co.moveTo(x1, y1); x1 += xLen; y1 += yLen; } while(count++ <= num); }; /** * Makes an AJAX call. It calls the given callback (a function) when ready * * @param string url The URL to retrieve * @param function callback A function that is called when the response is ready, there's an example below * called "myCallback". */ RG.AJAX = function (url, callback) { // Mozilla, Safari, ... if (window.XMLHttpRequest) { var httpRequest = new XMLHttpRequest(); // MSIE } else if (window.ActiveXObject) { var httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } httpRequest.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { this.__user_callback__ = callback; this.__user_callback__(this.responseText); } } httpRequest.open('GET', url, true); httpRequest.send(); }; /** * Makes an AJAX POST request. It calls the given callback (a function) when ready * * @param string url The URL to retrieve * @param object data The POST data * @param function callback A function that is called when the response is ready, there's an example below * called "myCallback". */ RG.AJAX.POST = function (url, data, callback) { // Used when building the POST string var crumbs = []; // Mozilla, Safari, ... if (window.XMLHttpRequest) { var httpRequest = new XMLHttpRequest(); // MSIE } else if (window.ActiveXObject) { var httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } httpRequest.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { this.__user_callback__ = callback; this.__user_callback__(this.responseText); } } httpRequest.open('POST', url, true); httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); for (i in data) { if (typeof i == 'string') { crumbs.push(i + '=' + encodeURIComponent(data[i])); } } httpRequest.send(crumbs.join('&')); }; /** * Uses the above function but calls the call back passing a number as its argument * * @param url string The URL to fetch * @param callback function Your callback function (which is passed the number as an argument) */ RG.AJAX.getNumber = function (url, callback) { RG.AJAX(url, function () { var num = parseFloat(this.responseText); callback(num); }); }; /** * Uses the above function but calls the call back passing a string as its argument * * @param url string The URL to fetch * @param callback function Your callback function (which is passed the string as an argument) */ RG.AJAX.getString = function (url, callback) { RG.AJAX(url, function () { var str = String(this.responseText); callback(str); }); }; /** * Uses the above function but calls the call back passing JSON (ie a JavaScript object ) as its argument * * @param url string The URL to fetch * @param callback function Your callback function (which is passed the JSON object as an argument) */ RG.AJAX.getJSON = function (url, callback) { RG.AJAX(url, function () { var json = eval('(' + this.responseText + ')'); callback(json); }); }; /** * Uses the above RGraph.AJAX function but calls the call back passing an array as its argument. * Useful if you're retrieving CSV data * * @param url string The URL to fetch * @param callback function Your callback function (which is passed the CSV/array as an argument) */ RG.AJAX.getCSV = function (url, callback) { var seperator = arguments[2] ? arguments[2] : ','; RG.AJAX(url, function () { var regexp = new RegExp(seperator); var arr = this.responseText.split(regexp); // Convert the strings to numbers for (var i=0,len=arr.length;i'); div.style.fontFamily = font; div.style.fontWeight = bold ? 'bold' : 'normal'; div.style.fontSize = (size || 12) + 'pt'; var size = [div.offsetWidth, div.offsetHeight]; //document.body.removeChild(div); RGraph.measuretext_cache[str] = size; return size; }; /* New text function. Accepts two arguments: * o obj - The chart object * o opt - An object/hash/map of properties. This can consist of: * x The X coordinate (REQUIRED) * y The Y coordinate (REQUIRED) * text The text to show (REQUIRED) * font The font to use * size The size of the text (in pt) * bold Whether the text shouldd be bold or not * marker Whether to show a marker that indicates the X/Y coordinates * valign The vertical alignment * halign The horizontal alignment * bounding Whether to draw a bounding box for the text * boundingStroke The strokeStyle of the bounding box * boundingFill The fillStyle of the bounding box */ RG.text2 = RG.Text2 = function (obj, opt) { /** * An RGraph object can be given, or a string or the 2D rendering context * The coords are placed on the obj.coordsText variable ONLY if it's an RGraph object. The function * still returns the cooords though in all cases. */ if (obj && obj.isRGraph) { var co = obj.context; var ca = obj.canvas; } else if (typeof obj == 'string') { var ca = document.getElementById(obj); var co = ca.getContext('2d'); } else if (typeof obj.getContext === 'function') { var ca = obj; var co = ca.getContext('2d'); } else if (obj.toString().indexOf('CanvasRenderingContext2D') != -1 || RGraph.ISIE8 && obj.moveTo) { var co = obj; var ca = obj.canvas; // IE7/8 } else if (RG.ISOLD && obj.fillText) { var co = obj; var ca = obj.canvas; } var x = opt.x; var y = opt.y; var originalX = x; var originalY = y; var text = opt.text; var text_multiline = text.split(/\r?\n/g); var numlines = text_multiline.length; var font = opt.font ? opt.font : 'Arial'; var size = opt.size ? opt.size : 10; var size_pixels = size * 1.5; var bold = opt.bold; var halign = opt.halign ? opt.halign : 'left'; var valign = opt.valign ? opt.valign : 'bottom'; var tag = typeof opt.tag == 'string' && opt.tag.length > 0 ? opt.tag : ''; var marker = opt.marker; var angle = opt.angle || 0; /** * Changed the name of boundingFill/boundingStroke - this allows you to still use those names */ if (typeof opt.boundingFill === 'string') opt['bounding.fill'] = opt.boundingFill; if (typeof opt.boundingStroke === 'string') opt['bounding.stroke'] = opt.boundingStroke; var bounding = opt.bounding; var bounding_stroke = opt['bounding.stroke'] ? opt['bounding.stroke'] : 'black'; var bounding_fill = opt['bounding.fill'] ? opt['bounding.fill'] : 'rgba(255,255,255,0.7)'; var bounding_shadow = opt['bounding.shadow']; var bounding_shadow_color = opt['bounding.shadow.color'] || '#ccc'; var bounding_shadow_blur = opt['bounding.shadow.blur'] || 3; var bounding_shadow_offsetx = opt['bounding.shadow.offsetx'] || 3; var bounding_shadow_offsety = opt['bounding.shadow.offsety'] || 3; var bounding_linewidth = opt['bounding.linewidth'] || 1; /** * Initialize the return value to an empty object */ var ret = {}; /** * The text arg must be a string or a number */ if (typeof text == 'number') { text = String(text); } if (typeof text != 'string') { alert('[RGRAPH TEXT] The text given must a string or a number'); return; } /** * This facilitates vertical text */ if (angle != 0) { co.save(); co.translate(x, y); co.rotate((ma.PI / 180) * angle) x = 0; y = 0; } /** * Set the font */ co.font = (opt.bold ? 'bold ' : '') + size + 'pt ' + font; /** * Measure the width/height. This must be done AFTER the font has been set */ var width=0; for (var i=0; i 1) { y -= ((numlines - 1) * size_pixels); } var boundingY = y - size_pixels - 2; } var boundingW = width + 4; var boundingH = height + 4; /** * Draw a bounding box if required */ if (bounding) { var pre_bounding_linewidth = co.lineWidth; var pre_bounding_strokestyle = co.strokeStyle; var pre_bounding_fillstyle = co.fillStyle; var pre_bounding_shadowcolor = co.shadowColor; var pre_bounding_shadowblur = co.shadowBlur; var pre_bounding_shadowoffsetx = co.shadowOffsetX; var pre_bounding_shadowoffsety = co.shadowOffsetY; co.lineWidth = bounding_linewidth; co.strokeStyle = bounding_stroke; co.fillStyle = bounding_fill; if (bounding_shadow) { co.shadowColor = bounding_shadow_color; co.shadowBlur = bounding_shadow_blur; co.shadowOffsetX = bounding_shadow_offsetx; co.shadowOffsetY = bounding_shadow_offsety; } //obj.context.strokeRect(boundingX, boundingY, width + 6, (size_pixels * numlines) + 4); //obj.context.fillRect(boundingX, boundingY, width + 6, (size_pixels * numlines) + 4); co.strokeRect(boundingX, boundingY, boundingW, boundingH); co.fillRect(boundingX, boundingY, boundingW, boundingH); // Reset the linewidth,colors and shadow to it's original setting co.lineWidth = pre_bounding_linewidth; co.strokeStyle = pre_bounding_strokestyle; co.fillStyle = pre_bounding_fillstyle; co.shadowColor = pre_bounding_shadowcolor co.shadowBlur = pre_bounding_shadowblur co.shadowOffsetX = pre_bounding_shadowoffsetx co.shadowOffsetY = pre_bounding_shadowoffsety } /** * Draw the text */ if (numlines > 1) { for (var i=0; i= 0) { if (RG.is_null(data[group])) { group++; grouped_index = 0; continue; } // Allow for numbers as well as arrays in the dataset if (typeof data[group] == 'number') { group++ grouped_index = 0; continue; } grouped_index++; if (grouped_index >= data[group].length) { group++; grouped_index = 0; } } return [group, grouped_index]; }; /** * This function highlights a rectangle * * @param object obj The chart object * @param number shape The coordinates of the rect to highlight */ RG.Highlight.rect = RG.Highlight.Rect = function (obj, shape) { var ca = obj.canvas; var co = obj.context; var prop = obj.properties; if (prop['chart.tooltips.highlight']) { // Safari seems to need this co.lineWidth = 1; /** * Draw a rectangle on the canvas to highlight the appropriate area */ co.beginPath(); co.strokeStyle = prop['chart.highlight.stroke']; co.fillStyle = prop['chart.highlight.fill']; co.rect(shape['x'],shape['y'],shape['width'],shape['height']); //co.fillRect(shape['x'],shape['y'],shape['width'],shape['height']); co.stroke(); co.fill(); } }; /** * This function highlights a point * * @param object obj The chart object * @param number shape The coordinates of the rect to highlight */ RG.Highlight.point = RG.Highlight.Point = function (obj, shape) { var prop = obj.properties; var ca = obj.canvas; var co = obj.context; if (prop['chart.tooltips.highlight']) { /** * Draw a rectangle on the canvas to highlight the appropriate area */ co.beginPath(); co.strokeStyle = prop['chart.highlight.stroke']; co.fillStyle = prop['chart.highlight.fill']; var radius = prop['chart.highlight.point.radius'] || 2; co.arc(shape['x'],shape['y'],radius, 0, RG.TWOPI, 0); co.stroke(); co.fill(); } }; /** * This is the same as Date.parse - though a little more flexible. * * @param string str The date string to parse * @return Returns the same thing as Date.parse */ RG.parseDate = function (str) { str = RG.trim(str); // Allow for: now (just the word "now") if (str === 'now') { str = (new Date()).toString(); } // Allow for: 2013-11-22 12:12:12 or 2013/11/22 12:12:12 if (str.match(/^(\d\d\d\d)(-|\/)(\d\d)(-|\/)(\d\d)( |T)(\d\d):(\d\d):(\d\d)$/)) { str = RegExp.$1 + '-' + RegExp.$3 + '-' + RegExp.$5 + 'T' + RegExp.$7 + ':' + RegExp.$8 + ':' + RegExp.$9; } // Allow for: 2013-11-22 if (str.match(/^\d\d\d\d-\d\d-\d\d$/)) { str = str.replace(/-/g, '/'); } // Allow for: 12:09:44 (time only using todays date) if (str.match(/^\d\d:\d\d:\d\d$/)) { var dateObj = new Date(); var date = dateObj.getDate(); var month = dateObj.getMonth() + 1; var year = dateObj.getFullYear(); // Pad the date/month with a zero if it's not two characters if (String(month).length === 1) month = '0' + month; if (String(date).length === 1) date = '0' + date; str = (year + '/' + month + '/' + date) + ' ' + str; } return Date.parse(str); }; /** * Reset all of the color values to their original values * * @param object */ RG.resetColorsToOriginalValues = function (obj) { if (obj.original_colors) { // Reset the colors to their original values for (var j in obj.original_colors) { if (typeof j === 'string' && j.substr(0,6) === 'chart.') { obj.properties[j] = RG.arrayClone(obj.original_colors[j]); } } } /** * If the function is present on the object to reset specific colors - use that */ if (typeof obj.resetColorsToOriginalValues === 'function') { obj.resetColorsToOriginalValues(); } // Reset the colorsParsed flag so that they're parsed for gradients again obj.colorsParsed = false; }; /** * This function is a short-cut for the canvas path syntax (which can be rather verbose) * * @param mixed obj This can either be the 2D context or an RGraph object * @param array path The path details */ RG.path = RG.Path = function (obj, path) { /** * Allow either the RGraph object or the context to be used as the first argument */ if (obj.isRGraph && typeof obj.type === 'string') { var co = obj.context; } else { var co = obj; } /** * If the Path information has been passed as a string - split it up */ if (typeof path == 'string') { path = path.split(/ +/); } /** * Go through the path information */ for (var i=0,len=path.length; i').attr({ width: obj.canvas.width, height: obj.canvas.height, id: 'background_cached_canvas' + obj.canvas.id }) //.appendTo($('body')) .get(0); //Add MSIE support if (typeof G_vmlCanvasManager === 'object' && G_vmlCanvasManager.initElement) { G_vmlCanvasManager.initElement(RG.cache[id].canvas); } RG.cache[id].context = RG.cache[id].canvas.getContext('2d'); // Antialiasing on the cache canvas RG.cache[id].context.translate(0.5,0.5); // Call the function func(obj, RG.cache[id].canvas, RG.cache[id].context); } // Now copy the contents of the cached canvas over to the main one. // The coordinates are -0.5 because of the anti-aliasing effect in // use on the main canvas obj.context.drawImage(RG.cache[id].canvas,-0.5,-0.5); }; /** * The function that runs through the supplied configuration and * converts it to the RGraph stylee. * * @param object conf The config * @param object The settings for the object */ RG.parseObjectStyleConfig = function (obj, config) { /** * The recursion function */ var recurse = function (obj, config, name, settings) { var i; for (key in config) { var isObject = false; // Default value var isArray = false; // Default value var value = config[key] if (!RG.isNull(value) && value.constructor) { isObject = value.constructor.toString().indexOf('Object') > 0; isArray = value.constructor.toString().indexOf('Array') > 0; } if (isObject && !isArray) { recurse(obj, config[key], name + '.' + key, settings); //} else if (isArray && value.length === 2 && typeof value[1] === 'object' && value[1].constructor.toString().indexOf('Array') === -1) { // settings[name + '.' + key] = value[0]; // recurse(obj, value[1], name + '.' + key, settings); } else if (key === 'self') { settings[name] = value; } else { settings[name + '.' + key] = value; } } return settings; }; /** * Go through the settings that we've been given */ var settings = recurse(obj, config, 'chart', {}); /** * Go through the settings and set them on the object */ for (key in settings) { if (typeof key === 'string') { obj.set(key, settings[key]); } } }; // End module pattern })(window, document); /** * Uses the alert() function to show the structure of the given variable * * @param mixed v The variable to print/alert the structure of */ window.$p = function (v) { RGraph.pr(arguments[0], arguments[1], arguments[3]); }; /** * A shorthand for the default alert() function */ window.$a = function (v) { alert(v); }; /** * Short-hand for console.log * * @param mixed v The variable to log to the console */ window.$cl = function (v) { return console.log(v); };