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
15 if (typeof(RGraph) == 'undefined') RGraph = {};
18 * The scatter graph constructor
20 * @param object canvas The cxanvas object
21 * @param array data The chart data
23 RGraph.Scatter = function (id, data)
25 // Get the canvas and context objects
27 this.canvas = document.getElementById(id);
28 this.canvas.__object__ = this;
29 this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
33 this.type = 'scatter';
38 * Compatibility with older browsers
40 RGraph.OldBrowserCompat(this.context);
43 // Various config properties
45 'chart.background.barcolor1': 'white',
46 'chart.background.barcolor2': 'white',
47 'chart.background.grid': true,
48 'chart.background.grid.width': 1,
49 'chart.background.grid.color': '#ddd',
50 'chart.background.grid.hsize': 20,
51 'chart.background.grid.vsize': 20,
52 'chart.background.hbars': null,
53 'chart.background.vbars': null,
54 'chart.background.grid.vlines': true,
55 'chart.background.grid.hlines': true,
56 'chart.background.grid.border': true,
57 'chart.background.grid.autofit':false,
58 'chart.background.grid.autofit.numhlines': 7,
59 'chart.background.grid.autofit.numvlines': 20,
60 'chart.text.size': 10,
61 'chart.text.angle': 0,
62 'chart.text.color': 'black',
63 'chart.text.font': 'Verdana',
64 'chart.tooltips.effect': 'fade',
65 'chart.tooltips.hotspot': 3,
66 'chart.tooltips.css.class': 'RGraph_tooltip',
67 'chart.tooltips.highlight': true,
68 'chart.tooltips.coords.adjust': [0,0],
69 'chart.units.pre': '',
70 'chart.units.post': '',
71 'chart.tickmarks': 'cross',
79 'chart.scale.decimals': null,
80 'chart.scale.point': '.',
81 'chart.scale.thousand': ',',
83 'chart.title.background': null,
84 'chart.title.hpos': null,
85 'chart.title.vpos': null,
86 'chart.title.xaxis': '',
87 'chart.title.yaxis': '',
88 'chart.title.xaxis.pos': 0.25,
89 'chart.title.yaxis.pos': 0.25,
91 'chart.ylabels': true,
92 'chart.ylabels.count': 5,
93 'chart.contextmenu': null,
94 'chart.defaultcolor': 'black',
95 'chart.xaxispos': 'bottom',
96 'chart.yaxispos': 'left',
97 'chart.noendxtick': false,
98 'chart.crosshairs': false,
99 'chart.crosshairs.color': '#333',
100 'chart.crosshairs.linewidth': 1,
101 'chart.crosshairs.coords': false,
102 'chart.crosshairs.coords.fixed':true,
103 'chart.crosshairs.coords.fadeout':false,
104 'chart.crosshairs.coords.labels.x': 'X',
105 'chart.crosshairs.coords.labels.y': 'Y',
106 'chart.annotatable': false,
107 'chart.annotate.color': 'black',
109 'chart.line.linewidth': 1,
110 'chart.line.colors': ['green', 'red'],
111 'chart.line.shadow.color': 'rgba(0,0,0,0)',
112 'chart.line.shadow.blur': 2,
113 'chart.line.shadow.offsetx': 3,
114 'chart.line.shadow.offsety': 3,
115 'chart.line.stepped': false,
116 'chart.noaxes': false,
118 'chart.key.background': 'white',
119 'chart.key.position': 'graph',
120 'chart.key.shadow': false,
121 'chart.key.shadow.color': '#666',
122 'chart.key.shadow.blur': 3,
123 'chart.key.shadow.offsetx': 2,
124 'chart.key.shadow.offsety': 2,
125 'chart.key.position.gutter.boxed': true,
126 'chart.key.position.x': null,
127 'chart.key.position.y': null,
128 'chart.key.color.shape': 'square',
129 'chart.key.rounded': true,
130 'chart.axis.color': 'black',
131 'chart.zoom.factor': 1.5,
132 'chart.zoom.fade.in': true,
133 'chart.zoom.fade.out': true,
134 'chart.zoom.hdir': 'right',
135 'chart.zoom.vdir': 'down',
136 'chart.zoom.frames': 10,
137 'chart.zoom.delay': 50,
138 'chart.zoom.shadow': true,
139 'chart.zoom.mode': 'canvas',
140 'chart.zoom.thumbnail.width': 75,
141 'chart.zoom.thumbnail.height': 75,
142 'chart.zoom.background': true,
143 'chart.zoom.action': 'zoom',
144 'chart.boxplot.width': 8,
145 'chart.resizable': false,
149 // Handle multiple datasets being given as one argument
150 if (arguments[1][0] && arguments[1][0][0] && typeof(arguments[1][0][0][0]) == 'number') {
151 // Store the data set(s)
152 for (var i=0; i<arguments[1].length; ++i) {
153 this.data[i] = arguments[1][i];
156 // Handle multiple data sets being supplied as seperate arguments
158 // Store the data set(s)
159 for (var i=1; i<arguments.length; ++i) {
160 this.data[i - 1] = arguments[i];
166 alert('[SCATTER] No canvas support');
170 // Check the common library has been included
171 if (typeof(RGraph) == 'undefined') {
172 alert('[SCATTER] Fatal error: The common library does not appear to have been included');
180 * @param string name The name of the property to set
181 * @param string value The value of the property
183 RGraph.Scatter.prototype.Set = function (name, value)
186 * This is here because the key expects a name of "chart.colors"
188 if (name == 'chart.line.colors') {
189 this.properties['chart.colors'] = value;
193 * Allow compatibility with older property names
195 if (name == 'chart.tooltip.hotspot') {
196 name = 'chart.tooltips.hotspot';
200 * chart.yaxispos should be left or right
202 if (name == 'chart.yaxispos' && value != 'left' && value != 'right') {
203 alert("[SCATTER] chart.yaxispos should be left or right. You've set it to: '" + value + "' Changing it to left");
207 this.properties[name.toLowerCase()] = value;
214 * @param string name The name of the property to set
216 RGraph.Scatter.prototype.Get = function (name)
218 return this.properties[name];
223 * The function you call to draw the line chart
225 RGraph.Scatter.prototype.Draw = function ()
228 * Fire the onbeforedraw event
230 RGraph.FireCustomEvent(this, 'onbeforedraw');
233 * Clear all of this canvases event handlers (the ones installed by RGraph)
235 RGraph.ClearEventListeners(this.id);
237 // Go through all the data points and see if a tooltip has been given
238 this.Set('chart.tooltips', false);
239 this.hasTooltips = false;
240 var overHotspot = false;
242 // Reset the coords array
245 if (!RGraph.isIE8()) {
246 for (var i=0; i<this.data.length; ++i) {
247 for (var j =0;j<this.data[i].length; ++j) {
248 if (this.data[i][j] && this.data[i][j][3] && typeof(this.data[i][j][3]) == 'string' && this.data[i][j][3].length) {
249 this.Set('chart.tooltips', [1]); // An array
250 this.hasTooltips = true;
256 // Reset the maximum value
259 // Work out the maximum Y value
260 if (this.Get('chart.ymax') && this.Get('chart.ymax') > 0) {
263 this.max = this.Get('chart.ymax');
264 this.min = this.Get('chart.ymin') ? this.Get('chart.ymin') : 0;
266 this.scale[0] = ((this.max - this.min) * (1/5)) + this.min;
267 this.scale[1] = ((this.max - this.min) * (2/5)) + this.min;
268 this.scale[2] = ((this.max - this.min) * (3/5)) + this.min;
269 this.scale[3] = ((this.max - this.min) * (4/5)) + this.min;
270 this.scale[4] = ((this.max - this.min) * (5/5)) + this.min;
272 var decimals = this.Get('chart.scale.decimals');
275 Number(this.scale[0]).toFixed(decimals),
276 Number(this.scale[1]).toFixed(decimals),
277 Number(this.scale[2]).toFixed(decimals),
278 Number(this.scale[3]).toFixed(decimals),
279 Number(this.scale[4]).toFixed(decimals)
287 for (i=0; i<this.data.length; ++i) {
288 for (j=0; j<this.data[i].length; ++j) {
289 this.max = Math.max(this.max, typeof(this.data[i][j][1]) == 'object' ? RGraph.array_max(this.data[i][j][1]) : Math.abs(this.data[i][j][1]));
293 this.scale = RGraph.getScale(this.max, this);
295 this.max = this.scale[4];
296 this.min = this.Get('chart.ymin') ? this.Get('chart.ymin') : 0;
299 this.scale[0] = ((this.max - this.min) * (1/5)) + this.min;
300 this.scale[1] = ((this.max - this.min) * (2/5)) + this.min;
301 this.scale[2] = ((this.max - this.min) * (3/5)) + this.min;
302 this.scale[3] = ((this.max - this.min) * (4/5)) + this.min;
303 this.scale[4] = ((this.max - this.min) * (5/5)) + this.min;
307 if (typeof(this.Get('chart.scale.decimals')) == 'number') {
308 var decimals = this.Get('chart.scale.decimals');
311 Number(this.scale[0]).toFixed(decimals),
312 Number(this.scale[1]).toFixed(decimals),
313 Number(this.scale[2]).toFixed(decimals),
314 Number(this.scale[3]).toFixed(decimals),
315 Number(this.scale[4]).toFixed(decimals)
320 this.grapharea = this.canvas.height - (2 * this.Get('chart.gutter'));
322 // Progressively Draw the chart
323 RGraph.background.Draw(this);
326 * Draw any horizontal bars that have been specified
328 if (this.Get('chart.background.hbars') && this.Get('chart.background.hbars').length) {
329 RGraph.DrawBars(this);
333 * Draw any vertical bars that have been specified
335 if (this.Get('chart.background.vbars') && this.Get('chart.background.vbars').length) {
339 if (!this.Get('chart.noaxes')) {
346 for(i=0; i<this.data.length; ++i) {
350 this.context.shadowColor = this.Get('chart.line.shadow.color');
351 this.context.shadowOffsetX = this.Get('chart.line.shadow.offsetx');
352 this.context.shadowOffsetY = this.Get('chart.line.shadow.offsety');
353 this.context.shadowBlur = this.Get('chart.line.shadow.blur');
357 // Turn the shadow off
358 RGraph.NoShadow(this);
362 if (this.Get('chart.line')) {
363 for (var i=0;i<this.data.length; ++i) {
364 this.DrawMarks(i); // Call this again so the tickmarks appear over the line
371 * Setup the context menu if required
373 if (this.Get('chart.contextmenu')) {
374 RGraph.ShowContext(this);
378 * Install the event handler for tooltips
380 if (this.hasTooltips) {
383 * Register all charts
385 RGraph.Register(this);
387 var overHotspot = false;
389 var canvas_onmousemove_func = function (e)
391 e = RGraph.FixEventObject(e);
393 var canvas = e.target;
394 var obj = canvas.__object__;
395 var context = canvas.getContext('2d');
396 var mouseCoords = RGraph.getMouseXY(e);
397 var overHotspot = false;
400 * Now loop through each point comparing the coords
403 var offset = obj.Get('chart.tooltips.hotspot'); // This is how far the hotspot extends
405 for (var set=0; set<obj.coords.length; ++set) {
406 for (var i=0; i<obj.coords[set].length; ++i) {
408 var adjust = obj.Get('chart.tooltips.coords.adjust');
409 var xCoord = obj.coords[set][i][0] + adjust[0];
410 var yCoord = obj.coords[set][i][1] + adjust[1];
411 var tooltip = obj.coords[set][i][2];
413 if (mouseCoords[0] <= (xCoord + offset) &&
414 mouseCoords[0] >= (xCoord - offset) &&
415 mouseCoords[1] <= (yCoord + offset) &&
416 mouseCoords[1] >= (yCoord - offset) &&
418 tooltip.length > 0) {
421 canvas.style.cursor = 'pointer';
424 !RGraph.Registry.Get('chart.tooltip') ||
425 RGraph.Registry.Get('chart.tooltip').__text__ != tooltip ||
426 RGraph.Registry.Get('chart.tooltip').__index__ != i ||
427 RGraph.Registry.Get('chart.tooltip').__dataset__ != set
430 if (obj.Get('chart.tooltips.highlight')) {
435 * Get the tooltip text
437 if (typeof(tooltip) == 'function') {
438 var text = String(tooltip(i));
441 var text = String(tooltip);
444 RGraph.Tooltip(canvas, text, e.pageX, e.pageY, i);
445 RGraph.Registry.Get('chart.tooltip').__dataset__ = set;
448 * Draw a circle around the mark
450 if (obj.Get('chart.tooltips.highlight')) {
452 context.fillStyle = 'rgba(255,255,255,0.5)';
453 context.arc(xCoord, yCoord, 3, 0, 6.28, 0);
465 canvas.style.cursor = 'default';
468 this.canvas.addEventListener('mousemove', canvas_onmousemove_func, false);
469 RGraph.AddEventListener(this.id, 'mousemove', canvas_onmousemove_func);
474 * Draw the key if necessary
476 if (this.Get('chart.key') && this.Get('chart.key').length) {
477 RGraph.DrawKey(this, this.Get('chart.key'), this.Get('chart.line.colors'));
484 RGraph.DrawCrosshairs(this);
487 * If the canvas is annotatable, do install the event handlers
489 if (this.Get('chart.annotatable')) {
490 RGraph.Annotate(this);
494 * This bit shows the mini zoom window if requested
496 if (this.Get('chart.zoom.mode') == 'thumbnail' || this.Get('chart.zoom.mode') == 'area') {
497 RGraph.ShowZoomWindow(this);
502 * This function enables resizing
504 if (this.Get('chart.resizable')) {
505 RGraph.AllowResizing(this);
509 * Fire the RGraph ondraw event
511 RGraph.FireCustomEvent(this, 'ondraw');
516 * Draws the axes of the scatter graph
518 RGraph.Scatter.prototype.DrawAxes = function ()
520 var canvas = this.canvas;
521 var context = this.context;
522 var graphHeight = this.canvas.height - (this.Get('chart.gutter') * 2);
523 var gutter = this.Get('chart.gutter');
526 context.strokeStyle = this.Get('chart.axis.color');
527 context.lineWidth = 1;
530 if (this.Get('chart.yaxispos') == 'left') {
531 context.moveTo(gutter, gutter);
532 context.lineTo(gutter, this.canvas.height - gutter);
534 context.moveTo(canvas.width - gutter, gutter);
535 context.lineTo(canvas.width - gutter, canvas.height - gutter);
540 if (this.Get('chart.xaxis')) {
541 if (this.Get('chart.xaxispos') == 'center') {
542 context.moveTo(gutter, canvas.height / 2);
543 context.lineTo(canvas.width - gutter, canvas.height / 2);
545 context.moveTo(gutter, canvas.height - gutter);
546 context.lineTo(canvas.width - gutter, canvas.height - gutter);
551 * Draw the Y tickmarks
553 for (y=gutter; y < canvas.height - gutter + (this.Get('chart.xaxispos') == 'center' ? 1 : 0) ; y+=(graphHeight / 5) / 2) {
555 // This is here to accomodate the X axis being at the center
556 if (y == (canvas.height / 2) ) continue;
558 if (this.Get('chart.yaxispos') == 'left') {
559 context.moveTo(gutter, y);
560 context.lineTo(gutter - 3, y);
562 context.moveTo(canvas.width - gutter +3, y);
563 context.lineTo(canvas.width - gutter, y);
569 * Draw the X tickmarks
571 if (this.Get('chart.xticks') && this.Get('chart.xaxis')) {
574 var y = (this.Get('chart.xaxispos') == 'center') ? (this.canvas.height / 2) : (this.canvas.height - gutter);
575 this.xTickGap = (this.canvas.width - (2 * gutter) ) / this.Get('chart.labels').length;
577 for (x = (gutter + (this.Get('chart.yaxispos') == 'left' ? this.xTickGap / 2 : 0) ); x<=(canvas.width - gutter - (this.Get('chart.yaxispos') == 'left' ? 0 : 1)); x += this.xTickGap / 2) {
579 if (this.Get('chart.yaxispos') == 'left' && this.Get('chart.noendxtick') == true && x == (canvas.width - gutter) ) {
582 } else if (this.Get('chart.yaxispos') == 'right' && this.Get('chart.noendxtick') == true && x == gutter) {
586 context.moveTo(x, y - (this.Get('chart.xaxispos') == 'center' ? 3 : 0));
587 context.lineTo(x, y + 3);
596 * Draws the labels on the scatter graph
598 RGraph.Scatter.prototype.DrawLabels = function ()
600 this.context.fillStyle = this.Get('chart.text.color');
601 var font = this.Get('chart.text.font');
602 var xMin = this.Get('chart.xmin');
603 var xMax = this.Get('chart.xmax');
604 var yMax = this.scale[4];
605 var gutter = this.Get('chart.gutter');
606 var text_size = this.Get('chart.text.size');
607 var units_pre = this.Get('chart.units.pre');
608 var units_post = this.Get('chart.units.post');
609 var numYLabels = this.Get('chart.ylabels.count');
610 var context = this.context;
611 var canvas = this.canvas;
613 this.halfTextHeight = text_size / 2;
616 this.halfGraphHeight = (this.canvas.height - (2 * this.Get('chart.gutter'))) / 2;
619 * Draw the Y yaxis labels, be it at the top or center
621 if (this.Get('chart.ylabels')) {
623 var xPos = this.Get('chart.yaxispos') == 'left' ? gutter - 5 : canvas.width - gutter + 5;
624 var align = this.Get('chart.yaxispos') == 'right' ? 'left' : 'right';
626 if (this.Get('chart.xaxispos') == 'center') {
632 if (typeof(this.Get('chart.ylabels.specific')) == 'object') {
634 var labels = this.Get('chart.ylabels.specific');
636 for (var i=0; i<this.Get('chart.ylabels.specific').length; ++i) {
637 var y = gutter + (i * (this.grapharea / (labels.length * 2) ) );
638 RGraph.Text(context, font, text_size, xPos, y, labels[i], 'center', align);
641 var reversed_labels = RGraph.array_reverse(labels);
643 for (var i=0; i<reversed_labels.length; ++i) {
644 var y = gutter + (this.grapharea / 2) + ((i+1) * (this.grapharea / (labels.length * 2) ) );
646 RGraph.Text(context, font, text_size, xPos, y, reversed_labels[i], 'center', align);
653 if (numYLabels == 1 || numYLabels == 3 || numYLabels == 5) {
654 // Draw the top halves labels
655 RGraph.Text(context, font, text_size, xPos, gutter, RGraph.number_format(this, this.scale[4], units_pre, units_post), 'center', align);
658 if (numYLabels >= 5) {
659 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (1/10) ), RGraph.number_format(this, this.scale[3], units_pre, units_post), 'center', align);
660 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (3/10) ), RGraph.number_format(this, this.scale[1], units_pre, units_post), 'center', align);
663 if (numYLabels >= 3) {
664 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (2/10) ), RGraph.number_format(this, this.scale[2], units_pre, units_post), 'center', align);
665 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (4/10) ), RGraph.number_format(this, this.scale[0], units_pre, units_post), 'center', align);
668 // Draw the bottom halves labels
669 if (numYLabels >= 3) {
670 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (1/10) ) + this.halfGraphHeight, '-' + RGraph.number_format(this, this.scale[0], units_pre, units_post), 'center', align);
671 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (3/10) ) + this.halfGraphHeight, '-' + RGraph.number_format(this, this.scale[2], units_pre, units_post), 'center', align);
674 if (numYLabels == 5) {
675 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (2/10) ) + this.halfGraphHeight, '-' + RGraph.number_format(this, this.scale[1], units_pre, units_post), 'center', align);
676 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (4/10) ) + this.halfGraphHeight, '-' + RGraph.number_format(this, this.scale[3], units_pre, units_post), 'center', align);
679 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (5/10) ) + this.halfGraphHeight, '-' + RGraph.number_format(this, this.scale[4], units_pre, units_post), 'center', align);
681 } else if (numYLabels == 10) {
683 var interval = (this.grapharea / numYLabels) / 2;
685 for (var i=0; i<numYLabels; ++i) {
686 RGraph.Text(context, font, text_size, xPos,gutter + ((canvas.height - (2 * gutter)) * (i/20) ),RGraph.number_format(this,
688 (this.max - (this.max * (i/10))).toFixed(this.Get('chart.scale.decimals')),
690 units_pre, units_post),'center', align);
691 RGraph.Text(context, font, text_size, xPos,gutter + ((canvas.height - (2 * gutter)) * (i/20) ) + (this.grapharea / 2) + (this.grapharea / 20),'-' + RGraph.number_format(this, ((this.max * (i/10)) + (this.max * (1/10))).toFixed((this.Get('chart.scale.decimals'))), units_pre, units_post), 'center', align);
695 alert('[SCATTER SCALE] Number of Y labels can be 1/3/5/10 only');
700 var xPos = this.Get('chart.yaxispos') == 'left' ? gutter - 5 : canvas.width - gutter + 5;
701 var align = this.Get('chart.yaxispos') == 'right' ? 'left' : 'right';
706 if (typeof(this.Get('chart.ylabels.specific')) == 'object') {
708 var labels = this.Get('chart.ylabels.specific');
710 for (var i=0; i<this.Get('chart.ylabels.specific').length; ++i) {
711 var y = gutter + (i * (this.grapharea / labels.length) );
713 RGraph.Text(context, font, text_size, xPos, y, labels[i], 'center', align);
719 if (numYLabels == 1 || numYLabels == 3 || numYLabels == 5) {
720 RGraph.Text(context, font, text_size, xPos, gutter, RGraph.number_format(this, this.scale[4], units_pre, units_post), 'center', align);
722 if (numYLabels >= 5) {
723 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (1/5) ), RGraph.number_format(this, this.scale[3], units_pre, units_post), 'center', align);
724 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (3/5) ), RGraph.number_format(this, this.scale[1], units_pre, units_post), 'center', align);
727 if (numYLabels >= 3) {
728 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (2/5) ), RGraph.number_format(this, this.scale[2], units_pre, units_post), 'center', align);
729 RGraph.Text(context, font, text_size, xPos, gutter + ((canvas.height - (2 * gutter)) * (4/5) ), RGraph.number_format(this, this.scale[0], units_pre, units_post), 'center', align);
731 } else if (numYLabels == 10) {
734 var interval = (this.grapharea / numYLabels) / 2;
736 for (var i=0; i<numYLabels; ++i) {
737 RGraph.Text(context, font, text_size, xPos,gutter + ((canvas.height - (2 * gutter)) * (i/10) ),RGraph.number_format(this,(this.max - (this.max * (i/10))).toFixed((this.Get('chart.scale.decimals'))), units_pre, units_post), 'center', align);
741 alert('[SCATTER SCALE] Number of Y labels can be 1/3/5/10 only');
744 if (this.Get('chart.ymin')) {
745 RGraph.Text(context, font, text_size, xPos, canvas.height - gutter,RGraph.number_format(this, this.Get('chart.ymin').toFixed(this.Get('chart.scale.decimals')), units_pre, units_post),'center', align);
750 // Put the text on the X axis
751 var graphArea = this.canvas.width - (2 * gutter);
752 var xInterval = graphArea / this.Get('chart.labels').length;
754 var yPos = (this.canvas.height - gutter) + 15;
755 var labels = this.Get('chart.labels');
762 var halign = 'center';
764 if (this.Get('chart.text.angle') > 0) {
765 angle = -1 * this.Get('chart.text.angle');
771 for (i=0; i<labels.length; ++i) {
773 if (typeof(labels[i]) == 'object') {
775 RGraph.Text(context, font, this.Get('chart.text.size'), gutter + (graphArea * ((labels[i][1] - xMin) / (this.Get('chart.xmax') - xMin))) + 5, yPos, String(labels[i][0]), valign, angle != 0 ? 'right' : 'left', null, angle);
778 * Draw the gray indicator line
780 this.context.beginPath();
781 this.context.strokeStyle = '#bbb';
782 this.context.moveTo(gutter + (graphArea * ((labels[i][1] - xMin)/ (this.Get('chart.xmax') - xMin))), this.canvas.height - gutter);
783 this.context.lineTo(gutter + (graphArea * ((labels[i][1] - xMin)/ (this.Get('chart.xmax') - xMin))), this.canvas.height - gutter + 20);
784 this.context.stroke();
787 RGraph.Text(context, font, this.Get('chart.text.size'), xPos + (this.xTickGap / 2), yPos, String(labels[i]), valign, halign, null, angle);
790 // Do this for the next time around
797 * Draws the actual scatter graph marks
799 * @param i integer The dataset index
801 RGraph.Scatter.prototype.DrawMarks = function (i)
804 * Reset the coords array
811 var xmax = this.Get('chart.xmax');
812 var default_color = this.Get('chart.defaultcolor');
814 for (var j=0; j<this.data[i].length; ++j) {
816 * This is here because tooltips are optional
818 var data_point = this.data[i];
820 var xCoord = data_point[j][0];
821 var yCoord = data_point[j][1];
822 var color = data_point[j][2] ? data_point[j][2] : default_color;
823 var tooltip = (data_point[j] && data_point[j][3]) ? data_point[j][3] : null;
842 * Draws a single scatter mark
844 RGraph.Scatter.prototype.DrawMark = function (index, x, y, xMax, yMax, color, tooltip, coords, data)
846 var tickmarks = this.Get('chart.tickmarks');
847 var tickSize = this.Get('chart.ticksize');
848 var gutter = this.Get('chart.gutter');
849 var xMin = this.Get('chart.xmin');
850 var x = ((x - xMin) / (xMax - xMin)) * (this.canvas.width - (2 * gutter));
855 * This allows chart.tickmarks to be an array
858 if (tickmarks && typeof(tickmarks) == 'object') {
859 tickmarks = tickmarks[index];
864 * This allows chart.ticksize to be an array
866 if (typeof(tickSize) == 'object') {
867 var tickSize = tickSize[index];
868 var halfTickSize = tickSize / 2;
870 var halfTickSize = tickSize / 2;
875 * This bit is for boxplots only
877 if ( typeof(y) == 'object'
878 && typeof(y[0]) == 'number'
879 && typeof(y[1]) == 'number'
880 && typeof(y[2]) == 'number'
881 && typeof(y[3]) == 'number'
882 && typeof(y[4]) == 'number'
885 var yMin = this.Get('chart.ymin') ? this.Get('chart.ymin') : 0;
886 this.Set('chart.boxplot', true);
887 this.graphheight = this.canvas.height - (2 * gutter);
889 if (this.Get('chart.xaxispos') == 'center') {
890 this.graphheight /= 2;
893 var y0 = (this.graphheight) - ((y[4] - yMin) / (yMax - yMin)) * (this.graphheight);
894 var y1 = (this.graphheight) - ((y[3] - yMin) / (yMax - yMin)) * (this.graphheight);
895 var y2 = (this.graphheight) - ((y[2] - yMin) / (yMax - yMin)) * (this.graphheight);
896 var y3 = (this.graphheight) - ((y[1] - yMin) / (yMax - yMin)) * (this.graphheight);
897 var y4 = (this.graphheight) - ((y[0] - yMin) / (yMax - yMin)) * (this.graphheight);
902 // Override the boxWidth
903 if (typeof(y[7]) == 'number') {
907 var y = this.graphheight - y2;
910 var yMin = this.Get('chart.ymin') ? this.Get('chart.ymin') : 0;
911 var y = (( (y - yMin) / (yMax - yMin)) * (this.canvas.height - (2 * gutter)));
915 * Account for the X axis being at the centre
917 if (this.Get('chart.xaxispos') == 'center') {
919 y += this.halfGraphHeight;
922 // This is so that points are on the graph, and not the gutter
924 y = this.canvas.height - gutter - y;
926 this.context.beginPath();
929 this.context.strokeStyle = color;
934 if ( this.Get('chart.boxplot')
935 && typeof(y0) == 'number'
936 && typeof(y1) == 'number'
937 && typeof(y2) == 'number'
938 && typeof(y3) == 'number'
939 && typeof(y4) == 'number'
942 var boxWidth = boxWidth ? boxWidth : this.Get('chart.boxplot.width');
943 var halfBoxWidth = boxWidth / 2;
945 this.context.beginPath();
947 // Draw the upper coloured box if a value is specified
949 this.context.fillStyle = col1;
950 this.context.fillRect(x - halfBoxWidth, y1 + gutter, boxWidth, y2 - y1);
953 // Draw the lower coloured box if a value is specified
955 this.context.fillStyle = col2;
956 this.context.fillRect(x - halfBoxWidth, y2 + gutter, boxWidth, y3 - y2);
959 this.context.strokeRect(x - halfBoxWidth, y1 + gutter, boxWidth, y3 - y1);
960 this.context.stroke();
962 // Now draw the whiskers
963 this.context.beginPath();
964 this.context.moveTo(x - halfBoxWidth, y0 + gutter);
965 this.context.lineTo(x + halfBoxWidth, y0 + gutter);
967 this.context.moveTo(x, y0 + gutter);
968 this.context.lineTo(x, y1 + gutter);
970 this.context.moveTo(x - halfBoxWidth, y4 + gutter);
971 this.context.lineTo(x + halfBoxWidth, y4 + gutter);
973 this.context.moveTo(x, y4 + gutter);
974 this.context.lineTo(x, y3 + gutter);
976 this.context.stroke();
981 * Draw the tickmark, but not for boxplots
984 if (!y0 && !y1 && !y2 && !y3 && !y4) {
986 this.graphheight = this.canvas.height - (2 * gutter);
990 if (tickmarks == 'circle') {
991 this.context.arc(x, y, halfTickSize, 0, 6.28, 0);
992 this.context.fillStyle = color;
995 } else if (tickmarks == 'plus') {
997 this.context.moveTo(x, y - halfTickSize);
998 this.context.lineTo(x, y + halfTickSize);
999 this.context.moveTo(x - halfTickSize, y);
1000 this.context.lineTo(x + halfTickSize, y);
1001 this.context.stroke();
1003 } else if (tickmarks == 'square') {
1004 this.context.strokeStyle = color;
1005 this.context.fillStyle = color;
1006 this.context.fillRect(
1012 //this.context.fill();
1014 } else if (tickmarks == 'cross') {
1016 this.context.moveTo(x - halfTickSize, y - halfTickSize);
1017 this.context.lineTo(x + halfTickSize, y + halfTickSize);
1018 this.context.moveTo(x + halfTickSize, y - halfTickSize);
1019 this.context.lineTo(x - halfTickSize, y + halfTickSize);
1021 this.context.stroke();
1024 * Diamond shape tickmarks
1026 } else if (tickmarks == 'diamond') {
1027 this.context.fillStyle = this.context.strokeStyle;
1029 this.context.moveTo(x, y - halfTickSize);
1030 this.context.lineTo(x + halfTickSize, y);
1031 this.context.lineTo(x, y + halfTickSize);
1032 this.context.lineTo(x - halfTickSize, y);
1033 this.context.lineTo(x, y - halfTickSize);
1035 this.context.fill();
1036 this.context.stroke();
1039 * Custom tickmark style
1041 } else if (typeof(tickmarks) == 'function') {
1043 var graphWidth = this.canvas.width - (2 * this.Get('chart.gutter'))
1044 var xVal = ((x - this.Get('chart.gutter')) / graphWidth) * xMax;
1045 var yVal = ((this.graphheight - (y - this.Get('chart.gutter'))) / this.graphheight) * yMax;
1047 tickmarks(this, data, x, y, xVal, yVal, xMax, yMax, color)
1052 } else if (tickmarks == null) {
1055 * Unknown tickmark type
1058 alert('[SCATTER] (' + this.id + ') Unknown tickmark style: ' + tickmarks );
1063 * Add the tickmark to the coords array
1065 coords.push([x, y, tooltip]);
1070 * Draws an optional line connecting the tick marks.
1072 * @param i The index of the dataset to use
1074 RGraph.Scatter.prototype.DrawLine = function (i)
1076 if (this.Get('chart.line') && this.coords[i].length >= 2) {
1078 this.context.lineCap = 'round';
1079 this.context.lineJoin = 'round';
1080 this.context.lineWidth = this.GetLineWidth(i);// i is the index of the set of coordinates
1081 this.context.strokeStyle = this.Get('chart.line.colors')[i];
1082 this.context.beginPath();
1084 var len = this.coords[i].length;
1086 for (var j=0; j<this.coords[i].length; ++j) {
1088 var xPos = this.coords[i][j][0];
1089 var yPos = this.coords[i][j][1];
1092 this.context.moveTo(xPos, yPos);
1096 var stepped = this.Get('chart.line.stepped');
1098 if ( (typeof(stepped) == 'boolean' && stepped)
1099 || (typeof(stepped) == 'object' && stepped[i])
1101 this.context.lineTo(this.coords[i][j][0], this.coords[i][j - 1][1]);
1104 this.context.lineTo(xPos, yPos);
1108 this.context.stroke();
1112 * Set the linewidth back to 1
1114 this.context.lineWidth = 1;
1119 * Returns the linewidth
1121 * @param number i The index of the "line" (/set of coordinates)
1123 RGraph.Scatter.prototype.GetLineWidth = function (i)
1125 var linewidth = this.Get('chart.line.linewidth');
1127 if (typeof(linewidth) == 'number') {
1130 } else if (typeof(linewidth) == 'object') {
1132 return linewidth[i];
1134 return linewidth[0];
1137 alert('[SCATTER] Error! chart.linewidth should be a single number or an array of one or more numbers');
1143 * Draws vertical bars. Line chart doesn't use a horizontal scale, hence this function
1146 RGraph.Scatter.prototype.DrawVBars = function ()
1148 var canvas = this.canvas;
1149 var context = this.context;
1150 var vbars = this.Get('chart.background.vbars');
1151 var gutter = this.Get('chart.gutter');
1152 var graphWidth = canvas.width - gutter - gutter;
1156 var xmax = this.Get('chart.xmax');
1158 for (var i=0; i<vbars.length; ++i) {
1159 var startX = ((vbars[i][0] / xmax) * graphWidth) + gutter;
1160 var width = (vbars[i][1] / xmax) * graphWidth;
1162 context.beginPath();
1163 context.fillStyle = vbars[i][2];
1164 context.fillRect(startX, gutter, width, (canvas.height - gutter - gutter));