6 * Copyright 2015 Nick Downie
7 * Released under the MIT license
8 * https://github.com/nnnick/Chart.js/blob/master/LICENSE.md
16 //Declare root variable - window in the browser, global on the server
18 previous = root.Chart;
20 //Occupy the global variable of Chart, and create a simple base class
21 var Chart = function(context){
23 this.canvas = context.canvas;
27 //Variables global to the chart
28 var computeDimension = function(element,dimension)
30 if (element['offset'+dimension])
32 return element['offset'+dimension];
36 return document.defaultView.getComputedStyle(element).getPropertyValue(dimension);
40 var width = this.width = computeDimension(context.canvas,'Width');
41 var height = this.height = computeDimension(context.canvas,'Height');
43 // Firefox requires this to work correctly
44 context.canvas.width = width;
45 context.canvas.height = height;
47 var width = this.width = context.canvas.width;
48 var height = this.height = context.canvas.height;
49 this.aspectRatio = this.width / this.height;
50 //High pixel density displays - multiply the size of the canvas height/width by the device pixel ratio, then scale.
51 helpers.retinaScale(this);
55 //Globally expose the defaults to allow for user updating/changing
58 // Boolean - Whether to animate the chart
61 // Number - Number of animation steps
64 // String - Animation easing effect
65 animationEasing: "easeOutQuart",
67 // Boolean - If we should show the scale at all
70 // Boolean - If we want to override with a hard coded scale
73 // ** Required if scaleOverride is true **
74 // Number - The number of steps in a hard coded scale
76 // Number - The value jump in the hard coded scale
78 // Number - The scale starting value
79 scaleStartValue: null,
81 // String - Colour of the scale line
82 scaleLineColor: "rgba(0,0,0,.1)",
84 // Number - Pixel width of the scale line
87 // Boolean - Whether to show labels on the scale
88 scaleShowLabels: true,
90 // Interpolated JS string - can access value
91 scaleLabel: "<%=value%>",
93 // Boolean - Whether the scale should stick to integers, and not show any floats even if drawing space is there
94 scaleIntegersOnly: true,
96 // Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
97 scaleBeginAtZero: false,
99 // String - Scale label font declaration for the scale label
100 scaleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
102 // Number - Scale label font size in pixels
105 // String - Scale label font weight style
106 scaleFontStyle: "normal",
108 // String - Scale label font colour
109 scaleFontColor: "#666",
111 // Boolean - whether or not the chart should be responsive and resize when the browser does.
114 // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
115 maintainAspectRatio: true,
117 // Boolean - Determines whether to draw tooltips on the canvas or not - attaches events to touchmove & mousemove
120 // Boolean - Determines whether to draw built-in tooltip or call custom tooltip function
121 customTooltips: false,
123 // Array - Array of string names to attach tooltip events
124 tooltipEvents: ["mousemove", "touchstart", "touchmove", "mouseout"],
126 // String - Tooltip background colour
127 tooltipFillColor: "rgba(0,0,0,0.8)",
129 // String - Tooltip label font declaration for the scale label
130 tooltipFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
132 // Number - Tooltip label font size in pixels
135 // String - Tooltip font weight style
136 tooltipFontStyle: "normal",
138 // String - Tooltip label font colour
139 tooltipFontColor: "#fff",
141 // String - Tooltip title font declaration for the scale label
142 tooltipTitleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
144 // Number - Tooltip title font size in pixels
145 tooltipTitleFontSize: 14,
147 // String - Tooltip title font weight style
148 tooltipTitleFontStyle: "bold",
150 // String - Tooltip title font colour
151 tooltipTitleFontColor: "#fff",
153 // Number - pixel width of padding around tooltip text
156 // Number - pixel width of padding around tooltip text
159 // Number - Size of the caret on the tooltip
162 // Number - Pixel radius of the tooltip border
163 tooltipCornerRadius: 6,
165 // Number - Pixel offset from point x to tooltip edge
168 // String - Template string for single tooltips
169 tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>",
171 // String - Template string for single tooltips
172 multiTooltipTemplate: "<%= value %>",
174 // String - Colour behind the legend colour block
175 multiTooltipKeyBackground: '#fff',
177 // Function - Will fire on animation progression.
178 onAnimationProgress: function(){},
180 // Function - Will fire on animation completion.
181 onAnimationComplete: function(){}
186 //Create a dictionary of chart types, to allow for extension of existing types
189 //Global Chart helpers object for utility methods and classes
190 var helpers = Chart.helpers = {};
192 //-- Basic js utility methods
193 var each = helpers.each = function(loopable,callback,self){
194 var additionalArgs = Array.prototype.slice.call(arguments, 3);
195 // Check to see if null or undefined firstly.
197 if (loopable.length === +loopable.length){
199 for (i=0; i<loopable.length; i++){
200 callback.apply(self,[loopable[i], i].concat(additionalArgs));
204 for (var item in loopable){
205 callback.apply(self,[loopable[item],item].concat(additionalArgs));
210 clone = helpers.clone = function(obj){
212 each(obj,function(value,key){
213 if (obj.hasOwnProperty(key)) objClone[key] = value;
217 extend = helpers.extend = function(base){
218 each(Array.prototype.slice.call(arguments,1), function(extensionObject) {
219 each(extensionObject,function(value,key){
220 if (extensionObject.hasOwnProperty(key)) base[key] = value;
225 merge = helpers.merge = function(base,master){
226 //Merge properties in left object over to a shallow clone of object right.
227 var args = Array.prototype.slice.call(arguments,0);
229 return extend.apply(null, args);
231 indexOf = helpers.indexOf = function(arrayToSearch, item){
232 if (Array.prototype.indexOf) {
233 return arrayToSearch.indexOf(item);
236 for (var i = 0; i < arrayToSearch.length; i++) {
237 if (arrayToSearch[i] === item) return i;
242 where = helpers.where = function(collection, filterCallback){
245 helpers.each(collection, function(item){
246 if (filterCallback(item)){
253 findNextWhere = helpers.findNextWhere = function(arrayToSearch, filterCallback, startIndex){
254 // Default to start of the array
258 for (var i = startIndex + 1; i < arrayToSearch.length; i++) {
259 var currentItem = arrayToSearch[i];
260 if (filterCallback(currentItem)){
265 findPreviousWhere = helpers.findPreviousWhere = function(arrayToSearch, filterCallback, startIndex){
266 // Default to end of the array
268 startIndex = arrayToSearch.length;
270 for (var i = startIndex - 1; i >= 0; i--) {
271 var currentItem = arrayToSearch[i];
272 if (filterCallback(currentItem)){
277 inherits = helpers.inherits = function(extensions){
278 //Basic javascript inheritance based on the model created in Backbone.js
280 var ChartElement = (extensions && extensions.hasOwnProperty("constructor")) ? extensions.constructor : function(){ return parent.apply(this, arguments); };
282 var Surrogate = function(){ this.constructor = ChartElement;};
283 Surrogate.prototype = parent.prototype;
284 ChartElement.prototype = new Surrogate();
286 ChartElement.extend = inherits;
288 if (extensions) extend(ChartElement.prototype, extensions);
290 ChartElement.__super__ = parent.prototype;
294 noop = helpers.noop = function(){},
295 uid = helpers.uid = (function(){
298 return "chart-" + id++;
301 warn = helpers.warn = function(str){
302 //Method for warning of errors
303 if (window.console && typeof window.console.warn == "function") console.warn(str);
305 amd = helpers.amd = (typeof define == 'function' && define.amd),
307 isNumber = helpers.isNumber = function(n){
308 return !isNaN(parseFloat(n)) && isFinite(n);
310 max = helpers.max = function(array){
311 return Math.max.apply( Math, array );
313 min = helpers.min = function(array){
314 return Math.min.apply( Math, array );
316 cap = helpers.cap = function(valueToCap,maxValue,minValue){
317 if(isNumber(maxValue)) {
318 if( valueToCap > maxValue ) {
322 else if(isNumber(minValue)){
323 if ( valueToCap < minValue ){
329 getDecimalPlaces = helpers.getDecimalPlaces = function(num){
330 if (num%1!==0 && isNumber(num)){
331 return num.toString().split(".")[1].length;
337 toRadians = helpers.radians = function(degrees){
338 return degrees * (Math.PI/180);
340 // Gets the angle from vertical upright to the point about a centre.
341 getAngleFromPoint = helpers.getAngleFromPoint = function(centrePoint, anglePoint){
342 var distanceFromXCenter = anglePoint.x - centrePoint.x,
343 distanceFromYCenter = anglePoint.y - centrePoint.y,
344 radialDistanceFromCenter = Math.sqrt( distanceFromXCenter * distanceFromXCenter + distanceFromYCenter * distanceFromYCenter);
347 var angle = Math.PI * 2 + Math.atan2(distanceFromYCenter, distanceFromXCenter);
349 //If the segment is in the top left quadrant, we need to add another rotation to the angle
350 if (distanceFromXCenter < 0 && distanceFromYCenter < 0){
356 distance: radialDistanceFromCenter
359 aliasPixel = helpers.aliasPixel = function(pixelWidth){
360 return (pixelWidth % 2 === 0) ? 0 : 0.5;
362 splineCurve = helpers.splineCurve = function(FirstPoint,MiddlePoint,AfterPoint,t){
363 //Props to Rob Spencer at scaled innovation for his post on splining between points
364 //http://scaledinnovation.com/analytics/splines/aboutSplines.html
365 var d01=Math.sqrt(Math.pow(MiddlePoint.x-FirstPoint.x,2)+Math.pow(MiddlePoint.y-FirstPoint.y,2)),
366 d12=Math.sqrt(Math.pow(AfterPoint.x-MiddlePoint.x,2)+Math.pow(AfterPoint.y-MiddlePoint.y,2)),
367 fa=t*d01/(d01+d12),// scaling factor for triangle Ta
371 x : MiddlePoint.x-fa*(AfterPoint.x-FirstPoint.x),
372 y : MiddlePoint.y-fa*(AfterPoint.y-FirstPoint.y)
375 x: MiddlePoint.x+fb*(AfterPoint.x-FirstPoint.x),
376 y : MiddlePoint.y+fb*(AfterPoint.y-FirstPoint.y)
380 calculateOrderOfMagnitude = helpers.calculateOrderOfMagnitude = function(val){
381 return Math.floor(Math.log(val) / Math.LN10);
383 calculateScaleRange = helpers.calculateScaleRange = function(valuesArray, drawingSize, textSize, startFromZero, integersOnly){
385 //Set a minimum step of two - a point at the top of the graph, and a point at the base
387 maxSteps = Math.floor(drawingSize/(textSize * 1.5)),
388 skipFitting = (minSteps >= maxSteps);
390 var maxValue = max(valuesArray),
391 minValue = min(valuesArray);
393 // We need some degree of seperation here to calculate the scales if all the values are the same
394 // Adding/minusing 0.5 will give us a range of 1.
395 if (maxValue === minValue){
397 // So we don't end up with a graph with a negative start value if we've said always start from zero
398 if (minValue >= 0.5 && !startFromZero){
402 // Make up a whole number above the values
407 var valueRange = Math.abs(maxValue - minValue),
408 rangeOrderOfMagnitude = calculateOrderOfMagnitude(valueRange),
409 graphMax = Math.ceil(maxValue / (1 * Math.pow(10, rangeOrderOfMagnitude))) * Math.pow(10, rangeOrderOfMagnitude),
410 graphMin = (startFromZero) ? 0 : Math.floor(minValue / (1 * Math.pow(10, rangeOrderOfMagnitude))) * Math.pow(10, rangeOrderOfMagnitude),
411 graphRange = graphMax - graphMin,
412 stepValue = Math.pow(10, rangeOrderOfMagnitude),
413 numberOfSteps = Math.round(graphRange / stepValue);
415 //If we have more space on the graph we'll use it to give more definition to the data
416 while((numberOfSteps > maxSteps || (numberOfSteps * 2) < maxSteps) && !skipFitting) {
417 if(numberOfSteps > maxSteps){
419 numberOfSteps = Math.round(graphRange/stepValue);
420 // Don't ever deal with a decimal number of steps - cancel fitting and just use the minimum number of steps.
421 if (numberOfSteps % 1 !== 0){
425 //We can fit in double the amount of scale points on the scale
427 //If user has declared ints only, and the step value isn't a decimal
428 if (integersOnly && rangeOrderOfMagnitude >= 0){
429 //If the user has said integers only, we need to check that making the scale more granular wouldn't make it a float
430 if(stepValue/2 % 1 === 0){
432 numberOfSteps = Math.round(graphRange/stepValue);
434 //If it would make it a float break out of the loop
439 //If the scale doesn't have to be an int, make the scale more granular anyway.
442 numberOfSteps = Math.round(graphRange/stepValue);
449 numberOfSteps = minSteps;
450 stepValue = graphRange / numberOfSteps;
454 steps : numberOfSteps,
455 stepValue : stepValue,
457 max : graphMin + (numberOfSteps * stepValue)
461 /* jshint ignore:start */
462 // Blows up jshint errors based on the new Function constructor
464 //Javascript micro templating by John Resig - source at http://ejohn.org/blog/javascript-micro-templating/
465 template = helpers.template = function(templateString, valuesObject){
467 // If templateString is function rather than string-template - call the function for valuesObject
469 if(templateString instanceof Function){
470 return templateString(valuesObject);
474 function tmpl(str, data){
475 // Figure out if we're getting a template, or if we need to
476 // load the template - and be sure to cache the result.
477 var fn = !/\W/.test(str) ?
478 cache[str] = cache[str] :
480 // Generate a reusable function that will serve as a template
481 // generator (and which will be cached).
483 "var p=[],print=function(){p.push.apply(p,arguments);};" +
485 // Introduce the data as local variables using with(){}
486 "with(obj){p.push('" +
488 // Convert the template into pure JavaScript
490 .replace(/[\r\t\n]/g, " ")
491 .split("<%").join("\t")
492 .replace(/((^|%>)[^\t]*)'/g, "$1\r")
493 .replace(/\t=(.*?)%>/g, "',$1,'")
494 .split("\t").join("');")
495 .split("%>").join("p.push('")
496 .split("\r").join("\\'") +
497 "');}return p.join('');"
500 // Provide some basic currying to the user
501 return data ? fn( data ) : fn;
503 return tmpl(templateString,valuesObject);
505 /* jshint ignore:end */
506 generateLabels = helpers.generateLabels = function(templateString,numberOfSteps,graphMin,stepValue){
507 var labelsArray = new Array(numberOfSteps);
508 if (labelTemplateString){
509 each(labelsArray,function(val,index){
510 labelsArray[index] = template(templateString,{value: (graphMin + (stepValue*(index+1)))});
515 //--Animation methods
516 //Easing functions adapted from Robert Penner's easing equations
517 //http://www.robertpenner.com/easing/
518 easingEffects = helpers.easingEffects = {
519 linear: function (t) {
522 easeInQuad: function (t) {
525 easeOutQuad: function (t) {
526 return -1 * t * (t - 2);
528 easeInOutQuad: function (t) {
529 if ((t /= 1 / 2) < 1) return 1 / 2 * t * t;
530 return -1 / 2 * ((--t) * (t - 2) - 1);
532 easeInCubic: function (t) {
535 easeOutCubic: function (t) {
536 return 1 * ((t = t / 1 - 1) * t * t + 1);
538 easeInOutCubic: function (t) {
539 if ((t /= 1 / 2) < 1) return 1 / 2 * t * t * t;
540 return 1 / 2 * ((t -= 2) * t * t + 2);
542 easeInQuart: function (t) {
543 return t * t * t * t;
545 easeOutQuart: function (t) {
546 return -1 * ((t = t / 1 - 1) * t * t * t - 1);
548 easeInOutQuart: function (t) {
549 if ((t /= 1 / 2) < 1) return 1 / 2 * t * t * t * t;
550 return -1 / 2 * ((t -= 2) * t * t * t - 2);
552 easeInQuint: function (t) {
553 return 1 * (t /= 1) * t * t * t * t;
555 easeOutQuint: function (t) {
556 return 1 * ((t = t / 1 - 1) * t * t * t * t + 1);
558 easeInOutQuint: function (t) {
559 if ((t /= 1 / 2) < 1) return 1 / 2 * t * t * t * t * t;
560 return 1 / 2 * ((t -= 2) * t * t * t * t + 2);
562 easeInSine: function (t) {
563 return -1 * Math.cos(t / 1 * (Math.PI / 2)) + 1;
565 easeOutSine: function (t) {
566 return 1 * Math.sin(t / 1 * (Math.PI / 2));
568 easeInOutSine: function (t) {
569 return -1 / 2 * (Math.cos(Math.PI * t / 1) - 1);
571 easeInExpo: function (t) {
572 return (t === 0) ? 1 : 1 * Math.pow(2, 10 * (t / 1 - 1));
574 easeOutExpo: function (t) {
575 return (t === 1) ? 1 : 1 * (-Math.pow(2, -10 * t / 1) + 1);
577 easeInOutExpo: function (t) {
578 if (t === 0) return 0;
579 if (t === 1) return 1;
580 if ((t /= 1 / 2) < 1) return 1 / 2 * Math.pow(2, 10 * (t - 1));
581 return 1 / 2 * (-Math.pow(2, -10 * --t) + 2);
583 easeInCirc: function (t) {
584 if (t >= 1) return t;
585 return -1 * (Math.sqrt(1 - (t /= 1) * t) - 1);
587 easeOutCirc: function (t) {
588 return 1 * Math.sqrt(1 - (t = t / 1 - 1) * t);
590 easeInOutCirc: function (t) {
591 if ((t /= 1 / 2) < 1) return -1 / 2 * (Math.sqrt(1 - t * t) - 1);
592 return 1 / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1);
594 easeInElastic: function (t) {
598 if (t === 0) return 0;
599 if ((t /= 1) == 1) return 1;
601 if (a < Math.abs(1)) {
604 } else s = p / (2 * Math.PI) * Math.asin(1 / a);
605 return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * 1 - s) * (2 * Math.PI) / p));
607 easeOutElastic: function (t) {
611 if (t === 0) return 0;
612 if ((t /= 1) == 1) return 1;
614 if (a < Math.abs(1)) {
617 } else s = p / (2 * Math.PI) * Math.asin(1 / a);
618 return a * Math.pow(2, -10 * t) * Math.sin((t * 1 - s) * (2 * Math.PI) / p) + 1;
620 easeInOutElastic: function (t) {
624 if (t === 0) return 0;
625 if ((t /= 1 / 2) == 2) return 1;
626 if (!p) p = 1 * (0.3 * 1.5);
627 if (a < Math.abs(1)) {
630 } else s = p / (2 * Math.PI) * Math.asin(1 / a);
631 if (t < 1) return -0.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * 1 - s) * (2 * Math.PI) / p));
632 return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * 1 - s) * (2 * Math.PI) / p) * 0.5 + 1;
634 easeInBack: function (t) {
636 return 1 * (t /= 1) * t * ((s + 1) * t - s);
638 easeOutBack: function (t) {
640 return 1 * ((t = t / 1 - 1) * t * ((s + 1) * t + s) + 1);
642 easeInOutBack: function (t) {
644 if ((t /= 1 / 2) < 1) return 1 / 2 * (t * t * (((s *= (1.525)) + 1) * t - s));
645 return 1 / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2);
647 easeInBounce: function (t) {
648 return 1 - easingEffects.easeOutBounce(1 - t);
650 easeOutBounce: function (t) {
651 if ((t /= 1) < (1 / 2.75)) {
652 return 1 * (7.5625 * t * t);
653 } else if (t < (2 / 2.75)) {
654 return 1 * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75);
655 } else if (t < (2.5 / 2.75)) {
656 return 1 * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375);
658 return 1 * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375);
661 easeInOutBounce: function (t) {
662 if (t < 1 / 2) return easingEffects.easeInBounce(t * 2) * 0.5;
663 return easingEffects.easeOutBounce(t * 2 - 1) * 0.5 + 1 * 0.5;
666 //Request animation polyfill - http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
667 requestAnimFrame = helpers.requestAnimFrame = (function(){
668 return window.requestAnimationFrame ||
669 window.webkitRequestAnimationFrame ||
670 window.mozRequestAnimationFrame ||
671 window.oRequestAnimationFrame ||
672 window.msRequestAnimationFrame ||
674 return window.setTimeout(callback, 1000 / 60);
677 cancelAnimFrame = helpers.cancelAnimFrame = (function(){
678 return window.cancelAnimationFrame ||
679 window.webkitCancelAnimationFrame ||
680 window.mozCancelAnimationFrame ||
681 window.oCancelAnimationFrame ||
682 window.msCancelAnimationFrame ||
684 return window.clearTimeout(callback, 1000 / 60);
687 animationLoop = helpers.animationLoop = function(callback,totalSteps,easingString,onProgress,onComplete,chartInstance){
690 easingFunction = easingEffects[easingString] || easingEffects.linear;
692 var animationFrame = function(){
694 var stepDecimal = currentStep/totalSteps;
695 var easeDecimal = easingFunction(stepDecimal);
697 callback.call(chartInstance,easeDecimal,stepDecimal, currentStep);
698 onProgress.call(chartInstance,easeDecimal,stepDecimal);
699 if (currentStep < totalSteps){
700 chartInstance.animationFrame = requestAnimFrame(animationFrame);
702 onComplete.apply(chartInstance);
705 requestAnimFrame(animationFrame);
708 getRelativePosition = helpers.getRelativePosition = function(evt){
710 var e = evt.originalEvent || evt,
711 canvas = evt.currentTarget || evt.srcElement,
712 boundingRect = canvas.getBoundingClientRect();
715 mouseX = e.touches[0].clientX - boundingRect.left;
716 mouseY = e.touches[0].clientY - boundingRect.top;
720 mouseX = e.clientX - boundingRect.left;
721 mouseY = e.clientY - boundingRect.top;
730 addEvent = helpers.addEvent = function(node,eventType,method){
731 if (node.addEventListener){
732 node.addEventListener(eventType,method);
733 } else if (node.attachEvent){
734 node.attachEvent("on"+eventType, method);
736 node["on"+eventType] = method;
739 removeEvent = helpers.removeEvent = function(node, eventType, handler){
740 if (node.removeEventListener){
741 node.removeEventListener(eventType, handler, false);
742 } else if (node.detachEvent){
743 node.detachEvent("on"+eventType,handler);
745 node["on" + eventType] = noop;
748 bindEvents = helpers.bindEvents = function(chartInstance, arrayOfEvents, handler){
749 // Create the events object if it's not already present
750 if (!chartInstance.events) chartInstance.events = {};
752 each(arrayOfEvents,function(eventName){
753 chartInstance.events[eventName] = function(){
754 handler.apply(chartInstance, arguments);
756 addEvent(chartInstance.chart.canvas,eventName,chartInstance.events[eventName]);
759 unbindEvents = helpers.unbindEvents = function (chartInstance, arrayOfEvents) {
760 each(arrayOfEvents, function(handler,eventName){
761 removeEvent(chartInstance.chart.canvas, eventName, handler);
764 getMaximumWidth = helpers.getMaximumWidth = function(domNode){
765 var container = domNode.parentNode;
766 // TODO = check cross browser stuff with this.
767 return container.clientWidth;
769 getMaximumHeight = helpers.getMaximumHeight = function(domNode){
770 var container = domNode.parentNode;
771 // TODO = check cross browser stuff with this.
772 return container.clientHeight;
774 getMaximumSize = helpers.getMaximumSize = helpers.getMaximumWidth, // legacy support
775 retinaScale = helpers.retinaScale = function(chart){
777 width = chart.canvas.width,
778 height = chart.canvas.height;
780 if (window.devicePixelRatio) {
781 ctx.canvas.style.width = width + "px";
782 ctx.canvas.style.height = height + "px";
783 ctx.canvas.height = height * window.devicePixelRatio;
784 ctx.canvas.width = width * window.devicePixelRatio;
785 ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
789 clear = helpers.clear = function(chart){
790 chart.ctx.clearRect(0,0,chart.width,chart.height);
792 fontString = helpers.fontString = function(pixelSize,fontStyle,fontFamily){
793 return fontStyle + " " + pixelSize+"px " + fontFamily;
795 longestText = helpers.longestText = function(ctx,font,arrayOfStrings){
798 each(arrayOfStrings,function(string){
799 var textWidth = ctx.measureText(string).width;
800 longest = (textWidth > longest) ? textWidth : longest;
804 drawRoundedRectangle = helpers.drawRoundedRectangle = function(ctx,x,y,width,height,radius){
806 ctx.moveTo(x + radius, y);
807 ctx.lineTo(x + width - radius, y);
808 ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
809 ctx.lineTo(x + width, y + height - radius);
810 ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
811 ctx.lineTo(x + radius, y + height);
812 ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
813 ctx.lineTo(x, y + radius);
814 ctx.quadraticCurveTo(x, y, x + radius, y);
819 //Store a reference to each instance - allowing us to globally resize chart instances on window resize.
820 //Destroy method on the chart will remove the instance of the chart from this reference.
821 Chart.instances = {};
823 Chart.Type = function(data,options,chart){
824 this.options = options;
827 //Add the chart instance to the global namespace
828 Chart.instances[this.id] = this;
830 // Initialize is always called when a chart type is created
831 // By default it is a no op, but it should be extended
832 if (options.responsive){
835 this.initialize.call(this,data);
838 //Core methods that'll be a part of every chart type
839 extend(Chart.Type.prototype,{
840 initialize : function(){return this;},
846 // Stops any current animation loop occuring
847 cancelAnimFrame(this.animationFrame);
850 resize : function(callback){
852 var canvas = this.chart.canvas,
853 newWidth = getMaximumWidth(this.chart.canvas),
854 newHeight = this.options.maintainAspectRatio ? newWidth / this.chart.aspectRatio : getMaximumHeight(this.chart.canvas);
856 canvas.width = this.chart.width = newWidth;
857 canvas.height = this.chart.height = newHeight;
859 retinaScale(this.chart);
861 if (typeof callback === "function"){
862 callback.apply(this, Array.prototype.slice.call(arguments, 1));
867 render : function(reflow){
871 if (this.options.animation && !reflow){
872 helpers.animationLoop(
874 this.options.animationSteps,
875 this.options.animationEasing,
876 this.options.onAnimationProgress,
877 this.options.onAnimationComplete,
883 this.options.onAnimationComplete.call(this);
887 generateLegend : function(){
888 return template(this.options.legendTemplate,this);
890 destroy : function(){
892 unbindEvents(this, this.events);
893 var canvas = this.chart.canvas;
895 // Reset canvas height/width attributes starts a fresh with the canvas context
896 canvas.width = this.chart.width;
897 canvas.height = this.chart.height;
899 // < IE9 doesn't support removeProperty
900 if (canvas.style.removeProperty) {
901 canvas.style.removeProperty('width');
902 canvas.style.removeProperty('height');
904 canvas.style.removeAttribute('width');
905 canvas.style.removeAttribute('height');
908 delete Chart.instances[this.id];
910 showTooltip : function(ChartElements, forceRedraw){
911 // Only redraw the chart if we've actually changed what we're hovering on.
912 if (typeof this.activeElements === 'undefined') this.activeElements = [];
914 var isChanged = (function(Elements){
917 if (Elements.length !== this.activeElements.length){
922 each(Elements, function(element, index){
923 if (element !== this.activeElements[index]){
928 }).call(this, ChartElements);
930 if (!isChanged && !forceRedraw){
934 this.activeElements = ChartElements;
937 if(this.options.customTooltips){
938 this.options.customTooltips(false);
940 if (ChartElements.length > 0){
941 // If we have multiple datasets, show a MultiTooltip for all of the data points at that index
942 if (this.datasets && this.datasets.length > 1) {
946 for (var i = this.datasets.length - 1; i >= 0; i--) {
947 dataArray = this.datasets[i].points || this.datasets[i].bars || this.datasets[i].segments;
948 dataIndex = indexOf(dataArray, ChartElements[0]);
949 if (dataIndex !== -1){
953 var tooltipLabels = [],
955 medianPosition = (function(index) {
957 // Get all the points at that particular index
966 helpers.each(this.datasets, function(dataset){
967 dataCollection = dataset.points || dataset.bars || dataset.segments;
968 if (dataCollection[dataIndex] && dataCollection[dataIndex].hasValue()){
969 Elements.push(dataCollection[dataIndex]);
973 helpers.each(Elements, function(element) {
974 xPositions.push(element.x);
975 yPositions.push(element.y);
978 //Include any colour information about the element
979 tooltipLabels.push(helpers.template(this.options.multiTooltipTemplate, element));
981 fill: element._saved.fillColor || element.fillColor,
982 stroke: element._saved.strokeColor || element.strokeColor
987 yMin = min(yPositions);
988 yMax = max(yPositions);
990 xMin = min(xPositions);
991 xMax = max(xPositions);
994 x: (xMin > this.chart.width/2) ? xMin : xMax,
997 }).call(this, dataIndex);
999 new Chart.MultiTooltip({
1000 x: medianPosition.x,
1001 y: medianPosition.y,
1002 xPadding: this.options.tooltipXPadding,
1003 yPadding: this.options.tooltipYPadding,
1004 xOffset: this.options.tooltipXOffset,
1005 fillColor: this.options.tooltipFillColor,
1006 textColor: this.options.tooltipFontColor,
1007 fontFamily: this.options.tooltipFontFamily,
1008 fontStyle: this.options.tooltipFontStyle,
1009 fontSize: this.options.tooltipFontSize,
1010 titleTextColor: this.options.tooltipTitleFontColor,
1011 titleFontFamily: this.options.tooltipTitleFontFamily,
1012 titleFontStyle: this.options.tooltipTitleFontStyle,
1013 titleFontSize: this.options.tooltipTitleFontSize,
1014 cornerRadius: this.options.tooltipCornerRadius,
1015 labels: tooltipLabels,
1016 legendColors: tooltipColors,
1017 legendColorBackground : this.options.multiTooltipKeyBackground,
1018 title: ChartElements[0].label,
1020 ctx: this.chart.ctx,
1021 custom: this.options.customTooltips
1025 each(ChartElements, function(Element) {
1026 var tooltipPosition = Element.tooltipPosition();
1028 x: Math.round(tooltipPosition.x),
1029 y: Math.round(tooltipPosition.y),
1030 xPadding: this.options.tooltipXPadding,
1031 yPadding: this.options.tooltipYPadding,
1032 fillColor: this.options.tooltipFillColor,
1033 textColor: this.options.tooltipFontColor,
1034 fontFamily: this.options.tooltipFontFamily,
1035 fontStyle: this.options.tooltipFontStyle,
1036 fontSize: this.options.tooltipFontSize,
1037 caretHeight: this.options.tooltipCaretSize,
1038 cornerRadius: this.options.tooltipCornerRadius,
1039 text: template(this.options.tooltipTemplate, Element),
1041 custom: this.options.customTooltips
1048 toBase64Image : function(){
1049 return this.chart.canvas.toDataURL.apply(this.chart.canvas, arguments);
1053 Chart.Type.extend = function(extensions){
1057 var ChartType = function(){
1058 return parent.apply(this,arguments);
1061 //Copy the prototype object of the this class
1062 ChartType.prototype = clone(parent.prototype);
1063 //Now overwrite some of the properties in the base class with the new extensions
1064 extend(ChartType.prototype, extensions);
1066 ChartType.extend = Chart.Type.extend;
1068 if (extensions.name || parent.prototype.name){
1070 var chartName = extensions.name || parent.prototype.name;
1071 //Assign any potential default values of the new chart type
1073 //If none are defined, we'll use a clone of the chart type this is being extended from.
1074 //I.e. if we extend a line chart, we'll use the defaults from the line chart if our new chart
1075 //doesn't define some defaults of their own.
1077 var baseDefaults = (Chart.defaults[parent.prototype.name]) ? clone(Chart.defaults[parent.prototype.name]) : {};
1079 Chart.defaults[chartName] = extend(baseDefaults,extensions.defaults);
1081 Chart.types[chartName] = ChartType;
1083 //Register this new chart type in the Chart prototype
1084 Chart.prototype[chartName] = function(data,options){
1085 var config = merge(Chart.defaults.global, Chart.defaults[chartName], options || {});
1086 return new ChartType(data,config,this);
1089 warn("Name not provided for this chart, so it hasn't been registered");
1094 Chart.Element = function(configuration){
1095 extend(this,configuration);
1096 this.initialize.apply(this,arguments);
1099 extend(Chart.Element.prototype,{
1100 initialize : function(){},
1101 restore : function(props){
1103 extend(this,this._saved);
1105 each(props,function(key){
1106 this[key] = this._saved[key];
1112 this._saved = clone(this);
1113 delete this._saved._saved;
1116 update : function(newProps){
1117 each(newProps,function(value,key){
1118 this._saved[key] = this[key];
1123 transition : function(props,ease){
1124 each(props,function(value,key){
1125 this[key] = ((value - this._saved[key]) * ease) + this._saved[key];
1129 tooltipPosition : function(){
1135 hasValue: function(){
1136 return isNumber(this.value);
1140 Chart.Element.extend = inherits;
1143 Chart.Point = Chart.Element.extend({
1145 inRange: function(chartX,chartY){
1146 var hitDetectionRange = this.hitDetectionRadius + this.radius;
1147 return ((Math.pow(chartX-this.x, 2)+Math.pow(chartY-this.y, 2)) < Math.pow(hitDetectionRange,2));
1154 ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
1157 ctx.strokeStyle = this.strokeColor;
1158 ctx.lineWidth = this.strokeWidth;
1160 ctx.fillStyle = this.fillColor;
1167 //Quick debug for bezier curve splining
1168 //Highlights control points and the line between them.
1169 //Handy for dev - stripped in the min version.
1172 // ctx.fillStyle = "black";
1173 // ctx.strokeStyle = "black"
1175 // ctx.arc(this.controlPoints.inner.x,this.controlPoints.inner.y, 2, 0, Math.PI*2);
1179 // ctx.arc(this.controlPoints.outer.x,this.controlPoints.outer.y, 2, 0, Math.PI*2);
1182 // ctx.moveTo(this.controlPoints.inner.x,this.controlPoints.inner.y);
1183 // ctx.lineTo(this.x, this.y);
1184 // ctx.lineTo(this.controlPoints.outer.x,this.controlPoints.outer.y);
1194 Chart.Arc = Chart.Element.extend({
1195 inRange : function(chartX,chartY){
1197 var pointRelativePosition = helpers.getAngleFromPoint(this, {
1202 //Check if within the range of the open/close angle
1203 var betweenAngles = (pointRelativePosition.angle >= this.startAngle && pointRelativePosition.angle <= this.endAngle),
1204 withinRadius = (pointRelativePosition.distance >= this.innerRadius && pointRelativePosition.distance <= this.outerRadius);
1206 return (betweenAngles && withinRadius);
1207 //Ensure within the outside of the arc centre, but inside arc outer
1209 tooltipPosition : function(){
1210 var centreAngle = this.startAngle + ((this.endAngle - this.startAngle) / 2),
1211 rangeFromCentre = (this.outerRadius - this.innerRadius) / 2 + this.innerRadius;
1213 x : this.x + (Math.cos(centreAngle) * rangeFromCentre),
1214 y : this.y + (Math.sin(centreAngle) * rangeFromCentre)
1217 draw : function(animationPercent){
1219 var easingDecimal = animationPercent || 1;
1225 ctx.arc(this.x, this.y, this.outerRadius, this.startAngle, this.endAngle);
1227 ctx.arc(this.x, this.y, this.innerRadius, this.endAngle, this.startAngle, true);
1230 ctx.strokeStyle = this.strokeColor;
1231 ctx.lineWidth = this.strokeWidth;
1233 ctx.fillStyle = this.fillColor;
1236 ctx.lineJoin = 'bevel';
1238 if (this.showStroke){
1244 Chart.Rectangle = Chart.Element.extend({
1247 halfWidth = this.width/2,
1248 leftX = this.x - halfWidth,
1249 rightX = this.x + halfWidth,
1250 top = this.base - (this.base - this.y),
1251 halfStroke = this.strokeWidth / 2;
1253 // Canvas doesn't allow us to stroke inside the width so we can
1254 // adjust the sizes to fit if we're setting a stroke on the line
1255 if (this.showStroke){
1256 leftX += halfStroke;
1257 rightX -= halfStroke;
1263 ctx.fillStyle = this.fillColor;
1264 ctx.strokeStyle = this.strokeColor;
1265 ctx.lineWidth = this.strokeWidth;
1267 // It'd be nice to keep this class totally generic to any rectangle
1268 // and simply specify which border to miss out.
1269 ctx.moveTo(leftX, this.base);
1270 ctx.lineTo(leftX, top);
1271 ctx.lineTo(rightX, top);
1272 ctx.lineTo(rightX, this.base);
1274 if (this.showStroke){
1278 height : function(){
1279 return this.base - this.y;
1281 inRange : function(chartX,chartY){
1282 return (chartX >= this.x - this.width/2 && chartX <= this.x + this.width/2) && (chartY >= this.y && chartY <= this.base);
1286 Chart.Tooltip = Chart.Element.extend({
1289 var ctx = this.chart.ctx;
1291 ctx.font = fontString(this.fontSize,this.fontStyle,this.fontFamily);
1293 this.xAlign = "center";
1294 this.yAlign = "above";
1296 //Distance between the actual element.y position and the start of the tooltip caret
1297 var caretPadding = this.caretPadding = 2;
1299 var tooltipWidth = ctx.measureText(this.text).width + 2*this.xPadding,
1300 tooltipRectHeight = this.fontSize + 2*this.yPadding,
1301 tooltipHeight = tooltipRectHeight + this.caretHeight + caretPadding;
1303 if (this.x + tooltipWidth/2 >this.chart.width){
1304 this.xAlign = "left";
1305 } else if (this.x - tooltipWidth/2 < 0){
1306 this.xAlign = "right";
1309 if (this.y - tooltipHeight < 0){
1310 this.yAlign = "below";
1314 var tooltipX = this.x - tooltipWidth/2,
1315 tooltipY = this.y - tooltipHeight;
1317 ctx.fillStyle = this.fillColor;
1327 //Draw a caret above the x/y
1329 ctx.moveTo(this.x,this.y - caretPadding);
1330 ctx.lineTo(this.x + this.caretHeight, this.y - (caretPadding + this.caretHeight));
1331 ctx.lineTo(this.x - this.caretHeight, this.y - (caretPadding + this.caretHeight));
1336 tooltipY = this.y + caretPadding + this.caretHeight;
1337 //Draw a caret below the x/y
1339 ctx.moveTo(this.x, this.y + caretPadding);
1340 ctx.lineTo(this.x + this.caretHeight, this.y + caretPadding + this.caretHeight);
1341 ctx.lineTo(this.x - this.caretHeight, this.y + caretPadding + this.caretHeight);
1350 tooltipX = this.x - tooltipWidth + (this.cornerRadius + this.caretHeight);
1353 tooltipX = this.x - (this.cornerRadius + this.caretHeight);
1357 drawRoundedRectangle(ctx,tooltipX,tooltipY,tooltipWidth,tooltipRectHeight,this.cornerRadius);
1361 ctx.fillStyle = this.textColor;
1362 ctx.textAlign = "center";
1363 ctx.textBaseline = "middle";
1364 ctx.fillText(this.text, tooltipX + tooltipWidth/2, tooltipY + tooltipRectHeight/2);
1369 Chart.MultiTooltip = Chart.Element.extend({
1370 initialize : function(){
1371 this.font = fontString(this.fontSize,this.fontStyle,this.fontFamily);
1373 this.titleFont = fontString(this.titleFontSize,this.titleFontStyle,this.titleFontFamily);
1375 this.height = (this.labels.length * this.fontSize) + ((this.labels.length-1) * (this.fontSize/2)) + (this.yPadding*2) + this.titleFontSize *1.5;
1377 this.ctx.font = this.titleFont;
1379 var titleWidth = this.ctx.measureText(this.title).width,
1380 //Label has a legend square as well so account for this.
1381 labelWidth = longestText(this.ctx,this.font,this.labels) + this.fontSize + 3,
1382 longestTextWidth = max([labelWidth,titleWidth]);
1384 this.width = longestTextWidth + (this.xPadding*2);
1387 var halfHeight = this.height/2;
1389 //Check to ensure the height will fit on the canvas
1390 if (this.y - halfHeight < 0 ){
1391 this.y = halfHeight;
1392 } else if (this.y + halfHeight > this.chart.height){
1393 this.y = this.chart.height - halfHeight;
1396 //Decide whether to align left or right based on position on canvas
1397 if (this.x > this.chart.width/2){
1398 this.x -= this.xOffset + this.width;
1400 this.x += this.xOffset;
1405 getLineHeight : function(index){
1406 var baseLineHeight = this.y - (this.height/2) + this.yPadding,
1407 afterTitleIndex = index-1;
1409 //If the index is zero, we're getting the title
1411 return baseLineHeight + this.titleFontSize/2;
1413 return baseLineHeight + ((this.fontSize*1.5*afterTitleIndex) + this.fontSize/2) + this.titleFontSize * 1.5;
1423 drawRoundedRectangle(this.ctx,this.x,this.y - this.height/2,this.width,this.height,this.cornerRadius);
1425 ctx.fillStyle = this.fillColor;
1429 ctx.textAlign = "left";
1430 ctx.textBaseline = "middle";
1431 ctx.fillStyle = this.titleTextColor;
1432 ctx.font = this.titleFont;
1434 ctx.fillText(this.title,this.x + this.xPadding, this.getLineHeight(0));
1436 ctx.font = this.font;
1437 helpers.each(this.labels,function(label,index){
1438 ctx.fillStyle = this.textColor;
1439 ctx.fillText(label,this.x + this.xPadding + this.fontSize + 3, this.getLineHeight(index + 1));
1441 //A bit gnarly, but clearing this rectangle breaks when using explorercanvas (clears whole canvas)
1442 //ctx.clearRect(this.x + this.xPadding, this.getLineHeight(index + 1) - this.fontSize/2, this.fontSize, this.fontSize);
1443 //Instead we'll make a white filled block to put the legendColour palette over.
1445 ctx.fillStyle = this.legendColorBackground;
1446 ctx.fillRect(this.x + this.xPadding, this.getLineHeight(index + 1) - this.fontSize/2, this.fontSize, this.fontSize);
1448 ctx.fillStyle = this.legendColors[index].fill;
1449 ctx.fillRect(this.x + this.xPadding, this.getLineHeight(index + 1) - this.fontSize/2, this.fontSize, this.fontSize);
1457 Chart.Scale = Chart.Element.extend({
1458 initialize : function(){
1461 buildYLabels : function(){
1464 var stepDecimalPlaces = getDecimalPlaces(this.stepValue);
1466 for (var i=0; i<=this.steps; i++){
1467 this.yLabels.push(template(this.templateString,{value:(this.min + (i * this.stepValue)).toFixed(stepDecimalPlaces)}));
1469 this.yLabelWidth = (this.display && this.showLabels) ? longestText(this.ctx,this.font,this.yLabels) : 0;
1471 addXLabel : function(label){
1472 this.xLabels.push(label);
1476 removeXLabel : function(){
1477 this.xLabels.shift();
1481 // Fitting loop to rotate x Labels and figure out what fits there, and also calculate how many Y steps to use
1483 // First we need the width of the yLabels, assuming the xLabels aren't rotated
1485 // To do that we need the base line at the top and base of the chart, assuming there is no x label rotation
1486 this.startPoint = (this.display) ? this.fontSize : 0;
1487 this.endPoint = (this.display) ? this.height - (this.fontSize * 1.5) - 5 : this.height; // -5 to pad labels
1489 // Apply padding settings to the start and end point.
1490 this.startPoint += this.padding;
1491 this.endPoint -= this.padding;
1493 // Cache the starting height, so can determine if we need to recalculate the scale yAxis
1494 var cachedHeight = this.endPoint - this.startPoint,
1497 // Build the current yLabels so we have an idea of what size they'll be to start
1499 * This sets what is returned from calculateScaleRange as static properties of this class:
1507 this.calculateYRange(cachedHeight);
1509 // With these properties set we can now build the array of yLabels
1510 // and also the width of the largest yLabel
1511 this.buildYLabels();
1513 this.calculateXLabelRotation();
1515 while((cachedHeight > this.endPoint - this.startPoint)){
1516 cachedHeight = this.endPoint - this.startPoint;
1517 cachedYLabelWidth = this.yLabelWidth;
1519 this.calculateYRange(cachedHeight);
1520 this.buildYLabels();
1522 // Only go through the xLabel loop again if the yLabel width has changed
1523 if (cachedYLabelWidth < this.yLabelWidth){
1524 this.calculateXLabelRotation();
1529 calculateXLabelRotation : function(){
1530 //Get the width of each grid by calculating the difference
1531 //between x offsets between 0 and 1.
1533 this.ctx.font = this.font;
1535 var firstWidth = this.ctx.measureText(this.xLabels[0]).width,
1536 lastWidth = this.ctx.measureText(this.xLabels[this.xLabels.length - 1]).width,
1541 this.xScalePaddingRight = lastWidth/2 + 3;
1542 this.xScalePaddingLeft = (firstWidth/2 > this.yLabelWidth + 10) ? firstWidth/2 : this.yLabelWidth + 10;
1544 this.xLabelRotation = 0;
1546 var originalLabelWidth = longestText(this.ctx,this.font,this.xLabels),
1549 this.xLabelWidth = originalLabelWidth;
1550 //Allow 3 pixels x2 padding either side for label readability
1551 var xGridWidth = Math.floor(this.calculateX(1) - this.calculateX(0)) - 6;
1553 //Max label rotate should be 90 - also act as a loop counter
1554 while ((this.xLabelWidth > xGridWidth && this.xLabelRotation === 0) || (this.xLabelWidth > xGridWidth && this.xLabelRotation <= 90 && this.xLabelRotation > 0)){
1555 cosRotation = Math.cos(toRadians(this.xLabelRotation));
1557 firstRotated = cosRotation * firstWidth;
1558 lastRotated = cosRotation * lastWidth;
1560 // We're right aligning the text now.
1561 if (firstRotated + this.fontSize / 2 > this.yLabelWidth + 8){
1562 this.xScalePaddingLeft = firstRotated + this.fontSize / 2;
1564 this.xScalePaddingRight = this.fontSize/2;
1567 this.xLabelRotation++;
1568 this.xLabelWidth = cosRotation * originalLabelWidth;
1571 if (this.xLabelRotation > 0){
1572 this.endPoint -= Math.sin(toRadians(this.xLabelRotation))*originalLabelWidth + 3;
1576 this.xLabelWidth = 0;
1577 this.xScalePaddingRight = this.padding;
1578 this.xScalePaddingLeft = this.padding;
1582 // Needs to be overidden in each Chart type
1583 // Otherwise we need to pass all the data into the scale class
1584 calculateYRange: noop,
1585 drawingArea: function(){
1586 return this.startPoint - this.endPoint;
1588 calculateY : function(value){
1589 var scalingFactor = this.drawingArea() / (this.min - this.max);
1590 return this.endPoint - (scalingFactor * (value - this.min));
1592 calculateX : function(index){
1593 var isRotated = (this.xLabelRotation > 0),
1594 // innerWidth = (this.offsetGridLines) ? this.width - offsetLeft - this.padding : this.width - (offsetLeft + halfLabelWidth * 2) - this.padding,
1595 innerWidth = this.width - (this.xScalePaddingLeft + this.xScalePaddingRight),
1596 valueWidth = innerWidth/Math.max((this.valuesCount - ((this.offsetGridLines) ? 0 : 1)), 1),
1597 valueOffset = (valueWidth * index) + this.xScalePaddingLeft;
1599 if (this.offsetGridLines){
1600 valueOffset += (valueWidth/2);
1603 return Math.round(valueOffset);
1605 update : function(newProps){
1606 helpers.extend(this, newProps);
1611 yLabelGap = (this.endPoint - this.startPoint) / this.steps,
1612 xStart = Math.round(this.xScalePaddingLeft);
1614 ctx.fillStyle = this.textColor;
1615 ctx.font = this.font;
1616 each(this.yLabels,function(labelString,index){
1617 var yLabelCenter = this.endPoint - (yLabelGap * index),
1618 linePositionY = Math.round(yLabelCenter),
1619 drawHorizontalLine = this.showHorizontalLines;
1621 ctx.textAlign = "right";
1622 ctx.textBaseline = "middle";
1623 if (this.showLabels){
1624 ctx.fillText(labelString,xStart - 10,yLabelCenter);
1627 // This is X axis, so draw it
1628 if (index === 0 && !drawHorizontalLine){
1629 drawHorizontalLine = true;
1632 if (drawHorizontalLine){
1637 // This is a grid line in the centre, so drop that
1638 ctx.lineWidth = this.gridLineWidth;
1639 ctx.strokeStyle = this.gridLineColor;
1641 // This is the first line on the scale
1642 ctx.lineWidth = this.lineWidth;
1643 ctx.strokeStyle = this.lineColor;
1646 linePositionY += helpers.aliasPixel(ctx.lineWidth);
1648 if(drawHorizontalLine){
1649 ctx.moveTo(xStart, linePositionY);
1650 ctx.lineTo(this.width, linePositionY);
1655 ctx.lineWidth = this.lineWidth;
1656 ctx.strokeStyle = this.lineColor;
1658 ctx.moveTo(xStart - 5, linePositionY);
1659 ctx.lineTo(xStart, linePositionY);
1665 each(this.xLabels,function(label,index){
1666 var xPos = this.calculateX(index) + aliasPixel(this.lineWidth),
1667 // Check to see if line/bar here and decide where to place the line
1668 linePos = this.calculateX(index - (this.offsetGridLines ? 0.5 : 0)) + aliasPixel(this.lineWidth),
1669 isRotated = (this.xLabelRotation > 0),
1670 drawVerticalLine = this.showVerticalLines;
1672 // This is Y axis, so draw it
1673 if (index === 0 && !drawVerticalLine){
1674 drawVerticalLine = true;
1677 if (drawVerticalLine){
1682 // This is a grid line in the centre, so drop that
1683 ctx.lineWidth = this.gridLineWidth;
1684 ctx.strokeStyle = this.gridLineColor;
1686 // This is the first line on the scale
1687 ctx.lineWidth = this.lineWidth;
1688 ctx.strokeStyle = this.lineColor;
1691 if (drawVerticalLine){
1692 ctx.moveTo(linePos,this.endPoint);
1693 ctx.lineTo(linePos,this.startPoint - 3);
1699 ctx.lineWidth = this.lineWidth;
1700 ctx.strokeStyle = this.lineColor;
1703 // Small lines at the bottom of the base grid line
1705 ctx.moveTo(linePos,this.endPoint);
1706 ctx.lineTo(linePos,this.endPoint + 5);
1711 ctx.translate(xPos,(isRotated) ? this.endPoint + 12 : this.endPoint + 8);
1712 ctx.rotate(toRadians(this.xLabelRotation)*-1);
1713 ctx.font = this.font;
1714 ctx.textAlign = (isRotated) ? "right" : "center";
1715 ctx.textBaseline = (isRotated) ? "middle" : "top";
1716 ctx.fillText(label, 0, 0);
1725 Chart.RadialScale = Chart.Element.extend({
1726 initialize: function(){
1727 this.size = min([this.height, this.width]);
1728 this.drawingArea = (this.display) ? (this.size/2) - (this.fontSize/2 + this.backdropPaddingY) : (this.size/2);
1730 calculateCenterOffset: function(value){
1731 // Take into account half font size + the yPadding of the top value
1732 var scalingFactor = this.drawingArea / (this.max - this.min);
1734 return (value - this.min) * scalingFactor;
1736 update : function(){
1738 this.setScaleSize();
1740 this.drawingArea = (this.display) ? (this.size/2) - (this.fontSize/2 + this.backdropPaddingY) : (this.size/2);
1742 this.buildYLabels();
1744 buildYLabels: function(){
1747 var stepDecimalPlaces = getDecimalPlaces(this.stepValue);
1749 for (var i=0; i<=this.steps; i++){
1750 this.yLabels.push(template(this.templateString,{value:(this.min + (i * this.stepValue)).toFixed(stepDecimalPlaces)}));
1753 getCircumference : function(){
1754 return ((Math.PI*2) / this.valuesCount);
1756 setScaleSize: function(){
1758 * Right, this is really confusing and there is a lot of maths going on here
1759 * The gist of the problem is here: https://gist.github.com/nnnick/696cc9c55f4b0beb8fe9
1761 * Reaction: https://dl.dropboxusercontent.com/u/34601363/toomuchscience.gif
1765 * We assume the radius of the polygon is half the size of the canvas at first
1766 * at each index we check if the text overlaps.
1768 * Where it does, we store that angle and that index.
1770 * After finding the largest index and angle we calculate how much we need to remove
1771 * from the shape radius to move the point inwards by that x.
1773 * We average the left and right distances to get the maximum shape radius that can fit in the box
1774 * along with labels.
1776 * Once we have that, we can find the centre point for the chart, by taking the x text protrusion
1777 * on each side, removing that from the size, halving it and adding the left x protrusion width.
1779 * This will mean we have a shape fitted to the canvas, as large as it can be with the labels
1780 * and position it in the most space efficient manner
1782 * https://dl.dropboxusercontent.com/u/34601363/yeahscience.gif
1786 // Get maximum radius of the polygon. Either half the height (minus the text width) or half the width.
1787 // Use this to calculate the offset + change. - Make sure L/R protrusion is at least 0 to stop issues with centre points
1788 var largestPossibleRadius = min([(this.height/2 - this.pointLabelFontSize - 5), this.width/2]),
1793 furthestRight = this.width,
1801 radiusReductionRight,
1802 radiusReductionLeft,
1804 this.ctx.font = fontString(this.pointLabelFontSize,this.pointLabelFontStyle,this.pointLabelFontFamily);
1805 for (i=0;i<this.valuesCount;i++){
1806 // 5px to space the text slightly out - similar to what we do in the draw function.
1807 pointPosition = this.getPointPosition(i, largestPossibleRadius);
1808 textWidth = this.ctx.measureText(template(this.templateString, { value: this.labels[i] })).width + 5;
1809 if (i === 0 || i === this.valuesCount/2){
1810 // If we're at index zero, or exactly the middle, we're at exactly the top/bottom
1811 // of the radar chart, so text will be aligned centrally, so we'll half it and compare
1812 // w/left and right text sizes
1813 halfTextWidth = textWidth/2;
1814 if (pointPosition.x + halfTextWidth > furthestRight) {
1815 furthestRight = pointPosition.x + halfTextWidth;
1816 furthestRightIndex = i;
1818 if (pointPosition.x - halfTextWidth < furthestLeft) {
1819 furthestLeft = pointPosition.x - halfTextWidth;
1820 furthestLeftIndex = i;
1823 else if (i < this.valuesCount/2) {
1824 // Less than half the values means we'll left align the text
1825 if (pointPosition.x + textWidth > furthestRight) {
1826 furthestRight = pointPosition.x + textWidth;
1827 furthestRightIndex = i;
1830 else if (i > this.valuesCount/2){
1831 // More than half the values means we'll right align the text
1832 if (pointPosition.x - textWidth < furthestLeft) {
1833 furthestLeft = pointPosition.x - textWidth;
1834 furthestLeftIndex = i;
1839 xProtrusionLeft = furthestLeft;
1841 xProtrusionRight = Math.ceil(furthestRight - this.width);
1843 furthestRightAngle = this.getIndexAngle(furthestRightIndex);
1845 furthestLeftAngle = this.getIndexAngle(furthestLeftIndex);
1847 radiusReductionRight = xProtrusionRight / Math.sin(furthestRightAngle + Math.PI/2);
1849 radiusReductionLeft = xProtrusionLeft / Math.sin(furthestLeftAngle + Math.PI/2);
1851 // Ensure we actually need to reduce the size of the chart
1852 radiusReductionRight = (isNumber(radiusReductionRight)) ? radiusReductionRight : 0;
1853 radiusReductionLeft = (isNumber(radiusReductionLeft)) ? radiusReductionLeft : 0;
1855 this.drawingArea = largestPossibleRadius - (radiusReductionLeft + radiusReductionRight)/2;
1857 //this.drawingArea = min([maxWidthRadius, (this.height - (2 * (this.pointLabelFontSize + 5)))/2])
1858 this.setCenterPoint(radiusReductionLeft, radiusReductionRight);
1861 setCenterPoint: function(leftMovement, rightMovement){
1863 var maxRight = this.width - rightMovement - this.drawingArea,
1864 maxLeft = leftMovement + this.drawingArea;
1866 this.xCenter = (maxLeft + maxRight)/2;
1867 // Always vertically in the centre as the text height doesn't change
1868 this.yCenter = (this.height/2);
1871 getIndexAngle : function(index){
1872 var angleMultiplier = (Math.PI * 2) / this.valuesCount;
1873 // Start from the top instead of right, so remove a quarter of the circle
1875 return index * angleMultiplier - (Math.PI/2);
1877 getPointPosition : function(index, distanceFromCenter){
1878 var thisAngle = this.getIndexAngle(index);
1880 x : (Math.cos(thisAngle) * distanceFromCenter) + this.xCenter,
1881 y : (Math.sin(thisAngle) * distanceFromCenter) + this.yCenter
1887 each(this.yLabels, function(label, index){
1888 // Don't draw a centre value
1890 var yCenterOffset = index * (this.drawingArea/this.steps),
1891 yHeight = this.yCenter - yCenterOffset,
1894 // Draw circular lines around the scale
1895 if (this.lineWidth > 0){
1896 ctx.strokeStyle = this.lineColor;
1897 ctx.lineWidth = this.lineWidth;
1901 ctx.arc(this.xCenter, this.yCenter, yCenterOffset, 0, Math.PI*2);
1906 for (var i=0;i<this.valuesCount;i++)
1908 pointPosition = this.getPointPosition(i, this.calculateCenterOffset(this.min + (index * this.stepValue)));
1910 ctx.moveTo(pointPosition.x, pointPosition.y);
1912 ctx.lineTo(pointPosition.x, pointPosition.y);
1919 if(this.showLabels){
1920 ctx.font = fontString(this.fontSize,this.fontStyle,this.fontFamily);
1921 if (this.showLabelBackdrop){
1922 var labelWidth = ctx.measureText(label).width;
1923 ctx.fillStyle = this.backdropColor;
1925 this.xCenter - labelWidth/2 - this.backdropPaddingX,
1926 yHeight - this.fontSize/2 - this.backdropPaddingY,
1927 labelWidth + this.backdropPaddingX*2,
1928 this.fontSize + this.backdropPaddingY*2
1931 ctx.textAlign = 'center';
1932 ctx.textBaseline = "middle";
1933 ctx.fillStyle = this.fontColor;
1934 ctx.fillText(label, this.xCenter, yHeight);
1940 ctx.lineWidth = this.angleLineWidth;
1941 ctx.strokeStyle = this.angleLineColor;
1942 for (var i = this.valuesCount - 1; i >= 0; i--) {
1943 if (this.angleLineWidth > 0){
1944 var outerPosition = this.getPointPosition(i, this.calculateCenterOffset(this.max));
1946 ctx.moveTo(this.xCenter, this.yCenter);
1947 ctx.lineTo(outerPosition.x, outerPosition.y);
1951 // Extra 3px out for some label spacing
1952 var pointLabelPosition = this.getPointPosition(i, this.calculateCenterOffset(this.max) + 5);
1953 ctx.font = fontString(this.pointLabelFontSize,this.pointLabelFontStyle,this.pointLabelFontFamily);
1954 ctx.fillStyle = this.pointLabelFontColor;
1956 var labelsCount = this.labels.length,
1957 halfLabelsCount = this.labels.length/2,
1958 quarterLabelsCount = halfLabelsCount/2,
1959 upperHalf = (i < quarterLabelsCount || i > labelsCount - quarterLabelsCount),
1960 exactQuarter = (i === quarterLabelsCount || i === labelsCount - quarterLabelsCount);
1962 ctx.textAlign = 'center';
1963 } else if(i === halfLabelsCount){
1964 ctx.textAlign = 'center';
1965 } else if (i < halfLabelsCount){
1966 ctx.textAlign = 'left';
1968 ctx.textAlign = 'right';
1971 // Set the correct text baseline based on outer positioning
1973 ctx.textBaseline = 'middle';
1974 } else if (upperHalf){
1975 ctx.textBaseline = 'bottom';
1977 ctx.textBaseline = 'top';
1980 ctx.fillText(this.labels[i], pointLabelPosition.x, pointLabelPosition.y);
1987 // Attach global event to resize each chart instance when the browser resizes
1988 helpers.addEvent(window, "resize", (function(){
1989 // Basic debounce of resize function so it doesn't hurt performance when resizing browser.
1992 clearTimeout(timeout);
1993 timeout = setTimeout(function(){
1994 each(Chart.instances,function(instance){
1995 // If the responsive flag is set in the chart instance config
1996 // Cascade the resize event down to the chart.
1997 if (instance.options.responsive){
1998 instance.resize(instance.render, true);
2010 } else if (typeof module === 'object' && module.exports) {
2011 module.exports = Chart;
2016 Chart.noConflict = function(){
2017 root.Chart = previous;
2028 helpers = Chart.helpers;
2031 var defaultConfig = {
2032 //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
2033 scaleBeginAtZero : true,
2035 //Boolean - Whether grid lines are shown across the chart
2036 scaleShowGridLines : true,
2038 //String - Colour of the grid lines
2039 scaleGridLineColor : "rgba(0,0,0,.05)",
2041 //Number - Width of the grid lines
2042 scaleGridLineWidth : 1,
2044 //Boolean - Whether to show horizontal lines (except X axis)
2045 scaleShowHorizontalLines: true,
2047 //Boolean - Whether to show vertical lines (except Y axis)
2048 scaleShowVerticalLines: true,
2050 //Boolean - If there is a stroke on each bar
2051 barShowStroke : true,
2053 //Number - Pixel width of the bar stroke
2056 //Number - Spacing between each of the X value sets
2057 barValueSpacing : 5,
2059 //Number - Spacing between data sets within X values
2060 barDatasetSpacing : 1,
2062 //String - A legend template
2063 legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
2070 defaults : defaultConfig,
2071 initialize: function(data){
2073 //Expose options as a scope variable here so we can access it in the ScaleClass
2074 var options = this.options;
2076 this.ScaleClass = Chart.Scale.extend({
2077 offsetGridLines : true,
2078 calculateBarX : function(datasetCount, datasetIndex, barIndex){
2079 //Reusable method for calculating the xPosition of a given bar based on datasetIndex & width of the bar
2080 var xWidth = this.calculateBaseWidth(),
2081 xAbsolute = this.calculateX(barIndex) - (xWidth/2),
2082 barWidth = this.calculateBarWidth(datasetCount);
2084 return xAbsolute + (barWidth * datasetIndex) + (datasetIndex * options.barDatasetSpacing) + barWidth/2;
2086 calculateBaseWidth : function(){
2087 return (this.calculateX(1) - this.calculateX(0)) - (2*options.barValueSpacing);
2089 calculateBarWidth : function(datasetCount){
2090 //The padding between datasets is to the right of each bar, providing that there are more than 1 dataset
2091 var baseWidth = this.calculateBaseWidth() - ((datasetCount - 1) * options.barDatasetSpacing);
2093 return (baseWidth / datasetCount);
2099 //Set up tooltip events on the chart
2100 if (this.options.showTooltips){
2101 helpers.bindEvents(this, this.options.tooltipEvents, function(evt){
2102 var activeBars = (evt.type !== 'mouseout') ? this.getBarsAtEvent(evt) : [];
2104 this.eachBars(function(bar){
2105 bar.restore(['fillColor', 'strokeColor']);
2107 helpers.each(activeBars, function(activeBar){
2108 activeBar.fillColor = activeBar.highlightFill;
2109 activeBar.strokeColor = activeBar.highlightStroke;
2111 this.showTooltip(activeBars);
2115 //Declare the extension of the default point, to cater for the options passed in to the constructor
2116 this.BarClass = Chart.Rectangle.extend({
2117 strokeWidth : this.options.barStrokeWidth,
2118 showStroke : this.options.barShowStroke,
2119 ctx : this.chart.ctx
2122 //Iterate through each of the datasets, and build this into a property of the chart
2123 helpers.each(data.datasets,function(dataset,datasetIndex){
2125 var datasetObject = {
2126 label : dataset.label || null,
2127 fillColor : dataset.fillColor,
2128 strokeColor : dataset.strokeColor,
2132 this.datasets.push(datasetObject);
2134 helpers.each(dataset.data,function(dataPoint,index){
2135 //Add a new point for each piece of data, passing any required data to draw.
2136 datasetObject.bars.push(new this.BarClass({
2138 label : data.labels[index],
2139 datasetLabel: dataset.label,
2140 strokeColor : dataset.strokeColor,
2141 fillColor : dataset.fillColor,
2142 highlightFill : dataset.highlightFill || dataset.fillColor,
2143 highlightStroke : dataset.highlightStroke || dataset.strokeColor
2149 this.buildScale(data.labels);
2151 this.BarClass.prototype.base = this.scale.endPoint;
2153 this.eachBars(function(bar, index, datasetIndex){
2154 helpers.extend(bar, {
2155 width : this.scale.calculateBarWidth(this.datasets.length),
2156 x: this.scale.calculateBarX(this.datasets.length, datasetIndex, index),
2157 y: this.scale.endPoint
2164 update : function(){
2165 this.scale.update();
2166 // Reset any highlight colours before updating.
2167 helpers.each(this.activeElements, function(activeElement){
2168 activeElement.restore(['fillColor', 'strokeColor']);
2171 this.eachBars(function(bar){
2176 eachBars : function(callback){
2177 helpers.each(this.datasets,function(dataset, datasetIndex){
2178 helpers.each(dataset.bars, callback, this, datasetIndex);
2181 getBarsAtEvent : function(e){
2183 eventPosition = helpers.getRelativePosition(e),
2184 datasetIterator = function(dataset){
2185 barsArray.push(dataset.bars[barIndex]);
2189 for (var datasetIndex = 0; datasetIndex < this.datasets.length; datasetIndex++) {
2190 for (barIndex = 0; barIndex < this.datasets[datasetIndex].bars.length; barIndex++) {
2191 if (this.datasets[datasetIndex].bars[barIndex].inRange(eventPosition.x,eventPosition.y)){
2192 helpers.each(this.datasets, datasetIterator);
2200 buildScale : function(labels){
2203 var dataTotal = function(){
2205 self.eachBars(function(bar){
2206 values.push(bar.value);
2211 var scaleOptions = {
2212 templateString : this.options.scaleLabel,
2213 height : this.chart.height,
2214 width : this.chart.width,
2215 ctx : this.chart.ctx,
2216 textColor : this.options.scaleFontColor,
2217 fontSize : this.options.scaleFontSize,
2218 fontStyle : this.options.scaleFontStyle,
2219 fontFamily : this.options.scaleFontFamily,
2220 valuesCount : labels.length,
2221 beginAtZero : this.options.scaleBeginAtZero,
2222 integersOnly : this.options.scaleIntegersOnly,
2223 calculateYRange: function(currentHeight){
2224 var updatedRanges = helpers.calculateScaleRange(
2231 helpers.extend(this, updatedRanges);
2234 font : helpers.fontString(this.options.scaleFontSize, this.options.scaleFontStyle, this.options.scaleFontFamily),
2235 lineWidth : this.options.scaleLineWidth,
2236 lineColor : this.options.scaleLineColor,
2237 showHorizontalLines : this.options.scaleShowHorizontalLines,
2238 showVerticalLines : this.options.scaleShowVerticalLines,
2239 gridLineWidth : (this.options.scaleShowGridLines) ? this.options.scaleGridLineWidth : 0,
2240 gridLineColor : (this.options.scaleShowGridLines) ? this.options.scaleGridLineColor : "rgba(0,0,0,0)",
2241 padding : (this.options.showScale) ? 0 : (this.options.barShowStroke) ? this.options.barStrokeWidth : 0,
2242 showLabels : this.options.scaleShowLabels,
2243 display : this.options.showScale
2246 if (this.options.scaleOverride){
2247 helpers.extend(scaleOptions, {
2248 calculateYRange: helpers.noop,
2249 steps: this.options.scaleSteps,
2250 stepValue: this.options.scaleStepWidth,
2251 min: this.options.scaleStartValue,
2252 max: this.options.scaleStartValue + (this.options.scaleSteps * this.options.scaleStepWidth)
2256 this.scale = new this.ScaleClass(scaleOptions);
2258 addData : function(valuesArray,label){
2259 //Map the values array for each of the datasets
2260 helpers.each(valuesArray,function(value,datasetIndex){
2261 //Add a new point for each piece of data, passing any required data to draw.
2262 this.datasets[datasetIndex].bars.push(new this.BarClass({
2265 x: this.scale.calculateBarX(this.datasets.length, datasetIndex, this.scale.valuesCount+1),
2266 y: this.scale.endPoint,
2267 width : this.scale.calculateBarWidth(this.datasets.length),
2268 base : this.scale.endPoint,
2269 strokeColor : this.datasets[datasetIndex].strokeColor,
2270 fillColor : this.datasets[datasetIndex].fillColor
2274 this.scale.addXLabel(label);
2275 //Then re-render the chart.
2278 removeData : function(){
2279 this.scale.removeXLabel();
2280 //Then re-render the chart.
2281 helpers.each(this.datasets,function(dataset){
2282 dataset.bars.shift();
2286 reflow : function(){
2287 helpers.extend(this.BarClass.prototype,{
2288 y: this.scale.endPoint,
2289 base : this.scale.endPoint
2291 var newScaleProps = helpers.extend({
2292 height : this.chart.height,
2293 width : this.chart.width
2295 this.scale.update(newScaleProps);
2297 draw : function(ease){
2298 var easingDecimal = ease || 1;
2301 var ctx = this.chart.ctx;
2303 this.scale.draw(easingDecimal);
2305 //Draw all the bars for each dataset
2306 helpers.each(this.datasets,function(dataset,datasetIndex){
2307 helpers.each(dataset.bars,function(bar,index){
2308 if (bar.hasValue()){
2309 bar.base = this.scale.endPoint;
2310 //Transition then draw
2312 x : this.scale.calculateBarX(this.datasets.length, datasetIndex, index),
2313 y : this.scale.calculateY(bar.value),
2314 width : this.scale.calculateBarWidth(this.datasets.length)
2315 }, easingDecimal).draw();
2331 //Cache a local reference to Chart.helpers
2332 helpers = Chart.helpers;
2334 var defaultConfig = {
2335 //Boolean - Whether we should show a stroke on each segment
2336 segmentShowStroke : true,
2338 //String - The colour of each segment stroke
2339 segmentStrokeColor : "#fff",
2341 //Number - The width of each segment stroke
2342 segmentStrokeWidth : 2,
2344 //The percentage of the chart that we cut out of the middle.
2345 percentageInnerCutout : 50,
2347 //Number - Amount of animation steps
2348 animationSteps : 100,
2350 //String - Animation easing effect
2351 animationEasing : "easeOutBounce",
2353 //Boolean - Whether we animate the rotation of the Doughnut
2354 animateRotate : true,
2356 //Boolean - Whether we animate scaling the Doughnut from the centre
2357 animateScale : false,
2359 //String - A legend template
2360 legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
2366 //Passing in a name registers this chart in the Chart namespace
2368 //Providing a defaults will also register the deafults in the chart namespace
2369 defaults : defaultConfig,
2370 //Initialize is fired when the chart is initialized - Data is passed in as a parameter
2371 //Config is automatically merged by the core of Chart.js, and is available at this.options
2372 initialize: function(data){
2374 //Declare segments as a static property to prevent inheriting across the Chart type prototype
2376 this.outerRadius = (helpers.min([this.chart.width,this.chart.height]) - this.options.segmentStrokeWidth/2)/2;
2378 this.SegmentArc = Chart.Arc.extend({
2379 ctx : this.chart.ctx,
2380 x : this.chart.width/2,
2381 y : this.chart.height/2
2384 //Set up tooltip events on the chart
2385 if (this.options.showTooltips){
2386 helpers.bindEvents(this, this.options.tooltipEvents, function(evt){
2387 var activeSegments = (evt.type !== 'mouseout') ? this.getSegmentsAtEvent(evt) : [];
2389 helpers.each(this.segments,function(segment){
2390 segment.restore(["fillColor"]);
2392 helpers.each(activeSegments,function(activeSegment){
2393 activeSegment.fillColor = activeSegment.highlightColor;
2395 this.showTooltip(activeSegments);
2398 this.calculateTotal(data);
2400 helpers.each(data,function(datapoint, index){
2401 this.addData(datapoint, index, true);
2406 getSegmentsAtEvent : function(e){
2407 var segmentsArray = [];
2409 var location = helpers.getRelativePosition(e);
2411 helpers.each(this.segments,function(segment){
2412 if (segment.inRange(location.x,location.y)) segmentsArray.push(segment);
2414 return segmentsArray;
2416 addData : function(segment, atIndex, silent){
2417 var index = atIndex || this.segments.length;
2418 this.segments.splice(index, 0, new this.SegmentArc({
2419 value : segment.value,
2420 outerRadius : (this.options.animateScale) ? 0 : this.outerRadius,
2421 innerRadius : (this.options.animateScale) ? 0 : (this.outerRadius/100) * this.options.percentageInnerCutout,
2422 fillColor : segment.color,
2423 highlightColor : segment.highlight || segment.color,
2424 showStroke : this.options.segmentShowStroke,
2425 strokeWidth : this.options.segmentStrokeWidth,
2426 strokeColor : this.options.segmentStrokeColor,
2427 startAngle : Math.PI * 1.5,
2428 circumference : (this.options.animateRotate) ? 0 : this.calculateCircumference(segment.value),
2429 label : segment.label
2436 calculateCircumference : function(value){
2437 return (Math.PI*2)*(Math.abs(value) / this.total);
2439 calculateTotal : function(data){
2441 helpers.each(data,function(segment){
2442 this.total += Math.abs(segment.value);
2445 update : function(){
2446 this.calculateTotal(this.segments);
2448 // Reset any highlight colours before updating.
2449 helpers.each(this.activeElements, function(activeElement){
2450 activeElement.restore(['fillColor']);
2453 helpers.each(this.segments,function(segment){
2459 removeData: function(atIndex){
2460 var indexToDelete = (helpers.isNumber(atIndex)) ? atIndex : this.segments.length-1;
2461 this.segments.splice(indexToDelete, 1);
2466 reflow : function(){
2467 helpers.extend(this.SegmentArc.prototype,{
2468 x : this.chart.width/2,
2469 y : this.chart.height/2
2471 this.outerRadius = (helpers.min([this.chart.width,this.chart.height]) - this.options.segmentStrokeWidth/2)/2;
2472 helpers.each(this.segments, function(segment){
2474 outerRadius : this.outerRadius,
2475 innerRadius : (this.outerRadius/100) * this.options.percentageInnerCutout
2479 draw : function(easeDecimal){
2480 var animDecimal = (easeDecimal) ? easeDecimal : 1;
2482 helpers.each(this.segments,function(segment,index){
2483 segment.transition({
2484 circumference : this.calculateCircumference(segment.value),
2485 outerRadius : this.outerRadius,
2486 innerRadius : (this.outerRadius/100) * this.options.percentageInnerCutout
2489 segment.endAngle = segment.startAngle + segment.circumference;
2493 segment.startAngle = Math.PI * 1.5;
2495 //Check to see if it's the last segment, if not get the next and update the start angle
2496 if (index < this.segments.length-1){
2497 this.segments[index+1].startAngle = segment.endAngle;
2504 Chart.types.Doughnut.extend({
2506 defaults : helpers.merge(defaultConfig,{percentageInnerCutout : 0})
2515 helpers = Chart.helpers;
2517 var defaultConfig = {
2519 ///Boolean - Whether grid lines are shown across the chart
2520 scaleShowGridLines : true,
2522 //String - Colour of the grid lines
2523 scaleGridLineColor : "rgba(0,0,0,.05)",
2525 //Number - Width of the grid lines
2526 scaleGridLineWidth : 1,
2528 //Boolean - Whether to show horizontal lines (except X axis)
2529 scaleShowHorizontalLines: true,
2531 //Boolean - Whether to show vertical lines (except Y axis)
2532 scaleShowVerticalLines: true,
2534 //Boolean - Whether the line is curved between points
2537 //Number - Tension of the bezier curve between points
2538 bezierCurveTension : 0.4,
2540 //Boolean - Whether to show a dot for each point
2543 //Number - Radius of each point dot in pixels
2546 //Number - Pixel width of point dot stroke
2547 pointDotStrokeWidth : 1,
2549 //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
2550 pointHitDetectionRadius : 20,
2552 //Boolean - Whether to show a stroke for datasets
2553 datasetStroke : true,
2555 //Number - Pixel width of dataset stroke
2556 datasetStrokeWidth : 2,
2558 //Boolean - Whether to fill the dataset with a colour
2561 //String - A legend template
2562 legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
2569 defaults : defaultConfig,
2570 initialize: function(data){
2571 //Declare the extension of the default point, to cater for the options passed in to the constructor
2572 this.PointClass = Chart.Point.extend({
2573 strokeWidth : this.options.pointDotStrokeWidth,
2574 radius : this.options.pointDotRadius,
2575 display: this.options.pointDot,
2576 hitDetectionRadius : this.options.pointHitDetectionRadius,
2577 ctx : this.chart.ctx,
2578 inRange : function(mouseX){
2579 return (Math.pow(mouseX-this.x, 2) < Math.pow(this.radius + this.hitDetectionRadius,2));
2585 //Set up tooltip events on the chart
2586 if (this.options.showTooltips){
2587 helpers.bindEvents(this, this.options.tooltipEvents, function(evt){
2588 var activePoints = (evt.type !== 'mouseout') ? this.getPointsAtEvent(evt) : [];
2589 this.eachPoints(function(point){
2590 point.restore(['fillColor', 'strokeColor']);
2592 helpers.each(activePoints, function(activePoint){
2593 activePoint.fillColor = activePoint.highlightFill;
2594 activePoint.strokeColor = activePoint.highlightStroke;
2596 this.showTooltip(activePoints);
2600 //Iterate through each of the datasets, and build this into a property of the chart
2601 helpers.each(data.datasets,function(dataset){
2603 var datasetObject = {
2604 label : dataset.label || null,
2605 fillColor : dataset.fillColor,
2606 strokeColor : dataset.strokeColor,
2607 pointColor : dataset.pointColor,
2608 pointStrokeColor : dataset.pointStrokeColor,
2612 this.datasets.push(datasetObject);
2615 helpers.each(dataset.data,function(dataPoint,index){
2616 //Add a new point for each piece of data, passing any required data to draw.
2617 datasetObject.points.push(new this.PointClass({
2619 label : data.labels[index],
2620 datasetLabel: dataset.label,
2621 strokeColor : dataset.pointStrokeColor,
2622 fillColor : dataset.pointColor,
2623 highlightFill : dataset.pointHighlightFill || dataset.pointColor,
2624 highlightStroke : dataset.pointHighlightStroke || dataset.pointStrokeColor
2628 this.buildScale(data.labels);
2631 this.eachPoints(function(point, index){
2632 helpers.extend(point, {
2633 x: this.scale.calculateX(index),
2634 y: this.scale.endPoint
2644 update : function(){
2645 this.scale.update();
2646 // Reset any highlight colours before updating.
2647 helpers.each(this.activeElements, function(activeElement){
2648 activeElement.restore(['fillColor', 'strokeColor']);
2650 this.eachPoints(function(point){
2655 eachPoints : function(callback){
2656 helpers.each(this.datasets,function(dataset){
2657 helpers.each(dataset.points,callback,this);
2660 getPointsAtEvent : function(e){
2661 var pointsArray = [],
2662 eventPosition = helpers.getRelativePosition(e);
2663 helpers.each(this.datasets,function(dataset){
2664 helpers.each(dataset.points,function(point){
2665 if (point.inRange(eventPosition.x,eventPosition.y)) pointsArray.push(point);
2670 buildScale : function(labels){
2673 var dataTotal = function(){
2675 self.eachPoints(function(point){
2676 values.push(point.value);
2682 var scaleOptions = {
2683 templateString : this.options.scaleLabel,
2684 height : this.chart.height,
2685 width : this.chart.width,
2686 ctx : this.chart.ctx,
2687 textColor : this.options.scaleFontColor,
2688 fontSize : this.options.scaleFontSize,
2689 fontStyle : this.options.scaleFontStyle,
2690 fontFamily : this.options.scaleFontFamily,
2691 valuesCount : labels.length,
2692 beginAtZero : this.options.scaleBeginAtZero,
2693 integersOnly : this.options.scaleIntegersOnly,
2694 calculateYRange : function(currentHeight){
2695 var updatedRanges = helpers.calculateScaleRange(
2702 helpers.extend(this, updatedRanges);
2705 font : helpers.fontString(this.options.scaleFontSize, this.options.scaleFontStyle, this.options.scaleFontFamily),
2706 lineWidth : this.options.scaleLineWidth,
2707 lineColor : this.options.scaleLineColor,
2708 showHorizontalLines : this.options.scaleShowHorizontalLines,
2709 showVerticalLines : this.options.scaleShowVerticalLines,
2710 gridLineWidth : (this.options.scaleShowGridLines) ? this.options.scaleGridLineWidth : 0,
2711 gridLineColor : (this.options.scaleShowGridLines) ? this.options.scaleGridLineColor : "rgba(0,0,0,0)",
2712 padding: (this.options.showScale) ? 0 : this.options.pointDotRadius + this.options.pointDotStrokeWidth,
2713 showLabels : this.options.scaleShowLabels,
2714 display : this.options.showScale
2717 if (this.options.scaleOverride){
2718 helpers.extend(scaleOptions, {
2719 calculateYRange: helpers.noop,
2720 steps: this.options.scaleSteps,
2721 stepValue: this.options.scaleStepWidth,
2722 min: this.options.scaleStartValue,
2723 max: this.options.scaleStartValue + (this.options.scaleSteps * this.options.scaleStepWidth)
2728 this.scale = new Chart.Scale(scaleOptions);
2730 addData : function(valuesArray,label){
2731 //Map the values array for each of the datasets
2733 helpers.each(valuesArray,function(value,datasetIndex){
2734 //Add a new point for each piece of data, passing any required data to draw.
2735 this.datasets[datasetIndex].points.push(new this.PointClass({
2738 x: this.scale.calculateX(this.scale.valuesCount+1),
2739 y: this.scale.endPoint,
2740 strokeColor : this.datasets[datasetIndex].pointStrokeColor,
2741 fillColor : this.datasets[datasetIndex].pointColor
2745 this.scale.addXLabel(label);
2746 //Then re-render the chart.
2749 removeData : function(){
2750 this.scale.removeXLabel();
2751 //Then re-render the chart.
2752 helpers.each(this.datasets,function(dataset){
2753 dataset.points.shift();
2757 reflow : function(){
2758 var newScaleProps = helpers.extend({
2759 height : this.chart.height,
2760 width : this.chart.width
2762 this.scale.update(newScaleProps);
2764 draw : function(ease){
2765 var easingDecimal = ease || 1;
2768 var ctx = this.chart.ctx;
2770 // Some helper methods for getting the next/prev points
2771 var hasValue = function(item){
2772 return item.value !== null;
2774 nextPoint = function(point, collection, index){
2775 return helpers.findNextWhere(collection, hasValue, index) || point;
2777 previousPoint = function(point, collection, index){
2778 return helpers.findPreviousWhere(collection, hasValue, index) || point;
2781 this.scale.draw(easingDecimal);
2784 helpers.each(this.datasets,function(dataset){
2785 var pointsWithValues = helpers.where(dataset.points, hasValue);
2787 //Transition each point first so that the line and point drawing isn't out of sync
2788 //We can use this extra loop to calculate the control points of this dataset also in this loop
2790 helpers.each(dataset.points, function(point, index){
2791 if (point.hasValue()){
2793 y : this.scale.calculateY(point.value),
2794 x : this.scale.calculateX(index)
2800 // Control points need to be calculated in a seperate loop, because we need to know the current x/y of the point
2801 // This would cause issues when there is no animation, because the y of the next point would be 0, so beziers would be skewed
2802 if (this.options.bezierCurve){
2803 helpers.each(pointsWithValues, function(point, index){
2804 var tension = (index > 0 && index < pointsWithValues.length - 1) ? this.options.bezierCurveTension : 0;
2805 point.controlPoints = helpers.splineCurve(
2806 previousPoint(point, pointsWithValues, index),
2808 nextPoint(point, pointsWithValues, index),
2812 // Prevent the bezier going outside of the bounds of the graph
2814 // Cap puter bezier handles to the upper/lower scale bounds
2815 if (point.controlPoints.outer.y > this.scale.endPoint){
2816 point.controlPoints.outer.y = this.scale.endPoint;
2818 else if (point.controlPoints.outer.y < this.scale.startPoint){
2819 point.controlPoints.outer.y = this.scale.startPoint;
2822 // Cap inner bezier handles to the upper/lower scale bounds
2823 if (point.controlPoints.inner.y > this.scale.endPoint){
2824 point.controlPoints.inner.y = this.scale.endPoint;
2826 else if (point.controlPoints.inner.y < this.scale.startPoint){
2827 point.controlPoints.inner.y = this.scale.startPoint;
2833 //Draw the line between all the points
2834 ctx.lineWidth = this.options.datasetStrokeWidth;
2835 ctx.strokeStyle = dataset.strokeColor;
2838 helpers.each(pointsWithValues, function(point, index){
2840 ctx.moveTo(point.x, point.y);
2843 if(this.options.bezierCurve){
2844 var previous = previousPoint(point, pointsWithValues, index);
2847 previous.controlPoints.outer.x,
2848 previous.controlPoints.outer.y,
2849 point.controlPoints.inner.x,
2850 point.controlPoints.inner.y,
2856 ctx.lineTo(point.x,point.y);
2863 if (this.options.datasetFill && pointsWithValues.length > 0){
2864 //Round off the line by going to the base of the chart, back to the start, then fill.
2865 ctx.lineTo(pointsWithValues[pointsWithValues.length - 1].x, this.scale.endPoint);
2866 ctx.lineTo(pointsWithValues[0].x, this.scale.endPoint);
2867 ctx.fillStyle = dataset.fillColor;
2872 //Now draw the points over the line
2873 //A little inefficient double looping, but better than the line
2874 //lagging behind the point positions
2875 helpers.each(pointsWithValues,function(point){
2890 //Cache a local reference to Chart.helpers
2891 helpers = Chart.helpers;
2893 var defaultConfig = {
2894 //Boolean - Show a backdrop to the scale label
2895 scaleShowLabelBackdrop : true,
2897 //String - The colour of the label backdrop
2898 scaleBackdropColor : "rgba(255,255,255,0.75)",
2900 // Boolean - Whether the scale should begin at zero
2901 scaleBeginAtZero : true,
2903 //Number - The backdrop padding above & below the label in pixels
2904 scaleBackdropPaddingY : 2,
2906 //Number - The backdrop padding to the side of the label in pixels
2907 scaleBackdropPaddingX : 2,
2909 //Boolean - Show line for each value in the scale
2910 scaleShowLine : true,
2912 //Boolean - Stroke a line around each segment in the chart
2913 segmentShowStroke : true,
2915 //String - The colour of the stroke on each segement.
2916 segmentStrokeColor : "#fff",
2918 //Number - The width of the stroke value in pixels
2919 segmentStrokeWidth : 2,
2921 //Number - Amount of animation steps
2922 animationSteps : 100,
2924 //String - Animation easing effect.
2925 animationEasing : "easeOutBounce",
2927 //Boolean - Whether to animate the rotation of the chart
2928 animateRotate : true,
2930 //Boolean - Whether to animate scaling the chart from the centre
2931 animateScale : false,
2933 //String - A legend template
2934 legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
2939 //Passing in a name registers this chart in the Chart namespace
2941 //Providing a defaults will also register the deafults in the chart namespace
2942 defaults : defaultConfig,
2943 //Initialize is fired when the chart is initialized - Data is passed in as a parameter
2944 //Config is automatically merged by the core of Chart.js, and is available at this.options
2945 initialize: function(data){
2947 //Declare segment class as a chart instance specific class, so it can share props for this instance
2948 this.SegmentArc = Chart.Arc.extend({
2949 showStroke : this.options.segmentShowStroke,
2950 strokeWidth : this.options.segmentStrokeWidth,
2951 strokeColor : this.options.segmentStrokeColor,
2952 ctx : this.chart.ctx,
2954 x : this.chart.width/2,
2955 y : this.chart.height/2
2957 this.scale = new Chart.RadialScale({
2958 display: this.options.showScale,
2959 fontStyle: this.options.scaleFontStyle,
2960 fontSize: this.options.scaleFontSize,
2961 fontFamily: this.options.scaleFontFamily,
2962 fontColor: this.options.scaleFontColor,
2963 showLabels: this.options.scaleShowLabels,
2964 showLabelBackdrop: this.options.scaleShowLabelBackdrop,
2965 backdropColor: this.options.scaleBackdropColor,
2966 backdropPaddingY : this.options.scaleBackdropPaddingY,
2967 backdropPaddingX: this.options.scaleBackdropPaddingX,
2968 lineWidth: (this.options.scaleShowLine) ? this.options.scaleLineWidth : 0,
2969 lineColor: this.options.scaleLineColor,
2971 width: this.chart.width,
2972 height: this.chart.height,
2973 xCenter: this.chart.width/2,
2974 yCenter: this.chart.height/2,
2975 ctx : this.chart.ctx,
2976 templateString: this.options.scaleLabel,
2977 valuesCount: data.length
2980 this.updateScaleRange(data);
2982 this.scale.update();
2984 helpers.each(data,function(segment,index){
2985 this.addData(segment,index,true);
2988 //Set up tooltip events on the chart
2989 if (this.options.showTooltips){
2990 helpers.bindEvents(this, this.options.tooltipEvents, function(evt){
2991 var activeSegments = (evt.type !== 'mouseout') ? this.getSegmentsAtEvent(evt) : [];
2992 helpers.each(this.segments,function(segment){
2993 segment.restore(["fillColor"]);
2995 helpers.each(activeSegments,function(activeSegment){
2996 activeSegment.fillColor = activeSegment.highlightColor;
2998 this.showTooltip(activeSegments);
3004 getSegmentsAtEvent : function(e){
3005 var segmentsArray = [];
3007 var location = helpers.getRelativePosition(e);
3009 helpers.each(this.segments,function(segment){
3010 if (segment.inRange(location.x,location.y)) segmentsArray.push(segment);
3012 return segmentsArray;
3014 addData : function(segment, atIndex, silent){
3015 var index = atIndex || this.segments.length;
3017 this.segments.splice(index, 0, new this.SegmentArc({
3018 fillColor: segment.color,
3019 highlightColor: segment.highlight || segment.color,
3020 label: segment.label,
3021 value: segment.value,
3022 outerRadius: (this.options.animateScale) ? 0 : this.scale.calculateCenterOffset(segment.value),
3023 circumference: (this.options.animateRotate) ? 0 : this.scale.getCircumference(),
3024 startAngle: Math.PI * 1.5
3031 removeData: function(atIndex){
3032 var indexToDelete = (helpers.isNumber(atIndex)) ? atIndex : this.segments.length-1;
3033 this.segments.splice(indexToDelete, 1);
3037 calculateTotal: function(data){
3039 helpers.each(data,function(segment){
3040 this.total += segment.value;
3042 this.scale.valuesCount = this.segments.length;
3044 updateScaleRange: function(datapoints){
3045 var valuesArray = [];
3046 helpers.each(datapoints,function(segment){
3047 valuesArray.push(segment.value);
3050 var scaleSizes = (this.options.scaleOverride) ?
3052 steps: this.options.scaleSteps,
3053 stepValue: this.options.scaleStepWidth,
3054 min: this.options.scaleStartValue,
3055 max: this.options.scaleStartValue + (this.options.scaleSteps * this.options.scaleStepWidth)
3057 helpers.calculateScaleRange(
3059 helpers.min([this.chart.width, this.chart.height])/2,
3060 this.options.scaleFontSize,
3061 this.options.scaleBeginAtZero,
3062 this.options.scaleIntegersOnly
3069 size: helpers.min([this.chart.width, this.chart.height]),
3070 xCenter: this.chart.width/2,
3071 yCenter: this.chart.height/2
3076 update : function(){
3077 this.calculateTotal(this.segments);
3079 helpers.each(this.segments,function(segment){
3086 reflow : function(){
3087 helpers.extend(this.SegmentArc.prototype,{
3088 x : this.chart.width/2,
3089 y : this.chart.height/2
3091 this.updateScaleRange(this.segments);
3092 this.scale.update();
3094 helpers.extend(this.scale,{
3095 xCenter: this.chart.width/2,
3096 yCenter: this.chart.height/2
3099 helpers.each(this.segments, function(segment){
3101 outerRadius : this.scale.calculateCenterOffset(segment.value)
3106 draw : function(ease){
3107 var easingDecimal = ease || 1;
3108 //Clear & draw the canvas
3110 helpers.each(this.segments,function(segment, index){
3111 segment.transition({
3112 circumference : this.scale.getCircumference(),
3113 outerRadius : this.scale.calculateCenterOffset(segment.value)
3116 segment.endAngle = segment.startAngle + segment.circumference;
3118 // If we've removed the first segment we need to set the first one to
3119 // start at the top.
3121 segment.startAngle = Math.PI * 1.5;
3124 //Check to see if it's the last segment, if not get the next and update the start angle
3125 if (index < this.segments.length - 1){
3126 this.segments[index+1].startAngle = segment.endAngle;
3140 helpers = Chart.helpers;
3147 //Boolean - Whether to show lines for each scale point
3148 scaleShowLine : true,
3150 //Boolean - Whether we show the angle lines out of the radar
3151 angleShowLineOut : true,
3153 //Boolean - Whether to show labels on the scale
3154 scaleShowLabels : false,
3156 // Boolean - Whether the scale should begin at zero
3157 scaleBeginAtZero : true,
3159 //String - Colour of the angle line
3160 angleLineColor : "rgba(0,0,0,.1)",
3162 //Number - Pixel width of the angle line
3165 //String - Point label font declaration
3166 pointLabelFontFamily : "'Arial'",
3168 //String - Point label font weight
3169 pointLabelFontStyle : "normal",
3171 //Number - Point label font size in pixels
3172 pointLabelFontSize : 10,
3174 //String - Point label font colour
3175 pointLabelFontColor : "#666",
3177 //Boolean - Whether to show a dot for each point
3180 //Number - Radius of each point dot in pixels
3183 //Number - Pixel width of point dot stroke
3184 pointDotStrokeWidth : 1,
3186 //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
3187 pointHitDetectionRadius : 20,
3189 //Boolean - Whether to show a stroke for datasets
3190 datasetStroke : true,
3192 //Number - Pixel width of dataset stroke
3193 datasetStrokeWidth : 2,
3195 //Boolean - Whether to fill the dataset with a colour
3198 //String - A legend template
3199 legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
3203 initialize: function(data){
3204 this.PointClass = Chart.Point.extend({
3205 strokeWidth : this.options.pointDotStrokeWidth,
3206 radius : this.options.pointDotRadius,
3207 display: this.options.pointDot,
3208 hitDetectionRadius : this.options.pointHitDetectionRadius,
3209 ctx : this.chart.ctx
3214 this.buildScale(data);
3216 //Set up tooltip events on the chart
3217 if (this.options.showTooltips){
3218 helpers.bindEvents(this, this.options.tooltipEvents, function(evt){
3219 var activePointsCollection = (evt.type !== 'mouseout') ? this.getPointsAtEvent(evt) : [];
3221 this.eachPoints(function(point){
3222 point.restore(['fillColor', 'strokeColor']);
3224 helpers.each(activePointsCollection, function(activePoint){
3225 activePoint.fillColor = activePoint.highlightFill;
3226 activePoint.strokeColor = activePoint.highlightStroke;
3229 this.showTooltip(activePointsCollection);
3233 //Iterate through each of the datasets, and build this into a property of the chart
3234 helpers.each(data.datasets,function(dataset){
3236 var datasetObject = {
3237 label: dataset.label || null,
3238 fillColor : dataset.fillColor,
3239 strokeColor : dataset.strokeColor,
3240 pointColor : dataset.pointColor,
3241 pointStrokeColor : dataset.pointStrokeColor,
3245 this.datasets.push(datasetObject);
3247 helpers.each(dataset.data,function(dataPoint,index){
3248 //Add a new point for each piece of data, passing any required data to draw.
3250 if (!this.scale.animation){
3251 pointPosition = this.scale.getPointPosition(index, this.scale.calculateCenterOffset(dataPoint));
3253 datasetObject.points.push(new this.PointClass({
3255 label : data.labels[index],
3256 datasetLabel: dataset.label,
3257 x: (this.options.animation) ? this.scale.xCenter : pointPosition.x,
3258 y: (this.options.animation) ? this.scale.yCenter : pointPosition.y,
3259 strokeColor : dataset.pointStrokeColor,
3260 fillColor : dataset.pointColor,
3261 highlightFill : dataset.pointHighlightFill || dataset.pointColor,
3262 highlightStroke : dataset.pointHighlightStroke || dataset.pointStrokeColor
3270 eachPoints : function(callback){
3271 helpers.each(this.datasets,function(dataset){
3272 helpers.each(dataset.points,callback,this);
3276 getPointsAtEvent : function(evt){
3277 var mousePosition = helpers.getRelativePosition(evt),
3278 fromCenter = helpers.getAngleFromPoint({
3279 x: this.scale.xCenter,
3280 y: this.scale.yCenter
3283 var anglePerIndex = (Math.PI * 2) /this.scale.valuesCount,
3284 pointIndex = Math.round((fromCenter.angle - Math.PI * 1.5) / anglePerIndex),
3285 activePointsCollection = [];
3287 // If we're at the top, make the pointIndex 0 to get the first of the array.
3288 if (pointIndex >= this.scale.valuesCount || pointIndex < 0){
3292 if (fromCenter.distance <= this.scale.drawingArea){
3293 helpers.each(this.datasets, function(dataset){
3294 activePointsCollection.push(dataset.points[pointIndex]);
3298 return activePointsCollection;
3301 buildScale : function(data){
3302 this.scale = new Chart.RadialScale({
3303 display: this.options.showScale,
3304 fontStyle: this.options.scaleFontStyle,
3305 fontSize: this.options.scaleFontSize,
3306 fontFamily: this.options.scaleFontFamily,
3307 fontColor: this.options.scaleFontColor,
3308 showLabels: this.options.scaleShowLabels,
3309 showLabelBackdrop: this.options.scaleShowLabelBackdrop,
3310 backdropColor: this.options.scaleBackdropColor,
3311 backdropPaddingY : this.options.scaleBackdropPaddingY,
3312 backdropPaddingX: this.options.scaleBackdropPaddingX,
3313 lineWidth: (this.options.scaleShowLine) ? this.options.scaleLineWidth : 0,
3314 lineColor: this.options.scaleLineColor,
3315 angleLineColor : this.options.angleLineColor,
3316 angleLineWidth : (this.options.angleShowLineOut) ? this.options.angleLineWidth : 0,
3317 // Point labels at the edge of each line
3318 pointLabelFontColor : this.options.pointLabelFontColor,
3319 pointLabelFontSize : this.options.pointLabelFontSize,
3320 pointLabelFontFamily : this.options.pointLabelFontFamily,
3321 pointLabelFontStyle : this.options.pointLabelFontStyle,
3322 height : this.chart.height,
3323 width: this.chart.width,
3324 xCenter: this.chart.width/2,
3325 yCenter: this.chart.height/2,
3326 ctx : this.chart.ctx,
3327 templateString: this.options.scaleLabel,
3328 labels: data.labels,
3329 valuesCount: data.datasets[0].data.length
3332 this.scale.setScaleSize();
3333 this.updateScaleRange(data.datasets);
3334 this.scale.buildYLabels();
3336 updateScaleRange: function(datasets){
3337 var valuesArray = (function(){
3338 var totalDataArray = [];
3339 helpers.each(datasets,function(dataset){
3341 totalDataArray = totalDataArray.concat(dataset.data);
3344 helpers.each(dataset.points, function(point){
3345 totalDataArray.push(point.value);
3349 return totalDataArray;
3353 var scaleSizes = (this.options.scaleOverride) ?
3355 steps: this.options.scaleSteps,
3356 stepValue: this.options.scaleStepWidth,
3357 min: this.options.scaleStartValue,
3358 max: this.options.scaleStartValue + (this.options.scaleSteps * this.options.scaleStepWidth)
3360 helpers.calculateScaleRange(
3362 helpers.min([this.chart.width, this.chart.height])/2,
3363 this.options.scaleFontSize,
3364 this.options.scaleBeginAtZero,
3365 this.options.scaleIntegersOnly
3374 addData : function(valuesArray,label){
3375 //Map the values array for each of the datasets
3376 this.scale.valuesCount++;
3377 helpers.each(valuesArray,function(value,datasetIndex){
3378 var pointPosition = this.scale.getPointPosition(this.scale.valuesCount, this.scale.calculateCenterOffset(value));
3379 this.datasets[datasetIndex].points.push(new this.PointClass({
3384 strokeColor : this.datasets[datasetIndex].pointStrokeColor,
3385 fillColor : this.datasets[datasetIndex].pointColor
3389 this.scale.labels.push(label);
3395 removeData : function(){
3396 this.scale.valuesCount--;
3397 this.scale.labels.shift();
3398 helpers.each(this.datasets,function(dataset){
3399 dataset.points.shift();
3404 update : function(){
3405 this.eachPoints(function(point){
3412 helpers.extend(this.scale, {
3413 width : this.chart.width,
3414 height: this.chart.height,
3415 size : helpers.min([this.chart.width, this.chart.height]),
3416 xCenter: this.chart.width/2,
3417 yCenter: this.chart.height/2
3419 this.updateScaleRange(this.datasets);
3420 this.scale.setScaleSize();
3421 this.scale.buildYLabels();
3423 draw : function(ease){
3424 var easeDecimal = ease || 1,
3425 ctx = this.chart.ctx;
3429 helpers.each(this.datasets,function(dataset){
3431 //Transition each point first so that the line and point drawing isn't out of sync
3432 helpers.each(dataset.points,function(point,index){
3433 if (point.hasValue()){
3434 point.transition(this.scale.getPointPosition(index, this.scale.calculateCenterOffset(point.value)), easeDecimal);
3440 //Draw the line between all the points
3441 ctx.lineWidth = this.options.datasetStrokeWidth;
3442 ctx.strokeStyle = dataset.strokeColor;
3444 helpers.each(dataset.points,function(point,index){
3446 ctx.moveTo(point.x,point.y);
3449 ctx.lineTo(point.x,point.y);
3455 ctx.fillStyle = dataset.fillColor;
3458 //Now draw the points over the line
3459 //A little inefficient double looping, but better than the line
3460 //lagging behind the point positions
3461 helpers.each(dataset.points,function(point){
3462 if (point.hasValue()){