Jian Li | d7a5a74 | 2016-02-12 13:51:18 -0800 | [diff] [blame] | 1 | /*! |
| 2 | * Chart.js |
| 3 | * http://chartjs.org/ |
| 4 | * Version: 1.0.2 |
| 5 | * |
| 6 | * Copyright 2015 Nick Downie |
| 7 | * Released under the MIT license |
| 8 | * https://github.com/nnnick/Chart.js/blob/master/LICENSE.md |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | (function(){ |
| 13 | |
| 14 | "use strict"; |
| 15 | |
| 16 | //Declare root variable - window in the browser, global on the server |
| 17 | var root = this, |
| 18 | previous = root.Chart; |
| 19 | |
| 20 | //Occupy the global variable of Chart, and create a simple base class |
| 21 | var Chart = function(context){ |
| 22 | var chart = this; |
| 23 | this.canvas = context.canvas; |
| 24 | |
| 25 | this.ctx = context; |
| 26 | |
| 27 | //Variables global to the chart |
| 28 | var computeDimension = function(element,dimension) |
| 29 | { |
| 30 | if (element['offset'+dimension]) |
| 31 | { |
| 32 | return element['offset'+dimension]; |
| 33 | } |
| 34 | else |
| 35 | { |
| 36 | return document.defaultView.getComputedStyle(element).getPropertyValue(dimension); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | var width = this.width = computeDimension(context.canvas,'Width') || context.canvas.width; |
| 41 | var height = this.height = computeDimension(context.canvas,'Height') || context.canvas.height; |
| 42 | |
| 43 | width = this.width = context.canvas.width; |
| 44 | height = this.height = context.canvas.height; |
| 45 | this.aspectRatio = this.width / this.height; |
| 46 | //High pixel density displays - multiply the size of the canvas height/width by the device pixel ratio, then scale. |
| 47 | helpers.retinaScale(this); |
| 48 | |
| 49 | return this; |
| 50 | }; |
| 51 | //Globally expose the defaults to allow for user updating/changing |
| 52 | Chart.defaults = { |
| 53 | global: { |
| 54 | // Boolean - Whether to animate the chart |
| 55 | animation: true, |
| 56 | |
| 57 | // Number - Number of animation steps |
| 58 | animationSteps: 60, |
| 59 | |
| 60 | // String - Animation easing effect |
| 61 | animationEasing: "easeOutQuart", |
| 62 | |
| 63 | // Boolean - If we should show the scale at all |
| 64 | showScale: true, |
| 65 | |
| 66 | // Boolean - If we want to override with a hard coded scale |
| 67 | scaleOverride: false, |
| 68 | |
| 69 | // ** Required if scaleOverride is true ** |
| 70 | // Number - The number of steps in a hard coded scale |
| 71 | scaleSteps: null, |
| 72 | // Number - The value jump in the hard coded scale |
| 73 | scaleStepWidth: null, |
| 74 | // Number - The scale starting value |
| 75 | scaleStartValue: null, |
| 76 | |
| 77 | // String - Colour of the scale line |
| 78 | scaleLineColor: "rgba(0,0,0,.1)", |
| 79 | |
| 80 | // Number - Pixel width of the scale line |
| 81 | scaleLineWidth: 1, |
| 82 | |
| 83 | // Boolean - Whether to show labels on the scale |
| 84 | scaleShowLabels: true, |
| 85 | |
| 86 | // Interpolated JS string - can access value |
| 87 | scaleLabel: "<%=value%>", |
| 88 | |
| 89 | // Boolean - Whether the scale should stick to integers, and not show any floats even if drawing space is there |
| 90 | scaleIntegersOnly: true, |
| 91 | |
| 92 | // Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value |
| 93 | scaleBeginAtZero: false, |
| 94 | |
| 95 | // String - Scale label font declaration for the scale label |
| 96 | scaleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", |
| 97 | |
| 98 | // Number - Scale label font size in pixels |
| 99 | scaleFontSize: 12, |
| 100 | |
| 101 | // String - Scale label font weight style |
| 102 | scaleFontStyle: "normal", |
| 103 | |
| 104 | // String - Scale label font colour |
| 105 | scaleFontColor: "#666", |
| 106 | |
| 107 | // Boolean - whether or not the chart should be responsive and resize when the browser does. |
| 108 | responsive: false, |
| 109 | |
| 110 | // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container |
| 111 | maintainAspectRatio: true, |
| 112 | |
| 113 | // Boolean - Determines whether to draw tooltips on the canvas or not - attaches events to touchmove & mousemove |
| 114 | showTooltips: true, |
| 115 | |
| 116 | // Boolean - Determines whether to draw built-in tooltip or call custom tooltip function |
| 117 | customTooltips: false, |
| 118 | |
| 119 | // Array - Array of string names to attach tooltip events |
| 120 | tooltipEvents: ["mousemove", "touchstart", "touchmove", "mouseout"], |
| 121 | |
| 122 | // String - Tooltip background colour |
| 123 | tooltipFillColor: "rgba(0,0,0,0.8)", |
| 124 | |
| 125 | // String - Tooltip label font declaration for the scale label |
| 126 | tooltipFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", |
| 127 | |
| 128 | // Number - Tooltip label font size in pixels |
| 129 | tooltipFontSize: 14, |
| 130 | |
| 131 | // String - Tooltip font weight style |
| 132 | tooltipFontStyle: "normal", |
| 133 | |
| 134 | // String - Tooltip label font colour |
| 135 | tooltipFontColor: "#fff", |
| 136 | |
| 137 | // String - Tooltip title font declaration for the scale label |
| 138 | tooltipTitleFontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", |
| 139 | |
| 140 | // Number - Tooltip title font size in pixels |
| 141 | tooltipTitleFontSize: 14, |
| 142 | |
| 143 | // String - Tooltip title font weight style |
| 144 | tooltipTitleFontStyle: "bold", |
| 145 | |
| 146 | // String - Tooltip title font colour |
| 147 | tooltipTitleFontColor: "#fff", |
| 148 | |
| 149 | // String - Tooltip title template |
| 150 | tooltipTitleTemplate: "<%= label%>", |
| 151 | |
| 152 | // Number - pixel width of padding around tooltip text |
| 153 | tooltipYPadding: 6, |
| 154 | |
| 155 | // Number - pixel width of padding around tooltip text |
| 156 | tooltipXPadding: 6, |
| 157 | |
| 158 | // Number - Size of the caret on the tooltip |
| 159 | tooltipCaretSize: 8, |
| 160 | |
| 161 | // Number - Pixel radius of the tooltip border |
| 162 | tooltipCornerRadius: 6, |
| 163 | |
| 164 | // Number - Pixel offset from point x to tooltip edge |
| 165 | tooltipXOffset: 10, |
| 166 | |
| 167 | // String - Template string for single tooltips |
| 168 | tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>", |
| 169 | |
| 170 | // String - Template string for single tooltips |
| 171 | multiTooltipTemplate: "<%= value %>", |
| 172 | |
| 173 | // String - Colour behind the legend colour block |
| 174 | multiTooltipKeyBackground: '#fff', |
| 175 | |
| 176 | // Array - A list of colors to use as the defaults |
| 177 | segmentColorDefault: ["#A6CEE3", "#1F78B4", "#B2DF8A", "#33A02C", "#FB9A99", "#E31A1C", "#FDBF6F", "#FF7F00", "#CAB2D6", "#6A3D9A", "#B4B482", "#B15928" ], |
| 178 | |
| 179 | // Array - A list of highlight colors to use as the defaults |
| 180 | segmentHighlightColorDefaults: [ "#CEF6FF", "#47A0DC", "#DAFFB2", "#5BC854", "#FFC2C1", "#FF4244", "#FFE797", "#FFA728", "#F2DAFE", "#9265C2", "#DCDCAA", "#D98150" ], |
| 181 | |
| 182 | // Function - Will fire on animation progression. |
| 183 | onAnimationProgress: function(){}, |
| 184 | |
| 185 | // Function - Will fire on animation completion. |
| 186 | onAnimationComplete: function(){} |
| 187 | |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | //Create a dictionary of chart types, to allow for extension of existing types |
| 192 | Chart.types = {}; |
| 193 | |
| 194 | //Global Chart helpers object for utility methods and classes |
| 195 | var helpers = Chart.helpers = {}; |
| 196 | |
| 197 | //-- Basic js utility methods |
| 198 | var each = helpers.each = function(loopable,callback,self){ |
| 199 | var additionalArgs = Array.prototype.slice.call(arguments, 3); |
| 200 | // Check to see if null or undefined firstly. |
| 201 | if (loopable){ |
| 202 | if (loopable.length === +loopable.length){ |
| 203 | var i; |
| 204 | for (i=0; i<loopable.length; i++){ |
| 205 | callback.apply(self,[loopable[i], i].concat(additionalArgs)); |
| 206 | } |
| 207 | } |
| 208 | else{ |
| 209 | for (var item in loopable){ |
| 210 | callback.apply(self,[loopable[item],item].concat(additionalArgs)); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | }, |
| 215 | clone = helpers.clone = function(obj){ |
| 216 | var objClone = {}; |
| 217 | each(obj,function(value,key){ |
| 218 | if (obj.hasOwnProperty(key)){ |
| 219 | objClone[key] = value; |
| 220 | } |
| 221 | }); |
| 222 | return objClone; |
| 223 | }, |
| 224 | extend = helpers.extend = function(base){ |
| 225 | each(Array.prototype.slice.call(arguments,1), function(extensionObject) { |
| 226 | each(extensionObject,function(value,key){ |
| 227 | if (extensionObject.hasOwnProperty(key)){ |
| 228 | base[key] = value; |
| 229 | } |
| 230 | }); |
| 231 | }); |
| 232 | return base; |
| 233 | }, |
| 234 | merge = helpers.merge = function(base,master){ |
| 235 | //Merge properties in left object over to a shallow clone of object right. |
| 236 | var args = Array.prototype.slice.call(arguments,0); |
| 237 | args.unshift({}); |
| 238 | return extend.apply(null, args); |
| 239 | }, |
| 240 | indexOf = helpers.indexOf = function(arrayToSearch, item){ |
| 241 | if (Array.prototype.indexOf) { |
| 242 | return arrayToSearch.indexOf(item); |
| 243 | } |
| 244 | else{ |
| 245 | for (var i = 0; i < arrayToSearch.length; i++) { |
| 246 | if (arrayToSearch[i] === item) return i; |
| 247 | } |
| 248 | return -1; |
| 249 | } |
| 250 | }, |
| 251 | where = helpers.where = function(collection, filterCallback){ |
| 252 | var filtered = []; |
| 253 | |
| 254 | helpers.each(collection, function(item){ |
| 255 | if (filterCallback(item)){ |
| 256 | filtered.push(item); |
| 257 | } |
| 258 | }); |
| 259 | |
| 260 | return filtered; |
| 261 | }, |
| 262 | findNextWhere = helpers.findNextWhere = function(arrayToSearch, filterCallback, startIndex){ |
| 263 | // Default to start of the array |
| 264 | if (!startIndex){ |
| 265 | startIndex = -1; |
| 266 | } |
| 267 | for (var i = startIndex + 1; i < arrayToSearch.length; i++) { |
| 268 | var currentItem = arrayToSearch[i]; |
| 269 | if (filterCallback(currentItem)){ |
| 270 | return currentItem; |
| 271 | } |
| 272 | } |
| 273 | }, |
| 274 | findPreviousWhere = helpers.findPreviousWhere = function(arrayToSearch, filterCallback, startIndex){ |
| 275 | // Default to end of the array |
| 276 | if (!startIndex){ |
| 277 | startIndex = arrayToSearch.length; |
| 278 | } |
| 279 | for (var i = startIndex - 1; i >= 0; i--) { |
| 280 | var currentItem = arrayToSearch[i]; |
| 281 | if (filterCallback(currentItem)){ |
| 282 | return currentItem; |
| 283 | } |
| 284 | } |
| 285 | }, |
| 286 | inherits = helpers.inherits = function(extensions){ |
| 287 | //Basic javascript inheritance based on the model created in Backbone.js |
| 288 | var parent = this; |
| 289 | var ChartElement = (extensions && extensions.hasOwnProperty("constructor")) ? extensions.constructor : function(){ return parent.apply(this, arguments); }; |
| 290 | |
| 291 | var Surrogate = function(){ this.constructor = ChartElement;}; |
| 292 | Surrogate.prototype = parent.prototype; |
| 293 | ChartElement.prototype = new Surrogate(); |
| 294 | |
| 295 | ChartElement.extend = inherits; |
| 296 | |
| 297 | if (extensions) extend(ChartElement.prototype, extensions); |
| 298 | |
| 299 | ChartElement.__super__ = parent.prototype; |
| 300 | |
| 301 | return ChartElement; |
| 302 | }, |
| 303 | noop = helpers.noop = function(){}, |
| 304 | uid = helpers.uid = (function(){ |
| 305 | var id=0; |
| 306 | return function(){ |
| 307 | return "chart-" + id++; |
| 308 | }; |
| 309 | })(), |
| 310 | warn = helpers.warn = function(str){ |
| 311 | //Method for warning of errors |
| 312 | if (window.console && typeof window.console.warn === "function") console.warn(str); |
| 313 | }, |
| 314 | amd = helpers.amd = (typeof define === 'function' && define.amd), |
| 315 | //-- Math methods |
| 316 | isNumber = helpers.isNumber = function(n){ |
| 317 | return !isNaN(parseFloat(n)) && isFinite(n); |
| 318 | }, |
| 319 | max = helpers.max = function(array){ |
| 320 | return Math.max.apply( Math, array ); |
| 321 | }, |
| 322 | min = helpers.min = function(array){ |
| 323 | return Math.min.apply( Math, array ); |
| 324 | }, |
| 325 | cap = helpers.cap = function(valueToCap,maxValue,minValue){ |
| 326 | if(isNumber(maxValue)) { |
| 327 | if( valueToCap > maxValue ) { |
| 328 | return maxValue; |
| 329 | } |
| 330 | } |
| 331 | else if(isNumber(minValue)){ |
| 332 | if ( valueToCap < minValue ){ |
| 333 | return minValue; |
| 334 | } |
| 335 | } |
| 336 | return valueToCap; |
| 337 | }, |
| 338 | getDecimalPlaces = helpers.getDecimalPlaces = function(num){ |
| 339 | if (num%1!==0 && isNumber(num)){ |
| 340 | var s = num.toString(); |
| 341 | if(s.indexOf("e-") < 0){ |
| 342 | // no exponent, e.g. 0.01 |
| 343 | return s.split(".")[1].length; |
| 344 | } |
| 345 | else if(s.indexOf(".") < 0) { |
| 346 | // no decimal point, e.g. 1e-9 |
| 347 | return parseInt(s.split("e-")[1]); |
| 348 | } |
| 349 | else { |
| 350 | // exponent and decimal point, e.g. 1.23e-9 |
| 351 | var parts = s.split(".")[1].split("e-"); |
| 352 | return parts[0].length + parseInt(parts[1]); |
| 353 | } |
| 354 | } |
| 355 | else { |
| 356 | return 0; |
| 357 | } |
| 358 | }, |
| 359 | toRadians = helpers.radians = function(degrees){ |
| 360 | return degrees * (Math.PI/180); |
| 361 | }, |
| 362 | // Gets the angle from vertical upright to the point about a centre. |
| 363 | getAngleFromPoint = helpers.getAngleFromPoint = function(centrePoint, anglePoint){ |
| 364 | var distanceFromXCenter = anglePoint.x - centrePoint.x, |
| 365 | distanceFromYCenter = anglePoint.y - centrePoint.y, |
| 366 | radialDistanceFromCenter = Math.sqrt( distanceFromXCenter * distanceFromXCenter + distanceFromYCenter * distanceFromYCenter); |
| 367 | |
| 368 | |
| 369 | var angle = Math.PI * 2 + Math.atan2(distanceFromYCenter, distanceFromXCenter); |
| 370 | |
| 371 | //If the segment is in the top left quadrant, we need to add another rotation to the angle |
| 372 | if (distanceFromXCenter < 0 && distanceFromYCenter < 0){ |
| 373 | angle += Math.PI*2; |
| 374 | } |
| 375 | |
| 376 | return { |
| 377 | angle: angle, |
| 378 | distance: radialDistanceFromCenter |
| 379 | }; |
| 380 | }, |
| 381 | aliasPixel = helpers.aliasPixel = function(pixelWidth){ |
| 382 | return (pixelWidth % 2 === 0) ? 0 : 0.5; |
| 383 | }, |
| 384 | splineCurve = helpers.splineCurve = function(FirstPoint,MiddlePoint,AfterPoint,t){ |
| 385 | //Props to Rob Spencer at scaled innovation for his post on splining between points |
| 386 | //http://scaledinnovation.com/analytics/splines/aboutSplines.html |
| 387 | var d01=Math.sqrt(Math.pow(MiddlePoint.x-FirstPoint.x,2)+Math.pow(MiddlePoint.y-FirstPoint.y,2)), |
| 388 | d12=Math.sqrt(Math.pow(AfterPoint.x-MiddlePoint.x,2)+Math.pow(AfterPoint.y-MiddlePoint.y,2)), |
| 389 | fa=t*d01/(d01+d12),// scaling factor for triangle Ta |
| 390 | fb=t*d12/(d01+d12); |
| 391 | return { |
| 392 | inner : { |
| 393 | x : MiddlePoint.x-fa*(AfterPoint.x-FirstPoint.x), |
| 394 | y : MiddlePoint.y-fa*(AfterPoint.y-FirstPoint.y) |
| 395 | }, |
| 396 | outer : { |
| 397 | x: MiddlePoint.x+fb*(AfterPoint.x-FirstPoint.x), |
| 398 | y : MiddlePoint.y+fb*(AfterPoint.y-FirstPoint.y) |
| 399 | } |
| 400 | }; |
| 401 | }, |
| 402 | calculateOrderOfMagnitude = helpers.calculateOrderOfMagnitude = function(val){ |
| 403 | return Math.floor(Math.log(val) / Math.LN10); |
| 404 | }, |
| 405 | calculateScaleRange = helpers.calculateScaleRange = function(valuesArray, drawingSize, textSize, startFromZero, integersOnly){ |
| 406 | |
| 407 | //Set a minimum step of two - a point at the top of the graph, and a point at the base |
| 408 | var minSteps = 2, |
| 409 | maxSteps = Math.floor(drawingSize/(textSize * 1.5)), |
| 410 | skipFitting = (minSteps >= maxSteps); |
| 411 | |
| 412 | // Filter out null values since these would min() to zero |
| 413 | var values = []; |
| 414 | each(valuesArray, function( v ){ |
| 415 | v == null || values.push( v ); |
| 416 | }); |
| 417 | var minValue = min(values), |
| 418 | maxValue = max(values); |
| 419 | |
| 420 | // We need some degree of separation here to calculate the scales if all the values are the same |
| 421 | // Adding/minusing 0.5 will give us a range of 1. |
| 422 | if (maxValue === minValue){ |
| 423 | maxValue += 0.5; |
| 424 | // So we don't end up with a graph with a negative start value if we've said always start from zero |
| 425 | if (minValue >= 0.5 && !startFromZero){ |
| 426 | minValue -= 0.5; |
| 427 | } |
| 428 | else{ |
| 429 | // Make up a whole number above the values |
| 430 | maxValue += 0.5; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | var valueRange = Math.abs(maxValue - minValue), |
| 435 | rangeOrderOfMagnitude = calculateOrderOfMagnitude(valueRange), |
| 436 | graphMax = Math.ceil(maxValue / (1 * Math.pow(10, rangeOrderOfMagnitude))) * Math.pow(10, rangeOrderOfMagnitude), |
| 437 | graphMin = (startFromZero) ? 0 : Math.floor(minValue / (1 * Math.pow(10, rangeOrderOfMagnitude))) * Math.pow(10, rangeOrderOfMagnitude), |
| 438 | graphRange = graphMax - graphMin, |
| 439 | stepValue = Math.pow(10, rangeOrderOfMagnitude), |
| 440 | numberOfSteps = Math.round(graphRange / stepValue); |
| 441 | |
| 442 | //If we have more space on the graph we'll use it to give more definition to the data |
| 443 | while((numberOfSteps > maxSteps || (numberOfSteps * 2) < maxSteps) && !skipFitting) { |
| 444 | if(numberOfSteps > maxSteps){ |
| 445 | stepValue *=2; |
| 446 | numberOfSteps = Math.round(graphRange/stepValue); |
| 447 | // Don't ever deal with a decimal number of steps - cancel fitting and just use the minimum number of steps. |
| 448 | if (numberOfSteps % 1 !== 0){ |
| 449 | skipFitting = true; |
| 450 | } |
| 451 | } |
| 452 | //We can fit in double the amount of scale points on the scale |
| 453 | else{ |
| 454 | //If user has declared ints only, and the step value isn't a decimal |
| 455 | if (integersOnly && rangeOrderOfMagnitude >= 0){ |
| 456 | //If the user has said integers only, we need to check that making the scale more granular wouldn't make it a float |
| 457 | if(stepValue/2 % 1 === 0){ |
| 458 | stepValue /=2; |
| 459 | numberOfSteps = Math.round(graphRange/stepValue); |
| 460 | } |
| 461 | //If it would make it a float break out of the loop |
| 462 | else{ |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | //If the scale doesn't have to be an int, make the scale more granular anyway. |
| 467 | else{ |
| 468 | stepValue /=2; |
| 469 | numberOfSteps = Math.round(graphRange/stepValue); |
| 470 | } |
| 471 | |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (skipFitting){ |
| 476 | numberOfSteps = minSteps; |
| 477 | stepValue = graphRange / numberOfSteps; |
| 478 | } |
| 479 | |
| 480 | return { |
| 481 | steps : numberOfSteps, |
| 482 | stepValue : stepValue, |
| 483 | min : graphMin, |
| 484 | max : graphMin + (numberOfSteps * stepValue) |
| 485 | }; |
| 486 | |
| 487 | }, |
| 488 | /* jshint ignore:start */ |
| 489 | // Blows up jshint errors based on the new Function constructor |
| 490 | //Templating methods |
| 491 | //Javascript micro templating by John Resig - source at http://ejohn.org/blog/javascript-micro-templating/ |
| 492 | template = helpers.template = function(templateString, valuesObject){ |
| 493 | |
| 494 | // If templateString is function rather than string-template - call the function for valuesObject |
| 495 | |
| 496 | if(templateString instanceof Function){ |
| 497 | return templateString(valuesObject); |
| 498 | } |
| 499 | |
| 500 | var cache = {}; |
| 501 | function tmpl(str, data){ |
| 502 | // Figure out if we're getting a template, or if we need to |
| 503 | // load the template - and be sure to cache the result. |
| 504 | var fn = !/\W/.test(str) ? |
| 505 | cache[str] = cache[str] : |
| 506 | |
| 507 | // Generate a reusable function that will serve as a template |
| 508 | // generator (and which will be cached). |
| 509 | new Function("obj", |
| 510 | "var p=[],print=function(){p.push.apply(p,arguments);};" + |
| 511 | |
| 512 | // Introduce the data as local variables using with(){} |
| 513 | "with(obj){p.push('" + |
| 514 | |
| 515 | // Convert the template into pure JavaScript |
| 516 | str |
| 517 | .replace(/[\r\t\n]/g, " ") |
| 518 | .split("<%").join("\t") |
| 519 | .replace(/((^|%>)[^\t]*)'/g, "$1\r") |
| 520 | .replace(/\t=(.*?)%>/g, "',$1,'") |
| 521 | .split("\t").join("');") |
| 522 | .split("%>").join("p.push('") |
| 523 | .split("\r").join("\\'") + |
| 524 | "');}return p.join('');" |
| 525 | ); |
| 526 | |
| 527 | // Provide some basic currying to the user |
| 528 | return data ? fn( data ) : fn; |
| 529 | } |
| 530 | return tmpl(templateString,valuesObject); |
| 531 | }, |
| 532 | /* jshint ignore:end */ |
| 533 | generateLabels = helpers.generateLabels = function(templateString,numberOfSteps,graphMin,stepValue){ |
| 534 | var labelsArray = new Array(numberOfSteps); |
| 535 | if (templateString){ |
| 536 | each(labelsArray,function(val,index){ |
| 537 | labelsArray[index] = template(templateString,{value: (graphMin + (stepValue*(index+1)))}); |
| 538 | }); |
| 539 | } |
| 540 | return labelsArray; |
| 541 | }, |
| 542 | //--Animation methods |
| 543 | //Easing functions adapted from Robert Penner's easing equations |
| 544 | //http://www.robertpenner.com/easing/ |
| 545 | easingEffects = helpers.easingEffects = { |
| 546 | linear: function (t) { |
| 547 | return t; |
| 548 | }, |
| 549 | easeInQuad: function (t) { |
| 550 | return t * t; |
| 551 | }, |
| 552 | easeOutQuad: function (t) { |
| 553 | return -1 * t * (t - 2); |
| 554 | }, |
| 555 | easeInOutQuad: function (t) { |
| 556 | if ((t /= 1 / 2) < 1){ |
| 557 | return 1 / 2 * t * t; |
| 558 | } |
| 559 | return -1 / 2 * ((--t) * (t - 2) - 1); |
| 560 | }, |
| 561 | easeInCubic: function (t) { |
| 562 | return t * t * t; |
| 563 | }, |
| 564 | easeOutCubic: function (t) { |
| 565 | return 1 * ((t = t / 1 - 1) * t * t + 1); |
| 566 | }, |
| 567 | easeInOutCubic: function (t) { |
| 568 | if ((t /= 1 / 2) < 1){ |
| 569 | return 1 / 2 * t * t * t; |
| 570 | } |
| 571 | return 1 / 2 * ((t -= 2) * t * t + 2); |
| 572 | }, |
| 573 | easeInQuart: function (t) { |
| 574 | return t * t * t * t; |
| 575 | }, |
| 576 | easeOutQuart: function (t) { |
| 577 | return -1 * ((t = t / 1 - 1) * t * t * t - 1); |
| 578 | }, |
| 579 | easeInOutQuart: function (t) { |
| 580 | if ((t /= 1 / 2) < 1){ |
| 581 | return 1 / 2 * t * t * t * t; |
| 582 | } |
| 583 | return -1 / 2 * ((t -= 2) * t * t * t - 2); |
| 584 | }, |
| 585 | easeInQuint: function (t) { |
| 586 | return 1 * (t /= 1) * t * t * t * t; |
| 587 | }, |
| 588 | easeOutQuint: function (t) { |
| 589 | return 1 * ((t = t / 1 - 1) * t * t * t * t + 1); |
| 590 | }, |
| 591 | easeInOutQuint: function (t) { |
| 592 | if ((t /= 1 / 2) < 1){ |
| 593 | return 1 / 2 * t * t * t * t * t; |
| 594 | } |
| 595 | return 1 / 2 * ((t -= 2) * t * t * t * t + 2); |
| 596 | }, |
| 597 | easeInSine: function (t) { |
| 598 | return -1 * Math.cos(t / 1 * (Math.PI / 2)) + 1; |
| 599 | }, |
| 600 | easeOutSine: function (t) { |
| 601 | return 1 * Math.sin(t / 1 * (Math.PI / 2)); |
| 602 | }, |
| 603 | easeInOutSine: function (t) { |
| 604 | return -1 / 2 * (Math.cos(Math.PI * t / 1) - 1); |
| 605 | }, |
| 606 | easeInExpo: function (t) { |
| 607 | return (t === 0) ? 1 : 1 * Math.pow(2, 10 * (t / 1 - 1)); |
| 608 | }, |
| 609 | easeOutExpo: function (t) { |
| 610 | return (t === 1) ? 1 : 1 * (-Math.pow(2, -10 * t / 1) + 1); |
| 611 | }, |
| 612 | easeInOutExpo: function (t) { |
| 613 | if (t === 0){ |
| 614 | return 0; |
| 615 | } |
| 616 | if (t === 1){ |
| 617 | return 1; |
| 618 | } |
| 619 | if ((t /= 1 / 2) < 1){ |
| 620 | return 1 / 2 * Math.pow(2, 10 * (t - 1)); |
| 621 | } |
| 622 | return 1 / 2 * (-Math.pow(2, -10 * --t) + 2); |
| 623 | }, |
| 624 | easeInCirc: function (t) { |
| 625 | if (t >= 1){ |
| 626 | return t; |
| 627 | } |
| 628 | return -1 * (Math.sqrt(1 - (t /= 1) * t) - 1); |
| 629 | }, |
| 630 | easeOutCirc: function (t) { |
| 631 | return 1 * Math.sqrt(1 - (t = t / 1 - 1) * t); |
| 632 | }, |
| 633 | easeInOutCirc: function (t) { |
| 634 | if ((t /= 1 / 2) < 1){ |
| 635 | return -1 / 2 * (Math.sqrt(1 - t * t) - 1); |
| 636 | } |
| 637 | return 1 / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1); |
| 638 | }, |
| 639 | easeInElastic: function (t) { |
| 640 | var s = 1.70158; |
| 641 | var p = 0; |
| 642 | var a = 1; |
| 643 | if (t === 0){ |
| 644 | return 0; |
| 645 | } |
| 646 | if ((t /= 1) == 1){ |
| 647 | return 1; |
| 648 | } |
| 649 | if (!p){ |
| 650 | p = 1 * 0.3; |
| 651 | } |
| 652 | if (a < Math.abs(1)) { |
| 653 | a = 1; |
| 654 | s = p / 4; |
| 655 | } else{ |
| 656 | s = p / (2 * Math.PI) * Math.asin(1 / a); |
| 657 | } |
| 658 | return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * 1 - s) * (2 * Math.PI) / p)); |
| 659 | }, |
| 660 | easeOutElastic: function (t) { |
| 661 | var s = 1.70158; |
| 662 | var p = 0; |
| 663 | var a = 1; |
| 664 | if (t === 0){ |
| 665 | return 0; |
| 666 | } |
| 667 | if ((t /= 1) == 1){ |
| 668 | return 1; |
| 669 | } |
| 670 | if (!p){ |
| 671 | p = 1 * 0.3; |
| 672 | } |
| 673 | if (a < Math.abs(1)) { |
| 674 | a = 1; |
| 675 | s = p / 4; |
| 676 | } else{ |
| 677 | s = p / (2 * Math.PI) * Math.asin(1 / a); |
| 678 | } |
| 679 | return a * Math.pow(2, -10 * t) * Math.sin((t * 1 - s) * (2 * Math.PI) / p) + 1; |
| 680 | }, |
| 681 | easeInOutElastic: function (t) { |
| 682 | var s = 1.70158; |
| 683 | var p = 0; |
| 684 | var a = 1; |
| 685 | if (t === 0){ |
| 686 | return 0; |
| 687 | } |
| 688 | if ((t /= 1 / 2) == 2){ |
| 689 | return 1; |
| 690 | } |
| 691 | if (!p){ |
| 692 | p = 1 * (0.3 * 1.5); |
| 693 | } |
| 694 | if (a < Math.abs(1)) { |
| 695 | a = 1; |
| 696 | s = p / 4; |
| 697 | } else { |
| 698 | s = p / (2 * Math.PI) * Math.asin(1 / a); |
| 699 | } |
| 700 | if (t < 1){ |
| 701 | return -0.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * 1 - s) * (2 * Math.PI) / p));} |
| 702 | return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * 1 - s) * (2 * Math.PI) / p) * 0.5 + 1; |
| 703 | }, |
| 704 | easeInBack: function (t) { |
| 705 | var s = 1.70158; |
| 706 | return 1 * (t /= 1) * t * ((s + 1) * t - s); |
| 707 | }, |
| 708 | easeOutBack: function (t) { |
| 709 | var s = 1.70158; |
| 710 | return 1 * ((t = t / 1 - 1) * t * ((s + 1) * t + s) + 1); |
| 711 | }, |
| 712 | easeInOutBack: function (t) { |
| 713 | var s = 1.70158; |
| 714 | if ((t /= 1 / 2) < 1){ |
| 715 | return 1 / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)); |
| 716 | } |
| 717 | return 1 / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2); |
| 718 | }, |
| 719 | easeInBounce: function (t) { |
| 720 | return 1 - easingEffects.easeOutBounce(1 - t); |
| 721 | }, |
| 722 | easeOutBounce: function (t) { |
| 723 | if ((t /= 1) < (1 / 2.75)) { |
| 724 | return 1 * (7.5625 * t * t); |
| 725 | } else if (t < (2 / 2.75)) { |
| 726 | return 1 * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75); |
| 727 | } else if (t < (2.5 / 2.75)) { |
| 728 | return 1 * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375); |
| 729 | } else { |
| 730 | return 1 * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375); |
| 731 | } |
| 732 | }, |
| 733 | easeInOutBounce: function (t) { |
| 734 | if (t < 1 / 2){ |
| 735 | return easingEffects.easeInBounce(t * 2) * 0.5; |
| 736 | } |
| 737 | return easingEffects.easeOutBounce(t * 2 - 1) * 0.5 + 1 * 0.5; |
| 738 | } |
| 739 | }, |
| 740 | //Request animation polyfill - http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/ |
| 741 | requestAnimFrame = helpers.requestAnimFrame = (function(){ |
| 742 | return window.requestAnimationFrame || |
| 743 | window.webkitRequestAnimationFrame || |
| 744 | window.mozRequestAnimationFrame || |
| 745 | window.oRequestAnimationFrame || |
| 746 | window.msRequestAnimationFrame || |
| 747 | function(callback) { |
| 748 | return window.setTimeout(callback, 1000 / 60); |
| 749 | }; |
| 750 | })(), |
| 751 | cancelAnimFrame = helpers.cancelAnimFrame = (function(){ |
| 752 | return window.cancelAnimationFrame || |
| 753 | window.webkitCancelAnimationFrame || |
| 754 | window.mozCancelAnimationFrame || |
| 755 | window.oCancelAnimationFrame || |
| 756 | window.msCancelAnimationFrame || |
| 757 | function(callback) { |
| 758 | return window.clearTimeout(callback, 1000 / 60); |
| 759 | }; |
| 760 | })(), |
| 761 | animationLoop = helpers.animationLoop = function(callback,totalSteps,easingString,onProgress,onComplete,chartInstance){ |
| 762 | |
| 763 | var currentStep = 0, |
| 764 | easingFunction = easingEffects[easingString] || easingEffects.linear; |
| 765 | |
| 766 | var animationFrame = function(){ |
| 767 | currentStep++; |
| 768 | var stepDecimal = currentStep/totalSteps; |
| 769 | var easeDecimal = easingFunction(stepDecimal); |
| 770 | |
| 771 | callback.call(chartInstance,easeDecimal,stepDecimal, currentStep); |
| 772 | onProgress.call(chartInstance,easeDecimal,stepDecimal); |
| 773 | if (currentStep < totalSteps){ |
| 774 | chartInstance.animationFrame = requestAnimFrame(animationFrame); |
| 775 | } else{ |
| 776 | onComplete.apply(chartInstance); |
| 777 | } |
| 778 | }; |
| 779 | requestAnimFrame(animationFrame); |
| 780 | }, |
| 781 | //-- DOM methods |
| 782 | getRelativePosition = helpers.getRelativePosition = function(evt){ |
| 783 | var mouseX, mouseY; |
| 784 | var e = evt.originalEvent || evt, |
| 785 | canvas = evt.currentTarget || evt.srcElement, |
| 786 | boundingRect = canvas.getBoundingClientRect(); |
| 787 | |
| 788 | if (e.touches){ |
| 789 | mouseX = e.touches[0].clientX - boundingRect.left; |
| 790 | mouseY = e.touches[0].clientY - boundingRect.top; |
| 791 | |
| 792 | } |
| 793 | else{ |
| 794 | mouseX = e.clientX - boundingRect.left; |
| 795 | mouseY = e.clientY - boundingRect.top; |
| 796 | } |
| 797 | |
| 798 | return { |
| 799 | x : mouseX, |
| 800 | y : mouseY |
| 801 | }; |
| 802 | |
| 803 | }, |
| 804 | addEvent = helpers.addEvent = function(node,eventType,method){ |
| 805 | if (node.addEventListener){ |
| 806 | node.addEventListener(eventType,method); |
| 807 | } else if (node.attachEvent){ |
| 808 | node.attachEvent("on"+eventType, method); |
| 809 | } else { |
| 810 | node["on"+eventType] = method; |
| 811 | } |
| 812 | }, |
| 813 | removeEvent = helpers.removeEvent = function(node, eventType, handler){ |
| 814 | if (node.removeEventListener){ |
| 815 | node.removeEventListener(eventType, handler, false); |
| 816 | } else if (node.detachEvent){ |
| 817 | node.detachEvent("on"+eventType,handler); |
| 818 | } else{ |
| 819 | node["on" + eventType] = noop; |
| 820 | } |
| 821 | }, |
| 822 | bindEvents = helpers.bindEvents = function(chartInstance, arrayOfEvents, handler){ |
| 823 | // Create the events object if it's not already present |
| 824 | if (!chartInstance.events) chartInstance.events = {}; |
| 825 | |
| 826 | each(arrayOfEvents,function(eventName){ |
| 827 | chartInstance.events[eventName] = function(){ |
| 828 | handler.apply(chartInstance, arguments); |
| 829 | }; |
| 830 | addEvent(chartInstance.chart.canvas,eventName,chartInstance.events[eventName]); |
| 831 | }); |
| 832 | }, |
| 833 | unbindEvents = helpers.unbindEvents = function (chartInstance, arrayOfEvents) { |
| 834 | each(arrayOfEvents, function(handler,eventName){ |
| 835 | removeEvent(chartInstance.chart.canvas, eventName, handler); |
| 836 | }); |
| 837 | }, |
| 838 | getMaximumWidth = helpers.getMaximumWidth = function(domNode){ |
| 839 | var container = domNode.parentNode, |
| 840 | padding = parseInt(getStyle(container, 'padding-left')) + parseInt(getStyle(container, 'padding-right')); |
| 841 | // TODO = check cross browser stuff with this. |
| 842 | return container.clientWidth - padding; |
| 843 | }, |
| 844 | getMaximumHeight = helpers.getMaximumHeight = function(domNode){ |
| 845 | var container = domNode.parentNode, |
| 846 | padding = parseInt(getStyle(container, 'padding-bottom')) + parseInt(getStyle(container, 'padding-top')); |
| 847 | // TODO = check cross browser stuff with this. |
| 848 | return container.clientHeight - padding; |
| 849 | }, |
| 850 | getStyle = helpers.getStyle = function (el, property) { |
| 851 | return el.currentStyle ? |
| 852 | el.currentStyle[property] : |
| 853 | document.defaultView.getComputedStyle(el, null).getPropertyValue(property); |
| 854 | }, |
| 855 | getMaximumSize = helpers.getMaximumSize = helpers.getMaximumWidth, // legacy support |
| 856 | retinaScale = helpers.retinaScale = function(chart){ |
| 857 | var ctx = chart.ctx, |
| 858 | width = chart.canvas.width, |
| 859 | height = chart.canvas.height; |
| 860 | |
| 861 | if (window.devicePixelRatio) { |
| 862 | ctx.canvas.style.width = width + "px"; |
| 863 | ctx.canvas.style.height = height + "px"; |
| 864 | ctx.canvas.height = height * window.devicePixelRatio; |
| 865 | ctx.canvas.width = width * window.devicePixelRatio; |
| 866 | ctx.scale(window.devicePixelRatio, window.devicePixelRatio); |
| 867 | } |
| 868 | }, |
| 869 | //-- Canvas methods |
| 870 | clear = helpers.clear = function(chart){ |
| 871 | chart.ctx.clearRect(0,0,chart.width,chart.height); |
| 872 | }, |
| 873 | fontString = helpers.fontString = function(pixelSize,fontStyle,fontFamily){ |
| 874 | return fontStyle + " " + pixelSize+"px " + fontFamily; |
| 875 | }, |
| 876 | longestText = helpers.longestText = function(ctx,font,arrayOfStrings){ |
| 877 | ctx.font = font; |
| 878 | var longest = 0; |
| 879 | each(arrayOfStrings,function(string){ |
| 880 | var textWidth = ctx.measureText(string).width; |
| 881 | longest = (textWidth > longest) ? textWidth : longest; |
| 882 | }); |
| 883 | return longest; |
| 884 | }, |
| 885 | drawRoundedRectangle = helpers.drawRoundedRectangle = function(ctx,x,y,width,height,radius){ |
| 886 | ctx.beginPath(); |
| 887 | ctx.moveTo(x + radius, y); |
| 888 | ctx.lineTo(x + width - radius, y); |
| 889 | ctx.quadraticCurveTo(x + width, y, x + width, y + radius); |
| 890 | ctx.lineTo(x + width, y + height - radius); |
| 891 | ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height); |
| 892 | ctx.lineTo(x + radius, y + height); |
| 893 | ctx.quadraticCurveTo(x, y + height, x, y + height - radius); |
| 894 | ctx.lineTo(x, y + radius); |
| 895 | ctx.quadraticCurveTo(x, y, x + radius, y); |
| 896 | ctx.closePath(); |
| 897 | }; |
| 898 | |
| 899 | |
| 900 | //Store a reference to each instance - allowing us to globally resize chart instances on window resize. |
| 901 | //Destroy method on the chart will remove the instance of the chart from this reference. |
| 902 | Chart.instances = {}; |
| 903 | |
| 904 | Chart.Type = function(data,options,chart){ |
| 905 | this.options = options; |
| 906 | this.chart = chart; |
| 907 | this.id = uid(); |
| 908 | //Add the chart instance to the global namespace |
| 909 | Chart.instances[this.id] = this; |
| 910 | |
| 911 | // Initialize is always called when a chart type is created |
| 912 | // By default it is a no op, but it should be extended |
| 913 | if (options.responsive){ |
| 914 | this.resize(); |
| 915 | } |
| 916 | this.initialize.call(this,data); |
| 917 | }; |
| 918 | |
| 919 | //Core methods that'll be a part of every chart type |
| 920 | extend(Chart.Type.prototype,{ |
| 921 | initialize : function(){return this;}, |
| 922 | clear : function(){ |
| 923 | clear(this.chart); |
| 924 | return this; |
| 925 | }, |
| 926 | stop : function(){ |
| 927 | // Stops any current animation loop occuring |
| 928 | Chart.animationService.cancelAnimation(this); |
| 929 | return this; |
| 930 | }, |
| 931 | resize : function(callback){ |
| 932 | this.stop(); |
| 933 | var canvas = this.chart.canvas, |
| 934 | newWidth = getMaximumWidth(this.chart.canvas), |
| 935 | newHeight = this.options.maintainAspectRatio ? newWidth / this.chart.aspectRatio : getMaximumHeight(this.chart.canvas); |
| 936 | |
| 937 | canvas.width = this.chart.width = newWidth; |
| 938 | canvas.height = this.chart.height = newHeight; |
| 939 | |
| 940 | retinaScale(this.chart); |
| 941 | |
| 942 | if (typeof callback === "function"){ |
| 943 | callback.apply(this, Array.prototype.slice.call(arguments, 1)); |
| 944 | } |
| 945 | return this; |
| 946 | }, |
| 947 | reflow : noop, |
| 948 | render : function(reflow){ |
| 949 | if (reflow){ |
| 950 | this.reflow(); |
| 951 | } |
| 952 | |
| 953 | if (this.options.animation && !reflow){ |
| 954 | var animation = new Chart.Animation(); |
| 955 | animation.numSteps = this.options.animationSteps; |
| 956 | animation.easing = this.options.animationEasing; |
| 957 | |
| 958 | // render function |
| 959 | animation.render = function(chartInstance, animationObject) { |
| 960 | var easingFunction = helpers.easingEffects[animationObject.easing]; |
| 961 | var stepDecimal = animationObject.currentStep / animationObject.numSteps; |
| 962 | var easeDecimal = easingFunction(stepDecimal); |
| 963 | |
| 964 | chartInstance.draw(easeDecimal, stepDecimal, animationObject.currentStep); |
| 965 | }; |
| 966 | |
| 967 | // user events |
| 968 | animation.onAnimationProgress = this.options.onAnimationProgress; |
| 969 | animation.onAnimationComplete = this.options.onAnimationComplete; |
| 970 | |
| 971 | Chart.animationService.addAnimation(this, animation); |
| 972 | } |
| 973 | else{ |
| 974 | this.draw(); |
| 975 | this.options.onAnimationComplete.call(this); |
| 976 | } |
| 977 | return this; |
| 978 | }, |
| 979 | generateLegend : function(){ |
| 980 | return template(this.options.legendTemplate,this); |
| 981 | }, |
| 982 | destroy : function(){ |
| 983 | this.clear(); |
| 984 | unbindEvents(this, this.events); |
| 985 | var canvas = this.chart.canvas; |
| 986 | |
| 987 | // Reset canvas height/width attributes starts a fresh with the canvas context |
| 988 | canvas.width = this.chart.width; |
| 989 | canvas.height = this.chart.height; |
| 990 | |
| 991 | // < IE9 doesn't support removeProperty |
| 992 | if (canvas.style.removeProperty) { |
| 993 | canvas.style.removeProperty('width'); |
| 994 | canvas.style.removeProperty('height'); |
| 995 | } else { |
| 996 | canvas.style.removeAttribute('width'); |
| 997 | canvas.style.removeAttribute('height'); |
| 998 | } |
| 999 | |
| 1000 | delete Chart.instances[this.id]; |
| 1001 | }, |
| 1002 | showTooltip : function(ChartElements, forceRedraw){ |
| 1003 | // Only redraw the chart if we've actually changed what we're hovering on. |
| 1004 | if (typeof this.activeElements === 'undefined') this.activeElements = []; |
| 1005 | |
| 1006 | var isChanged = (function(Elements){ |
| 1007 | var changed = false; |
| 1008 | |
| 1009 | if (Elements.length !== this.activeElements.length){ |
| 1010 | changed = true; |
| 1011 | return changed; |
| 1012 | } |
| 1013 | |
| 1014 | each(Elements, function(element, index){ |
| 1015 | if (element !== this.activeElements[index]){ |
| 1016 | changed = true; |
| 1017 | } |
| 1018 | }, this); |
| 1019 | return changed; |
| 1020 | }).call(this, ChartElements); |
| 1021 | |
| 1022 | if (!isChanged && !forceRedraw){ |
| 1023 | return; |
| 1024 | } |
| 1025 | else{ |
| 1026 | this.activeElements = ChartElements; |
| 1027 | } |
| 1028 | this.draw(); |
| 1029 | if(this.options.customTooltips){ |
| 1030 | this.options.customTooltips(false); |
| 1031 | } |
| 1032 | if (ChartElements.length > 0){ |
| 1033 | // If we have multiple datasets, show a MultiTooltip for all of the data points at that index |
| 1034 | if (this.datasets && this.datasets.length > 1) { |
| 1035 | var dataArray, |
| 1036 | dataIndex; |
| 1037 | |
| 1038 | for (var i = this.datasets.length - 1; i >= 0; i--) { |
| 1039 | dataArray = this.datasets[i].points || this.datasets[i].bars || this.datasets[i].segments; |
| 1040 | dataIndex = indexOf(dataArray, ChartElements[0]); |
| 1041 | if (dataIndex !== -1){ |
| 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | var tooltipLabels = [], |
| 1046 | tooltipColors = [], |
| 1047 | medianPosition = (function(index) { |
| 1048 | |
| 1049 | // Get all the points at that particular index |
| 1050 | var Elements = [], |
| 1051 | dataCollection, |
| 1052 | xPositions = [], |
| 1053 | yPositions = [], |
| 1054 | xMax, |
| 1055 | yMax, |
| 1056 | xMin, |
| 1057 | yMin; |
| 1058 | helpers.each(this.datasets, function(dataset){ |
| 1059 | dataCollection = dataset.points || dataset.bars || dataset.segments; |
| 1060 | if (dataCollection[dataIndex] && dataCollection[dataIndex].hasValue()){ |
| 1061 | Elements.push(dataCollection[dataIndex]); |
| 1062 | } |
| 1063 | }); |
| 1064 | |
| 1065 | helpers.each(Elements, function(element) { |
| 1066 | xPositions.push(element.x); |
| 1067 | yPositions.push(element.y); |
| 1068 | |
| 1069 | |
| 1070 | //Include any colour information about the element |
| 1071 | tooltipLabels.push(helpers.template(this.options.multiTooltipTemplate, element)); |
| 1072 | tooltipColors.push({ |
| 1073 | fill: element._saved.fillColor || element.fillColor, |
| 1074 | stroke: element._saved.strokeColor || element.strokeColor |
| 1075 | }); |
| 1076 | |
| 1077 | }, this); |
| 1078 | |
| 1079 | yMin = min(yPositions); |
| 1080 | yMax = max(yPositions); |
| 1081 | |
| 1082 | xMin = min(xPositions); |
| 1083 | xMax = max(xPositions); |
| 1084 | |
| 1085 | return { |
| 1086 | x: (xMin > this.chart.width/2) ? xMin : xMax, |
| 1087 | y: (yMin + yMax)/2 |
| 1088 | }; |
| 1089 | }).call(this, dataIndex); |
| 1090 | |
| 1091 | new Chart.MultiTooltip({ |
| 1092 | x: medianPosition.x, |
| 1093 | y: medianPosition.y, |
| 1094 | xPadding: this.options.tooltipXPadding, |
| 1095 | yPadding: this.options.tooltipYPadding, |
| 1096 | xOffset: this.options.tooltipXOffset, |
| 1097 | fillColor: this.options.tooltipFillColor, |
| 1098 | textColor: this.options.tooltipFontColor, |
| 1099 | fontFamily: this.options.tooltipFontFamily, |
| 1100 | fontStyle: this.options.tooltipFontStyle, |
| 1101 | fontSize: this.options.tooltipFontSize, |
| 1102 | titleTextColor: this.options.tooltipTitleFontColor, |
| 1103 | titleFontFamily: this.options.tooltipTitleFontFamily, |
| 1104 | titleFontStyle: this.options.tooltipTitleFontStyle, |
| 1105 | titleFontSize: this.options.tooltipTitleFontSize, |
| 1106 | cornerRadius: this.options.tooltipCornerRadius, |
| 1107 | labels: tooltipLabels, |
| 1108 | legendColors: tooltipColors, |
| 1109 | legendColorBackground : this.options.multiTooltipKeyBackground, |
| 1110 | title: template(this.options.tooltipTitleTemplate,ChartElements[0]), |
| 1111 | chart: this.chart, |
| 1112 | ctx: this.chart.ctx, |
| 1113 | custom: this.options.customTooltips |
| 1114 | }).draw(); |
| 1115 | |
| 1116 | } else { |
| 1117 | each(ChartElements, function(Element) { |
| 1118 | var tooltipPosition = Element.tooltipPosition(); |
| 1119 | new Chart.Tooltip({ |
| 1120 | x: Math.round(tooltipPosition.x), |
| 1121 | y: Math.round(tooltipPosition.y), |
| 1122 | xPadding: this.options.tooltipXPadding, |
| 1123 | yPadding: this.options.tooltipYPadding, |
| 1124 | fillColor: this.options.tooltipFillColor, |
| 1125 | textColor: this.options.tooltipFontColor, |
| 1126 | fontFamily: this.options.tooltipFontFamily, |
| 1127 | fontStyle: this.options.tooltipFontStyle, |
| 1128 | fontSize: this.options.tooltipFontSize, |
| 1129 | caretHeight: this.options.tooltipCaretSize, |
| 1130 | cornerRadius: this.options.tooltipCornerRadius, |
| 1131 | text: template(this.options.tooltipTemplate, Element), |
| 1132 | chart: this.chart, |
| 1133 | custom: this.options.customTooltips |
| 1134 | }).draw(); |
| 1135 | }, this); |
| 1136 | } |
| 1137 | } |
| 1138 | return this; |
| 1139 | }, |
| 1140 | toBase64Image : function(){ |
| 1141 | return this.chart.canvas.toDataURL.apply(this.chart.canvas, arguments); |
| 1142 | } |
| 1143 | }); |
| 1144 | |
| 1145 | Chart.Type.extend = function(extensions){ |
| 1146 | |
| 1147 | var parent = this; |
| 1148 | |
| 1149 | var ChartType = function(){ |
| 1150 | return parent.apply(this,arguments); |
| 1151 | }; |
| 1152 | |
| 1153 | //Copy the prototype object of the this class |
| 1154 | ChartType.prototype = clone(parent.prototype); |
| 1155 | //Now overwrite some of the properties in the base class with the new extensions |
| 1156 | extend(ChartType.prototype, extensions); |
| 1157 | |
| 1158 | ChartType.extend = Chart.Type.extend; |
| 1159 | |
| 1160 | if (extensions.name || parent.prototype.name){ |
| 1161 | |
| 1162 | var chartName = extensions.name || parent.prototype.name; |
| 1163 | //Assign any potential default values of the new chart type |
| 1164 | |
| 1165 | //If none are defined, we'll use a clone of the chart type this is being extended from. |
| 1166 | //I.e. if we extend a line chart, we'll use the defaults from the line chart if our new chart |
| 1167 | //doesn't define some defaults of their own. |
| 1168 | |
| 1169 | var baseDefaults = (Chart.defaults[parent.prototype.name]) ? clone(Chart.defaults[parent.prototype.name]) : {}; |
| 1170 | |
| 1171 | Chart.defaults[chartName] = extend(baseDefaults,extensions.defaults); |
| 1172 | |
| 1173 | Chart.types[chartName] = ChartType; |
| 1174 | |
| 1175 | //Register this new chart type in the Chart prototype |
| 1176 | Chart.prototype[chartName] = function(data,options){ |
| 1177 | var config = merge(Chart.defaults.global, Chart.defaults[chartName], options || {}); |
| 1178 | return new ChartType(data,config,this); |
| 1179 | }; |
| 1180 | } else{ |
| 1181 | warn("Name not provided for this chart, so it hasn't been registered"); |
| 1182 | } |
| 1183 | return parent; |
| 1184 | }; |
| 1185 | |
| 1186 | Chart.Element = function(configuration){ |
| 1187 | extend(this,configuration); |
| 1188 | this.initialize.apply(this,arguments); |
| 1189 | this.save(); |
| 1190 | }; |
| 1191 | extend(Chart.Element.prototype,{ |
| 1192 | initialize : function(){}, |
| 1193 | restore : function(props){ |
| 1194 | if (!props){ |
| 1195 | extend(this,this._saved); |
| 1196 | } else { |
| 1197 | each(props,function(key){ |
| 1198 | this[key] = this._saved[key]; |
| 1199 | },this); |
| 1200 | } |
| 1201 | return this; |
| 1202 | }, |
| 1203 | save : function(){ |
| 1204 | this._saved = clone(this); |
| 1205 | delete this._saved._saved; |
| 1206 | return this; |
| 1207 | }, |
| 1208 | update : function(newProps){ |
| 1209 | each(newProps,function(value,key){ |
| 1210 | this._saved[key] = this[key]; |
| 1211 | this[key] = value; |
| 1212 | },this); |
| 1213 | return this; |
| 1214 | }, |
| 1215 | transition : function(props,ease){ |
| 1216 | each(props,function(value,key){ |
| 1217 | this[key] = ((value - this._saved[key]) * ease) + this._saved[key]; |
| 1218 | },this); |
| 1219 | return this; |
| 1220 | }, |
| 1221 | tooltipPosition : function(){ |
| 1222 | return { |
| 1223 | x : this.x, |
| 1224 | y : this.y |
| 1225 | }; |
| 1226 | }, |
| 1227 | hasValue: function(){ |
| 1228 | return isNumber(this.value); |
| 1229 | } |
| 1230 | }); |
| 1231 | |
| 1232 | Chart.Element.extend = inherits; |
| 1233 | |
| 1234 | |
| 1235 | Chart.Point = Chart.Element.extend({ |
| 1236 | display: true, |
| 1237 | inRange: function(chartX,chartY){ |
| 1238 | var hitDetectionRange = this.hitDetectionRadius + this.radius; |
| 1239 | return ((Math.pow(chartX-this.x, 2)+Math.pow(chartY-this.y, 2)) < Math.pow(hitDetectionRange,2)); |
| 1240 | }, |
| 1241 | draw : function(){ |
| 1242 | if (this.display){ |
| 1243 | var ctx = this.ctx; |
| 1244 | ctx.beginPath(); |
| 1245 | |
| 1246 | ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2); |
| 1247 | ctx.closePath(); |
| 1248 | |
| 1249 | ctx.strokeStyle = this.strokeColor; |
| 1250 | ctx.lineWidth = this.strokeWidth; |
| 1251 | |
| 1252 | ctx.fillStyle = this.fillColor; |
| 1253 | |
| 1254 | ctx.fill(); |
| 1255 | ctx.stroke(); |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | //Quick debug for bezier curve splining |
| 1260 | //Highlights control points and the line between them. |
| 1261 | //Handy for dev - stripped in the min version. |
| 1262 | |
| 1263 | // ctx.save(); |
| 1264 | // ctx.fillStyle = "black"; |
| 1265 | // ctx.strokeStyle = "black" |
| 1266 | // ctx.beginPath(); |
| 1267 | // ctx.arc(this.controlPoints.inner.x,this.controlPoints.inner.y, 2, 0, Math.PI*2); |
| 1268 | // ctx.fill(); |
| 1269 | |
| 1270 | // ctx.beginPath(); |
| 1271 | // ctx.arc(this.controlPoints.outer.x,this.controlPoints.outer.y, 2, 0, Math.PI*2); |
| 1272 | // ctx.fill(); |
| 1273 | |
| 1274 | // ctx.moveTo(this.controlPoints.inner.x,this.controlPoints.inner.y); |
| 1275 | // ctx.lineTo(this.x, this.y); |
| 1276 | // ctx.lineTo(this.controlPoints.outer.x,this.controlPoints.outer.y); |
| 1277 | // ctx.stroke(); |
| 1278 | |
| 1279 | // ctx.restore(); |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | } |
| 1284 | }); |
| 1285 | |
| 1286 | Chart.Arc = Chart.Element.extend({ |
| 1287 | inRange : function(chartX,chartY){ |
| 1288 | |
| 1289 | var pointRelativePosition = helpers.getAngleFromPoint(this, { |
| 1290 | x: chartX, |
| 1291 | y: chartY |
| 1292 | }); |
| 1293 | |
| 1294 | // Normalize all angles to 0 - 2*PI (0 - 360°) |
| 1295 | var pointRelativeAngle = pointRelativePosition.angle % (Math.PI * 2), |
| 1296 | startAngle = (Math.PI * 2 + this.startAngle) % (Math.PI * 2), |
| 1297 | endAngle = (Math.PI * 2 + this.endAngle) % (Math.PI * 2) || 360; |
| 1298 | |
| 1299 | // Calculate wether the pointRelativeAngle is between the start and the end angle |
| 1300 | var betweenAngles = (endAngle < startAngle) ? |
| 1301 | pointRelativeAngle <= endAngle || pointRelativeAngle >= startAngle: |
| 1302 | pointRelativeAngle >= startAngle && pointRelativeAngle <= endAngle; |
| 1303 | |
| 1304 | //Check if within the range of the open/close angle |
| 1305 | var withinRadius = (pointRelativePosition.distance >= this.innerRadius && pointRelativePosition.distance <= this.outerRadius); |
| 1306 | |
| 1307 | return (betweenAngles && withinRadius); |
| 1308 | //Ensure within the outside of the arc centre, but inside arc outer |
| 1309 | }, |
| 1310 | tooltipPosition : function(){ |
| 1311 | var centreAngle = this.startAngle + ((this.endAngle - this.startAngle) / 2), |
| 1312 | rangeFromCentre = (this.outerRadius - this.innerRadius) / 2 + this.innerRadius; |
| 1313 | return { |
| 1314 | x : this.x + (Math.cos(centreAngle) * rangeFromCentre), |
| 1315 | y : this.y + (Math.sin(centreAngle) * rangeFromCentre) |
| 1316 | }; |
| 1317 | }, |
| 1318 | draw : function(animationPercent){ |
| 1319 | |
| 1320 | var easingDecimal = animationPercent || 1; |
| 1321 | |
| 1322 | var ctx = this.ctx; |
| 1323 | |
| 1324 | ctx.beginPath(); |
| 1325 | |
| 1326 | ctx.arc(this.x, this.y, this.outerRadius < 0 ? 0 : this.outerRadius, this.startAngle, this.endAngle); |
| 1327 | |
| 1328 | ctx.arc(this.x, this.y, this.innerRadius < 0 ? 0 : this.innerRadius, this.endAngle, this.startAngle, true); |
| 1329 | |
| 1330 | ctx.closePath(); |
| 1331 | ctx.strokeStyle = this.strokeColor; |
| 1332 | ctx.lineWidth = this.strokeWidth; |
| 1333 | |
| 1334 | ctx.fillStyle = this.fillColor; |
| 1335 | |
| 1336 | ctx.fill(); |
| 1337 | ctx.lineJoin = 'bevel'; |
| 1338 | |
| 1339 | if (this.showStroke){ |
| 1340 | ctx.stroke(); |
| 1341 | } |
| 1342 | } |
| 1343 | }); |
| 1344 | |
| 1345 | Chart.Rectangle = Chart.Element.extend({ |
| 1346 | draw : function(){ |
| 1347 | var ctx = this.ctx, |
| 1348 | halfWidth = this.width/2, |
| 1349 | leftX = this.x - halfWidth, |
| 1350 | rightX = this.x + halfWidth, |
| 1351 | top = this.base - (this.base - this.y), |
| 1352 | halfStroke = this.strokeWidth / 2; |
| 1353 | |
| 1354 | // Canvas doesn't allow us to stroke inside the width so we can |
| 1355 | // adjust the sizes to fit if we're setting a stroke on the line |
| 1356 | if (this.showStroke){ |
| 1357 | leftX += halfStroke; |
| 1358 | rightX -= halfStroke; |
| 1359 | top += halfStroke; |
| 1360 | } |
| 1361 | |
| 1362 | ctx.beginPath(); |
| 1363 | |
| 1364 | ctx.fillStyle = this.fillColor; |
| 1365 | ctx.strokeStyle = this.strokeColor; |
| 1366 | ctx.lineWidth = this.strokeWidth; |
| 1367 | |
| 1368 | // It'd be nice to keep this class totally generic to any rectangle |
| 1369 | // and simply specify which border to miss out. |
| 1370 | ctx.moveTo(leftX, this.base); |
| 1371 | ctx.lineTo(leftX, top); |
| 1372 | ctx.lineTo(rightX, top); |
| 1373 | ctx.lineTo(rightX, this.base); |
| 1374 | ctx.fill(); |
| 1375 | if (this.showStroke){ |
| 1376 | ctx.stroke(); |
| 1377 | } |
| 1378 | }, |
| 1379 | height : function(){ |
| 1380 | return this.base - this.y; |
| 1381 | }, |
| 1382 | inRange : function(chartX,chartY){ |
| 1383 | return (chartX >= this.x - this.width/2 && chartX <= this.x + this.width/2) && (chartY >= this.y && chartY <= this.base); |
| 1384 | } |
| 1385 | }); |
| 1386 | |
| 1387 | Chart.Animation = Chart.Element.extend({ |
| 1388 | currentStep: null, // the current animation step |
| 1389 | numSteps: 60, // default number of steps |
| 1390 | easing: "", // the easing to use for this animation |
| 1391 | render: null, // render function used by the animation service |
| 1392 | |
| 1393 | onAnimationProgress: null, // user specified callback to fire on each step of the animation |
| 1394 | onAnimationComplete: null, // user specified callback to fire when the animation finishes |
| 1395 | }); |
| 1396 | |
| 1397 | Chart.Tooltip = Chart.Element.extend({ |
| 1398 | draw : function(){ |
| 1399 | |
| 1400 | var ctx = this.chart.ctx; |
| 1401 | |
| 1402 | ctx.font = fontString(this.fontSize,this.fontStyle,this.fontFamily); |
| 1403 | |
| 1404 | this.xAlign = "center"; |
| 1405 | this.yAlign = "above"; |
| 1406 | |
| 1407 | //Distance between the actual element.y position and the start of the tooltip caret |
| 1408 | var caretPadding = this.caretPadding = 2; |
| 1409 | |
| 1410 | var tooltipWidth = ctx.measureText(this.text).width + 2*this.xPadding, |
| 1411 | tooltipRectHeight = this.fontSize + 2*this.yPadding, |
| 1412 | tooltipHeight = tooltipRectHeight + this.caretHeight + caretPadding; |
| 1413 | |
| 1414 | if (this.x + tooltipWidth/2 >this.chart.width){ |
| 1415 | this.xAlign = "left"; |
| 1416 | } else if (this.x - tooltipWidth/2 < 0){ |
| 1417 | this.xAlign = "right"; |
| 1418 | } |
| 1419 | |
| 1420 | if (this.y - tooltipHeight < 0){ |
| 1421 | this.yAlign = "below"; |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | var tooltipX = this.x - tooltipWidth/2, |
| 1426 | tooltipY = this.y - tooltipHeight; |
| 1427 | |
| 1428 | ctx.fillStyle = this.fillColor; |
| 1429 | |
| 1430 | // Custom Tooltips |
| 1431 | if(this.custom){ |
| 1432 | this.custom(this); |
| 1433 | } |
| 1434 | else{ |
| 1435 | switch(this.yAlign) |
| 1436 | { |
| 1437 | case "above": |
| 1438 | //Draw a caret above the x/y |
| 1439 | ctx.beginPath(); |
| 1440 | ctx.moveTo(this.x,this.y - caretPadding); |
| 1441 | ctx.lineTo(this.x + this.caretHeight, this.y - (caretPadding + this.caretHeight)); |
| 1442 | ctx.lineTo(this.x - this.caretHeight, this.y - (caretPadding + this.caretHeight)); |
| 1443 | ctx.closePath(); |
| 1444 | ctx.fill(); |
| 1445 | break; |
| 1446 | case "below": |
| 1447 | tooltipY = this.y + caretPadding + this.caretHeight; |
| 1448 | //Draw a caret below the x/y |
| 1449 | ctx.beginPath(); |
| 1450 | ctx.moveTo(this.x, this.y + caretPadding); |
| 1451 | ctx.lineTo(this.x + this.caretHeight, this.y + caretPadding + this.caretHeight); |
| 1452 | ctx.lineTo(this.x - this.caretHeight, this.y + caretPadding + this.caretHeight); |
| 1453 | ctx.closePath(); |
| 1454 | ctx.fill(); |
| 1455 | break; |
| 1456 | } |
| 1457 | |
| 1458 | switch(this.xAlign) |
| 1459 | { |
| 1460 | case "left": |
| 1461 | tooltipX = this.x - tooltipWidth + (this.cornerRadius + this.caretHeight); |
| 1462 | break; |
| 1463 | case "right": |
| 1464 | tooltipX = this.x - (this.cornerRadius + this.caretHeight); |
| 1465 | break; |
| 1466 | } |
| 1467 | |
| 1468 | drawRoundedRectangle(ctx,tooltipX,tooltipY,tooltipWidth,tooltipRectHeight,this.cornerRadius); |
| 1469 | |
| 1470 | ctx.fill(); |
| 1471 | |
| 1472 | ctx.fillStyle = this.textColor; |
| 1473 | ctx.textAlign = "center"; |
| 1474 | ctx.textBaseline = "middle"; |
| 1475 | ctx.fillText(this.text, tooltipX + tooltipWidth/2, tooltipY + tooltipRectHeight/2); |
| 1476 | } |
| 1477 | } |
| 1478 | }); |
| 1479 | |
| 1480 | Chart.MultiTooltip = Chart.Element.extend({ |
| 1481 | initialize : function(){ |
| 1482 | this.font = fontString(this.fontSize,this.fontStyle,this.fontFamily); |
| 1483 | |
| 1484 | this.titleFont = fontString(this.titleFontSize,this.titleFontStyle,this.titleFontFamily); |
| 1485 | |
| 1486 | this.titleHeight = this.title ? this.titleFontSize * 1.5 : 0; |
| 1487 | this.height = (this.labels.length * this.fontSize) + ((this.labels.length-1) * (this.fontSize/2)) + (this.yPadding*2) + this.titleHeight; |
| 1488 | |
| 1489 | this.ctx.font = this.titleFont; |
| 1490 | |
| 1491 | var titleWidth = this.ctx.measureText(this.title).width, |
| 1492 | //Label has a legend square as well so account for this. |
| 1493 | labelWidth = longestText(this.ctx,this.font,this.labels) + this.fontSize + 3, |
| 1494 | longestTextWidth = max([labelWidth,titleWidth]); |
| 1495 | |
| 1496 | this.width = longestTextWidth + (this.xPadding*2); |
| 1497 | |
| 1498 | |
| 1499 | var halfHeight = this.height/2; |
| 1500 | |
| 1501 | //Check to ensure the height will fit on the canvas |
| 1502 | if (this.y - halfHeight < 0 ){ |
| 1503 | this.y = halfHeight; |
| 1504 | } else if (this.y + halfHeight > this.chart.height){ |
| 1505 | this.y = this.chart.height - halfHeight; |
| 1506 | } |
| 1507 | |
| 1508 | //Decide whether to align left or right based on position on canvas |
| 1509 | if (this.x > this.chart.width/2){ |
| 1510 | this.x -= this.xOffset + this.width; |
| 1511 | } else { |
| 1512 | this.x += this.xOffset; |
| 1513 | } |
| 1514 | |
| 1515 | |
| 1516 | }, |
| 1517 | getLineHeight : function(index){ |
| 1518 | var baseLineHeight = this.y - (this.height/2) + this.yPadding, |
| 1519 | afterTitleIndex = index-1; |
| 1520 | |
| 1521 | //If the index is zero, we're getting the title |
| 1522 | if (index === 0){ |
| 1523 | return baseLineHeight + this.titleHeight / 3; |
| 1524 | } else{ |
| 1525 | return baseLineHeight + ((this.fontSize * 1.5 * afterTitleIndex) + this.fontSize / 2) + this.titleHeight; |
| 1526 | } |
| 1527 | |
| 1528 | }, |
| 1529 | draw : function(){ |
| 1530 | // Custom Tooltips |
| 1531 | if(this.custom){ |
| 1532 | this.custom(this); |
| 1533 | } |
| 1534 | else{ |
| 1535 | drawRoundedRectangle(this.ctx,this.x,this.y - this.height/2,this.width,this.height,this.cornerRadius); |
| 1536 | var ctx = this.ctx; |
| 1537 | ctx.fillStyle = this.fillColor; |
| 1538 | ctx.fill(); |
| 1539 | ctx.closePath(); |
| 1540 | |
| 1541 | ctx.textAlign = "left"; |
| 1542 | ctx.textBaseline = "middle"; |
| 1543 | ctx.fillStyle = this.titleTextColor; |
| 1544 | ctx.font = this.titleFont; |
| 1545 | |
| 1546 | ctx.fillText(this.title,this.x + this.xPadding, this.getLineHeight(0)); |
| 1547 | |
| 1548 | ctx.font = this.font; |
| 1549 | helpers.each(this.labels,function(label,index){ |
| 1550 | ctx.fillStyle = this.textColor; |
| 1551 | ctx.fillText(label,this.x + this.xPadding + this.fontSize + 3, this.getLineHeight(index + 1)); |
| 1552 | |
| 1553 | //A bit gnarly, but clearing this rectangle breaks when using explorercanvas (clears whole canvas) |
| 1554 | //ctx.clearRect(this.x + this.xPadding, this.getLineHeight(index + 1) - this.fontSize/2, this.fontSize, this.fontSize); |
| 1555 | //Instead we'll make a white filled block to put the legendColour palette over. |
| 1556 | |
| 1557 | ctx.fillStyle = this.legendColorBackground; |
| 1558 | ctx.fillRect(this.x + this.xPadding, this.getLineHeight(index + 1) - this.fontSize/2, this.fontSize, this.fontSize); |
| 1559 | |
| 1560 | ctx.fillStyle = this.legendColors[index].fill; |
| 1561 | ctx.fillRect(this.x + this.xPadding, this.getLineHeight(index + 1) - this.fontSize/2, this.fontSize, this.fontSize); |
| 1562 | |
| 1563 | |
| 1564 | },this); |
| 1565 | } |
| 1566 | } |
| 1567 | }); |
| 1568 | |
| 1569 | Chart.Scale = Chart.Element.extend({ |
| 1570 | initialize : function(){ |
| 1571 | this.fit(); |
| 1572 | }, |
| 1573 | buildYLabels : function(){ |
| 1574 | this.yLabels = []; |
| 1575 | |
| 1576 | var stepDecimalPlaces = getDecimalPlaces(this.stepValue); |
| 1577 | |
| 1578 | for (var i=0; i<=this.steps; i++){ |
| 1579 | this.yLabels.push(template(this.templateString,{value:(this.min + (i * this.stepValue)).toFixed(stepDecimalPlaces)})); |
| 1580 | } |
| 1581 | this.yLabelWidth = (this.display && this.showLabels) ? longestText(this.ctx,this.font,this.yLabels) + 10 : 0; |
| 1582 | }, |
| 1583 | addXLabel : function(label){ |
| 1584 | this.xLabels.push(label); |
| 1585 | this.valuesCount++; |
| 1586 | this.fit(); |
| 1587 | }, |
| 1588 | removeXLabel : function(){ |
| 1589 | this.xLabels.shift(); |
| 1590 | this.valuesCount--; |
| 1591 | this.fit(); |
| 1592 | }, |
| 1593 | // Fitting loop to rotate x Labels and figure out what fits there, and also calculate how many Y steps to use |
| 1594 | fit: function(){ |
| 1595 | // First we need the width of the yLabels, assuming the xLabels aren't rotated |
| 1596 | |
| 1597 | // To do that we need the base line at the top and base of the chart, assuming there is no x label rotation |
| 1598 | this.startPoint = (this.display) ? this.fontSize : 0; |
| 1599 | this.endPoint = (this.display) ? this.height - (this.fontSize * 1.5) - 5 : this.height; // -5 to pad labels |
| 1600 | |
| 1601 | // Apply padding settings to the start and end point. |
| 1602 | this.startPoint += this.padding; |
| 1603 | this.endPoint -= this.padding; |
| 1604 | |
| 1605 | // Cache the starting endpoint, excluding the space for x labels |
| 1606 | var cachedEndPoint = this.endPoint; |
| 1607 | |
| 1608 | // Cache the starting height, so can determine if we need to recalculate the scale yAxis |
| 1609 | var cachedHeight = this.endPoint - this.startPoint, |
| 1610 | cachedYLabelWidth; |
| 1611 | |
| 1612 | // Build the current yLabels so we have an idea of what size they'll be to start |
| 1613 | /* |
| 1614 | * This sets what is returned from calculateScaleRange as static properties of this class: |
| 1615 | * |
| 1616 | this.steps; |
| 1617 | this.stepValue; |
| 1618 | this.min; |
| 1619 | this.max; |
| 1620 | * |
| 1621 | */ |
| 1622 | this.calculateYRange(cachedHeight); |
| 1623 | |
| 1624 | // With these properties set we can now build the array of yLabels |
| 1625 | // and also the width of the largest yLabel |
| 1626 | this.buildYLabels(); |
| 1627 | |
| 1628 | this.calculateXLabelRotation(); |
| 1629 | |
| 1630 | while((cachedHeight > this.endPoint - this.startPoint)){ |
| 1631 | cachedHeight = this.endPoint - this.startPoint; |
| 1632 | cachedYLabelWidth = this.yLabelWidth; |
| 1633 | |
| 1634 | this.calculateYRange(cachedHeight); |
| 1635 | this.buildYLabels(); |
| 1636 | |
| 1637 | // Only go through the xLabel loop again if the yLabel width has changed |
| 1638 | if (cachedYLabelWidth < this.yLabelWidth){ |
| 1639 | this.endPoint = cachedEndPoint; |
| 1640 | this.calculateXLabelRotation(); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | }, |
| 1645 | calculateXLabelRotation : function(){ |
| 1646 | //Get the width of each grid by calculating the difference |
| 1647 | //between x offsets between 0 and 1. |
| 1648 | |
| 1649 | this.ctx.font = this.font; |
| 1650 | |
| 1651 | var firstWidth = this.ctx.measureText(this.xLabels[0]).width, |
| 1652 | lastWidth = this.ctx.measureText(this.xLabels[this.xLabels.length - 1]).width, |
| 1653 | firstRotated, |
| 1654 | lastRotated; |
| 1655 | |
| 1656 | |
| 1657 | this.xScalePaddingRight = lastWidth/2 + 3; |
| 1658 | this.xScalePaddingLeft = (firstWidth/2 > this.yLabelWidth) ? firstWidth/2 : this.yLabelWidth; |
| 1659 | |
| 1660 | this.xLabelRotation = 0; |
| 1661 | if (this.display){ |
| 1662 | var originalLabelWidth = longestText(this.ctx,this.font,this.xLabels), |
| 1663 | cosRotation, |
| 1664 | firstRotatedWidth; |
| 1665 | this.xLabelWidth = originalLabelWidth; |
| 1666 | //Allow 3 pixels x2 padding either side for label readability |
| 1667 | var xGridWidth = Math.floor(this.calculateX(1) - this.calculateX(0)) - 6; |
| 1668 | |
| 1669 | //Max label rotate should be 90 - also act as a loop counter |
| 1670 | while ((this.xLabelWidth > xGridWidth && this.xLabelRotation === 0) || (this.xLabelWidth > xGridWidth && this.xLabelRotation <= 90 && this.xLabelRotation > 0)){ |
| 1671 | cosRotation = Math.cos(toRadians(this.xLabelRotation)); |
| 1672 | |
| 1673 | firstRotated = cosRotation * firstWidth; |
| 1674 | lastRotated = cosRotation * lastWidth; |
| 1675 | |
| 1676 | // We're right aligning the text now. |
| 1677 | if (firstRotated + this.fontSize / 2 > this.yLabelWidth){ |
| 1678 | this.xScalePaddingLeft = firstRotated + this.fontSize / 2; |
| 1679 | } |
| 1680 | this.xScalePaddingRight = this.fontSize/2; |
| 1681 | |
| 1682 | |
| 1683 | this.xLabelRotation++; |
| 1684 | this.xLabelWidth = cosRotation * originalLabelWidth; |
| 1685 | |
| 1686 | } |
| 1687 | if (this.xLabelRotation > 0){ |
| 1688 | this.endPoint -= Math.sin(toRadians(this.xLabelRotation))*originalLabelWidth + 3; |
| 1689 | } |
| 1690 | } |
| 1691 | else{ |
| 1692 | this.xLabelWidth = 0; |
| 1693 | this.xScalePaddingRight = this.padding; |
| 1694 | this.xScalePaddingLeft = this.padding; |
| 1695 | } |
| 1696 | |
| 1697 | }, |
| 1698 | // Needs to be overidden in each Chart type |
| 1699 | // Otherwise we need to pass all the data into the scale class |
| 1700 | calculateYRange: noop, |
| 1701 | drawingArea: function(){ |
| 1702 | return this.startPoint - this.endPoint; |
| 1703 | }, |
| 1704 | calculateY : function(value){ |
| 1705 | var scalingFactor = this.drawingArea() / (this.min - this.max); |
| 1706 | return this.endPoint - (scalingFactor * (value - this.min)); |
| 1707 | }, |
| 1708 | calculateX : function(index){ |
| 1709 | var isRotated = (this.xLabelRotation > 0), |
| 1710 | // innerWidth = (this.offsetGridLines) ? this.width - offsetLeft - this.padding : this.width - (offsetLeft + halfLabelWidth * 2) - this.padding, |
| 1711 | innerWidth = this.width - (this.xScalePaddingLeft + this.xScalePaddingRight), |
| 1712 | valueWidth = innerWidth/Math.max((this.valuesCount - ((this.offsetGridLines) ? 0 : 1)), 1), |
| 1713 | valueOffset = (valueWidth * index) + this.xScalePaddingLeft; |
| 1714 | |
| 1715 | if (this.offsetGridLines){ |
| 1716 | valueOffset += (valueWidth/2); |
| 1717 | } |
| 1718 | |
| 1719 | return Math.round(valueOffset); |
| 1720 | }, |
| 1721 | update : function(newProps){ |
| 1722 | helpers.extend(this, newProps); |
| 1723 | this.fit(); |
| 1724 | }, |
| 1725 | draw : function(){ |
| 1726 | var ctx = this.ctx, |
| 1727 | yLabelGap = (this.endPoint - this.startPoint) / this.steps, |
| 1728 | xStart = Math.round(this.xScalePaddingLeft); |
| 1729 | if (this.display){ |
| 1730 | ctx.fillStyle = this.textColor; |
| 1731 | ctx.font = this.font; |
| 1732 | each(this.yLabels,function(labelString,index){ |
| 1733 | var yLabelCenter = this.endPoint - (yLabelGap * index), |
| 1734 | linePositionY = Math.round(yLabelCenter), |
| 1735 | drawHorizontalLine = this.showHorizontalLines; |
| 1736 | |
| 1737 | ctx.textAlign = "right"; |
| 1738 | ctx.textBaseline = "middle"; |
| 1739 | if (this.showLabels){ |
| 1740 | ctx.fillText(labelString,xStart - 10,yLabelCenter); |
| 1741 | } |
| 1742 | |
| 1743 | // This is X axis, so draw it |
| 1744 | if (index === 0 && !drawHorizontalLine){ |
| 1745 | drawHorizontalLine = true; |
| 1746 | } |
| 1747 | |
| 1748 | if (drawHorizontalLine){ |
| 1749 | ctx.beginPath(); |
| 1750 | } |
| 1751 | |
| 1752 | if (index > 0){ |
| 1753 | // This is a grid line in the centre, so drop that |
| 1754 | ctx.lineWidth = this.gridLineWidth; |
| 1755 | ctx.strokeStyle = this.gridLineColor; |
| 1756 | } else { |
| 1757 | // This is the first line on the scale |
| 1758 | ctx.lineWidth = this.lineWidth; |
| 1759 | ctx.strokeStyle = this.lineColor; |
| 1760 | } |
| 1761 | |
| 1762 | linePositionY += helpers.aliasPixel(ctx.lineWidth); |
| 1763 | |
| 1764 | if(drawHorizontalLine){ |
| 1765 | ctx.moveTo(xStart, linePositionY); |
| 1766 | ctx.lineTo(this.width, linePositionY); |
| 1767 | ctx.stroke(); |
| 1768 | ctx.closePath(); |
| 1769 | } |
| 1770 | |
| 1771 | ctx.lineWidth = this.lineWidth; |
| 1772 | ctx.strokeStyle = this.lineColor; |
| 1773 | ctx.beginPath(); |
| 1774 | ctx.moveTo(xStart - 5, linePositionY); |
| 1775 | ctx.lineTo(xStart, linePositionY); |
| 1776 | ctx.stroke(); |
| 1777 | ctx.closePath(); |
| 1778 | |
| 1779 | },this); |
| 1780 | |
| 1781 | each(this.xLabels,function(label,index){ |
| 1782 | var xPos = this.calculateX(index) + aliasPixel(this.lineWidth), |
| 1783 | // Check to see if line/bar here and decide where to place the line |
| 1784 | linePos = this.calculateX(index - (this.offsetGridLines ? 0.5 : 0)) + aliasPixel(this.lineWidth), |
| 1785 | isRotated = (this.xLabelRotation > 0), |
| 1786 | drawVerticalLine = this.showVerticalLines; |
| 1787 | |
| 1788 | // This is Y axis, so draw it |
| 1789 | if (index === 0 && !drawVerticalLine){ |
| 1790 | drawVerticalLine = true; |
| 1791 | } |
| 1792 | |
| 1793 | if (drawVerticalLine){ |
| 1794 | ctx.beginPath(); |
| 1795 | } |
| 1796 | |
| 1797 | if (index > 0){ |
| 1798 | // This is a grid line in the centre, so drop that |
| 1799 | ctx.lineWidth = this.gridLineWidth; |
| 1800 | ctx.strokeStyle = this.gridLineColor; |
| 1801 | } else { |
| 1802 | // This is the first line on the scale |
| 1803 | ctx.lineWidth = this.lineWidth; |
| 1804 | ctx.strokeStyle = this.lineColor; |
| 1805 | } |
| 1806 | |
| 1807 | if (drawVerticalLine){ |
| 1808 | ctx.moveTo(linePos,this.endPoint); |
| 1809 | ctx.lineTo(linePos,this.startPoint - 3); |
| 1810 | ctx.stroke(); |
| 1811 | ctx.closePath(); |
| 1812 | } |
| 1813 | |
| 1814 | |
| 1815 | ctx.lineWidth = this.lineWidth; |
| 1816 | ctx.strokeStyle = this.lineColor; |
| 1817 | |
| 1818 | |
| 1819 | // Small lines at the bottom of the base grid line |
| 1820 | ctx.beginPath(); |
| 1821 | ctx.moveTo(linePos,this.endPoint); |
| 1822 | ctx.lineTo(linePos,this.endPoint + 5); |
| 1823 | ctx.stroke(); |
| 1824 | ctx.closePath(); |
| 1825 | |
| 1826 | ctx.save(); |
| 1827 | ctx.translate(xPos,(isRotated) ? this.endPoint + 12 : this.endPoint + 8); |
| 1828 | ctx.rotate(toRadians(this.xLabelRotation)*-1); |
| 1829 | ctx.font = this.font; |
| 1830 | ctx.textAlign = (isRotated) ? "right" : "center"; |
| 1831 | ctx.textBaseline = (isRotated) ? "middle" : "top"; |
| 1832 | ctx.fillText(label, 0, 0); |
| 1833 | ctx.restore(); |
| 1834 | },this); |
| 1835 | |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | }); |
| 1840 | |
| 1841 | Chart.RadialScale = Chart.Element.extend({ |
| 1842 | initialize: function(){ |
| 1843 | this.size = min([this.height, this.width]); |
| 1844 | this.drawingArea = (this.display) ? (this.size/2) - (this.fontSize/2 + this.backdropPaddingY) : (this.size/2); |
| 1845 | }, |
| 1846 | calculateCenterOffset: function(value){ |
| 1847 | // Take into account half font size + the yPadding of the top value |
| 1848 | var scalingFactor = this.drawingArea / (this.max - this.min); |
| 1849 | |
| 1850 | return (value - this.min) * scalingFactor; |
| 1851 | }, |
| 1852 | update : function(){ |
| 1853 | if (!this.lineArc){ |
| 1854 | this.setScaleSize(); |
| 1855 | } else { |
| 1856 | this.drawingArea = (this.display) ? (this.size/2) - (this.fontSize/2 + this.backdropPaddingY) : (this.size/2); |
| 1857 | } |
| 1858 | this.buildYLabels(); |
| 1859 | }, |
| 1860 | buildYLabels: function(){ |
| 1861 | this.yLabels = []; |
| 1862 | |
| 1863 | var stepDecimalPlaces = getDecimalPlaces(this.stepValue); |
| 1864 | |
| 1865 | for (var i=0; i<=this.steps; i++){ |
| 1866 | this.yLabels.push(template(this.templateString,{value:(this.min + (i * this.stepValue)).toFixed(stepDecimalPlaces)})); |
| 1867 | } |
| 1868 | }, |
| 1869 | getCircumference : function(){ |
| 1870 | return ((Math.PI*2) / this.valuesCount); |
| 1871 | }, |
| 1872 | setScaleSize: function(){ |
| 1873 | /* |
| 1874 | * Right, this is really confusing and there is a lot of maths going on here |
| 1875 | * The gist of the problem is here: https://gist.github.com/nnnick/696cc9c55f4b0beb8fe9 |
| 1876 | * |
| 1877 | * Reaction: https://dl.dropboxusercontent.com/u/34601363/toomuchscience.gif |
| 1878 | * |
| 1879 | * Solution: |
| 1880 | * |
| 1881 | * We assume the radius of the polygon is half the size of the canvas at first |
| 1882 | * at each index we check if the text overlaps. |
| 1883 | * |
| 1884 | * Where it does, we store that angle and that index. |
| 1885 | * |
| 1886 | * After finding the largest index and angle we calculate how much we need to remove |
| 1887 | * from the shape radius to move the point inwards by that x. |
| 1888 | * |
| 1889 | * We average the left and right distances to get the maximum shape radius that can fit in the box |
| 1890 | * along with labels. |
| 1891 | * |
| 1892 | * Once we have that, we can find the centre point for the chart, by taking the x text protrusion |
| 1893 | * on each side, removing that from the size, halving it and adding the left x protrusion width. |
| 1894 | * |
| 1895 | * This will mean we have a shape fitted to the canvas, as large as it can be with the labels |
| 1896 | * and position it in the most space efficient manner |
| 1897 | * |
| 1898 | * https://dl.dropboxusercontent.com/u/34601363/yeahscience.gif |
| 1899 | */ |
| 1900 | |
| 1901 | |
| 1902 | // Get maximum radius of the polygon. Either half the height (minus the text width) or half the width. |
| 1903 | // Use this to calculate the offset + change. - Make sure L/R protrusion is at least 0 to stop issues with centre points |
| 1904 | var largestPossibleRadius = min([(this.height/2 - this.pointLabelFontSize - 5), this.width/2]), |
| 1905 | pointPosition, |
| 1906 | i, |
| 1907 | textWidth, |
| 1908 | halfTextWidth, |
| 1909 | furthestRight = this.width, |
| 1910 | furthestRightIndex, |
| 1911 | furthestRightAngle, |
| 1912 | furthestLeft = 0, |
| 1913 | furthestLeftIndex, |
| 1914 | furthestLeftAngle, |
| 1915 | xProtrusionLeft, |
| 1916 | xProtrusionRight, |
| 1917 | radiusReductionRight, |
| 1918 | radiusReductionLeft, |
| 1919 | maxWidthRadius; |
| 1920 | this.ctx.font = fontString(this.pointLabelFontSize,this.pointLabelFontStyle,this.pointLabelFontFamily); |
| 1921 | for (i=0;i<this.valuesCount;i++){ |
| 1922 | // 5px to space the text slightly out - similar to what we do in the draw function. |
| 1923 | pointPosition = this.getPointPosition(i, largestPossibleRadius); |
| 1924 | textWidth = this.ctx.measureText(template(this.templateString, { value: this.labels[i] })).width + 5; |
| 1925 | if (i === 0 || i === this.valuesCount/2){ |
| 1926 | // If we're at index zero, or exactly the middle, we're at exactly the top/bottom |
| 1927 | // of the radar chart, so text will be aligned centrally, so we'll half it and compare |
| 1928 | // w/left and right text sizes |
| 1929 | halfTextWidth = textWidth/2; |
| 1930 | if (pointPosition.x + halfTextWidth > furthestRight) { |
| 1931 | furthestRight = pointPosition.x + halfTextWidth; |
| 1932 | furthestRightIndex = i; |
| 1933 | } |
| 1934 | if (pointPosition.x - halfTextWidth < furthestLeft) { |
| 1935 | furthestLeft = pointPosition.x - halfTextWidth; |
| 1936 | furthestLeftIndex = i; |
| 1937 | } |
| 1938 | } |
| 1939 | else if (i < this.valuesCount/2) { |
| 1940 | // Less than half the values means we'll left align the text |
| 1941 | if (pointPosition.x + textWidth > furthestRight) { |
| 1942 | furthestRight = pointPosition.x + textWidth; |
| 1943 | furthestRightIndex = i; |
| 1944 | } |
| 1945 | } |
| 1946 | else if (i > this.valuesCount/2){ |
| 1947 | // More than half the values means we'll right align the text |
| 1948 | if (pointPosition.x - textWidth < furthestLeft) { |
| 1949 | furthestLeft = pointPosition.x - textWidth; |
| 1950 | furthestLeftIndex = i; |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | xProtrusionLeft = furthestLeft; |
| 1956 | |
| 1957 | xProtrusionRight = Math.ceil(furthestRight - this.width); |
| 1958 | |
| 1959 | furthestRightAngle = this.getIndexAngle(furthestRightIndex); |
| 1960 | |
| 1961 | furthestLeftAngle = this.getIndexAngle(furthestLeftIndex); |
| 1962 | |
| 1963 | radiusReductionRight = xProtrusionRight / Math.sin(furthestRightAngle + Math.PI/2); |
| 1964 | |
| 1965 | radiusReductionLeft = xProtrusionLeft / Math.sin(furthestLeftAngle + Math.PI/2); |
| 1966 | |
| 1967 | // Ensure we actually need to reduce the size of the chart |
| 1968 | radiusReductionRight = (isNumber(radiusReductionRight)) ? radiusReductionRight : 0; |
| 1969 | radiusReductionLeft = (isNumber(radiusReductionLeft)) ? radiusReductionLeft : 0; |
| 1970 | |
| 1971 | this.drawingArea = largestPossibleRadius - (radiusReductionLeft + radiusReductionRight)/2; |
| 1972 | |
| 1973 | //this.drawingArea = min([maxWidthRadius, (this.height - (2 * (this.pointLabelFontSize + 5)))/2]) |
| 1974 | this.setCenterPoint(radiusReductionLeft, radiusReductionRight); |
| 1975 | |
| 1976 | }, |
| 1977 | setCenterPoint: function(leftMovement, rightMovement){ |
| 1978 | |
| 1979 | var maxRight = this.width - rightMovement - this.drawingArea, |
| 1980 | maxLeft = leftMovement + this.drawingArea; |
| 1981 | |
| 1982 | this.xCenter = (maxLeft + maxRight)/2; |
| 1983 | // Always vertically in the centre as the text height doesn't change |
| 1984 | this.yCenter = (this.height/2); |
| 1985 | }, |
| 1986 | |
| 1987 | getIndexAngle : function(index){ |
| 1988 | var angleMultiplier = (Math.PI * 2) / this.valuesCount; |
| 1989 | // Start from the top instead of right, so remove a quarter of the circle |
| 1990 | |
| 1991 | return index * angleMultiplier - (Math.PI/2); |
| 1992 | }, |
| 1993 | getPointPosition : function(index, distanceFromCenter){ |
| 1994 | var thisAngle = this.getIndexAngle(index); |
| 1995 | return { |
| 1996 | x : (Math.cos(thisAngle) * distanceFromCenter) + this.xCenter, |
| 1997 | y : (Math.sin(thisAngle) * distanceFromCenter) + this.yCenter |
| 1998 | }; |
| 1999 | }, |
| 2000 | draw: function(){ |
| 2001 | if (this.display){ |
| 2002 | var ctx = this.ctx; |
| 2003 | each(this.yLabels, function(label, index){ |
| 2004 | // Don't draw a centre value |
| 2005 | if (index > 0){ |
| 2006 | var yCenterOffset = index * (this.drawingArea/this.steps), |
| 2007 | yHeight = this.yCenter - yCenterOffset, |
| 2008 | pointPosition; |
| 2009 | |
| 2010 | // Draw circular lines around the scale |
| 2011 | if (this.lineWidth > 0){ |
| 2012 | ctx.strokeStyle = this.lineColor; |
| 2013 | ctx.lineWidth = this.lineWidth; |
| 2014 | |
| 2015 | if(this.lineArc){ |
| 2016 | ctx.beginPath(); |
| 2017 | ctx.arc(this.xCenter, this.yCenter, yCenterOffset, 0, Math.PI*2); |
| 2018 | ctx.closePath(); |
| 2019 | ctx.stroke(); |
| 2020 | } else{ |
| 2021 | ctx.beginPath(); |
| 2022 | for (var i=0;i<this.valuesCount;i++) |
| 2023 | { |
| 2024 | pointPosition = this.getPointPosition(i, this.calculateCenterOffset(this.min + (index * this.stepValue))); |
| 2025 | if (i === 0){ |
| 2026 | ctx.moveTo(pointPosition.x, pointPosition.y); |
| 2027 | } else { |
| 2028 | ctx.lineTo(pointPosition.x, pointPosition.y); |
| 2029 | } |
| 2030 | } |
| 2031 | ctx.closePath(); |
| 2032 | ctx.stroke(); |
| 2033 | } |
| 2034 | } |
| 2035 | if(this.showLabels){ |
| 2036 | ctx.font = fontString(this.fontSize,this.fontStyle,this.fontFamily); |
| 2037 | if (this.showLabelBackdrop){ |
| 2038 | var labelWidth = ctx.measureText(label).width; |
| 2039 | ctx.fillStyle = this.backdropColor; |
| 2040 | ctx.fillRect( |
| 2041 | this.xCenter - labelWidth/2 - this.backdropPaddingX, |
| 2042 | yHeight - this.fontSize/2 - this.backdropPaddingY, |
| 2043 | labelWidth + this.backdropPaddingX*2, |
| 2044 | this.fontSize + this.backdropPaddingY*2 |
| 2045 | ); |
| 2046 | } |
| 2047 | ctx.textAlign = 'center'; |
| 2048 | ctx.textBaseline = "middle"; |
| 2049 | ctx.fillStyle = this.fontColor; |
| 2050 | ctx.fillText(label, this.xCenter, yHeight); |
| 2051 | } |
| 2052 | } |
| 2053 | }, this); |
| 2054 | |
| 2055 | if (!this.lineArc){ |
| 2056 | ctx.lineWidth = this.angleLineWidth; |
| 2057 | ctx.strokeStyle = this.angleLineColor; |
| 2058 | for (var i = this.valuesCount - 1; i >= 0; i--) { |
| 2059 | var centerOffset = null, outerPosition = null; |
| 2060 | |
| 2061 | if (this.angleLineWidth > 0){ |
| 2062 | centerOffset = this.calculateCenterOffset(this.max); |
| 2063 | outerPosition = this.getPointPosition(i, centerOffset); |
| 2064 | ctx.beginPath(); |
| 2065 | ctx.moveTo(this.xCenter, this.yCenter); |
| 2066 | ctx.lineTo(outerPosition.x, outerPosition.y); |
| 2067 | ctx.stroke(); |
| 2068 | ctx.closePath(); |
| 2069 | } |
| 2070 | |
| 2071 | if (this.backgroundColors && this.backgroundColors.length == this.valuesCount) { |
| 2072 | if (centerOffset == null) |
| 2073 | centerOffset = this.calculateCenterOffset(this.max); |
| 2074 | |
| 2075 | if (outerPosition == null) |
| 2076 | outerPosition = this.getPointPosition(i, centerOffset); |
| 2077 | |
| 2078 | var previousOuterPosition = this.getPointPosition(i === 0 ? this.valuesCount - 1 : i - 1, centerOffset); |
| 2079 | var nextOuterPosition = this.getPointPosition(i === this.valuesCount - 1 ? 0 : i + 1, centerOffset); |
| 2080 | |
| 2081 | var previousOuterHalfway = { x: (previousOuterPosition.x + outerPosition.x) / 2, y: (previousOuterPosition.y + outerPosition.y) / 2 }; |
| 2082 | var nextOuterHalfway = { x: (outerPosition.x + nextOuterPosition.x) / 2, y: (outerPosition.y + nextOuterPosition.y) / 2 }; |
| 2083 | |
| 2084 | ctx.beginPath(); |
| 2085 | ctx.moveTo(this.xCenter, this.yCenter); |
| 2086 | ctx.lineTo(previousOuterHalfway.x, previousOuterHalfway.y); |
| 2087 | ctx.lineTo(outerPosition.x, outerPosition.y); |
| 2088 | ctx.lineTo(nextOuterHalfway.x, nextOuterHalfway.y); |
| 2089 | ctx.fillStyle = this.backgroundColors[i]; |
| 2090 | ctx.fill(); |
| 2091 | ctx.closePath(); |
| 2092 | } |
| 2093 | // Extra 3px out for some label spacing |
| 2094 | var pointLabelPosition = this.getPointPosition(i, this.calculateCenterOffset(this.max) + 5); |
| 2095 | ctx.font = fontString(this.pointLabelFontSize,this.pointLabelFontStyle,this.pointLabelFontFamily); |
| 2096 | ctx.fillStyle = this.pointLabelFontColor; |
| 2097 | |
| 2098 | var labelsCount = this.labels.length, |
| 2099 | halfLabelsCount = this.labels.length/2, |
| 2100 | quarterLabelsCount = halfLabelsCount/2, |
| 2101 | upperHalf = (i < quarterLabelsCount || i > labelsCount - quarterLabelsCount), |
| 2102 | exactQuarter = (i === quarterLabelsCount || i === labelsCount - quarterLabelsCount); |
| 2103 | if (i === 0){ |
| 2104 | ctx.textAlign = 'center'; |
| 2105 | } else if(i === halfLabelsCount){ |
| 2106 | ctx.textAlign = 'center'; |
| 2107 | } else if (i < halfLabelsCount){ |
| 2108 | ctx.textAlign = 'left'; |
| 2109 | } else { |
| 2110 | ctx.textAlign = 'right'; |
| 2111 | } |
| 2112 | |
| 2113 | // Set the correct text baseline based on outer positioning |
| 2114 | if (exactQuarter){ |
| 2115 | ctx.textBaseline = 'middle'; |
| 2116 | } else if (upperHalf){ |
| 2117 | ctx.textBaseline = 'bottom'; |
| 2118 | } else { |
| 2119 | ctx.textBaseline = 'top'; |
| 2120 | } |
| 2121 | |
| 2122 | ctx.fillText(this.labels[i], pointLabelPosition.x, pointLabelPosition.y); |
| 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | }); |
| 2128 | |
| 2129 | Chart.animationService = { |
| 2130 | frameDuration: 17, |
| 2131 | animations: [], |
| 2132 | dropFrames: 0, |
| 2133 | addAnimation: function(chartInstance, animationObject) { |
| 2134 | for (var index = 0; index < this.animations.length; ++ index){ |
| 2135 | if (this.animations[index].chartInstance === chartInstance){ |
| 2136 | // replacing an in progress animation |
| 2137 | this.animations[index].animationObject = animationObject; |
| 2138 | return; |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | this.animations.push({ |
| 2143 | chartInstance: chartInstance, |
| 2144 | animationObject: animationObject |
| 2145 | }); |
| 2146 | |
| 2147 | // If there are no animations queued, manually kickstart a digest, for lack of a better word |
| 2148 | if (this.animations.length == 1) { |
| 2149 | helpers.requestAnimFrame.call(window, this.digestWrapper); |
| 2150 | } |
| 2151 | }, |
| 2152 | // Cancel the animation for a given chart instance |
| 2153 | cancelAnimation: function(chartInstance) { |
| 2154 | var index = helpers.findNextWhere(this.animations, function(animationWrapper) { |
| 2155 | return animationWrapper.chartInstance === chartInstance; |
| 2156 | }); |
| 2157 | |
| 2158 | if (index) |
| 2159 | { |
| 2160 | this.animations.splice(index, 1); |
| 2161 | } |
| 2162 | }, |
| 2163 | // calls startDigest with the proper context |
| 2164 | digestWrapper: function() { |
| 2165 | Chart.animationService.startDigest.call(Chart.animationService); |
| 2166 | }, |
| 2167 | startDigest: function() { |
| 2168 | |
| 2169 | var startTime = Date.now(); |
| 2170 | var framesToDrop = 0; |
| 2171 | |
| 2172 | if(this.dropFrames > 1){ |
| 2173 | framesToDrop = Math.floor(this.dropFrames); |
| 2174 | this.dropFrames -= framesToDrop; |
| 2175 | } |
| 2176 | |
| 2177 | for (var i = 0; i < this.animations.length; i++) { |
| 2178 | |
| 2179 | if (this.animations[i].animationObject.currentStep === null){ |
| 2180 | this.animations[i].animationObject.currentStep = 0; |
| 2181 | } |
| 2182 | |
| 2183 | this.animations[i].animationObject.currentStep += 1 + framesToDrop; |
| 2184 | if(this.animations[i].animationObject.currentStep > this.animations[i].animationObject.numSteps){ |
| 2185 | this.animations[i].animationObject.currentStep = this.animations[i].animationObject.numSteps; |
| 2186 | } |
| 2187 | |
| 2188 | this.animations[i].animationObject.render(this.animations[i].chartInstance, this.animations[i].animationObject); |
| 2189 | |
| 2190 | // Check if executed the last frame. |
| 2191 | if (this.animations[i].animationObject.currentStep == this.animations[i].animationObject.numSteps){ |
| 2192 | // Call onAnimationComplete |
| 2193 | this.animations[i].animationObject.onAnimationComplete.call(this.animations[i].chartInstance); |
| 2194 | // Remove the animation. |
| 2195 | this.animations.splice(i, 1); |
| 2196 | // Keep the index in place to offset the splice |
| 2197 | i--; |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | var endTime = Date.now(); |
| 2202 | var delay = endTime - startTime - this.frameDuration; |
| 2203 | var frameDelay = delay / this.frameDuration; |
| 2204 | |
| 2205 | if(frameDelay > 1){ |
| 2206 | this.dropFrames += frameDelay; |
| 2207 | } |
| 2208 | |
| 2209 | // Do we have more stuff to animate? |
| 2210 | if (this.animations.length > 0){ |
| 2211 | helpers.requestAnimFrame.call(window, this.digestWrapper); |
| 2212 | } |
| 2213 | } |
| 2214 | }; |
| 2215 | |
| 2216 | // Attach global event to resize each chart instance when the browser resizes |
| 2217 | helpers.addEvent(window, "resize", (function(){ |
| 2218 | // Basic debounce of resize function so it doesn't hurt performance when resizing browser. |
| 2219 | var timeout; |
| 2220 | return function(){ |
| 2221 | clearTimeout(timeout); |
| 2222 | timeout = setTimeout(function(){ |
| 2223 | each(Chart.instances,function(instance){ |
| 2224 | // If the responsive flag is set in the chart instance config |
| 2225 | // Cascade the resize event down to the chart. |
| 2226 | if (instance.options.responsive){ |
| 2227 | instance.resize(instance.render, true); |
| 2228 | } |
| 2229 | }); |
| 2230 | }, 50); |
| 2231 | }; |
| 2232 | })()); |
| 2233 | |
| 2234 | |
| 2235 | if (amd) { |
| 2236 | define(function(){ |
| 2237 | return Chart; |
| 2238 | }); |
| 2239 | } else if (typeof module === 'object' && module.exports) { |
| 2240 | module.exports = Chart; |
| 2241 | } |
| 2242 | |
| 2243 | root.Chart = Chart; |
| 2244 | |
| 2245 | Chart.noConflict = function(){ |
| 2246 | root.Chart = previous; |
| 2247 | return Chart; |
| 2248 | }; |
| 2249 | |
| 2250 | }).call(this); |
| 2251 | |
| 2252 | (function(){ |
| 2253 | "use strict"; |
| 2254 | |
| 2255 | var root = this, |
| 2256 | Chart = root.Chart, |
| 2257 | helpers = Chart.helpers; |
| 2258 | |
| 2259 | |
| 2260 | var defaultConfig = { |
| 2261 | //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value |
| 2262 | scaleBeginAtZero : true, |
| 2263 | |
| 2264 | //Boolean - Whether grid lines are shown across the chart |
| 2265 | scaleShowGridLines : true, |
| 2266 | |
| 2267 | //String - Colour of the grid lines |
| 2268 | scaleGridLineColor : "rgba(0,0,0,.05)", |
| 2269 | |
| 2270 | //Number - Width of the grid lines |
| 2271 | scaleGridLineWidth : 1, |
| 2272 | |
| 2273 | //Boolean - Whether to show horizontal lines (except X axis) |
| 2274 | scaleShowHorizontalLines: true, |
| 2275 | |
| 2276 | //Boolean - Whether to show vertical lines (except Y axis) |
| 2277 | scaleShowVerticalLines: true, |
| 2278 | |
| 2279 | //Boolean - If there is a stroke on each bar |
| 2280 | barShowStroke : true, |
| 2281 | |
| 2282 | //Number - Pixel width of the bar stroke |
| 2283 | barStrokeWidth : 2, |
| 2284 | |
| 2285 | //Number - Spacing between each of the X value sets |
| 2286 | barValueSpacing : 5, |
| 2287 | |
| 2288 | //Number - Spacing between data sets within X values |
| 2289 | barDatasetSpacing : 1, |
| 2290 | |
| 2291 | //String - A legend template |
| 2292 | legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].fillColor%>\"><%if(datasets[i].label){%><%=datasets[i].label%><%}%></span></li><%}%></ul>" |
| 2293 | |
| 2294 | }; |
| 2295 | |
| 2296 | |
| 2297 | Chart.Type.extend({ |
| 2298 | name: "Bar", |
| 2299 | defaults : defaultConfig, |
| 2300 | initialize: function(data){ |
| 2301 | |
| 2302 | //Expose options as a scope variable here so we can access it in the ScaleClass |
| 2303 | var options = this.options; |
| 2304 | |
| 2305 | this.ScaleClass = Chart.Scale.extend({ |
| 2306 | offsetGridLines : true, |
| 2307 | calculateBarX : function(datasetCount, datasetIndex, barIndex){ |
| 2308 | //Reusable method for calculating the xPosition of a given bar based on datasetIndex & width of the bar |
| 2309 | var xWidth = this.calculateBaseWidth(), |
| 2310 | xAbsolute = this.calculateX(barIndex) - (xWidth/2), |
| 2311 | barWidth = this.calculateBarWidth(datasetCount); |
| 2312 | |
| 2313 | return xAbsolute + (barWidth * datasetIndex) + (datasetIndex * options.barDatasetSpacing) + barWidth/2; |
| 2314 | }, |
| 2315 | calculateBaseWidth : function(){ |
| 2316 | return (this.calculateX(1) - this.calculateX(0)) - (2*options.barValueSpacing); |
| 2317 | }, |
| 2318 | calculateBarWidth : function(datasetCount){ |
| 2319 | //The padding between datasets is to the right of each bar, providing that there are more than 1 dataset |
| 2320 | var baseWidth = this.calculateBaseWidth() - ((datasetCount - 1) * options.barDatasetSpacing); |
| 2321 | |
| 2322 | return (baseWidth / datasetCount); |
| 2323 | } |
| 2324 | }); |
| 2325 | |
| 2326 | this.datasets = []; |
| 2327 | |
| 2328 | //Set up tooltip events on the chart |
| 2329 | if (this.options.showTooltips){ |
| 2330 | helpers.bindEvents(this, this.options.tooltipEvents, function(evt){ |
| 2331 | var activeBars = (evt.type !== 'mouseout') ? this.getBarsAtEvent(evt) : []; |
| 2332 | |
| 2333 | this.eachBars(function(bar){ |
| 2334 | bar.restore(['fillColor', 'strokeColor']); |
| 2335 | }); |
| 2336 | helpers.each(activeBars, function(activeBar){ |
| 2337 | activeBar.fillColor = activeBar.highlightFill; |
| 2338 | activeBar.strokeColor = activeBar.highlightStroke; |
| 2339 | }); |
| 2340 | this.showTooltip(activeBars); |
| 2341 | }); |
| 2342 | } |
| 2343 | |
| 2344 | //Declare the extension of the default point, to cater for the options passed in to the constructor |
| 2345 | this.BarClass = Chart.Rectangle.extend({ |
| 2346 | strokeWidth : this.options.barStrokeWidth, |
| 2347 | showStroke : this.options.barShowStroke, |
| 2348 | ctx : this.chart.ctx |
| 2349 | }); |
| 2350 | |
| 2351 | //Iterate through each of the datasets, and build this into a property of the chart |
| 2352 | helpers.each(data.datasets,function(dataset,datasetIndex){ |
| 2353 | |
| 2354 | var datasetObject = { |
| 2355 | label : dataset.label || null, |
| 2356 | fillColor : dataset.fillColor, |
| 2357 | strokeColor : dataset.strokeColor, |
| 2358 | bars : [] |
| 2359 | }; |
| 2360 | |
| 2361 | this.datasets.push(datasetObject); |
| 2362 | |
| 2363 | helpers.each(dataset.data,function(dataPoint,index){ |
| 2364 | //Add a new point for each piece of data, passing any required data to draw. |
| 2365 | datasetObject.bars.push(new this.BarClass({ |
| 2366 | value : dataPoint, |
| 2367 | label : data.labels[index], |
| 2368 | datasetLabel: dataset.label, |
| 2369 | strokeColor : dataset.strokeColor, |
| 2370 | fillColor : dataset.fillColor, |
| 2371 | highlightFill : dataset.highlightFill || dataset.fillColor, |
| 2372 | highlightStroke : dataset.highlightStroke || dataset.strokeColor |
| 2373 | })); |
| 2374 | },this); |
| 2375 | |
| 2376 | },this); |
| 2377 | |
| 2378 | this.buildScale(data.labels); |
| 2379 | |
| 2380 | this.BarClass.prototype.base = this.scale.endPoint; |
| 2381 | |
| 2382 | this.eachBars(function(bar, index, datasetIndex){ |
| 2383 | helpers.extend(bar, { |
| 2384 | width : this.scale.calculateBarWidth(this.datasets.length), |
| 2385 | x: this.scale.calculateBarX(this.datasets.length, datasetIndex, index), |
| 2386 | y: this.scale.endPoint |
| 2387 | }); |
| 2388 | bar.save(); |
| 2389 | }, this); |
| 2390 | |
| 2391 | this.render(); |
| 2392 | }, |
| 2393 | update : function(){ |
| 2394 | this.scale.update(); |
| 2395 | // Reset any highlight colours before updating. |
| 2396 | helpers.each(this.activeElements, function(activeElement){ |
| 2397 | activeElement.restore(['fillColor', 'strokeColor']); |
| 2398 | }); |
| 2399 | |
| 2400 | this.eachBars(function(bar){ |
| 2401 | bar.save(); |
| 2402 | }); |
| 2403 | this.render(); |
| 2404 | }, |
| 2405 | eachBars : function(callback){ |
| 2406 | helpers.each(this.datasets,function(dataset, datasetIndex){ |
| 2407 | helpers.each(dataset.bars, callback, this, datasetIndex); |
| 2408 | },this); |
| 2409 | }, |
| 2410 | getBarsAtEvent : function(e){ |
| 2411 | var barsArray = [], |
| 2412 | eventPosition = helpers.getRelativePosition(e), |
| 2413 | datasetIterator = function(dataset){ |
| 2414 | barsArray.push(dataset.bars[barIndex]); |
| 2415 | }, |
| 2416 | barIndex; |
| 2417 | |
| 2418 | for (var datasetIndex = 0; datasetIndex < this.datasets.length; datasetIndex++) { |
| 2419 | for (barIndex = 0; barIndex < this.datasets[datasetIndex].bars.length; barIndex++) { |
| 2420 | if (this.datasets[datasetIndex].bars[barIndex].inRange(eventPosition.x,eventPosition.y)){ |
| 2421 | helpers.each(this.datasets, datasetIterator); |
| 2422 | return barsArray; |
| 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | return barsArray; |
| 2428 | }, |
| 2429 | buildScale : function(labels){ |
| 2430 | var self = this; |
| 2431 | |
| 2432 | var dataTotal = function(){ |
| 2433 | var values = []; |
| 2434 | self.eachBars(function(bar){ |
| 2435 | values.push(bar.value); |
| 2436 | }); |
| 2437 | return values; |
| 2438 | }; |
| 2439 | |
| 2440 | var scaleOptions = { |
| 2441 | templateString : this.options.scaleLabel, |
| 2442 | height : this.chart.height, |
| 2443 | width : this.chart.width, |
| 2444 | ctx : this.chart.ctx, |
| 2445 | textColor : this.options.scaleFontColor, |
| 2446 | fontSize : this.options.scaleFontSize, |
| 2447 | fontStyle : this.options.scaleFontStyle, |
| 2448 | fontFamily : this.options.scaleFontFamily, |
| 2449 | valuesCount : labels.length, |
| 2450 | beginAtZero : this.options.scaleBeginAtZero, |
| 2451 | integersOnly : this.options.scaleIntegersOnly, |
| 2452 | calculateYRange: function(currentHeight){ |
| 2453 | var updatedRanges = helpers.calculateScaleRange( |
| 2454 | dataTotal(), |
| 2455 | currentHeight, |
| 2456 | this.fontSize, |
| 2457 | this.beginAtZero, |
| 2458 | this.integersOnly |
| 2459 | ); |
| 2460 | helpers.extend(this, updatedRanges); |
| 2461 | }, |
| 2462 | xLabels : labels, |
| 2463 | font : helpers.fontString(this.options.scaleFontSize, this.options.scaleFontStyle, this.options.scaleFontFamily), |
| 2464 | lineWidth : this.options.scaleLineWidth, |
| 2465 | lineColor : this.options.scaleLineColor, |
| 2466 | showHorizontalLines : this.options.scaleShowHorizontalLines, |
| 2467 | showVerticalLines : this.options.scaleShowVerticalLines, |
| 2468 | gridLineWidth : (this.options.scaleShowGridLines) ? this.options.scaleGridLineWidth : 0, |
| 2469 | gridLineColor : (this.options.scaleShowGridLines) ? this.options.scaleGridLineColor : "rgba(0,0,0,0)", |
| 2470 | padding : (this.options.showScale) ? 0 : (this.options.barShowStroke) ? this.options.barStrokeWidth : 0, |
| 2471 | showLabels : this.options.scaleShowLabels, |
| 2472 | display : this.options.showScale |
| 2473 | }; |
| 2474 | |
| 2475 | if (this.options.scaleOverride){ |
| 2476 | helpers.extend(scaleOptions, { |
| 2477 | calculateYRange: helpers.noop, |
| 2478 | steps: this.options.scaleSteps, |
| 2479 | stepValue: this.options.scaleStepWidth, |
| 2480 | min: this.options.scaleStartValue, |
| 2481 | max: this.options.scaleStartValue + (this.options.scaleSteps * this.options.scaleStepWidth) |
| 2482 | }); |
| 2483 | } |
| 2484 | |
| 2485 | this.scale = new this.ScaleClass(scaleOptions); |
| 2486 | }, |
| 2487 | addData : function(valuesArray,label){ |
| 2488 | //Map the values array for each of the datasets |
| 2489 | helpers.each(valuesArray,function(value,datasetIndex){ |
| 2490 | //Add a new point for each piece of data, passing any required data to draw. |
| 2491 | this.datasets[datasetIndex].bars.push(new this.BarClass({ |
| 2492 | value : value, |
| 2493 | label : label, |
| 2494 | datasetLabel: this.datasets[datasetIndex].label, |
| 2495 | x: this.scale.calculateBarX(this.datasets.length, datasetIndex, this.scale.valuesCount+1), |
| 2496 | y: this.scale.endPoint, |
| 2497 | width : this.scale.calculateBarWidth(this.datasets.length), |
| 2498 | base : this.scale.endPoint, |
| 2499 | strokeColor : this.datasets[datasetIndex].strokeColor, |
| 2500 | fillColor : this.datasets[datasetIndex].fillColor |
| 2501 | })); |
| 2502 | },this); |
| 2503 | |
| 2504 | this.scale.addXLabel(label); |
| 2505 | //Then re-render the chart. |
| 2506 | this.update(); |
| 2507 | }, |
| 2508 | removeData : function(){ |
| 2509 | this.scale.removeXLabel(); |
| 2510 | //Then re-render the chart. |
| 2511 | helpers.each(this.datasets,function(dataset){ |
| 2512 | dataset.bars.shift(); |
| 2513 | },this); |
| 2514 | this.update(); |
| 2515 | }, |
| 2516 | reflow : function(){ |
| 2517 | helpers.extend(this.BarClass.prototype,{ |
| 2518 | y: this.scale.endPoint, |
| 2519 | base : this.scale.endPoint |
| 2520 | }); |
| 2521 | var newScaleProps = helpers.extend({ |
| 2522 | height : this.chart.height, |
| 2523 | width : this.chart.width |
| 2524 | }); |
| 2525 | this.scale.update(newScaleProps); |
| 2526 | }, |
| 2527 | draw : function(ease){ |
| 2528 | var easingDecimal = ease || 1; |
| 2529 | this.clear(); |
| 2530 | |
| 2531 | var ctx = this.chart.ctx; |
| 2532 | |
| 2533 | this.scale.draw(easingDecimal); |
| 2534 | |
| 2535 | //Draw all the bars for each dataset |
| 2536 | helpers.each(this.datasets,function(dataset,datasetIndex){ |
| 2537 | helpers.each(dataset.bars,function(bar,index){ |
| 2538 | if (bar.hasValue()){ |
| 2539 | bar.base = this.scale.endPoint; |
| 2540 | //Transition then draw |
| 2541 | bar.transition({ |
| 2542 | x : this.scale.calculateBarX(this.datasets.length, datasetIndex, index), |
| 2543 | y : this.scale.calculateY(bar.value), |
| 2544 | width : this.scale.calculateBarWidth(this.datasets.length) |
| 2545 | }, easingDecimal).draw(); |
| 2546 | } |
| 2547 | },this); |
| 2548 | |
| 2549 | },this); |
| 2550 | } |
| 2551 | }); |
| 2552 | |
| 2553 | |
| 2554 | }).call(this); |
| 2555 | |
| 2556 | (function(){ |
| 2557 | "use strict"; |
| 2558 | |
| 2559 | var root = this, |
| 2560 | Chart = root.Chart, |
| 2561 | //Cache a local reference to Chart.helpers |
| 2562 | helpers = Chart.helpers; |
| 2563 | |
| 2564 | var defaultConfig = { |
| 2565 | //Boolean - Whether we should show a stroke on each segment |
| 2566 | segmentShowStroke : true, |
| 2567 | |
| 2568 | //String - The colour of each segment stroke |
| 2569 | segmentStrokeColor : "#fff", |
| 2570 | |
| 2571 | //Number - The width of each segment stroke |
| 2572 | segmentStrokeWidth : 2, |
| 2573 | |
| 2574 | //The percentage of the chart that we cut out of the middle. |
| 2575 | percentageInnerCutout : 50, |
| 2576 | |
| 2577 | //Number - Amount of animation steps |
| 2578 | animationSteps : 100, |
| 2579 | |
| 2580 | //String - Animation easing effect |
| 2581 | animationEasing : "easeOutBounce", |
| 2582 | |
| 2583 | //Boolean - Whether we animate the rotation of the Doughnut |
| 2584 | animateRotate : true, |
| 2585 | |
| 2586 | //Boolean - Whether we animate scaling the Doughnut from the centre |
| 2587 | animateScale : false, |
| 2588 | |
| 2589 | //String - A legend template |
| 2590 | legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"><%if(segments[i].label){%><%=segments[i].label%><%}%></span></li><%}%></ul>" |
| 2591 | |
| 2592 | }; |
| 2593 | |
| 2594 | Chart.Type.extend({ |
| 2595 | //Passing in a name registers this chart in the Chart namespace |
| 2596 | name: "Doughnut", |
| 2597 | //Providing a defaults will also register the deafults in the chart namespace |
| 2598 | defaults : defaultConfig, |
| 2599 | //Initialize is fired when the chart is initialized - Data is passed in as a parameter |
| 2600 | //Config is automatically merged by the core of Chart.js, and is available at this.options |
| 2601 | initialize: function(data){ |
| 2602 | |
| 2603 | //Declare segments as a static property to prevent inheriting across the Chart type prototype |
| 2604 | this.segments = []; |
| 2605 | this.outerRadius = (helpers.min([this.chart.width,this.chart.height]) - this.options.segmentStrokeWidth/2)/2; |
| 2606 | |
| 2607 | this.SegmentArc = Chart.Arc.extend({ |
| 2608 | ctx : this.chart.ctx, |
| 2609 | x : this.chart.width/2, |
| 2610 | y : this.chart.height/2 |
| 2611 | }); |
| 2612 | |
| 2613 | //Set up tooltip events on the chart |
| 2614 | if (this.options.showTooltips){ |
| 2615 | helpers.bindEvents(this, this.options.tooltipEvents, function(evt){ |
| 2616 | var activeSegments = (evt.type !== 'mouseout') ? this.getSegmentsAtEvent(evt) : []; |
| 2617 | |
| 2618 | helpers.each(this.segments,function(segment){ |
| 2619 | segment.restore(["fillColor"]); |
| 2620 | }); |
| 2621 | helpers.each(activeSegments,function(activeSegment){ |
| 2622 | activeSegment.fillColor = activeSegment.highlightColor; |
| 2623 | }); |
| 2624 | this.showTooltip(activeSegments); |
| 2625 | }); |
| 2626 | } |
| 2627 | this.calculateTotal(data); |
| 2628 | |
| 2629 | helpers.each(data,function(datapoint, index){ |
| 2630 | if (!datapoint.color) { |
| 2631 | datapoint.color = 'hsl(' + (360 * index / data.length) + ', 100%, 50%)'; |
| 2632 | } |
| 2633 | this.addData(datapoint, index, true); |
| 2634 | },this); |
| 2635 | |
| 2636 | this.render(); |
| 2637 | }, |
| 2638 | getSegmentsAtEvent : function(e){ |
| 2639 | var segmentsArray = []; |
| 2640 | |
| 2641 | var location = helpers.getRelativePosition(e); |
| 2642 | |
| 2643 | helpers.each(this.segments,function(segment){ |
| 2644 | if (segment.inRange(location.x,location.y)) segmentsArray.push(segment); |
| 2645 | },this); |
| 2646 | return segmentsArray; |
| 2647 | }, |
| 2648 | addData : function(segment, atIndex, silent){ |
| 2649 | var index = atIndex !== undefined ? atIndex : this.segments.length; |
| 2650 | if ( typeof(segment.color) === "undefined" ) { |
| 2651 | segment.color = Chart.defaults.global.segmentColorDefault[index % Chart.defaults.global.segmentColorDefault.length]; |
| 2652 | segment.highlight = Chart.defaults.global.segmentHighlightColorDefaults[index % Chart.defaults.global.segmentHighlightColorDefaults.length]; |
| 2653 | } |
| 2654 | this.segments.splice(index, 0, new this.SegmentArc({ |
| 2655 | value : segment.value, |
| 2656 | outerRadius : (this.options.animateScale) ? 0 : this.outerRadius, |
| 2657 | innerRadius : (this.options.animateScale) ? 0 : (this.outerRadius/100) * this.options.percentageInnerCutout, |
| 2658 | fillColor : segment.color, |
| 2659 | highlightColor : segment.highlight || segment.color, |
| 2660 | showStroke : this.options.segmentShowStroke, |
| 2661 | strokeWidth : this.options.segmentStrokeWidth, |
| 2662 | strokeColor : this.options.segmentStrokeColor, |
| 2663 | startAngle : Math.PI * 1.5, |
| 2664 | circumference : (this.options.animateRotate) ? 0 : this.calculateCircumference(segment.value), |
| 2665 | label : segment.label |
| 2666 | })); |
| 2667 | if (!silent){ |
| 2668 | this.reflow(); |
| 2669 | this.update(); |
| 2670 | } |
| 2671 | }, |
| 2672 | calculateCircumference : function(value) { |
| 2673 | if ( this.total > 0 ) { |
| 2674 | return (Math.PI*2)*(value / this.total); |
| 2675 | } else { |
| 2676 | return 0; |
| 2677 | } |
| 2678 | }, |
| 2679 | calculateTotal : function(data){ |
| 2680 | this.total = 0; |
| 2681 | helpers.each(data,function(segment){ |
| 2682 | this.total += Math.abs(segment.value); |
| 2683 | },this); |
| 2684 | }, |
| 2685 | update : function(){ |
| 2686 | this.calculateTotal(this.segments); |
| 2687 | |
| 2688 | // Reset any highlight colours before updating. |
| 2689 | helpers.each(this.activeElements, function(activeElement){ |
| 2690 | activeElement.restore(['fillColor']); |
| 2691 | }); |
| 2692 | |
| 2693 | helpers.each(this.segments,function(segment){ |
| 2694 | segment.save(); |
| 2695 | }); |
| 2696 | this.render(); |
| 2697 | }, |
| 2698 | |
| 2699 | removeData: function(atIndex){ |
| 2700 | var indexToDelete = (helpers.isNumber(atIndex)) ? atIndex : this.segments.length-1; |
| 2701 | this.segments.splice(indexToDelete, 1); |
| 2702 | this.reflow(); |
| 2703 | this.update(); |
| 2704 | }, |
| 2705 | |
| 2706 | reflow : function(){ |
| 2707 | helpers.extend(this.SegmentArc.prototype,{ |
| 2708 | x : this.chart.width/2, |
| 2709 | y : this.chart.height/2 |
| 2710 | }); |
| 2711 | this.outerRadius = (helpers.min([this.chart.width,this.chart.height]) - this.options.segmentStrokeWidth/2)/2; |
| 2712 | helpers.each(this.segments, function(segment){ |
| 2713 | segment.update({ |
| 2714 | outerRadius : this.outerRadius, |
| 2715 | innerRadius : (this.outerRadius/100) * this.options.percentageInnerCutout |
| 2716 | }); |
| 2717 | }, this); |
| 2718 | }, |
| 2719 | draw : function(easeDecimal){ |
| 2720 | var animDecimal = (easeDecimal) ? easeDecimal : 1; |
| 2721 | this.clear(); |
| 2722 | helpers.each(this.segments,function(segment,index){ |
| 2723 | segment.transition({ |
| 2724 | circumference : this.calculateCircumference(segment.value), |
| 2725 | outerRadius : this.outerRadius, |
| 2726 | innerRadius : (this.outerRadius/100) * this.options.percentageInnerCutout |
| 2727 | },animDecimal); |
| 2728 | |
| 2729 | segment.endAngle = segment.startAngle + segment.circumference; |
| 2730 | |
| 2731 | segment.draw(); |
| 2732 | if (index === 0){ |
| 2733 | segment.startAngle = Math.PI * 1.5; |
| 2734 | } |
| 2735 | //Check to see if it's the last segment, if not get the next and update the start angle |
| 2736 | if (index < this.segments.length-1){ |
| 2737 | this.segments[index+1].startAngle = segment.endAngle; |
| 2738 | } |
| 2739 | },this); |
| 2740 | |
| 2741 | } |
| 2742 | }); |
| 2743 | |
| 2744 | Chart.types.Doughnut.extend({ |
| 2745 | name : "Pie", |
| 2746 | defaults : helpers.merge(defaultConfig,{percentageInnerCutout : 0}) |
| 2747 | }); |
| 2748 | |
| 2749 | }).call(this); |
| 2750 | |
| 2751 | (function(){ |
| 2752 | "use strict"; |
| 2753 | |
| 2754 | var root = this, |
| 2755 | Chart = root.Chart, |
| 2756 | helpers = Chart.helpers; |
| 2757 | |
| 2758 | var defaultConfig = { |
| 2759 | |
| 2760 | ///Boolean - Whether grid lines are shown across the chart |
| 2761 | scaleShowGridLines : true, |
| 2762 | |
| 2763 | //String - Colour of the grid lines |
| 2764 | scaleGridLineColor : "rgba(0,0,0,.05)", |
| 2765 | |
| 2766 | //Number - Width of the grid lines |
| 2767 | scaleGridLineWidth : 1, |
| 2768 | |
| 2769 | //Boolean - Whether to show horizontal lines (except X axis) |
| 2770 | scaleShowHorizontalLines: true, |
| 2771 | |
| 2772 | //Boolean - Whether to show vertical lines (except Y axis) |
| 2773 | scaleShowVerticalLines: true, |
| 2774 | |
| 2775 | //Boolean - Whether the line is curved between points |
| 2776 | bezierCurve : true, |
| 2777 | |
| 2778 | //Number - Tension of the bezier curve between points |
| 2779 | bezierCurveTension : 0.4, |
| 2780 | |
| 2781 | //Boolean - Whether to show a dot for each point |
| 2782 | pointDot : true, |
| 2783 | |
| 2784 | //Number - Radius of each point dot in pixels |
| 2785 | pointDotRadius : 4, |
| 2786 | |
| 2787 | //Number - Pixel width of point dot stroke |
| 2788 | pointDotStrokeWidth : 1, |
| 2789 | |
| 2790 | //Number - amount extra to add to the radius to cater for hit detection outside the drawn point |
| 2791 | pointHitDetectionRadius : 20, |
| 2792 | |
| 2793 | //Boolean - Whether to show a stroke for datasets |
| 2794 | datasetStroke : true, |
| 2795 | |
| 2796 | //Number - Pixel width of dataset stroke |
| 2797 | datasetStrokeWidth : 2, |
| 2798 | |
| 2799 | //Boolean - Whether to fill the dataset with a colour |
| 2800 | datasetFill : true, |
| 2801 | |
| 2802 | //String - A legend template |
| 2803 | legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"><%if(datasets[i].label){%><%=datasets[i].label%><%}%></span></li><%}%></ul>", |
| 2804 | |
| 2805 | //Boolean - Whether to horizontally center the label and point dot inside the grid |
| 2806 | offsetGridLines : false |
| 2807 | |
| 2808 | }; |
| 2809 | |
| 2810 | |
| 2811 | Chart.Type.extend({ |
| 2812 | name: "Line", |
| 2813 | defaults : defaultConfig, |
| 2814 | initialize: function(data){ |
| 2815 | //Declare the extension of the default point, to cater for the options passed in to the constructor |
| 2816 | this.PointClass = Chart.Point.extend({ |
| 2817 | offsetGridLines : this.options.offsetGridLines, |
| 2818 | strokeWidth : this.options.pointDotStrokeWidth, |
| 2819 | radius : this.options.pointDotRadius, |
| 2820 | display: this.options.pointDot, |
| 2821 | hitDetectionRadius : this.options.pointHitDetectionRadius, |
| 2822 | ctx : this.chart.ctx, |
| 2823 | inRange : function(mouseX){ |
| 2824 | return (Math.pow(mouseX-this.x, 2) < Math.pow(this.radius + this.hitDetectionRadius,2)); |
| 2825 | } |
| 2826 | }); |
| 2827 | |
| 2828 | this.datasets = []; |
| 2829 | |
| 2830 | //Set up tooltip events on the chart |
| 2831 | if (this.options.showTooltips){ |
| 2832 | helpers.bindEvents(this, this.options.tooltipEvents, function(evt){ |
| 2833 | var activePoints = (evt.type !== 'mouseout') ? this.getPointsAtEvent(evt) : []; |
| 2834 | this.eachPoints(function(point){ |
| 2835 | point.restore(['fillColor', 'strokeColor']); |
| 2836 | }); |
| 2837 | helpers.each(activePoints, function(activePoint){ |
| 2838 | activePoint.fillColor = activePoint.highlightFill; |
| 2839 | activePoint.strokeColor = activePoint.highlightStroke; |
| 2840 | }); |
| 2841 | this.showTooltip(activePoints); |
| 2842 | }); |
| 2843 | } |
| 2844 | |
| 2845 | //Iterate through each of the datasets, and build this into a property of the chart |
| 2846 | helpers.each(data.datasets,function(dataset){ |
| 2847 | |
| 2848 | var datasetObject = { |
| 2849 | label : dataset.label || null, |
| 2850 | fillColor : dataset.fillColor, |
| 2851 | strokeColor : dataset.strokeColor, |
| 2852 | pointColor : dataset.pointColor, |
| 2853 | pointStrokeColor : dataset.pointStrokeColor, |
| 2854 | points : [] |
| 2855 | }; |
| 2856 | |
| 2857 | this.datasets.push(datasetObject); |
| 2858 | |
| 2859 | |
| 2860 | helpers.each(dataset.data,function(dataPoint,index){ |
| 2861 | //Add a new point for each piece of data, passing any required data to draw. |
| 2862 | datasetObject.points.push(new this.PointClass({ |
| 2863 | value : dataPoint, |
| 2864 | label : data.labels[index], |
| 2865 | datasetLabel: dataset.label, |
| 2866 | strokeColor : dataset.pointStrokeColor, |
| 2867 | fillColor : dataset.pointColor, |
| 2868 | highlightFill : dataset.pointHighlightFill || dataset.pointColor, |
| 2869 | highlightStroke : dataset.pointHighlightStroke || dataset.pointStrokeColor |
| 2870 | })); |
| 2871 | },this); |
| 2872 | |
| 2873 | this.buildScale(data.labels); |
| 2874 | |
| 2875 | |
| 2876 | this.eachPoints(function(point, index){ |
| 2877 | helpers.extend(point, { |
| 2878 | x: this.scale.calculateX(index), |
| 2879 | y: this.scale.endPoint |
| 2880 | }); |
| 2881 | point.save(); |
| 2882 | }, this); |
| 2883 | |
| 2884 | },this); |
| 2885 | |
| 2886 | |
| 2887 | this.render(); |
| 2888 | }, |
| 2889 | update : function(){ |
| 2890 | this.scale.update(); |
| 2891 | // Reset any highlight colours before updating. |
| 2892 | helpers.each(this.activeElements, function(activeElement){ |
| 2893 | activeElement.restore(['fillColor', 'strokeColor']); |
| 2894 | }); |
| 2895 | this.eachPoints(function(point){ |
| 2896 | point.save(); |
| 2897 | }); |
| 2898 | this.render(); |
| 2899 | }, |
| 2900 | eachPoints : function(callback){ |
| 2901 | helpers.each(this.datasets,function(dataset){ |
| 2902 | helpers.each(dataset.points,callback,this); |
| 2903 | },this); |
| 2904 | }, |
| 2905 | getPointsAtEvent : function(e){ |
| 2906 | var pointsArray = [], |
| 2907 | eventPosition = helpers.getRelativePosition(e); |
| 2908 | helpers.each(this.datasets,function(dataset){ |
| 2909 | helpers.each(dataset.points,function(point){ |
| 2910 | if (point.inRange(eventPosition.x,eventPosition.y)) pointsArray.push(point); |
| 2911 | }); |
| 2912 | },this); |
| 2913 | return pointsArray; |
| 2914 | }, |
| 2915 | buildScale : function(labels){ |
| 2916 | var self = this; |
| 2917 | |
| 2918 | var dataTotal = function(){ |
| 2919 | var values = []; |
| 2920 | self.eachPoints(function(point){ |
| 2921 | values.push(point.value); |
| 2922 | }); |
| 2923 | |
| 2924 | return values; |
| 2925 | }; |
| 2926 | |
| 2927 | var scaleOptions = { |
| 2928 | templateString : this.options.scaleLabel, |
| 2929 | height : this.chart.height, |
| 2930 | width : this.chart.width, |
| 2931 | ctx : this.chart.ctx, |
| 2932 | textColor : this.options.scaleFontColor, |
| 2933 | offsetGridLines : this.options.offsetGridLines, |
| 2934 | fontSize : this.options.scaleFontSize, |
| 2935 | fontStyle : this.options.scaleFontStyle, |
| 2936 | fontFamily : this.options.scaleFontFamily, |
| 2937 | valuesCount : labels.length, |
| 2938 | beginAtZero : this.options.scaleBeginAtZero, |
| 2939 | integersOnly : this.options.scaleIntegersOnly, |
| 2940 | calculateYRange : function(currentHeight){ |
| 2941 | var updatedRanges = helpers.calculateScaleRange( |
| 2942 | dataTotal(), |
| 2943 | currentHeight, |
| 2944 | this.fontSize, |
| 2945 | this.beginAtZero, |
| 2946 | this.integersOnly |
| 2947 | ); |
| 2948 | helpers.extend(this, updatedRanges); |
| 2949 | }, |
| 2950 | xLabels : labels, |
| 2951 | font : helpers.fontString(this.options.scaleFontSize, this.options.scaleFontStyle, this.options.scaleFontFamily), |
| 2952 | lineWidth : this.options.scaleLineWidth, |
| 2953 | lineColor : this.options.scaleLineColor, |
| 2954 | showHorizontalLines : this.options.scaleShowHorizontalLines, |
| 2955 | showVerticalLines : this.options.scaleShowVerticalLines, |
| 2956 | gridLineWidth : (this.options.scaleShowGridLines) ? this.options.scaleGridLineWidth : 0, |
| 2957 | gridLineColor : (this.options.scaleShowGridLines) ? this.options.scaleGridLineColor : "rgba(0,0,0,0)", |
| 2958 | padding: (this.options.showScale) ? 0 : this.options.pointDotRadius + this.options.pointDotStrokeWidth, |
| 2959 | showLabels : this.options.scaleShowLabels, |
| 2960 | display : this.options.showScale |
| 2961 | }; |
| 2962 | |
| 2963 | if (this.options.scaleOverride){ |
| 2964 | helpers.extend(scaleOptions, { |
| 2965 | calculateYRange: helpers.noop, |
| 2966 | steps: this.options.scaleSteps, |
| 2967 | stepValue: this.options.scaleStepWidth, |
| 2968 | min: this.options.scaleStartValue, |
| 2969 | max: this.options.scaleStartValue + (this.options.scaleSteps * this.options.scaleStepWidth) |
| 2970 | }); |
| 2971 | } |
| 2972 | |
| 2973 | |
| 2974 | this.scale = new Chart.Scale(scaleOptions); |
| 2975 | }, |
| 2976 | addData : function(valuesArray,label){ |
| 2977 | //Map the values array for each of the datasets |
| 2978 | |
| 2979 | helpers.each(valuesArray,function(value,datasetIndex){ |
| 2980 | //Add a new point for each piece of data, passing any required data to draw. |
| 2981 | this.datasets[datasetIndex].points.push(new this.PointClass({ |
| 2982 | value : value, |
| 2983 | label : label, |
| 2984 | datasetLabel: this.datasets[datasetIndex].label, |
| 2985 | x: this.scale.calculateX(this.scale.valuesCount+1), |
| 2986 | y: this.scale.endPoint, |
| 2987 | strokeColor : this.datasets[datasetIndex].pointStrokeColor, |
| 2988 | fillColor : this.datasets[datasetIndex].pointColor |
| 2989 | })); |
| 2990 | },this); |
| 2991 | |
| 2992 | this.scale.addXLabel(label); |
| 2993 | //Then re-render the chart. |
| 2994 | this.update(); |
| 2995 | }, |
| 2996 | removeData : function(){ |
| 2997 | this.scale.removeXLabel(); |
| 2998 | //Then re-render the chart. |
| 2999 | helpers.each(this.datasets,function(dataset){ |
| 3000 | dataset.points.shift(); |
| 3001 | },this); |
| 3002 | this.update(); |
| 3003 | }, |
| 3004 | reflow : function(){ |
| 3005 | var newScaleProps = helpers.extend({ |
| 3006 | height : this.chart.height, |
| 3007 | width : this.chart.width |
| 3008 | }); |
| 3009 | this.scale.update(newScaleProps); |
| 3010 | }, |
| 3011 | draw : function(ease){ |
| 3012 | var easingDecimal = ease || 1; |
| 3013 | this.clear(); |
| 3014 | |
| 3015 | var ctx = this.chart.ctx; |
| 3016 | |
| 3017 | // Some helper methods for getting the next/prev points |
| 3018 | var hasValue = function(item){ |
| 3019 | return item.value !== null; |
| 3020 | }, |
| 3021 | nextPoint = function(point, collection, index){ |
| 3022 | return helpers.findNextWhere(collection, hasValue, index) || point; |
| 3023 | }, |
| 3024 | previousPoint = function(point, collection, index){ |
| 3025 | return helpers.findPreviousWhere(collection, hasValue, index) || point; |
| 3026 | }; |
| 3027 | |
| 3028 | if (!this.scale) return; |
| 3029 | this.scale.draw(easingDecimal); |
| 3030 | |
| 3031 | |
| 3032 | helpers.each(this.datasets,function(dataset){ |
| 3033 | var pointsWithValues = helpers.where(dataset.points, hasValue); |
| 3034 | |
| 3035 | //Transition each point first so that the line and point drawing isn't out of sync |
| 3036 | //We can use this extra loop to calculate the control points of this dataset also in this loop |
| 3037 | |
| 3038 | helpers.each(dataset.points, function(point, index){ |
| 3039 | if (point.hasValue()){ |
| 3040 | point.transition({ |
| 3041 | y : this.scale.calculateY(point.value), |
| 3042 | x : this.scale.calculateX(index) |
| 3043 | }, easingDecimal); |
| 3044 | } |
| 3045 | },this); |
| 3046 | |
| 3047 | |
| 3048 | // Control points need to be calculated in a separate loop, because we need to know the current x/y of the point |
| 3049 | // This would cause issues when there is no animation, because the y of the next point would be 0, so beziers would be skewed |
| 3050 | if (this.options.bezierCurve){ |
| 3051 | helpers.each(pointsWithValues, function(point, index){ |
| 3052 | var tension = (index > 0 && index < pointsWithValues.length - 1) ? this.options.bezierCurveTension : 0; |
| 3053 | point.controlPoints = helpers.splineCurve( |
| 3054 | previousPoint(point, pointsWithValues, index), |
| 3055 | point, |
| 3056 | nextPoint(point, pointsWithValues, index), |
| 3057 | tension |
| 3058 | ); |
| 3059 | |
| 3060 | // Prevent the bezier going outside of the bounds of the graph |
| 3061 | |
| 3062 | // Cap puter bezier handles to the upper/lower scale bounds |
| 3063 | if (point.controlPoints.outer.y > this.scale.endPoint){ |
| 3064 | point.controlPoints.outer.y = this.scale.endPoint; |
| 3065 | } |
| 3066 | else if (point.controlPoints.outer.y < this.scale.startPoint){ |
| 3067 | point.controlPoints.outer.y = this.scale.startPoint; |
| 3068 | } |
| 3069 | |
| 3070 | // Cap inner bezier handles to the upper/lower scale bounds |
| 3071 | if (point.controlPoints.inner.y > this.scale.endPoint){ |
| 3072 | point.controlPoints.inner.y = this.scale.endPoint; |
| 3073 | } |
| 3074 | else if (point.controlPoints.inner.y < this.scale.startPoint){ |
| 3075 | point.controlPoints.inner.y = this.scale.startPoint; |
| 3076 | } |
| 3077 | },this); |
| 3078 | } |
| 3079 | |
| 3080 | |
| 3081 | //Draw the line between all the points |
| 3082 | ctx.lineWidth = this.options.datasetStrokeWidth; |
| 3083 | ctx.strokeStyle = dataset.strokeColor; |
| 3084 | ctx.beginPath(); |
| 3085 | |
| 3086 | helpers.each(pointsWithValues, function(point, index){ |
| 3087 | if (index === 0){ |
| 3088 | ctx.moveTo(point.x, point.y); |
| 3089 | } |
| 3090 | else{ |
| 3091 | if(this.options.bezierCurve){ |
| 3092 | var previous = previousPoint(point, pointsWithValues, index); |
| 3093 | |
| 3094 | ctx.bezierCurveTo( |
| 3095 | previous.controlPoints.outer.x, |
| 3096 | previous.controlPoints.outer.y, |
| 3097 | point.controlPoints.inner.x, |
| 3098 | point.controlPoints.inner.y, |
| 3099 | point.x, |
| 3100 | point.y |
| 3101 | ); |
| 3102 | } |
| 3103 | else{ |
| 3104 | ctx.lineTo(point.x,point.y); |
| 3105 | } |
| 3106 | } |
| 3107 | }, this); |
| 3108 | |
| 3109 | if (this.options.datasetStroke) { |
| 3110 | ctx.stroke(); |
| 3111 | } |
| 3112 | |
| 3113 | if (this.options.datasetFill && pointsWithValues.length > 0){ |
| 3114 | //Round off the line by going to the base of the chart, back to the start, then fill. |
| 3115 | ctx.lineTo(pointsWithValues[pointsWithValues.length - 1].x, this.scale.endPoint); |
| 3116 | ctx.lineTo(pointsWithValues[0].x, this.scale.endPoint); |
| 3117 | ctx.fillStyle = dataset.fillColor; |
| 3118 | ctx.closePath(); |
| 3119 | ctx.fill(); |
| 3120 | } |
| 3121 | |
| 3122 | //Now draw the points over the line |
| 3123 | //A little inefficient double looping, but better than the line |
| 3124 | //lagging behind the point positions |
| 3125 | helpers.each(pointsWithValues,function(point){ |
| 3126 | point.draw(); |
| 3127 | }); |
| 3128 | },this); |
| 3129 | } |
| 3130 | }); |
| 3131 | |
| 3132 | |
| 3133 | }).call(this); |
| 3134 | |
| 3135 | (function(){ |
| 3136 | "use strict"; |
| 3137 | |
| 3138 | var root = this, |
| 3139 | Chart = root.Chart, |
| 3140 | //Cache a local reference to Chart.helpers |
| 3141 | helpers = Chart.helpers; |
| 3142 | |
| 3143 | var defaultConfig = { |
| 3144 | //Boolean - Show a backdrop to the scale label |
| 3145 | scaleShowLabelBackdrop : true, |
| 3146 | |
| 3147 | //String - The colour of the label backdrop |
| 3148 | scaleBackdropColor : "rgba(255,255,255,0.75)", |
| 3149 | |
| 3150 | // Boolean - Whether the scale should begin at zero |
| 3151 | scaleBeginAtZero : true, |
| 3152 | |
| 3153 | //Number - The backdrop padding above & below the label in pixels |
| 3154 | scaleBackdropPaddingY : 2, |
| 3155 | |
| 3156 | //Number - The backdrop padding to the side of the label in pixels |
| 3157 | scaleBackdropPaddingX : 2, |
| 3158 | |
| 3159 | //Boolean - Show line for each value in the scale |
| 3160 | scaleShowLine : true, |
| 3161 | |
| 3162 | //Boolean - Stroke a line around each segment in the chart |
| 3163 | segmentShowStroke : true, |
| 3164 | |
| 3165 | //String - The colour of the stroke on each segment. |
| 3166 | segmentStrokeColor : "#fff", |
| 3167 | |
| 3168 | //Number - The width of the stroke value in pixels |
| 3169 | segmentStrokeWidth : 2, |
| 3170 | |
| 3171 | //Number - Amount of animation steps |
| 3172 | animationSteps : 100, |
| 3173 | |
| 3174 | //String - Animation easing effect. |
| 3175 | animationEasing : "easeOutBounce", |
| 3176 | |
| 3177 | //Boolean - Whether to animate the rotation of the chart |
| 3178 | animateRotate : true, |
| 3179 | |
| 3180 | //Boolean - Whether to animate scaling the chart from the centre |
| 3181 | animateScale : false, |
| 3182 | |
| 3183 | //String - A legend template |
| 3184 | legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"><%if(segments[i].label){%><%=segments[i].label%><%}%></span></li><%}%></ul>" |
| 3185 | }; |
| 3186 | |
| 3187 | |
| 3188 | Chart.Type.extend({ |
| 3189 | //Passing in a name registers this chart in the Chart namespace |
| 3190 | name: "PolarArea", |
| 3191 | //Providing a defaults will also register the deafults in the chart namespace |
| 3192 | defaults : defaultConfig, |
| 3193 | //Initialize is fired when the chart is initialized - Data is passed in as a parameter |
| 3194 | //Config is automatically merged by the core of Chart.js, and is available at this.options |
| 3195 | initialize: function(data){ |
| 3196 | this.segments = []; |
| 3197 | //Declare segment class as a chart instance specific class, so it can share props for this instance |
| 3198 | this.SegmentArc = Chart.Arc.extend({ |
| 3199 | showStroke : this.options.segmentShowStroke, |
| 3200 | strokeWidth : this.options.segmentStrokeWidth, |
| 3201 | strokeColor : this.options.segmentStrokeColor, |
| 3202 | ctx : this.chart.ctx, |
| 3203 | innerRadius : 0, |
| 3204 | x : this.chart.width/2, |
| 3205 | y : this.chart.height/2 |
| 3206 | }); |
| 3207 | this.scale = new Chart.RadialScale({ |
| 3208 | display: this.options.showScale, |
| 3209 | fontStyle: this.options.scaleFontStyle, |
| 3210 | fontSize: this.options.scaleFontSize, |
| 3211 | fontFamily: this.options.scaleFontFamily, |
| 3212 | fontColor: this.options.scaleFontColor, |
| 3213 | showLabels: this.options.scaleShowLabels, |
| 3214 | showLabelBackdrop: this.options.scaleShowLabelBackdrop, |
| 3215 | backdropColor: this.options.scaleBackdropColor, |
| 3216 | backdropPaddingY : this.options.scaleBackdropPaddingY, |
| 3217 | backdropPaddingX: this.options.scaleBackdropPaddingX, |
| 3218 | lineWidth: (this.options.scaleShowLine) ? this.options.scaleLineWidth : 0, |
| 3219 | lineColor: this.options.scaleLineColor, |
| 3220 | lineArc: true, |
| 3221 | width: this.chart.width, |
| 3222 | height: this.chart.height, |
| 3223 | xCenter: this.chart.width/2, |
| 3224 | yCenter: this.chart.height/2, |
| 3225 | ctx : this.chart.ctx, |
| 3226 | templateString: this.options.scaleLabel, |
| 3227 | valuesCount: data.length |
| 3228 | }); |
| 3229 | |
| 3230 | this.updateScaleRange(data); |
| 3231 | |
| 3232 | this.scale.update(); |
| 3233 | |
| 3234 | helpers.each(data,function(segment,index){ |
| 3235 | this.addData(segment,index,true); |
| 3236 | },this); |
| 3237 | |
| 3238 | //Set up tooltip events on the chart |
| 3239 | if (this.options.showTooltips){ |
| 3240 | helpers.bindEvents(this, this.options.tooltipEvents, function(evt){ |
| 3241 | var activeSegments = (evt.type !== 'mouseout') ? this.getSegmentsAtEvent(evt) : []; |
| 3242 | helpers.each(this.segments,function(segment){ |
| 3243 | segment.restore(["fillColor"]); |
| 3244 | }); |
| 3245 | helpers.each(activeSegments,function(activeSegment){ |
| 3246 | activeSegment.fillColor = activeSegment.highlightColor; |
| 3247 | }); |
| 3248 | this.showTooltip(activeSegments); |
| 3249 | }); |
| 3250 | } |
| 3251 | |
| 3252 | this.render(); |
| 3253 | }, |
| 3254 | getSegmentsAtEvent : function(e){ |
| 3255 | var segmentsArray = []; |
| 3256 | |
| 3257 | var location = helpers.getRelativePosition(e); |
| 3258 | |
| 3259 | helpers.each(this.segments,function(segment){ |
| 3260 | if (segment.inRange(location.x,location.y)) segmentsArray.push(segment); |
| 3261 | },this); |
| 3262 | return segmentsArray; |
| 3263 | }, |
| 3264 | addData : function(segment, atIndex, silent){ |
| 3265 | var index = atIndex || this.segments.length; |
| 3266 | |
| 3267 | this.segments.splice(index, 0, new this.SegmentArc({ |
| 3268 | fillColor: segment.color, |
| 3269 | highlightColor: segment.highlight || segment.color, |
| 3270 | label: segment.label, |
| 3271 | value: segment.value, |
| 3272 | outerRadius: (this.options.animateScale) ? 0 : this.scale.calculateCenterOffset(segment.value), |
| 3273 | circumference: (this.options.animateRotate) ? 0 : this.scale.getCircumference(), |
| 3274 | startAngle: Math.PI * 1.5 |
| 3275 | })); |
| 3276 | if (!silent){ |
| 3277 | this.reflow(); |
| 3278 | this.update(); |
| 3279 | } |
| 3280 | }, |
| 3281 | removeData: function(atIndex){ |
| 3282 | var indexToDelete = (helpers.isNumber(atIndex)) ? atIndex : this.segments.length-1; |
| 3283 | this.segments.splice(indexToDelete, 1); |
| 3284 | this.reflow(); |
| 3285 | this.update(); |
| 3286 | }, |
| 3287 | calculateTotal: function(data){ |
| 3288 | this.total = 0; |
| 3289 | helpers.each(data,function(segment){ |
| 3290 | this.total += segment.value; |
| 3291 | },this); |
| 3292 | this.scale.valuesCount = this.segments.length; |
| 3293 | }, |
| 3294 | updateScaleRange: function(datapoints){ |
| 3295 | var valuesArray = []; |
| 3296 | helpers.each(datapoints,function(segment){ |
| 3297 | valuesArray.push(segment.value); |
| 3298 | }); |
| 3299 | |
| 3300 | var scaleSizes = (this.options.scaleOverride) ? |
| 3301 | { |
| 3302 | steps: this.options.scaleSteps, |
| 3303 | stepValue: this.options.scaleStepWidth, |
| 3304 | min: this.options.scaleStartValue, |
| 3305 | max: this.options.scaleStartValue + (this.options.scaleSteps * this.options.scaleStepWidth) |
| 3306 | } : |
| 3307 | helpers.calculateScaleRange( |
| 3308 | valuesArray, |
| 3309 | helpers.min([this.chart.width, this.chart.height])/2, |
| 3310 | this.options.scaleFontSize, |
| 3311 | this.options.scaleBeginAtZero, |
| 3312 | this.options.scaleIntegersOnly |
| 3313 | ); |
| 3314 | |
| 3315 | helpers.extend( |
| 3316 | this.scale, |
| 3317 | scaleSizes, |
| 3318 | { |
| 3319 | size: helpers.min([this.chart.width, this.chart.height]), |
| 3320 | xCenter: this.chart.width/2, |
| 3321 | yCenter: this.chart.height/2 |
| 3322 | } |
| 3323 | ); |
| 3324 | |
| 3325 | }, |
| 3326 | update : function(){ |
| 3327 | this.calculateTotal(this.segments); |
| 3328 | |
| 3329 | helpers.each(this.segments,function(segment){ |
| 3330 | segment.save(); |
| 3331 | }); |
| 3332 | |
| 3333 | this.reflow(); |
| 3334 | this.render(); |
| 3335 | }, |
| 3336 | reflow : function(){ |
| 3337 | helpers.extend(this.SegmentArc.prototype,{ |
| 3338 | x : this.chart.width/2, |
| 3339 | y : this.chart.height/2 |
| 3340 | }); |
| 3341 | this.updateScaleRange(this.segments); |
| 3342 | this.scale.update(); |
| 3343 | |
| 3344 | helpers.extend(this.scale,{ |
| 3345 | xCenter: this.chart.width/2, |
| 3346 | yCenter: this.chart.height/2 |
| 3347 | }); |
| 3348 | |
| 3349 | helpers.each(this.segments, function(segment){ |
| 3350 | segment.update({ |
| 3351 | outerRadius : this.scale.calculateCenterOffset(segment.value) |
| 3352 | }); |
| 3353 | }, this); |
| 3354 | |
| 3355 | }, |
| 3356 | draw : function(ease){ |
| 3357 | var easingDecimal = ease || 1; |
| 3358 | //Clear & draw the canvas |
| 3359 | this.clear(); |
| 3360 | helpers.each(this.segments,function(segment, index){ |
| 3361 | segment.transition({ |
| 3362 | circumference : this.scale.getCircumference(), |
| 3363 | outerRadius : this.scale.calculateCenterOffset(segment.value) |
| 3364 | },easingDecimal); |
| 3365 | |
| 3366 | segment.endAngle = segment.startAngle + segment.circumference; |
| 3367 | |
| 3368 | // If we've removed the first segment we need to set the first one to |
| 3369 | // start at the top. |
| 3370 | if (index === 0){ |
| 3371 | segment.startAngle = Math.PI * 1.5; |
| 3372 | } |
| 3373 | |
| 3374 | //Check to see if it's the last segment, if not get the next and update the start angle |
| 3375 | if (index < this.segments.length - 1){ |
| 3376 | this.segments[index+1].startAngle = segment.endAngle; |
| 3377 | } |
| 3378 | segment.draw(); |
| 3379 | }, this); |
| 3380 | this.scale.draw(); |
| 3381 | } |
| 3382 | }); |
| 3383 | |
| 3384 | }).call(this); |
| 3385 | |
| 3386 | (function(){ |
| 3387 | "use strict"; |
| 3388 | |
| 3389 | var root = this, |
| 3390 | Chart = root.Chart, |
| 3391 | helpers = Chart.helpers; |
| 3392 | |
| 3393 | |
| 3394 | |
| 3395 | Chart.Type.extend({ |
| 3396 | name: "Radar", |
| 3397 | defaults:{ |
| 3398 | //Boolean - Whether to show lines for each scale point |
| 3399 | scaleShowLine : true, |
| 3400 | |
| 3401 | //Boolean - Whether we show the angle lines out of the radar |
| 3402 | angleShowLineOut : true, |
| 3403 | |
| 3404 | //Boolean - Whether to show labels on the scale |
| 3405 | scaleShowLabels : false, |
| 3406 | |
| 3407 | // Boolean - Whether the scale should begin at zero |
| 3408 | scaleBeginAtZero : true, |
| 3409 | |
| 3410 | //String - Colour of the angle line |
| 3411 | angleLineColor : "rgba(0,0,0,.1)", |
| 3412 | |
| 3413 | //Number - Pixel width of the angle line |
| 3414 | angleLineWidth : 1, |
| 3415 | |
| 3416 | //String - Point label font declaration |
| 3417 | pointLabelFontFamily : "'Arial'", |
| 3418 | |
| 3419 | //String - Point label font weight |
| 3420 | pointLabelFontStyle : "normal", |
| 3421 | |
| 3422 | //Number - Point label font size in pixels |
| 3423 | pointLabelFontSize : 10, |
| 3424 | |
| 3425 | //String - Point label font colour |
| 3426 | pointLabelFontColor : "#666", |
| 3427 | |
| 3428 | //Boolean - Whether to show a dot for each point |
| 3429 | pointDot : true, |
| 3430 | |
| 3431 | //Number - Radius of each point dot in pixels |
| 3432 | pointDotRadius : 3, |
| 3433 | |
| 3434 | //Number - Pixel width of point dot stroke |
| 3435 | pointDotStrokeWidth : 1, |
| 3436 | |
| 3437 | //Number - amount extra to add to the radius to cater for hit detection outside the drawn point |
| 3438 | pointHitDetectionRadius : 20, |
| 3439 | |
| 3440 | //Boolean - Whether to show a stroke for datasets |
| 3441 | datasetStroke : true, |
| 3442 | |
| 3443 | //Number - Pixel width of dataset stroke |
| 3444 | datasetStrokeWidth : 2, |
| 3445 | |
| 3446 | //Boolean - Whether to fill the dataset with a colour |
| 3447 | datasetFill : true, |
| 3448 | |
| 3449 | //String - A legend template |
| 3450 | legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"><%if(datasets[i].label){%><%=datasets[i].label%><%}%></span></li><%}%></ul>" |
| 3451 | |
| 3452 | }, |
| 3453 | |
| 3454 | initialize: function(data){ |
| 3455 | this.PointClass = Chart.Point.extend({ |
| 3456 | strokeWidth : this.options.pointDotStrokeWidth, |
| 3457 | radius : this.options.pointDotRadius, |
| 3458 | display: this.options.pointDot, |
| 3459 | hitDetectionRadius : this.options.pointHitDetectionRadius, |
| 3460 | ctx : this.chart.ctx |
| 3461 | }); |
| 3462 | |
| 3463 | this.datasets = []; |
| 3464 | |
| 3465 | this.buildScale(data); |
| 3466 | |
| 3467 | //Set up tooltip events on the chart |
| 3468 | if (this.options.showTooltips){ |
| 3469 | helpers.bindEvents(this, this.options.tooltipEvents, function(evt){ |
| 3470 | var activePointsCollection = (evt.type !== 'mouseout') ? this.getPointsAtEvent(evt) : []; |
| 3471 | |
| 3472 | this.eachPoints(function(point){ |
| 3473 | point.restore(['fillColor', 'strokeColor']); |
| 3474 | }); |
| 3475 | helpers.each(activePointsCollection, function(activePoint){ |
| 3476 | activePoint.fillColor = activePoint.highlightFill; |
| 3477 | activePoint.strokeColor = activePoint.highlightStroke; |
| 3478 | }); |
| 3479 | |
| 3480 | this.showTooltip(activePointsCollection); |
| 3481 | }); |
| 3482 | } |
| 3483 | |
| 3484 | //Iterate through each of the datasets, and build this into a property of the chart |
| 3485 | helpers.each(data.datasets,function(dataset){ |
| 3486 | |
| 3487 | var datasetObject = { |
| 3488 | label: dataset.label || null, |
| 3489 | fillColor : dataset.fillColor, |
| 3490 | strokeColor : dataset.strokeColor, |
| 3491 | pointColor : dataset.pointColor, |
| 3492 | pointStrokeColor : dataset.pointStrokeColor, |
| 3493 | points : [] |
| 3494 | }; |
| 3495 | |
| 3496 | this.datasets.push(datasetObject); |
| 3497 | |
| 3498 | helpers.each(dataset.data,function(dataPoint,index){ |
| 3499 | //Add a new point for each piece of data, passing any required data to draw. |
| 3500 | var pointPosition; |
| 3501 | if (!this.scale.animation){ |
| 3502 | pointPosition = this.scale.getPointPosition(index, this.scale.calculateCenterOffset(dataPoint)); |
| 3503 | } |
| 3504 | datasetObject.points.push(new this.PointClass({ |
| 3505 | value : dataPoint, |
| 3506 | label : data.labels[index], |
| 3507 | datasetLabel: dataset.label, |
| 3508 | x: (this.options.animation) ? this.scale.xCenter : pointPosition.x, |
| 3509 | y: (this.options.animation) ? this.scale.yCenter : pointPosition.y, |
| 3510 | strokeColor : dataset.pointStrokeColor, |
| 3511 | fillColor : dataset.pointColor, |
| 3512 | highlightFill : dataset.pointHighlightFill || dataset.pointColor, |
| 3513 | highlightStroke : dataset.pointHighlightStroke || dataset.pointStrokeColor |
| 3514 | })); |
| 3515 | },this); |
| 3516 | |
| 3517 | },this); |
| 3518 | |
| 3519 | this.render(); |
| 3520 | }, |
| 3521 | eachPoints : function(callback){ |
| 3522 | helpers.each(this.datasets,function(dataset){ |
| 3523 | helpers.each(dataset.points,callback,this); |
| 3524 | },this); |
| 3525 | }, |
| 3526 | |
| 3527 | getPointsAtEvent : function(evt){ |
| 3528 | var mousePosition = helpers.getRelativePosition(evt), |
| 3529 | fromCenter = helpers.getAngleFromPoint({ |
| 3530 | x: this.scale.xCenter, |
| 3531 | y: this.scale.yCenter |
| 3532 | }, mousePosition); |
| 3533 | |
| 3534 | var anglePerIndex = (Math.PI * 2) /this.scale.valuesCount, |
| 3535 | pointIndex = Math.round((fromCenter.angle - Math.PI * 1.5) / anglePerIndex), |
| 3536 | activePointsCollection = []; |
| 3537 | |
| 3538 | // If we're at the top, make the pointIndex 0 to get the first of the array. |
| 3539 | if (pointIndex >= this.scale.valuesCount || pointIndex < 0){ |
| 3540 | pointIndex = 0; |
| 3541 | } |
| 3542 | |
| 3543 | if (fromCenter.distance <= this.scale.drawingArea){ |
| 3544 | helpers.each(this.datasets, function(dataset){ |
| 3545 | activePointsCollection.push(dataset.points[pointIndex]); |
| 3546 | }); |
| 3547 | } |
| 3548 | |
| 3549 | return activePointsCollection; |
| 3550 | }, |
| 3551 | |
| 3552 | buildScale : function(data){ |
| 3553 | this.scale = new Chart.RadialScale({ |
| 3554 | display: this.options.showScale, |
| 3555 | fontStyle: this.options.scaleFontStyle, |
| 3556 | fontSize: this.options.scaleFontSize, |
| 3557 | fontFamily: this.options.scaleFontFamily, |
| 3558 | fontColor: this.options.scaleFontColor, |
| 3559 | showLabels: this.options.scaleShowLabels, |
| 3560 | showLabelBackdrop: this.options.scaleShowLabelBackdrop, |
| 3561 | backdropColor: this.options.scaleBackdropColor, |
| 3562 | backgroundColors: this.options.scaleBackgroundColors, |
| 3563 | backdropPaddingY : this.options.scaleBackdropPaddingY, |
| 3564 | backdropPaddingX: this.options.scaleBackdropPaddingX, |
| 3565 | lineWidth: (this.options.scaleShowLine) ? this.options.scaleLineWidth : 0, |
| 3566 | lineColor: this.options.scaleLineColor, |
| 3567 | angleLineColor : this.options.angleLineColor, |
| 3568 | angleLineWidth : (this.options.angleShowLineOut) ? this.options.angleLineWidth : 0, |
| 3569 | // Point labels at the edge of each line |
| 3570 | pointLabelFontColor : this.options.pointLabelFontColor, |
| 3571 | pointLabelFontSize : this.options.pointLabelFontSize, |
| 3572 | pointLabelFontFamily : this.options.pointLabelFontFamily, |
| 3573 | pointLabelFontStyle : this.options.pointLabelFontStyle, |
| 3574 | height : this.chart.height, |
| 3575 | width: this.chart.width, |
| 3576 | xCenter: this.chart.width/2, |
| 3577 | yCenter: this.chart.height/2, |
| 3578 | ctx : this.chart.ctx, |
| 3579 | templateString: this.options.scaleLabel, |
| 3580 | labels: data.labels, |
| 3581 | valuesCount: data.datasets[0].data.length |
| 3582 | }); |
| 3583 | |
| 3584 | this.scale.setScaleSize(); |
| 3585 | this.updateScaleRange(data.datasets); |
| 3586 | this.scale.buildYLabels(); |
| 3587 | }, |
| 3588 | updateScaleRange: function(datasets){ |
| 3589 | var valuesArray = (function(){ |
| 3590 | var totalDataArray = []; |
| 3591 | helpers.each(datasets,function(dataset){ |
| 3592 | if (dataset.data){ |
| 3593 | totalDataArray = totalDataArray.concat(dataset.data); |
| 3594 | } |
| 3595 | else { |
| 3596 | helpers.each(dataset.points, function(point){ |
| 3597 | totalDataArray.push(point.value); |
| 3598 | }); |
| 3599 | } |
| 3600 | }); |
| 3601 | return totalDataArray; |
| 3602 | })(); |
| 3603 | |
| 3604 | |
| 3605 | var scaleSizes = (this.options.scaleOverride) ? |
| 3606 | { |
| 3607 | steps: this.options.scaleSteps, |
| 3608 | stepValue: this.options.scaleStepWidth, |
| 3609 | min: this.options.scaleStartValue, |
| 3610 | max: this.options.scaleStartValue + (this.options.scaleSteps * this.options.scaleStepWidth) |
| 3611 | } : |
| 3612 | helpers.calculateScaleRange( |
| 3613 | valuesArray, |
| 3614 | helpers.min([this.chart.width, this.chart.height])/2, |
| 3615 | this.options.scaleFontSize, |
| 3616 | this.options.scaleBeginAtZero, |
| 3617 | this.options.scaleIntegersOnly |
| 3618 | ); |
| 3619 | |
| 3620 | helpers.extend( |
| 3621 | this.scale, |
| 3622 | scaleSizes |
| 3623 | ); |
| 3624 | |
| 3625 | }, |
| 3626 | addData : function(valuesArray,label){ |
| 3627 | //Map the values array for each of the datasets |
| 3628 | this.scale.valuesCount++; |
| 3629 | helpers.each(valuesArray,function(value,datasetIndex){ |
| 3630 | var pointPosition = this.scale.getPointPosition(this.scale.valuesCount, this.scale.calculateCenterOffset(value)); |
| 3631 | this.datasets[datasetIndex].points.push(new this.PointClass({ |
| 3632 | value : value, |
| 3633 | label : label, |
| 3634 | datasetLabel: this.datasets[datasetIndex].label, |
| 3635 | x: pointPosition.x, |
| 3636 | y: pointPosition.y, |
| 3637 | strokeColor : this.datasets[datasetIndex].pointStrokeColor, |
| 3638 | fillColor : this.datasets[datasetIndex].pointColor |
| 3639 | })); |
| 3640 | },this); |
| 3641 | |
| 3642 | this.scale.labels.push(label); |
| 3643 | |
| 3644 | this.reflow(); |
| 3645 | |
| 3646 | this.update(); |
| 3647 | }, |
| 3648 | removeData : function(){ |
| 3649 | this.scale.valuesCount--; |
| 3650 | this.scale.labels.shift(); |
| 3651 | helpers.each(this.datasets,function(dataset){ |
| 3652 | dataset.points.shift(); |
| 3653 | },this); |
| 3654 | this.reflow(); |
| 3655 | this.update(); |
| 3656 | }, |
| 3657 | update : function(){ |
| 3658 | this.eachPoints(function(point){ |
| 3659 | point.save(); |
| 3660 | }); |
| 3661 | this.reflow(); |
| 3662 | this.render(); |
| 3663 | }, |
| 3664 | reflow: function(){ |
| 3665 | helpers.extend(this.scale, { |
| 3666 | width : this.chart.width, |
| 3667 | height: this.chart.height, |
| 3668 | size : helpers.min([this.chart.width, this.chart.height]), |
| 3669 | xCenter: this.chart.width/2, |
| 3670 | yCenter: this.chart.height/2 |
| 3671 | }); |
| 3672 | this.updateScaleRange(this.datasets); |
| 3673 | this.scale.setScaleSize(); |
| 3674 | this.scale.buildYLabels(); |
| 3675 | }, |
| 3676 | draw : function(ease){ |
| 3677 | var easeDecimal = ease || 1, |
| 3678 | ctx = this.chart.ctx; |
| 3679 | this.clear(); |
| 3680 | this.scale.draw(); |
| 3681 | |
| 3682 | helpers.each(this.datasets,function(dataset){ |
| 3683 | |
| 3684 | //Transition each point first so that the line and point drawing isn't out of sync |
| 3685 | helpers.each(dataset.points,function(point,index){ |
| 3686 | if (point.hasValue()){ |
| 3687 | point.transition(this.scale.getPointPosition(index, this.scale.calculateCenterOffset(point.value)), easeDecimal); |
| 3688 | } |
| 3689 | },this); |
| 3690 | |
| 3691 | |
| 3692 | |
| 3693 | //Draw the line between all the points |
| 3694 | ctx.lineWidth = this.options.datasetStrokeWidth; |
| 3695 | ctx.strokeStyle = dataset.strokeColor; |
| 3696 | ctx.beginPath(); |
| 3697 | helpers.each(dataset.points,function(point,index){ |
| 3698 | if (index === 0){ |
| 3699 | ctx.moveTo(point.x,point.y); |
| 3700 | } |
| 3701 | else{ |
| 3702 | ctx.lineTo(point.x,point.y); |
| 3703 | } |
| 3704 | },this); |
| 3705 | ctx.closePath(); |
| 3706 | ctx.stroke(); |
| 3707 | |
| 3708 | ctx.fillStyle = dataset.fillColor; |
| 3709 | if(this.options.datasetFill){ |
| 3710 | ctx.fill(); |
| 3711 | } |
| 3712 | //Now draw the points over the line |
| 3713 | //A little inefficient double looping, but better than the line |
| 3714 | //lagging behind the point positions |
| 3715 | helpers.each(dataset.points,function(point){ |
| 3716 | if (point.hasValue()){ |
| 3717 | point.draw(); |
| 3718 | } |
| 3719 | }); |
| 3720 | |
| 3721 | },this); |
| 3722 | |
| 3723 | } |
| 3724 | |
| 3725 | }); |
| 3726 | |
| 3727 | |
| 3728 | |
| 3729 | |
| 3730 | |
| 3731 | }).call(this); |