2 * o------------------------------------------------------------------------------o
3 * | This file is part of the RGraph package - you can learn more at: |
5 * | http://www.rgraph.net |
7 * | This package is licensed under the RGraph license. For all kinds of business |
8 * | purposes there is a small one-time licensing fee to pay and for non |
9 * | commercial purposes it is free to use. You can read the full license here: |
11 * | http://www.rgraph.net/LICENSE.txt |
12 * o------------------------------------------------------------------------------o
16 * Initialise the various objects
18 if (typeof(RGraph) == 'undefined') RGraph = {isRGraph:true,type:'common'};
22 RGraph.Registry.store = [];
23 RGraph.Registry.store['chart.event.handlers'] = [];
24 RGraph.background = {};
32 * Returns five values which are used as a nice scale
34 * @param max int The maximum value of the graph
35 * @param obj object The graph object
36 * @return array An appropriate scale
38 RGraph.getScale = function (max, obj)
44 return ['0.2', '0.4', '0.6', '0.8', '1.0'];
47 var original_max = max;
50 * Manually do decimals
54 return [0.2,0.4,0.6,0.8, Number(1).toFixed(1)];
56 } else if (max >= 0.1) {
57 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];
69 var ret = ['2e-' + exp, '4e-' + exp, '6e-' + exp, '8e-' + exp, '10e-' + exp];
72 if (max <= ('5e-' + exp)) {
73 ret = ['1e-' + exp, '2e-' + exp, '3e-' + exp, '4e-' + exp, '5e-' + exp];
80 // Take off any decimals
81 if (String(max).indexOf('.') > 0) {
82 max = String(max).replace(/\.\d+$/, '');
85 var interval = Math.pow(10, Number(String(Number(max)).length - 1));
86 var topValue = interval;
88 while (topValue < max) {
89 topValue += (interval / 2);
92 // Handles cases where the max is (for example) 50.5
93 if (Number(original_max) > Number(topValue)) {
94 topValue += (interval / 2);
97 // Custom if the max is greater than 5 and less than 10
99 topValue = (Number(original_max) <= 5 ? 5 : 10);
103 * Added 02/11/2010 to create "nicer" scales
105 if (obj && typeof(obj.Get('chart.scale.round')) == 'boolean' && obj.Get('chart.scale.round')) {
106 topValue = 10 * interval;
109 return [topValue * 0.2, topValue * 0.4, topValue * 0.6, topValue * 0.8, topValue];
114 * Returns the maximum value which is in an array
116 * @param array arr The array
117 * @param int Whether to ignore signs (ie negative/positive)
118 * @return int The maximum value in the array
120 RGraph.array_max = function (arr)
124 for (var i=0; i<arr.length; ++i) {
125 if (typeof(arr[i]) == 'number') {
126 max = (max ? Math.max(max, arguments[1] ? Math.abs(arr[i]) : arr[i]) : arr[i]);
135 * Returns the maximum value which is in an array
137 * @param array arr The array
138 * @param int len The length to pad the array to
139 * @param mixed The value to use to pad the array (optional)
141 RGraph.array_pad = function (arr, len)
143 if (arr.length < len) {
144 var val = arguments[2] ? arguments[2] : null;
146 for (var i=arr.length; i<len; ++i) {
156 * An array sum function
158 * @param array arr The array to calculate the total of
159 * @return int The summed total of the arrays elements
161 RGraph.array_sum = function (arr)
164 if (typeof(arr) == 'number') {
169 var len = arr.length;
171 for(i=0,sum=0;i<len;sum+=arr[i++]);
178 * A simple is_array() function
180 * @param mixed obj The object you want to check
181 * @return bool Whether the object is an array or not
183 RGraph.is_array = function (obj)
185 return obj != null && obj.constructor.toString().indexOf('Array') != -1;
190 * Converts degrees to radians
192 * @param int degrees The number of degrees
193 * @return float The number of radians
195 RGraph.degrees2Radians = function (degrees)
197 return degrees * (Math.PI / 180);
202 * This function draws an angled line. The angle is cosidered to be clockwise
204 * @param obj ctxt The context object
205 * @param int x The X position
206 * @param int y The Y position
207 * @param int angle The angle in RADIANS
208 * @param int length The length of the line
210 RGraph.lineByAngle = function (context, x, y, angle, length)
212 context.arc(x, y, length, angle, angle, false);
213 context.lineTo(x, y);
214 context.arc(x, y, length, angle, angle, false);
219 * This is a useful function which is basically a shortcut for drawing left, right, top and bottom alligned text.
221 * @param object context The context
222 * @param string font The font
223 * @param int size The size of the text
224 * @param int x The X coordinate
225 * @param int y The Y coordinate
226 * @param string text The text to draw
227 * @parm string The vertical alignment. Can be null. "center" gives center aligned text, "top" gives top aligned text.
228 * Anything else produces bottom aligned text. Default is bottom.
229 * @param string The horizontal alignment. Can be null. "center" gives center aligned text, "right" gives right aligned text.
230 * Anything else produces left aligned text. Default is left.
231 * @param bool Whether to show a bounding box around the text. Defaults not to
232 * @param int The angle that the text should be rotate at (IN DEGREES)
233 * @param string Background color for the text
234 * @param bool Whether the text is bold or not
235 * @param bool Whether the bounding box has a placement indicator
237 RGraph.Text = function (context, font, size, x, y, text)
240 * This calls the text function recursively to accommodate multi-line text
242 if (typeof(text) == 'string' && text.match(/\r?\n/)) {
244 var nextline = text.replace(/^.*\r?\n/, '');
246 RGraph.Text(context, font, size, arguments[9] == -90 ? (x + (size * 1.5)) : x, y + (size * 1.5), nextline, arguments[6] ? arguments[6] : null, 'center', arguments[8], arguments[9], arguments[10], arguments[11], arguments[12]);
248 text = text.replace(/\r?\n.*$/, '');
254 if (RGraph.isIE8()) {
259 context.font = (arguments[11] ? 'Bold ': '') + size + 'pt ' + font;
264 var originalFillStyle = context.fillStyle;
265 var originalLineWidth = context.lineWidth;
267 // Need these now the angle can be specified, ie defaults for the former two args
268 if (typeof(arguments[6]) == null) arguments[6] = 'bottom'; // Vertical alignment. Default to bottom/baseline
269 if (typeof(arguments[7]) == null) arguments[7] = 'left'; // Horizontal alignment. Default to left
270 if (typeof(arguments[8]) == null) arguments[8] = null; // Show a bounding box. Useful for positioning during development. Defaults to false
271 if (typeof(arguments[9]) == null) arguments[9] = 0; // Angle (IN DEGREES) that the text should be drawn at. 0 is middle right, and it goes clockwise
272 if (typeof(arguments[12]) == null) arguments[12] = true; // Whether the bounding box has the placement indicator
274 // The alignment is recorded here for purposes of Opera compatibility
275 if (navigator.userAgent.indexOf('Opera') != -1) {
276 context.canvas.__rgraph_valign__ = arguments[6];
277 context.canvas.__rgraph_halign__ = arguments[7];
280 // First, translate to x/y coords
283 context.canvas.__rgraph_originalx__ = x;
284 context.canvas.__rgraph_originaly__ = y;
286 context.translate(x, y);
290 // Rotate the canvas if need be
292 context.rotate(arguments[9] / 57.3);
295 // Vertical alignment - defaults to bottom
297 var vAlign = arguments[6];
299 if (vAlign == 'center') {
300 context.translate(0, size / 2);
301 } else if (vAlign == 'top') {
302 context.translate(0, size);
307 // Hoeizontal alignment - defaults to left
309 var hAlign = arguments[7];
310 var width = context.measureText(text).width;
313 if (hAlign == 'center') {
314 context.translate(-1 * (width / 2), 0)
315 } else if (hAlign == 'right') {
316 context.translate(-1 * width, 0)
322 context.fillStyle = originalFillStyle;
325 * Draw a bounding box if requested
328 context.fillText(text,0,0);
329 context.lineWidth = 0.5;
333 var width = context.measureText(text).width;
334 var ieOffset = RGraph.isIE8() ? 2 : 0;
336 context.translate(x, y);
337 context.strokeRect(0 - 3, 0 - 3 - size - ieOffset, width + 6, 0 + size + 6);
340 * If requested, draw a background for the text
345 var ieOffset = RGraph.isIE8() ? 2 : 0;
346 var width = context.measureText(text).width
348 //context.strokeStyle = 'gray';
349 context.fillStyle = arguments[10];
350 context.fillRect(x - offset, y - size - offset - ieOffset, width + (2 * offset), size + (2 * offset));
351 //context.strokeRect(x - offset, y - size - offset - ieOffset, width + (2 * offset), size + (2 * offset));
355 * Do the actual drawing of the text
357 context.fillStyle = originalFillStyle;
358 context.fillText(text,0,0);
362 arguments[7] == 'left' ? 0 : (arguments[7] == 'center' ? width / 2 : width ) - 2,
363 arguments[6] == 'bottom' ? 0 : (arguments[6] == 'center' ? (0 - size) / 2 : 0 - size) - 2,
371 // Reset the lineWidth
372 context.lineWidth = originalLineWidth;
379 * Clears the canvas by setting the width. You can specify a colour if you wish.
381 * @param object canvas The canvas to clear
383 RGraph.Clear = function (canvas)
385 var context = canvas.getContext('2d');
387 context.fillStyle = arguments[1] ? String(arguments[1]) : 'white';
389 context = canvas.getContext('2d');
391 context.fillRect(-5,-5,canvas.width + 5,canvas.height + 5);
394 if (RGraph.ClearAnnotations) {
395 RGraph.ClearAnnotations(canvas.id);
401 * Draws the title of the graph
403 * @param object canvas The canvas object
404 * @param string text The title to write
405 * @param integer gutter The size of the gutter
406 * @param integer The center X point (optional - if not given it will be generated from the canvas width)
407 * @param integer Size of the text. If not given it will be 14
409 RGraph.DrawTitle = function (canvas, text, gutter)
411 var obj = canvas.__object__;
412 var context = canvas.getContext('2d');
413 var size = arguments[4] ? arguments[4] : 12;
414 var centerx = (arguments[3] ? arguments[3] : canvas.width / 2);
415 var keypos = obj.Get('chart.key.position');
416 var vpos = gutter / 2;
417 var hpos = obj.Get('chart.title.hpos');
418 var bgcolor = obj.Get('chart.title.background');
420 // Account for 3D effect by faking the key position
421 if (obj.type == 'bar' && obj.Get('chart.variant') == '3d') {
426 context.fillStyle = obj.Get('chart.text.color') ? obj.Get('chart.text.color') : 'black';
429 * Vertically center the text if the key is not present
431 if (keypos && keypos != 'gutter') {
432 var vCenter = 'center';
434 } else if (!keypos) {
435 var vCenter = 'center';
438 var vCenter = 'bottom';
441 // if chart.title.vpos does not equal 0.5, use that
442 if (typeof(obj.Get('chart.title.vpos')) == 'number') {
443 vpos = obj.Get('chart.title.vpos') * gutter;
446 // if chart.title.hpos is a number, use that. It's multiplied with the (entire) canvas width
447 if (typeof(hpos) == 'number') {
448 centerx = hpos * canvas.width;
452 if (typeof(obj.Get('chart.title.color') != null)) {
453 var oldColor = context.fillStyle
454 var newColor = obj.Get('chart.title.color')
455 context.fillStyle = newColor ? newColor : 'black';
459 * Default font is Verdana
461 var font = obj.Get('chart.text.font');
464 * Draw the title itself
466 RGraph.Text(context, font, size, centerx, vpos, text, vCenter, 'center', bgcolor != null, null, bgcolor, true);
468 // Reset the fill colour
469 context.fillStyle = oldColor;
474 * This function returns the mouse position in relation to the canvas
476 * @param object e The event object.
478 RGraph.getMouseXY = function (e)
480 var obj = (RGraph.isIE8() ? event.srcElement : e.target);
484 if (RGraph.isIE8()) e = event;
486 // Browser with offsetX and offsetY
487 if (typeof(e.offsetX) == 'number' && typeof(e.offsetY) == 'number') {
496 while (obj != document.body && obj) {
500 obj = obj.offsetParent;
512 * This function returns a two element array of the canvas x/y position in
513 * relation to the page
515 * @param object canvas
517 RGraph.getCanvasXY = function (canvas)
528 obj = obj.offsetParent;
530 } while (obj && obj.tagName.toLowerCase() != 'body');
537 * Registers a graph object (used when the canvas is redrawn)
539 * @param object obj The object to be registered
541 RGraph.Register = function (obj)
543 var key = obj.id + '_' + obj.type;
545 RGraph.objects[key] = obj;
550 * Causes all registered objects to be redrawn
552 * @param string An optional string indicating which canvas is not to be redrawn
553 * @param string An optional color to use to clear the canvas
555 RGraph.Redraw = function ()
557 for (i in RGraph.objects) {
558 // TODO FIXME Maybe include more intense checking for whether the object is an RGraph object, eg obj.isRGraph == true ...?
560 typeof(i) == 'string'
561 && typeof(RGraph.objects[i]) == 'object'
562 && typeof(RGraph.objects[i].type) == 'string'
563 && RGraph.objects[i].isRGraph) {
565 if (!arguments[0] || arguments[0] != RGraph.objects[i].id) {
566 RGraph.Clear(RGraph.objects[i].canvas, arguments[1] ? arguments[1] : null);
567 RGraph.objects[i].Draw();
575 * Loosly mimicks the PHP function print_r();
577 RGraph.pr = function (obj)
580 var indent = (arguments[2] ? arguments[2] : '');
582 switch (typeof(obj)) {
592 str+= 'String (' + obj.length + '):'
594 str += '"' + String(obj) + '"';
604 str += 'Object\n' + indent + '(\n';
606 for (var i=0; i<obj.length; ++i) {
607 str += indent + ' ' + i + ' => ' + RGraph.pr(obj[i], true, indent + ' ') + '\n';
610 var str = str + indent + ')';
618 str += 'Boolean: ' + (obj ? 'true' : 'false');
623 * Finished, now either return if we're in a recursed call, or alert()
635 * The RGraph registry Set() function
637 * @param string name The name of the key
638 * @param mixed value The value to set
639 * @return mixed Returns the same value as you pass it
641 RGraph.Registry.Set = function (name, value)
644 RGraph.Registry.store[name] = value;
646 // Don't really need to do this, but ho-hum
652 * The RGraph registry Get() function
654 * @param string name The name of the particular setting to fetch
655 * @return mixed The value if exists, null otherwise
657 RGraph.Registry.Get = function (name)
659 //return RGraph.Registry.store[name] == null ? null : RGraph.Registry.store[name];
660 return RGraph.Registry.store[name];
665 * This function draws the background for the bar chart, line chart and scatter chart.
667 * @param object obj The graph object
669 RGraph.background.Draw = function (obj)
671 var canvas = obj.canvas;
672 var context = obj.context;
674 var gutter = obj.Get('chart.gutter');
675 var variant = obj.Get('chart.variant');
677 context.fillStyle = obj.Get('chart.text.color');
679 // If it's a bar and 3D variant, translate
680 if (variant == '3d') {
682 context.translate(10, -5);
686 if (typeof(obj.Get('chart.title.xaxis')) == 'string' && obj.Get('chart.title.xaxis').length) {
688 var size = obj.Get('chart.text.size');
689 var font = obj.Get('chart.text.font');
692 RGraph.Text(context, font, size + 2, obj.canvas.width / 2, canvas.height - (gutter * obj.Get('chart.title.xaxis.pos')), obj.Get('chart.title.xaxis'), 'center', 'center', false, false, false, true);
697 if (typeof(obj.Get('chart.title.yaxis')) == 'string' && obj.Get('chart.title.yaxis').length) {
699 var size = obj.Get('chart.text.size');
700 var font = obj.Get('chart.text.font');
703 RGraph.Text(context, font, size + 2, gutter * obj.Get('chart.title.yaxis.pos'), canvas.height / 2, obj.Get('chart.title.yaxis'), 'center', 'center', false, 270, false, true);
707 obj.context.beginPath();
709 // Draw the horizontal bars
710 context.fillStyle = obj.Get('chart.background.barcolor1');
711 height = (obj.canvas.height - obj.Get('chart.gutter'));
713 for (var i=gutter; i < height ; i+=80) {
714 obj.context.fillRect(gutter, i, obj.canvas.width - (gutter * 2), Math.min(40, obj.canvas.height - gutter - i) );
717 context.fillStyle = obj.Get('chart.background.barcolor2');
718 height = (obj.canvas.height - gutter);
720 for (var i= (40 + gutter); i < height; i+=80) {
721 obj.context.fillRect(gutter, i, obj.canvas.width - (gutter * 2), i + 40 > (obj.canvas.height - gutter) ? obj.canvas.height - (gutter + i) : 40);
727 // Draw the background grid
728 if (obj.Get('chart.background.grid')) {
730 // If autofit is specified, use the .numhlines and .numvlines along with the width to work
731 // out the hsize and vsize
732 if (obj.Get('chart.background.grid.autofit')) {
733 var vsize = (canvas.width - (2 * obj.Get('chart.gutter')) - (obj.type == 'gantt' ? 2 * obj.Get('chart.gutter') : 0)) / obj.Get('chart.background.grid.autofit.numvlines');
734 var hsize = (canvas.height - (2 * obj.Get('chart.gutter'))) / obj.Get('chart.background.grid.autofit.numhlines');
736 obj.Set('chart.background.grid.vsize', vsize);
737 obj.Set('chart.background.grid.hsize', hsize);
741 context.lineWidth = obj.Get('chart.background.grid.width') ? obj.Get('chart.background.grid.width') : 1;
742 context.strokeStyle = obj.Get('chart.background.grid.color');
744 // Draw the horizontal lines
745 if (obj.Get('chart.background.grid.hlines')) {
746 height = (canvas.height - gutter)
747 for (y=gutter; y < height; y+=obj.Get('chart.background.grid.hsize')) {
748 context.moveTo(gutter, y);
749 context.lineTo(canvas.width - gutter, y);
753 if (obj.Get('chart.background.grid.vlines')) {
754 // Draw the vertical lines
755 var width = (canvas.width - gutter)
756 for (x=gutter + (obj.type == 'gantt' ? (2 * gutter) : 0); x<=width; x+=obj.Get('chart.background.grid.vsize')) {
757 context.moveTo(x, gutter);
758 context.lineTo(x, obj.canvas.height - gutter);
762 if (obj.Get('chart.background.grid.border')) {
763 // Make sure a rectangle, the same colour as the grid goes around the graph
764 context.strokeStyle = obj.Get('chart.background.grid.color');
765 context.strokeRect(gutter, gutter, canvas.width - (2 * gutter), canvas.height - (2 * gutter));
771 // If it's a bar and 3D variant, translate
772 if (variant == '3d') {
776 // Draw the title if one is set
777 if ( typeof(obj.Get('chart.title')) == 'string') {
779 if (obj.type == 'gantt') {
783 RGraph.DrawTitle(canvas, obj.Get('chart.title'), gutter, null, obj.Get('chart.text.size') + 2);
791 * Returns the day number for a particular date. Eg 1st February would be 32
793 * @param object obj A date object
794 * @return int The day number of the given date
796 RGraph.GetDays = function (obj)
798 var year = obj.getFullYear();
799 var days = obj.getDate();
800 var month = obj.getMonth();
802 if (month == 0) return days;
803 if (month >= 1) days += 31;
804 if (month >= 2) days += 28;
806 // Leap years. Crude, but if this code is still being used
807 // when it stops working, then you have my permission to shoot
808 // me. Oh, you won't be able to - I'll be dead...
809 if (year >= 2008 && year % 4 == 0) days += 1;
811 if (month >= 3) days += 31;
812 if (month >= 4) days += 30;
813 if (month >= 5) days += 31;
814 if (month >= 6) days += 30;
815 if (month >= 7) days += 31;
816 if (month >= 8) days += 31;
817 if (month >= 9) days += 30;
818 if (month >= 10) days += 31;
819 if (month >= 11) days += 30;
839 * Draws the graph key (used by various graphs)
841 * @param object obj The graph object
842 * @param array key An array of the texts to be listed in the key
843 * @param colors An array of the colors to be used
845 RGraph.DrawKey = function (obj, key, colors)
847 var canvas = obj.canvas;
848 var context = obj.context;
849 context.lineWidth = 1;
854 * Key positioned in the gutter
856 var keypos = obj.Get('chart.key.position');
857 var textsize = obj.Get('chart.text.size');
858 var gutter = obj.Get('chart.gutter');
861 * Change the older chart.key.vpos to chart.key.position.y
863 if (typeof(obj.Get('chart.key.vpos')) == 'number') {
864 obj.Set('chart.key.position.y', obj.Get('chart.key.vpos') * gutter);
867 if (keypos && keypos == 'gutter') {
869 RGraph.DrawKey_gutter(obj, key, colors);
875 } else if (keypos && keypos == 'graph') {
877 RGraph.DrawKey_graph(obj, key, colors);
880 alert('[COMMON] (' + obj.id + ') Unknown key position: ' + keypos);
889 * This does the actual drawing of the key when it's in the graph
891 * @param object obj The graph object
892 * @param array key The key items to draw
893 * @param array colors An aray of colors that the key will use
895 RGraph.DrawKey_graph = function (obj, key, colors)
897 var canvas = obj.canvas;
898 var context = obj.context;
899 var text_size = typeof(obj.Get('chart.key.text.size')) == 'number' ? obj.Get('chart.key.text.size') : obj.Get('chart.text.size');
900 var text_font = obj.Get('chart.text.font');
901 var gutter = obj.Get('chart.gutter');
902 var hpos = obj.Get('chart.yaxispos') == 'right' ? gutter + 10 : canvas.width - gutter - 10;
903 var vpos = gutter + 10;
904 var title = obj.Get('chart.title');
905 var blob_size = text_size; // The blob of color
906 var hmargin = 8; // This is the size of the gaps between the blob of color and the text
907 var vmargin = 4; // This is the vertical margin of the key
908 var fillstyle = obj.Get('chart.key.background');
909 var strokestyle = 'black';
914 // Need to set this so that measuring the text works out OK
915 context.font = text_size + 'pt ' + obj.Get('chart.text.font');
917 // Work out the longest bit of text
918 for (i=0; i<key.length; ++i) {
919 width = Math.max(width, context.measureText(key[i]).width);
929 * Now we know the width, we can move the key left more accurately
931 if ( obj.Get('chart.yaxispos') == 'left'
932 || (obj.type == 'pie' && !obj.Get('chart.yaxispos'))
933 || (obj.type == 'hbar' && !obj.Get('chart.yaxispos'))
934 || (obj.type == 'rscatter' && !obj.Get('chart.yaxispos'))
935 || (obj.type == 'tradar' && !obj.Get('chart.yaxispos'))
936 || (obj.type == 'rose' && !obj.Get('chart.yaxispos'))
937 || (obj.type == 'funnel' && !obj.Get('chart.yaxispos'))
944 * Specific location coordinates
946 if (typeof(obj.Get('chart.key.position.x')) == 'number') {
947 hpos = obj.Get('chart.key.position.x');
950 if (typeof(obj.Get('chart.key.position.y')) == 'number') {
951 vpos = obj.Get('chart.key.position.y');
955 // Stipulate the shadow for the key box
956 if (obj.Get('chart.key.shadow')) {
957 context.shadowColor = obj.Get('chart.key.shadow.color');
958 context.shadowBlur = obj.Get('chart.key.shadow.blur');
959 context.shadowOffsetX = obj.Get('chart.key.shadow.offsetx');
960 context.shadowOffsetY = obj.Get('chart.key.shadow.offsety');
966 // Draw the box that the key resides in
968 context.fillStyle = obj.Get('chart.key.background');
969 context.strokeStyle = 'black';
972 if (arguments[3] != false) {
974 // Manually draw the MSIE shadow
975 if (RGraph.isIE8() && obj.Get('chart.key.shadow')) {
977 context.fillStyle = '#666';
979 if (obj.Get('chart.key.rounded')) {
980 RGraph.NoShadow(obj);
982 RGraph.filledCurvyRect(context,
983 xpos + obj.Get('chart.key.shadow.offsetx'),
984 gutter + 5 + obj.Get('chart.key.shadow.offsety'),
986 5 + ( (textsize + 5) * key.length),
992 context.fillRect(xpos + 2, gutter + 5 + 2, width - 5, 5 + ( (textsize + 5) * key.length));
995 context.fillStyle = obj.Get('chart.key.background');
999 // The older square rectangled key
1000 if (obj.Get('chart.key.rounded') == true) {
1001 context.beginPath();
1002 context.strokeStyle = strokestyle;
1003 RGraph.strokedCurvyRect(context, hpos, vpos, width - 5, 5 + ( (text_size + 5) * key.length),4);
1008 RGraph.NoShadow(obj);
1011 context.strokeRect(hpos, vpos, width - 5, 5 + ( (text_size + 5) * key.length));
1012 context.fillRect(hpos, vpos, width - 5, 5 + ( (text_size + 5) * key.length));
1016 RGraph.NoShadow(obj);
1018 context.beginPath();
1019 // Draw the labels given
1020 for (var i=key.length - 1; i>=0; i--) {
1021 var j = Number(i) + 1;
1023 // Draw the blob of color
1024 if (obj.Get('chart.key.color.shape') == 'circle') {
1025 context.beginPath();
1026 context.strokeStyle = 'rgba(0,0,0,0)';
1027 context.fillStyle = colors[i];
1028 context.arc(hpos + 5 + (blob_size / 2), vpos + (5 * j) + (text_size * j) - text_size + (blob_size / 2), blob_size / 2, 0, 6.26, 0);
1031 } else if (obj.Get('chart.key.color.shape') == 'line') {
1032 context.beginPath();
1033 context.strokeStyle = colors[i];
1034 context.moveTo(hpos + 5, vpos + (5 * j) + (text_size * j) - text_size + (blob_size / 2));
1035 context.lineTo(hpos + blob_size + 5, vpos + (5 * j) + (text_size * j) - text_size + (blob_size / 2));
1039 context.fillStyle = colors[i];
1040 context.fillRect(hpos + 5, vpos + (5 * j) + (text_size * j) - text_size, text_size, text_size + 1);
1043 context.beginPath();
1045 context.fillStyle = 'black';
1047 RGraph.Text(context,
1050 hpos + blob_size + 5 + 5,
1051 vpos + (5 * j) + (text_size * j),
1063 * This does the actual drawing of the key when it's in the gutter
1065 * @param object obj The graph object
1066 * @param array key The key items to draw
1067 * @param array colors An aray of colors that the key will use
1069 RGraph.DrawKey_gutter = function (obj, key, colors)
1071 var canvas = obj.canvas;
1072 var context = obj.context;
1073 var text_size = typeof(obj.Get('chart.key.text.size')) == 'number' ? obj.Get('chart.key.text.size') : obj.Get('chart.text.size');
1074 var text_font = obj.Get('chart.text.font');
1075 var gutter = obj.Get('chart.gutter');
1076 var hpos = canvas.width / 2;
1077 var vpos = (gutter / 2) - 5;
1078 var title = obj.Get('chart.title');
1079 var blob_size = text_size; // The blob of color
1080 var hmargin = 8; // This is the size of the gaps between the blob of color and the text
1081 var vmargin = 4; // This is the vertical margin of the key
1082 var fillstyle = obj.Get('chart.key.background');
1083 var strokestyle = 'black';
1088 // Need to work out the length of the key first
1089 context.font = text_size + 'pt ' + text_font;
1090 for (i=0; i<key.length; ++i) {
1092 length += blob_size;
1094 length += context.measureText(key[i]).width;
1102 * Work out hpos since in the Pie it isn't necessarily dead center
1104 if (obj.type == 'pie') {
1105 if (obj.Get('chart.align') == 'left') {
1106 var hpos = obj.radius + obj.Get('chart.gutter');
1108 } else if (obj.Get('chart.align') == 'right') {
1109 var hpos = obj.canvas.width - obj.radius - obj.Get('chart.gutter');
1112 hpos = canvas.width / 2;
1121 * This makes the key centered
1123 hpos -= (length / 2);
1127 * Override the horizontal/vertical positioning
1129 if (typeof(obj.Get('chart.key.position.x')) == 'number') {
1130 hpos = obj.Get('chart.key.position.x');
1132 if (typeof(obj.Get('chart.key.position.y')) == 'number') {
1133 vpos = obj.Get('chart.key.position.y');
1139 * Draw the box that the key sits in
1141 if (obj.Get('chart.key.position.gutter.boxed')) {
1143 if (obj.Get('chart.key.shadow')) {
1144 context.shadowColor = obj.Get('chart.key.shadow.color');
1145 context.shadowBlur = obj.Get('chart.key.shadow.blur');
1146 context.shadowOffsetX = obj.Get('chart.key.shadow.offsetx');
1147 context.shadowOffsetY = obj.Get('chart.key.shadow.offsety');
1151 context.beginPath();
1152 context.fillStyle = fillstyle;
1153 context.strokeStyle = strokestyle;
1155 if (obj.Get('chart.key.rounded')) {
1156 RGraph.strokedCurvyRect(context, hpos, vpos - vmargin, length, text_size + vmargin + vmargin)
1157 // Odd... RGraph.filledCurvyRect(context, hpos, vpos - vmargin, length, text_size + vmargin + vmargin);
1159 context.strokeRect(hpos, vpos - vmargin, length, text_size + vmargin + vmargin);
1160 context.fillRect(hpos, vpos - vmargin, length, text_size + vmargin + vmargin);
1167 RGraph.NoShadow(obj);
1172 * Draw the blobs of color and the text
1174 for (var i=0, pos=hpos; i<key.length; ++i) {
1177 // Draw the blob of color - line
1178 if (obj.Get('chart.key.color.shape') =='line') {
1180 context.beginPath();
1181 context.strokeStyle = colors[i];
1182 context.moveTo(pos, vpos + (blob_size / 2));
1183 context.lineTo(pos + blob_size, vpos + (blob_size / 2));
1187 } else if (obj.Get('chart.key.color.shape') == 'circle') {
1189 context.beginPath();
1190 context.fillStyle = colors[i];
1191 context.moveTo(pos, vpos + (blob_size / 2));
1192 context.arc(pos + (blob_size / 2), vpos + (blob_size / 2), (blob_size / 2), 0, 6.28, 0);
1198 context.beginPath();
1199 context.fillStyle = colors[i];
1200 context.fillRect(pos, vpos, blob_size, blob_size);
1208 context.beginPath();
1209 context.fillStyle = 'black';
1210 RGraph.Text(context, text_font, text_size, pos, vpos + text_size - 1, key[i]);
1212 pos += context.measureText(key[i]).width;
1222 * A shortcut for RGraph.pr()
1224 function pd(variable)
1226 RGraph.pr(variable);
1229 function p(variable)
1231 RGraph.pr(variable);
1235 * A shortcut for console.log - as used by Firebug and Chromes console
1237 function cl (variable)
1239 return console.log(variable);
1244 * Makes a clone of an object
1246 * @param obj val The object to clone
1248 RGraph.array_clone = function (obj)
1250 if(obj == null || typeof(obj) != 'object') {
1255 //var temp = new obj.constructor();
1257 for(var i=0;i<obj.length; ++i) {
1258 temp[i] = RGraph.array_clone(obj[i]);
1266 * This function reverses an array
1268 RGraph.array_reverse = function (arr)
1272 for (var i=arr.length - 1; i>=0; i--) {
1273 newarr.push(arr[i]);
1281 * Formats a number with thousand seperators so it's easier to read
1283 * @param integer num The number to format
1284 * @param string The (optional) string to prepend to the string
1285 * @param string The (optional) string to ap
1286 * pend to the string
1287 * @return string The formatted number
1289 RGraph.number_format = function (obj, num)
1292 var prepend = arguments[2] ? String(arguments[2]) : '';
1293 var append = arguments[3] ? String(arguments[3]) : '';
1296 var decimal_seperator = obj.Get('chart.scale.point') ? obj.Get('chart.scale.point') : '.';
1297 var thousand_seperator = obj.Get('chart.scale.thousand') ? obj.Get('chart.scale.thousand') : ',';
1301 // Ignore the preformatted version of "1e-2"
1302 if (String(num).indexOf('e') > 0) {
1303 return String(prepend + String(num) + append);
1306 // We need then number as a string
1309 // Take off the decimal part - we re-append it later
1310 if (num.indexOf('.') > 0) {
1311 num = num.replace(/\.(.*)/, '');
1312 decimal = RegExp.$1;
1315 // Thousand seperator
1316 //var seperator = arguments[1] ? String(arguments[1]) : ',';
1317 var seperator = thousand_seperator;
1320 * Work backwards adding the thousand seperators
1323 for (i=(num.length - 1),j=0; i>=0; j++,i--) {
1324 var character = num.charAt(i);
1326 if ( j % 3 == 0 && j != 0) {
1327 output += seperator;
1333 output += character;
1337 * Now need to reverse the string
1341 for (i=(rev.length - 1); i>=0; i--) {
1342 output += rev.charAt(i);
1346 output = output.replace(/^-,/, '-');
1348 // Reappend the decimal
1349 if (decimal.length) {
1350 output = output + decimal_seperator + decimal;
1356 if (output.charAt(0) == '-') {
1358 prepend = '-' + prepend;
1361 return prepend + output + append;
1366 * Draws horizontal coloured bars on something like the bar, line or scatter
1368 RGraph.DrawBars = function (obj)
1370 var hbars = obj.Get('chart.background.hbars');
1373 * Draws a horizontal bar
1375 obj.context.beginPath();
1377 for (i=0; i<hbars.length; ++i) {
1379 // If null is specified as the "height", set it to the upper max value
1380 if (hbars[i][1] == null) {
1381 hbars[i][1] = obj.max;
1383 // If the first index plus the second index is greater than the max value, adjust accordingly
1384 } else if (hbars[i][0] + hbars[i][1] > obj.max) {
1385 hbars[i][1] = obj.max - hbars[i][0];
1389 // If height is negative, and the abs() value is greater than .max, use a negative max instead
1390 if (Math.abs(hbars[i][1]) > obj.max) {
1391 hbars[i][1] = -1 * obj.max;
1395 // If start point is greater than max, change it to max
1396 if (Math.abs(hbars[i][0]) > obj.max) {
1397 hbars[i][0] = obj.max;
1400 // If start point plus height is less than negative max, use the negative max plus the start point
1401 if (hbars[i][0] + hbars[i][1] < (-1 * obj.max) ) {
1402 hbars[i][1] = -1 * (obj.max + hbars[i][0]);
1405 // If the X axis is at the bottom, and a negative max is given, warn the user
1406 if (obj.Get('chart.xaxispos') == 'bottom' && (hbars[i][0] < 0 || (hbars[i][1] + hbars[i][1] < 0)) ) {
1407 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');
1410 var ystart = (obj.grapharea - ((hbars[i][0] / obj.max) * obj.grapharea));
1411 var height = (Math.min(hbars[i][1], obj.max - hbars[i][0]) / obj.max) * obj.grapharea;
1413 // Account for the X axis being in the center
1414 if (obj.Get('chart.xaxispos') == 'center') {
1419 ystart += obj.Get('chart.gutter')
1421 var x = obj.Get('chart.gutter');
1422 var y = ystart - height;
1423 var w = obj.canvas.width - (2 * obj.Get('chart.gutter'));
1426 // Accommodate Opera :-/
1427 if (navigator.userAgent.indexOf('Opera') != -1 && obj.Get('chart.xaxispos') == 'center' && h < 0) {
1432 obj.context.fillStyle = hbars[i][2];
1433 obj.context.fillRect(x, y, w, h);
1441 * Draws in-graph labels.
1443 * @param object obj The graph object
1445 RGraph.DrawInGraphLabels = function (obj)
1447 var canvas = obj.canvas;
1448 var context = obj.context;
1449 var labels = obj.Get('chart.labels.ingraph');
1450 var labels_processed = [];
1453 var fgcolor = 'black';
1454 var bgcolor = 'white';
1462 * Preprocess the labels array. Numbers are expanded
1464 for (var i=0; i<labels.length; ++i) {
1465 if (typeof(labels[i]) == 'number') {
1466 for (var j=0; j<labels[i]; ++j) {
1467 labels_processed.push(null);
1469 } else if (typeof(labels[i]) == 'string' || typeof(labels[i]) == 'object') {
1470 labels_processed.push(labels[i]);
1473 labels_processed.push('');
1478 * Turn off any shadow
1480 RGraph.NoShadow(obj);
1482 if (labels_processed && labels_processed.length > 0) {
1484 for (var i=0; i<labels_processed.length; ++i) {
1485 if (labels_processed[i]) {
1486 var coords = obj.coords[i];
1488 if (coords && coords.length > 0) {
1489 var x = (obj.type == 'bar' ? coords[0] + (coords[2] / 2) : coords[0]);
1490 var y = (obj.type == 'bar' ? coords[1] + (coords[3] / 2) : coords[1]);
1491 var length = typeof(labels_processed[i][4]) == 'number' ? labels_processed[i][4] : 25;
1493 context.beginPath();
1494 context.fillStyle = 'black';
1495 context.strokeStyle = 'black';
1498 if (obj.type == 'bar') {
1500 if (obj.Get('chart.variant') == 'dot') {
1501 context.moveTo(x, obj.coords[i][1] - 5);
1502 context.lineTo(x, obj.coords[i][1] - 5 - length);
1505 var text_y = obj.coords[i][1] - 5 - length;
1507 } else if (obj.Get('chart.variant') == 'arrow') {
1508 context.moveTo(x, obj.coords[i][1] - 5);
1509 context.lineTo(x, obj.coords[i][1] - 5 - length);
1512 var text_y = obj.coords[i][1] - 5 - length;
1516 context.arc(x, y, 2.5, 0, 6.28, 0);
1517 context.moveTo(x, y);
1518 context.lineTo(x, y - length);
1521 var text_y = y - length;
1528 } else if (obj.type == 'line') {
1531 typeof(labels_processed[i]) == 'object' &&
1532 typeof(labels_processed[i][3]) == 'number' &&
1533 labels_processed[i][3] == -1
1536 context.moveTo(x, y + 5);
1537 context.lineTo(x, y + 5 + length);
1540 context.beginPath();
1542 // This draws the arrow
1543 context.moveTo(x, y + 5);
1544 context.lineTo(x - 3, y + 10);
1545 context.lineTo(x + 3, y + 10);
1546 context.closePath();
1549 var text_y = y + 5 + length;
1554 var text_y = y - 5 - length;
1556 context.moveTo(x, y - 5);
1557 context.lineTo(x, y - 5 - length);
1560 context.beginPath();
1562 // This draws the arrow
1563 context.moveTo(x, y - 5);
1564 context.lineTo(x - 3, y - 10);
1565 context.lineTo(x + 3, y - 10);
1566 context.closePath();
1573 // Taken out on the 10th Nov 2010 - unnecessary
1574 //var width = context.measureText(labels[i]).width;
1576 context.beginPath();
1578 // Fore ground color
1579 context.fillStyle = (typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][1]) == 'string') ? labels_processed[i][1] : 'black';
1581 RGraph.Text(context,
1582 obj.Get('chart.text.font'),
1583 obj.Get('chart.text.size'),
1586 (typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][0]) == 'string') ? labels_processed[i][0] : labels_processed[i],
1591 (typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][2]) == 'string') ? labels_processed[i][2] : 'white');
1601 * This function "fills in" key missing properties that various implementations lack
1603 * @param object e The event object
1605 RGraph.FixEventObject = function (e)
1607 if (RGraph.isIE8()) {
1611 e.pageX = (event.clientX + document.body.scrollLeft);
1612 e.pageY = (event.clientY + document.body.scrollTop);
1613 e.target = event.srcElement;
1615 if (!document.body.scrollTop && document.documentElement.scrollTop) {
1616 e.pageX += parseInt(document.documentElement.scrollLeft);
1617 e.pageY += parseInt(document.documentElement.scrollTop);
1621 // This is mainly for FF which doesn't provide offsetX
1622 if (typeof(e.offsetX) == 'undefined' && typeof(e.offsetY) == 'undefined') {
1623 var coords = RGraph.getMouseXY(e);
1624 e.offsetX = coords[0];
1625 e.offsetY = coords[1];
1628 // Any browser that doesn't implement stopPropagation() (MSIE)
1629 if (!e.stopPropagation) {
1630 e.stopPropagation = function () {window.event.cancelBubble = true;}
1638 * Draw crosshairs if enabled
1640 * @param object obj The graph object (from which we can get the context and canvas as required)
1642 RGraph.DrawCrosshairs = function (obj)
1644 if (obj.Get('chart.crosshairs')) {
1645 var canvas = obj.canvas;
1646 var context = obj.context;
1648 // 5th November 2010 - removed now that tooltips are DOM2 based.
1649 //if (obj.Get('chart.tooltips') && obj.Get('chart.tooltips').length > 0) {
1650 //alert('[' + obj.type.toUpperCase() + '] Sorry - you cannot have crosshairs enabled with tooltips! Turning off crosshairs...');
1651 //obj.Set('chart.crosshairs', false);
1655 canvas.onmousemove = function (e)
1657 var e = RGraph.FixEventObject(e);
1658 var canvas = obj.canvas;
1659 var context = obj.context;
1660 var gutter = obj.Get('chart.gutter');
1661 var width = canvas.width;
1662 var height = canvas.height;
1663 var adjustments = obj.Get('chart.tooltips.coords.adjust');
1665 var mouseCoords = RGraph.getMouseXY(e);
1666 var x = mouseCoords[0];
1667 var y = mouseCoords[1];
1669 if (typeof(adjustments) == 'object' && adjustments[0] && adjustments[1]) {
1670 x = x - adjustments[0];
1671 y = y - adjustments[1];
1674 RGraph.Clear(canvas);
1679 && x <= (width - gutter)
1680 && y <= (height - gutter)
1683 var linewidth = obj.Get('chart.crosshairs.linewidth');
1684 context.lineWidth = linewidth ? linewidth : 1;
1686 context.beginPath();
1687 context.strokeStyle = obj.Get('chart.crosshairs.color');
1689 // Draw a top vertical line
1690 context.moveTo(x, gutter);
1691 context.lineTo(x, height - gutter);
1693 // Draw a horizontal line
1694 context.moveTo(gutter, y);
1695 context.lineTo(width - gutter, y);
1700 * Need to show the coords?
1702 if (obj.Get('chart.crosshairs.coords')) {
1703 if (obj.type == 'scatter') {
1705 var xCoord = (((x - obj.Get('chart.gutter')) / (obj.canvas.width - (2 * obj.Get('chart.gutter')))) * (obj.Get('chart.xmax') - obj.Get('chart.xmin'))) + obj.Get('chart.xmin');
1706 xCoord = xCoord.toFixed(obj.Get('chart.scale.decimals'));
1707 var yCoord = obj.max - (((y - obj.Get('chart.gutter')) / (obj.canvas.height - (2 * obj.Get('chart.gutter')))) * obj.max);
1709 if (obj.type == 'scatter' && obj.Get('chart.xaxispos') == 'center') {
1710 yCoord = (yCoord - (obj.max / 2)) * 2;
1713 yCoord = yCoord.toFixed(obj.Get('chart.scale.decimals'));
1714 var div = RGraph.Registry.Get('chart.coordinates.coords.div');
1715 var mouseCoords = RGraph.getMouseXY(e);
1716 var canvasXY = RGraph.getCanvasXY(canvas);
1720 div = document.createElement('DIV');
1721 div.__object__ = obj;
1722 div.style.position = 'absolute';
1723 div.style.backgroundColor = 'white';
1724 div.style.border = '1px solid black';
1725 div.style.fontFamily = 'Arial, Verdana, sans-serif';
1726 div.style.fontSize = '10pt'
1727 div.style.padding = '2px';
1728 div.style.opacity = 1;
1729 div.style.WebkitBorderRadius = '3px';
1730 div.style.borderRadius = '3px';
1731 div.style.MozBorderRadius = '3px';
1732 document.body.appendChild(div);
1734 RGraph.Registry.Set('chart.coordinates.coords.div', div);
1737 // Convert the X/Y pixel coords to correspond to the scale
1739 div.style.opacity = 1;
1740 div.style.display = 'inline';
1742 if (!obj.Get('chart.crosshairs.coords.fixed')) {
1743 div.style.left = Math.max(2, (e.pageX - div.offsetWidth - 3)) + 'px';
1744 div.style.top = Math.max(2, (e.pageY - div.offsetHeight - 3)) + 'px';
1746 div.style.left = canvasXY[0] + obj.Get('chart.gutter') + 3 + 'px';
1747 div.style.top = canvasXY[1] + obj.Get('chart.gutter') + 3 + 'px';
1750 div.innerHTML = '<span style="color: #666">' + obj.Get('chart.crosshairs.coords.labels.x') + ':</span> ' + xCoord + '<br><span style="color: #666">' + obj.Get('chart.crosshairs.coords.labels.y') + ':</span> ' + yCoord;
1752 canvas.addEventListener('mouseout', RGraph.HideCrosshairCoords, false);
1755 alert('[RGRAPH] Showing crosshair coordinates is only supported on the Scatter chart');
1759 RGraph.HideCrosshairCoords();
1766 * Thisz function hides the crosshairs coordinates
1768 RGraph.HideCrosshairCoords = function ()
1770 var div = RGraph.Registry.Get('chart.coordinates.coords.div');
1773 && div.style.opacity == 1
1774 && div.__object__.Get('chart.crosshairs.coords.fadeout')
1776 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.9;}, 50);
1777 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.8;}, 100);
1778 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.7;}, 150);
1779 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.6;}, 200);
1780 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.5;}, 250);
1781 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.4;}, 300);
1782 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.3;}, 350);
1783 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.2;}, 400);
1784 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.1;}, 450);
1785 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.opacity = 0;}, 500);
1786 setTimeout(function() {RGraph.Registry.Get('chart.coordinates.coords.div').style.display = 'none';}, 550);
1792 * Trims the right hand side of a string. Removes SPACE, TAB
1795 * @param string str The string to trim
1797 RGraph.rtrim = function (str)
1799 return str.replace(/( |\n|\r|\t)+$/, '');
1804 * Draws the3D axes/background
1806 RGraph.Draw3DAxes = function (obj)
1808 var gutter = obj.Get('chart.gutter');
1809 var context = obj.context;
1810 var canvas = obj.canvas;
1812 context.strokeStyle = '#aaa';
1813 context.fillStyle = '#ddd';
1815 // Draw the vertical left side
1816 context.beginPath();
1817 context.moveTo(gutter, gutter);
1818 context.lineTo(gutter + 10, gutter - 5);
1819 context.lineTo(gutter + 10, canvas.height - gutter - 5);
1820 context.lineTo(gutter, canvas.height - gutter);
1821 context.closePath();
1826 // Draw the bottom floor
1827 context.beginPath();
1828 context.moveTo(gutter, canvas.height - gutter);
1829 context.lineTo(gutter + 10, canvas.height - gutter - 5);
1830 context.lineTo(canvas.width - gutter + 10, canvas.height - gutter - 5);
1831 context.lineTo(canvas.width - gutter, canvas.height - gutter);
1832 context.closePath();
1839 * Turns off any shadow
1841 * @param object obj The graph object
1843 RGraph.NoShadow = function (obj)
1845 obj.context.shadowColor = 'rgba(0,0,0,0)';
1846 obj.context.shadowBlur = 0;
1847 obj.context.shadowOffsetX = 0;
1848 obj.context.shadowOffsetY = 0;
1853 * Sets the four shadow properties - a shortcut function
1855 * @param object obj Your graph object
1856 * @param string color The shadow color
1857 * @param number offsetx The shadows X offset
1858 * @param number offsety The shadows Y offset
1859 * @param number blur The blurring effect applied to the shadow
1861 RGraph.SetShadow = function (obj, color, offsetx, offsety, blur)
1863 obj.context.shadowColor = color;
1864 obj.context.shadowOffsetX = offsetx;
1865 obj.context.shadowOffsetY = offsety;
1866 obj.context.shadowBlur = blur;
1871 * This function attempts to "fill in" missing functions from the canvas
1872 * context object. Only two at the moment - measureText() nd fillText().
1874 * @param object context The canvas 2D context
1876 RGraph.OldBrowserCompat = function (context)
1878 if (!context.measureText) {
1880 // This emulates the measureText() function
1881 context.measureText = function (text)
1883 var textObj = document.createElement('DIV');
1884 textObj.innerHTML = text;
1885 textObj.style.backgroundColor = 'white';
1886 textObj.style.position = 'absolute';
1887 textObj.style.top = -100
1888 textObj.style.left = 0;
1889 document.body.appendChild(textObj);
1891 var width = {width: textObj.offsetWidth};
1893 textObj.style.display = 'none';
1899 if (!context.fillText) {
1900 // This emulates the fillText() method
1901 context.fillText = function (text, targetX, targetY)
1907 // If IE8, add addEventListener()
1908 if (!context.canvas.addEventListener) {
1909 window.addEventListener = function (ev, func, bubble)
1911 return this.attachEvent('on' + ev, func);
1914 context.canvas.addEventListener = function (ev, func, bubble)
1916 return this.attachEvent('on' + ev, func);
1923 * This function is for use with circular graph types, eg the Pie or Radar. Pass it your event object
1924 * and it will pass you back the corresponding segment details as an array:
1926 * [x, y, r, startAngle, endAngle]
1928 * Angles are measured in degrees, and are measured from the "east" axis (just like the canvas).
1930 * @param object e Your event object
1932 RGraph.getSegment = function (e)
1934 RGraph.FixEventObject(e);
1936 // The optional arg provides a way of allowing some accuracy (pixels)
1937 var accuracy = arguments[1] ? arguments[1] : 0;
1939 var obj = e.target.__object__;
1940 var canvas = obj.canvas;
1941 var context = obj.context;
1942 var mouseCoords = RGraph.getMouseXY(e);
1943 var x = mouseCoords[0] - obj.centerx;
1944 var y = mouseCoords[1] - obj.centery;
1946 var theta = Math.atan(y / x); // RADIANS
1947 var hyp = y / Math.sin(theta);
1948 var angles = obj.angles;
1950 var hyp = (hyp < 0) ? hyp + accuracy : hyp - accuracy;
1953 // Put theta in DEGREES
1956 // hyp should not be greater than radius if it's a Rose chart
1957 if (obj.type == 'rose') {
1958 if ( (isNaN(hyp) && Math.abs(mouseCoords[0]) < (obj.centerx - r) )
1959 || (isNaN(hyp) && Math.abs(mouseCoords[0]) > (obj.centerx + r))
1960 || (!isNaN(hyp) && Math.abs(hyp) > r)) {
1966 * Account for the correct quadrant
1968 if (x < 0 && y >= 0) {
1970 } else if (x < 0 && y < 0) {
1972 } else if (x > 0 && y < 0) {
1977 * Account for the rose chart
1979 if (obj.type == 'rose') {
1987 for (var i=0; i<angles.length; ++i) {
1988 if (theta >= angles[i][0] && theta < angles[i][1]) {
1990 hyp = Math.abs(hyp);
1992 if (obj.type == 'rose' && hyp > angles[i][2]) {
1996 if (!hyp || (obj.type == 'pie' && obj.radius && hyp > obj.radius) ) {
2000 if (obj.type == 'pie' && obj.Get('chart.variant') == 'donut' && (hyp > obj.radius || hyp < (obj.radius / 2) ) ) {
2004 ret[0] = obj.centerx;
2005 ret[1] = obj.centery;
2006 ret[2] = (obj.type == 'rose') ? angles[i][2] : obj.radius;
2007 ret[3] = angles[i][0];
2008 ret[4] = angles[i][1];
2011 if (obj.type == 'rose') {
2016 if (x > 0 && y < 0) {
2022 if (ret[3] < 0) ret[3] += 360;
2023 if (ret[4] > 360) ret[4] -= 360;
2034 * This is a function that can be used to run code asynchronously, which can
2035 * be used to speed up the loading of you pages.
2037 * @param string func This is the code to run. It can also be a function pointer.
2038 * The front page graphs show this function in action. Basically
2039 * each graphs code is made in a function, and that function is
2040 * passed to this function to run asychronously.
2042 RGraph.Async = function (func)
2044 return setTimeout(func, arguments[1] ? arguments[1] : 1);
2049 * A custom random number function
2051 * @param number min The minimum that the number should be
2052 * @param number max The maximum that the number should be
2053 * @param number How many decimal places there should be. Default for this is 0
2055 RGraph.random = function (min, max)
2057 var dp = arguments[2] ? arguments[2] : 0;
2058 var r = Math.random();
2060 return Number((((max - min) * r) + min).toFixed(dp));
2065 * Draws a rectangle with curvy corners
2067 * @param context object The context
2068 * @param x number The X coordinate (top left of the square)
2069 * @param y number The Y coordinate (top left of the square)
2070 * @param w number The width of the rectangle
2071 * @param h number The height of the rectangle
2072 * @param number The radius of the curved corners
2073 * @param boolean Whether the top left corner is curvy
2074 * @param boolean Whether the top right corner is curvy
2075 * @param boolean Whether the bottom right corner is curvy
2076 * @param boolean Whether the bottom left corner is curvy
2078 RGraph.strokedCurvyRect = function (context, x, y, w, h)
2080 // The corner radius
2081 var r = arguments[5] ? arguments[5] : 3;
2084 var corner_tl = (arguments[6] || arguments[6] == null) ? true : false;
2085 var corner_tr = (arguments[7] || arguments[7] == null) ? true : false;
2086 var corner_br = (arguments[8] || arguments[8] == null) ? true : false;
2087 var corner_bl = (arguments[9] || arguments[9] == null) ? true : false;
2089 context.beginPath();
2092 context.moveTo(x + (corner_tl ? r : 0), y);
2093 context.lineTo(x + w - (corner_tr ? r : 0), y);
2097 context.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2, false);
2101 context.lineTo(x + w, y + h - (corner_br ? r : 0) );
2103 // Bottom right corner
2105 context.arc(x + w - r, y - r + h, r, Math.PI * 2, Math.PI * 0.5, false);
2108 // Bottom right side
2109 context.lineTo(x + (corner_bl ? r : 0), y + h);
2111 // Bottom left corner
2113 context.arc(x + r, y - r + h, r, Math.PI * 0.5, Math.PI, false);
2117 context.lineTo(x, y + (corner_tl ? r : 0) );
2121 context.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5, false);
2129 * Draws a filled rectangle with curvy corners
2131 * @param context object The context
2132 * @param x number The X coordinate (top left of the square)
2133 * @param y number The Y coordinate (top left of the square)
2134 * @param w number The width of the rectangle
2135 * @param h number The height of the rectangle
2136 * @param number The radius of the curved corners
2137 * @param boolean Whether the top left corner is curvy
2138 * @param boolean Whether the top right corner is curvy
2139 * @param boolean Whether the bottom right corner is curvy
2140 * @param boolean Whether the bottom left corner is curvy
2142 RGraph.filledCurvyRect = function (context, x, y, w, h)
2144 // The corner radius
2145 var r = arguments[5] ? arguments[5] : 3;
2148 var corner_tl = (arguments[6] || arguments[6] == null) ? true : false;
2149 var corner_tr = (arguments[7] || arguments[7] == null) ? true : false;
2150 var corner_br = (arguments[8] || arguments[8] == null) ? true : false;
2151 var corner_bl = (arguments[9] || arguments[9] == null) ? true : false;
2153 context.beginPath();
2155 // First draw the corners
2159 context.moveTo(x + r, y + r);
2160 context.arc(x + r, y + r, r, Math.PI, 1.5 * Math.PI, false);
2162 context.fillRect(x, y, r, r);
2167 context.moveTo(x + w - r, y + r);
2168 context.arc(x + w - r, y + r, r, 1.5 * Math.PI, 0, false);
2170 context.moveTo(x + w - r, y);
2171 context.fillRect(x + w - r, y, r, r);
2175 // Bottom right corner
2177 context.moveTo(x + w - r, y + h - r);
2178 context.arc(x + w - r, y - r + h, r, 0, Math.PI / 2, false);
2180 context.moveTo(x + w - r, y + h - r);
2181 context.fillRect(x + w - r, y + h - r, r, r);
2184 // Bottom left corner
2186 context.moveTo(x + r, y + h - r);
2187 context.arc(x + r, y - r + h, r, Math.PI / 2, Math.PI, false);
2189 context.moveTo(x, y + h - r);
2190 context.fillRect(x, y + h - r, r, r);
2194 context.fillRect(x + r, y, w - r - r, h);
2195 context.fillRect(x, y + r, r + 1, h - r - r);
2196 context.fillRect(x + w - r - 1, y + r, r + 1, h - r - r);
2203 * A crude timing function
2205 * @param string label The label to use for the time
2207 RGraph.Timer = function (label)
2211 // This uses the Firebug console
2212 console.log(label + ': ' + d.getSeconds() + '.' + d.getMilliseconds());
2217 * Hides the palette if it's visible
2219 RGraph.HidePalette = function ()
2221 var div = RGraph.Registry.Get('palette');
2223 if (typeof(div) == 'object' && div) {
2224 div.style.visibility = 'hidden';
2225 div.style.display = 'none';
2226 RGraph.Registry.Set('palette', null);
2232 * Hides the zoomed canvas
2234 RGraph.HideZoomedCanvas = function ()
2236 if (typeof(__zoomedimage__) == 'object') {
2237 obj = __zoomedimage__.obj;
2242 if (obj.Get('chart.zoom.fade.out')) {
2243 for (var i=10,j=1; i>=0; --i, ++j) {
2244 if (typeof(__zoomedimage__) == 'object') {
2245 setTimeout("__zoomedimage__.style.opacity = " + String(i / 10), j * 30);
2249 if (typeof(__zoomedbackground__) == 'object') {
2250 setTimeout("__zoomedbackground__.style.opacity = " + String(i / 10), j * 30);
2254 if (typeof(__zoomedimage__) == 'object') {
2255 setTimeout("__zoomedimage__.style.display = 'none'", obj.Get('chart.zoom.fade.out') ? 310 : 0);
2258 if (typeof(__zoomedbackground__) == 'object') {
2259 setTimeout("__zoomedbackground__.style.display = 'none'", obj.Get('chart.zoom.fade.out') ? 310 : 0);
2265 * Adds an event handler
2267 * @param object obj The graph object
2268 * @param string event The name of the event, eg ontooltip
2269 * @param object func The callback function
2271 RGraph.AddCustomEventListener = function (obj, name, func)
2273 if (typeof(RGraph.events[obj.id]) == 'undefined') {
2274 RGraph.events[obj.id] = [];
2277 RGraph.events[obj.id].push([obj, name, func]);
2282 * Used to fire one of the RGraph custom events
2284 * @param object obj The graph object that fires the event
2285 * @param string event The name of the event to fire
2287 RGraph.FireCustomEvent = function (obj, name)
2289 for (i in RGraph.events) {
2290 if (typeof(i) == 'string' && i == obj.id && RGraph.events[i].length > 0) {
2291 for(var j=0; j<RGraph.events[i].length; ++j) {
2292 if (RGraph.events[i][j][1] == name) {
2293 RGraph.events[i][j][2](obj);
2302 * Checks the browser for traces of MSIE8
2304 RGraph.isIE8 = function ()
2306 return navigator.userAgent.indexOf('MSIE 8') > 0;
2311 * Checks the browser for traces of MSIE9
2313 RGraph.isIE9 = function ()
2315 return navigator.userAgent.indexOf('MSIE 9') > 0;
2320 * Checks the browser for traces of MSIE9
2322 RGraph.isIE9up = function ()
2324 navigator.userAgent.match(/MSIE (\d+)/);
2326 return Number(RegExp.$1) >= 9;
2331 * This clears a canvases event handlers.
2333 * @param string id The ID of the canvas whose event handlers will be cleared
2335 RGraph.ClearEventListeners = function (id)
2337 for (var i=0; i<RGraph.Registry.Get('chart.event.handlers').length; ++i) {
2339 var el = RGraph.Registry.Get('chart.event.handlers')[i];
2341 if (el && (el[0] == id || el[0] == ('window_' + id)) ) {
2342 if (el[0].substring(0, 7) == 'window_') {
2343 window.removeEventListener(el[1], el[2], false);
2345 document.getElementById(id).removeEventListener(el[1], el[2], false);
2348 RGraph.Registry.Get('chart.event.handlers')[i] = null;
2357 RGraph.AddEventListener = function (id, e, func)
2359 RGraph.Registry.Get('chart.event.handlers').push([id, e, func]);
2364 * This function suggests a gutter size based on the widest left label. Given that the bottom
2365 * labels may be longer, this may be a little out.
2367 * @param object obj The graph object
2368 * @param array data An array of graph data
2369 * @return int A suggested gutter setting
2371 RGraph.getGutterSuggest = function (obj, data)
2373 var str = RGraph.number_format(obj, RGraph.array_max(RGraph.getScale(RGraph.array_max(data), obj)), obj.Get('chart.units.pre'), obj.Get('chart.units.post'));
2375 // Take into account the HBar
2376 if (obj.type == 'hbar') {
2381 for (var i=0; i<obj.Get('chart.labels').length; ++i) {
2382 str = (obj.Get('chart.labels').length > str.length ? obj.Get('chart.labels')[i] : str);
2386 obj.context.font = obj.Get('chart.text.size') + 'pt ' + obj.Get('chart.text.font');
2388 len = obj.context.measureText(str).width + 5;
2390 return (obj.type == 'hbar' ? len / 3 : len);
2395 * A basic Array shift gunction
2397 * @param object The numerical array to work on
2398 * @return The new array
2400 RGraph.array_shift = function (arr)
2404 for (var i=1; i<arr.length; ++i) ret.push(arr[i]);
2411 * If you prefer, you can use the SetConfig() method to set the configuration information
2412 * for your chart. You may find that setting the configuration this way eases reuse.
2414 * @param object obj The graph object
2415 * @param object config The graph configuration information
2417 RGraph.SetConfig = function (obj, config)
2420 if (typeof(i) == 'string') {
2421 obj.Set(i, config[i]);
2430 * This function gets the canvas height. Defaults to the actual
2431 * height but this can be changed by setting chart.height.
2433 * @param object obj The graph object
2435 RGraph.GetHeight = function (obj)
2437 var height = obj.Get('chart.height');
2439 return height ? height : obj.canvas.height;
2444 * This function gets the canvas width. Defaults to the actual
2445 * width but this can be changed by setting chart.width.
2447 * @param object obj The graph object
2449 RGraph.GetWidth = function (obj)
2451 var width = obj.Get('chart.width');
2453 return width ? width : obj.canvas.width;