// 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 pie chart constructor * * @param data array The data to be represented on the Pie chart */ RGraph.Pie = function (conf) { /** * Allow for object config style */ if ( typeof conf === 'object' && typeof conf.data === 'object' && typeof conf.id === 'string') { var id = conf.id; var canvas = document.getElementById(id); var data = conf.data; 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 data = arguments[1]; } // Get the canvas and context objects this.id = id; this.canvas = canvas; this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null; this.canvas.__object__ = this; this.total = 0; this.subTotal = 0; this.angles = []; this.data = data; this.properties = []; this.type = 'pie'; this.isRGraph = true; this.coords = []; this.coords.key = []; this.coordsSticks = []; this.coordsText = []; this.uid = RGraph.CreateUID(); this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID(); this.colorsParsed = false; this.original_colors = []; this.firstDraw = true; // After the first draw this will be false /** * Compatibility with older browsers */ //RGraph.OldBrowserCompat(this.context); this.properties = { 'chart.colors': ['Gradient(red:#fcc)', 'Gradient(#ddd:#eee)', 'Gradient(#0f0:#cfc)', 'Gradient(blue:#ccf)', 'Gradient(#FB7BA3:#FCC7EE)', 'Gradient(yellow:#ffc)', 'Gradient(#000:#ccc)', 'Gradient(#EE9D80:#FEE5C8)', 'Gradient(cyan:#ccf)','Gradient(#9E7BF6:#C7B6D2)','Gradient(#78CAEA:#C5FBFD)','Gradient(#E284E9:#FDC4FF)','Gradient(white:#ccf)','Gradient(blue:#ccf)','Gradient(#9E7BF6:#C7B6D2)'], 'chart.strokestyle': 'white', 'chart.linewidth': 3, 'chart.labels': [], 'chart.labels.sticks': false, 'chart.labels.sticks.length': 7, 'chart.labels.sticks.color': '#aaa', 'chart.labels.sticks.hlength': 5, 'chart.labels.ingraph': null, 'chart.labels.ingraph.font': null, 'chart.labels.ingraph.size': null, 'chart.labels.ingraph.specific':null, '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': 0.5, '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.shadow': true, 'chart.shadow.color': '#aaa', 'chart.shadow.offsetx': 0, 'chart.shadow.offsety': 0, 'chart.shadow.blur': 15, 'chart.text.size': 10, 'chart.text.color': 'black', 'chart.text.font': 'Arial', 'chart.contextmenu': null, 'chart.tooltips': null, 'chart.tooltips.event': 'onclick', 'chart.tooltips.effect': 'fade', 'chart.tooltips.css.class': 'RGraph_tooltip', 'chart.tooltips.highlight': true, 'chart.highlight.style': '2d', 'chart.highlight.style.2d.fill': 'rgba(255,255,255,0.7)', 'chart.highlight.style.2d.stroke': 'rgba(255,255,255,0.7)', 'chart.centerx': null, 'chart.centery': null, 'chart.radius': null, 'chart.border': false, 'chart.border.color': 'rgba(255,255,255,0.5)', 'chart.key': null, 'chart.key.background': 'white', 'chart.key.position': 'graph', 'chart.key.halign': 'right', 'chart.key.shadow': false, 'chart.key.shadow.color': '#666', 'chart.key.shadow.blur': 3, 'chart.key.shadow.offsetx': 2, 'chart.key.shadow.offsety': 2, 'chart.key.position.gutter.boxed': false, 'chart.key.position.x': null, 'chart.key.position.y': null, 'chart.key.color.shape': 'square', 'chart.key.rounded': true, 'chart.key.linewidth': 1, 'chart.key.colors': null, 'chart.key.interactive': false, 'chart.key.interactive.highlight.chart.stroke': 'black', 'chart.key.interactive.highlight.chart.fill': 'rgba(255,255,255,0.7)', 'chart.key.interactive.highlight.label': 'rgba(255,0,0,0.2)', 'chart.key.text.color': 'black', 'chart.annotatable': false, 'chart.annotate.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.adjust': [0,0], 'chart.resize.handle.background': null, 'chart.variant': 'pie', 'chart.variant.donut.width': null, 'chart.exploded': [], 'chart.effect.roundrobin.multiplier': 1, 'chart.events.click': null, 'chart.events.mousemove': null, 'chart.centerpin': null, 'chart.centerpin.fill': 'gray', 'chart.centerpin.stroke': 'white', 'chart.origin': 0 - (Math.PI / 2), 'chart.events': true, 'chart.labels.colors': [] } /** * Calculate the total */ for (var i=0,len=data.length; i 0) { this.DrawBorders(); } /** * Now draw the segments again with shadow turned off. This is always performed, * not just if the shadow is on. */ var len = this.angles.length; var r = this.radius; for (var action=0; action<2; action+=1) { for (var i=0; i 0) || typeof(prop['chart.exploded']) == 'number') { var explosion = typeof(prop['chart.exploded']) == 'number' ? prop['chart.exploded'] : prop['chart.exploded'][index]; var x = 0; var y = 0; var h = explosion; var t = subTotal + (radians / 2); var x = (Math.cos(t) * explosion); var y = (Math.sin(t) * explosion); var r = this.radius; co.moveTo(this.centerx + x, this.centery + y); } else { var x = 0; var y = 0; var r = this.radius; } /** * Calculate the angles */ var startAngle = subTotal; var endAngle = ((subTotal + radians)); co.arc(this.centerx + x, this.centery + y, r, startAngle, endAngle, 0); if (prop['chart.variant'] == 'donut') { co.arc(this.centerx + x, this.centery + y, typeof(prop['chart.variant.donut.width']) == 'number' ? r - prop['chart.variant.donut.width'] : r / 2, endAngle, startAngle, true); } else { co.lineTo(this.centerx + x, this.centery + y); } co.closePath(); // Keep hold of the angles this.angles.push([subTotal, subTotal + radians, this.centerx + x, this.centery + y]); //co.stroke(); co.fill(); /** * Calculate the segment angle */ this.subTotal += radians; }; /** * Draws the graphs labels */ this.drawLabels = this.DrawLabels = function () { var hAlignment = 'left'; var vAlignment = 'center'; var labels = prop['chart.labels']; var context = co; var font = prop['chart.text.font']; var text_size = prop['chart.text.size']; var cx = this.centerx; var cy = this.centery; var r = this.radius; /** * Turn the shadow off */ RG.NoShadow(this); co.fillStyle = 'black'; co.beginPath(); /** * Draw the labels */ if (labels && labels.length) { for (i=0; i (RG.TWOPI + RG.HALFPI) ? 2 : -2) : 0) var y = cy + explosion_offsety + (((r + 10) * Math.sin(a))); /** * If sticks are enabled use the endpoints that have been saved */ if (this.coordsSticks && this.coordsSticks[i]) { var x = this.coordsSticks[i][4][0] + (x < cx ? -5 : 5); var y = this.coordsSticks[i][4][1]; } /** * Alignment */ //vAlignment = y < cy ? 'center' : 'center'; vAlignment = 'center'; hAlignment = x < cx ? 'right' : 'left'; co.fillStyle = prop['chart.text.color']; if ( typeof(prop['chart.labels.colors']) == 'object' && prop['chart.labels.colors'] && prop['chart.labels.colors'][i]) { co.fillStyle = prop['chart.labels.colors'][i]; } RG.Text2(this, {'font':font, 'size':text_size, 'x':x, 'y':y, 'text':labels[i], 'valign':vAlignment, 'halign':hAlignment, 'tag': 'labels' }); } co.fill(); } }; /** * This function draws the pie chart sticks (for the labels) */ this.drawSticks = this.DrawSticks = function () { var context = co; var offset = prop['chart.linewidth'] / 2; var exploded = prop['chart.exploded']; var sticks = prop['chart.labels.sticks']; var cx = this.centerx; var cy = this.centery; var radius = this.radius; var points = []; for (var i=0,len=this.angles.length; i cx ? 5 : -5); points[4] = [ points[2][0] + (points[2][0] > cx ? 5 + prop['chart.labels.sticks.hlength'] : -5 - prop['chart.labels.sticks.hlength']), points[2][1] ]; co.moveTo(points[0][0], points[0][1]); co.lineTo(points[1][0], points[1][1]); co.quadraticCurveTo(points[2][0], points[2][1], points[3][0], points[3][1]); co.lineTo(points[4][0], points[4][1]); co.stroke(); /** * Save the stick end coords */ this.coordsSticks[i] = [points[0],points[1], points[2], points[3], points[4]]; } }; /** * The (now Pie chart specific) getSegment function * * @param object e The event object */ this.getShape = this.getSegment = function (e) { RG.FixEventObject(e); // The optional arg provides a way of allowing some accuracy (pixels) var accuracy = arguments[1] ? arguments[1] : 0; var canvas = ca; var context = co; var mouseCoords = RG.getMouseXY(e); var mouseX = mouseCoords[0]; var mouseY = mouseCoords[1]; var r = this.radius; var angles = this.angles; var ret = []; for (var i=0,len=angles.length; i RG.TWOPI) ret[4] -= RG.TWOPI; /** * Add the tooltip to the returned shape */ var tooltip = RG.parseTooltipText ? RG.parseTooltipText(prop['chart.tooltips'], ret[5]) : null; /** * Now return textual keys as well as numerics */ ret['object'] = this; ret['x'] = ret[0]; ret['y'] = ret[1]; ret['radius'] = ret[2]; ret['angle.start'] = ret[3]; ret['angle.end'] = ret[4]; ret['index'] = ret[5]; ret['tooltip'] = tooltip; return ret; } return null; }; this.drawBorders = this.DrawBorders = function () { if (prop['chart.linewidth'] > 0) { co.lineWidth = prop['chart.linewidth']; co.strokeStyle = prop['chart.strokestyle']; var r = this.radius; for (var i=0,len=this.angles.length; i 0) { var cx = this.centerx; var cy = this.centery; co.beginPath(); co.strokeStyle = prop['chart.centerpin.stroke'] ? prop['chart.centerpin.stroke'] : prop['chart.strokestyle']; co.fillStyle = prop['chart.centerpin.fill'] ? prop['chart.centerpin.fill'] : prop['chart.strokestyle']; co.moveTo(cx, cy); co.arc(cx, cy, prop['chart.centerpin'], 0, RG.TWOPI, false); co.stroke(); co.fill(); } }; /** * 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.angles[idx][2]; var coordY = obj.angles[idx][3]; var angleStart = obj.angles[idx][0]; var angleEnd = obj.angles[idx][1]; var angleCenter = ((angleEnd - angleStart) / 2) + angleStart; var canvasXY = RGraph.getCanvasXY(obj.canvas); var gutterLeft = prop['chart.gutter.left']; var gutterTop = prop['chart.gutter.top']; var width = tooltip.offsetWidth; var height = tooltip.offsetHeight; var x = canvasXY[0] + this.angles[idx][2] + (Math.cos(angleCenter) * (prop['chart.variant'] == 'donut' && typeof(prop['chart.variant.donut.width']) == 'number' ? ((this.radius - prop['chart.variant.donut.width']) + (prop['chart.variant.donut.width'] / 2)) : (this.radius * (prop['chart.variant'] == 'donut' ? 0.75 : 0.5)))); var y = canvasXY[1] + this.angles[idx][3] + (Math.sin(angleCenter) * (prop['chart.variant'] == 'donut' && typeof(prop['chart.variant.donut.width']) == 'number' ? ((this.radius - prop['chart.variant.donut.width']) + (prop['chart.variant.donut.width'] / 2)) : (this.radius * (prop['chart.variant'] == 'donut' ? 0.75 : 0.5)))); // 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 ((x - (width / 2)) < 10) { tooltip.style.left = (x - (width * 0.1)) + 'px'; tooltip.style.top = (y - height - 4) + 'px'; img.style.left = ((width * 0.1) - 8.5) + 'px'; // RIGHT edge } else if ((x + (width / 2)) > (doc.body.offsetWidth - 10) ) { tooltip.style.left = (x - (width * 0.9)) + 'px'; tooltip.style.top = (y - height - 4) + 'px'; img.style.left = ((width * 0.9) - 8.5) + 'px'; // Default positioning - CENTERED } else { tooltip.style.left = (x - (width / 2)) + 'px'; tooltip.style.top = (y - height - 4) + 'px'; img.style.left = ((width * 0.5) - 8.5) + 'px'; } }; /** * This draws Ingraph labels */ this.drawInGraphLabels = this.DrawInGraphLabels = function () { var context = co; var cx = this.centerx; var cy = this.centery; if (prop['chart.variant'] == 'donut') { var r = this.radius * 0.75; if (typeof(prop['chart.variant.donut.width']) == 'number') { var r = (this.radius - prop['chart.variant.donut.width']) + (prop['chart.variant.donut.width'] / 2); } } else { var r = this.radius / 2; } for (var i=0,len=this.angles.length; i this.total) { return null; } var angle = (value / this.total) * RG.TWOPI; // Handle the origin (it can br -HALFPI or 0) angle += prop['chart.origin']; return angle; }; /** * 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.key.colors'] = RG.array_clone(prop['chart.key.colors']); this.original_colors['chart.strokestyle'] = RG.array_clone(prop['chart.strokestyle']); this.original_colors['chart.highlight.stroke'] = RG.array_clone(prop['chart.highlight.stroke']); this.original_colors['chart.highlight.style.2d.fill'] = RG.array_clone(prop['chart.highlight.style.2d.fill']); this.original_colors['chart.highlight.style.stroke'] = RG.array_clone(prop['chart.highlight.style.2d.stroke']); } for (var i=0; i