chartjs-rails 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chartjs-rails.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marek Jelen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # Chart.js - Rails
2
+
3
+ (Chart.js)[http://www.chartjs.org] is "Easy, object orientated client side graphs for designers and developers".
4
+
5
+ This is a package for simple integration in Rails.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'chartjs-rails'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install chartjs-rails
20
+
21
+ ## Usage
22
+
23
+ Require using Assets pipeline
24
+
25
+ //= require chart
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chartjs/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'chartjs-rails'
8
+ spec.version = Chartjs::Rails::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.authors = ['Marek Jelen']
11
+ spec.email = %w(marek@jelen.biz)
12
+ spec.summary = %q{Chart.js packaged for Rails}
13
+ spec.description = %q{Easy, object orientated client side graphs for designers and developers}
14
+ spec.homepage = 'http://github.com/marekjelen/chartjs-rails'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = %w(lib)
21
+
22
+ spec.add_dependency 'railties', '>= 3.1'
23
+
24
+ spec.add_development_dependency 'rails', '>= 3.1'
25
+ spec.add_development_dependency 'bundler', '~> 1.3'
26
+ spec.add_development_dependency 'rake'
27
+ end
@@ -0,0 +1 @@
1
+ require 'chartjs/rails'
@@ -0,0 +1,8 @@
1
+ require 'chartjs/rails/engine'
2
+ require 'chartjs/rails/version'
3
+
4
+ module Chartjs
5
+ module Rails
6
+
7
+ end
8
+ end
@@ -0,0 +1,6 @@
1
+ module Chartjs
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Chartjs
2
+ module Rails
3
+ VERSION = "0.1"
4
+ D3_VERSION = "0.1"
5
+ end
6
+ end
@@ -0,0 +1,1430 @@
1
+ //Define the global Chart Variable as a class.
2
+ var Chart = function(context){
3
+
4
+ var chart = this;
5
+
6
+
7
+ //Easing functions adapted from Robert Penner's easing equations
8
+ //http://www.robertpenner.com/easing/
9
+
10
+ var animationOptions = {
11
+ linear : function (t){
12
+ return t;
13
+ },
14
+ easeInQuad: function (t) {
15
+ return t*t;
16
+ },
17
+ easeOutQuad: function (t) {
18
+ return -1 *t*(t-2);
19
+ },
20
+ easeInOutQuad: function (t) {
21
+ if ((t/=1/2) < 1) return 1/2*t*t;
22
+ return -1/2 * ((--t)*(t-2) - 1);
23
+ },
24
+ easeInCubic: function (t) {
25
+ return t*t*t;
26
+ },
27
+ easeOutCubic: function (t) {
28
+ return 1*((t=t/1-1)*t*t + 1);
29
+ },
30
+ easeInOutCubic: function (t) {
31
+ if ((t/=1/2) < 1) return 1/2*t*t*t;
32
+ return 1/2*((t-=2)*t*t + 2);
33
+ },
34
+ easeInQuart: function (t) {
35
+ return t*t*t*t;
36
+ },
37
+ easeOutQuart: function (t) {
38
+ return -1 * ((t=t/1-1)*t*t*t - 1);
39
+ },
40
+ easeInOutQuart: function (t) {
41
+ if ((t/=1/2) < 1) return 1/2*t*t*t*t;
42
+ return -1/2 * ((t-=2)*t*t*t - 2);
43
+ },
44
+ easeInQuint: function (t) {
45
+ return 1*(t/=1)*t*t*t*t;
46
+ },
47
+ easeOutQuint: function (t) {
48
+ return 1*((t=t/1-1)*t*t*t*t + 1);
49
+ },
50
+ easeInOutQuint: function (t) {
51
+ if ((t/=1/2) < 1) return 1/2*t*t*t*t*t;
52
+ return 1/2*((t-=2)*t*t*t*t + 2);
53
+ },
54
+ easeInSine: function (t) {
55
+ return -1 * Math.cos(t/1 * (Math.PI/2)) + 1;
56
+ },
57
+ easeOutSine: function (t) {
58
+ return 1 * Math.sin(t/1 * (Math.PI/2));
59
+ },
60
+ easeInOutSine: function (t) {
61
+ return -1/2 * (Math.cos(Math.PI*t/1) - 1);
62
+ },
63
+ easeInExpo: function (t) {
64
+ return (t==0) ? 1 : 1 * Math.pow(2, 10 * (t/1 - 1));
65
+ },
66
+ easeOutExpo: function (t) {
67
+ return (t==1) ? 1 : 1 * (-Math.pow(2, -10 * t/1) + 1);
68
+ },
69
+ easeInOutExpo: function (t) {
70
+ if (t==0) return 0;
71
+ if (t==1) return 1;
72
+ if ((t/=1/2) < 1) return 1/2 * Math.pow(2, 10 * (t - 1));
73
+ return 1/2 * (-Math.pow(2, -10 * --t) + 2);
74
+ },
75
+ easeInCirc: function (t) {
76
+ if (t>=1) return t;
77
+ return -1 * (Math.sqrt(1 - (t/=1)*t) - 1);
78
+ },
79
+ easeOutCirc: function (t) {
80
+ return 1 * Math.sqrt(1 - (t=t/1-1)*t);
81
+ },
82
+ easeInOutCirc: function (t) {
83
+ if ((t/=1/2) < 1) return -1/2 * (Math.sqrt(1 - t*t) - 1);
84
+ return 1/2 * (Math.sqrt(1 - (t-=2)*t) + 1);
85
+ },
86
+ easeInElastic: function (t) {
87
+ var s=1.70158;var p=0;var a=1;
88
+ if (t==0) return 0; if ((t/=1)==1) return 1; if (!p) p=1*.3;
89
+ if (a < Math.abs(1)) { a=1; var s=p/4; }
90
+ else var s = p/(2*Math.PI) * Math.asin (1/a);
91
+ return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*1-s)*(2*Math.PI)/p ));
92
+ },
93
+ easeOutElastic: function (t) {
94
+ var s=1.70158;var p=0;var a=1;
95
+ if (t==0) return 0; if ((t/=1)==1) return 1; if (!p) p=1*.3;
96
+ if (a < Math.abs(1)) { a=1; var s=p/4; }
97
+ else var s = p/(2*Math.PI) * Math.asin (1/a);
98
+ return a*Math.pow(2,-10*t) * Math.sin( (t*1-s)*(2*Math.PI)/p ) + 1;
99
+ },
100
+ easeInOutElastic: function (t) {
101
+ var s=1.70158;var p=0;var a=1;
102
+ if (t==0) return 0; if ((t/=1/2)==2) return 1; if (!p) p=1*(.3*1.5);
103
+ if (a < Math.abs(1)) { a=1; var s=p/4; }
104
+ else var s = p/(2*Math.PI) * Math.asin (1/a);
105
+ if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*1-s)*(2*Math.PI)/p ));
106
+ return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*1-s)*(2*Math.PI)/p )*.5 + 1;
107
+ },
108
+ easeInBack: function (t) {
109
+ var s = 1.70158;
110
+ return 1*(t/=1)*t*((s+1)*t - s);
111
+ },
112
+ easeOutBack: function (t) {
113
+ var s = 1.70158;
114
+ return 1*((t=t/1-1)*t*((s+1)*t + s) + 1);
115
+ },
116
+ easeInOutBack: function (t) {
117
+ var s = 1.70158;
118
+ if ((t/=1/2) < 1) return 1/2*(t*t*(((s*=(1.525))+1)*t - s));
119
+ return 1/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2);
120
+ },
121
+ easeInBounce: function (t) {
122
+ return 1 - animationOptions.easeOutBounce (1-t);
123
+ },
124
+ easeOutBounce: function (t) {
125
+ if ((t/=1) < (1/2.75)) {
126
+ return 1*(7.5625*t*t);
127
+ } else if (t < (2/2.75)) {
128
+ return 1*(7.5625*(t-=(1.5/2.75))*t + .75);
129
+ } else if (t < (2.5/2.75)) {
130
+ return 1*(7.5625*(t-=(2.25/2.75))*t + .9375);
131
+ } else {
132
+ return 1*(7.5625*(t-=(2.625/2.75))*t + .984375);
133
+ }
134
+ },
135
+ easeInOutBounce: function (t) {
136
+ if (t < 1/2) return animationOptions.easeInBounce (t*2) * .5;
137
+ return animationOptions.easeOutBounce (t*2-1) * .5 + 1*.5;
138
+ }
139
+ };
140
+
141
+ //Variables global to the chart
142
+ var width = context.canvas.width;
143
+ var height = context.canvas.height;
144
+
145
+
146
+ //High pixel density displays - multiply the size of the canvas height/width by the device pixel ratio, then scale.
147
+ if (window.devicePixelRatio) {
148
+ context.canvas.style.width = width + "px";
149
+ context.canvas.style.height = height + "px";
150
+ context.canvas.height = height * window.devicePixelRatio;
151
+ context.canvas.width = width * window.devicePixelRatio;
152
+ context.scale(window.devicePixelRatio, window.devicePixelRatio);
153
+ }
154
+
155
+ this.PolarArea = function(data,options){
156
+
157
+ chart.PolarArea.defaults = {
158
+ scaleOverlay : true,
159
+ scaleOverride : false,
160
+ scaleSteps : null,
161
+ scaleStepWidth : null,
162
+ scaleStartValue : null,
163
+ scaleShowLine : true,
164
+ scaleLineColor : "rgba(0,0,0,.1)",
165
+ scaleLineWidth : 1,
166
+ scaleShowLabels : true,
167
+ scaleLabel : "<%=value%>",
168
+ scaleFontFamily : "'Arial'",
169
+ scaleFontSize : 12,
170
+ scaleFontStyle : "normal",
171
+ scaleFontColor : "#666",
172
+ scaleShowLabelBackdrop : true,
173
+ scaleBackdropColor : "rgba(255,255,255,0.75)",
174
+ scaleBackdropPaddingY : 2,
175
+ scaleBackdropPaddingX : 2,
176
+ segmentShowStroke : true,
177
+ segmentStrokeColor : "#fff",
178
+ segmentStrokeWidth : 2,
179
+ animation : true,
180
+ animationSteps : 100,
181
+ animationEasing : "easeOutBounce",
182
+ animateRotate : true,
183
+ animateScale : false,
184
+ onAnimationComplete : null
185
+ };
186
+
187
+ var config = (options)? mergeChartConfig(chart.PolarArea.defaults,options) : chart.PolarArea.defaults;
188
+
189
+ return new PolarArea(data,config,context);
190
+ };
191
+
192
+ this.Radar = function(data,options){
193
+
194
+ chart.Radar.defaults = {
195
+ scaleOverlay : false,
196
+ scaleOverride : false,
197
+ scaleSteps : null,
198
+ scaleStepWidth : null,
199
+ scaleStartValue : null,
200
+ scaleShowLine : true,
201
+ scaleLineColor : "rgba(0,0,0,.1)",
202
+ scaleLineWidth : 1,
203
+ scaleShowLabels : false,
204
+ scaleLabel : "<%=value%>",
205
+ scaleFontFamily : "'Arial'",
206
+ scaleFontSize : 12,
207
+ scaleFontStyle : "normal",
208
+ scaleFontColor : "#666",
209
+ scaleShowLabelBackdrop : true,
210
+ scaleBackdropColor : "rgba(255,255,255,0.75)",
211
+ scaleBackdropPaddingY : 2,
212
+ scaleBackdropPaddingX : 2,
213
+ angleShowLineOut : true,
214
+ angleLineColor : "rgba(0,0,0,.1)",
215
+ angleLineWidth : 1,
216
+ pointLabelFontFamily : "'Arial'",
217
+ pointLabelFontStyle : "normal",
218
+ pointLabelFontSize : 12,
219
+ pointLabelFontColor : "#666",
220
+ pointDot : true,
221
+ pointDotRadius : 3,
222
+ pointDotStrokeWidth : 1,
223
+ datasetStroke : true,
224
+ datasetStrokeWidth : 2,
225
+ datasetFill : true,
226
+ animation : true,
227
+ animationSteps : 60,
228
+ animationEasing : "easeOutQuart",
229
+ onAnimationComplete : null
230
+ };
231
+
232
+ var config = (options)? mergeChartConfig(chart.Radar.defaults,options) : chart.Radar.defaults;
233
+
234
+ return new Radar(data,config,context);
235
+ };
236
+
237
+ this.Pie = function(data,options){
238
+ chart.Pie.defaults = {
239
+ segmentShowStroke : true,
240
+ segmentStrokeColor : "#fff",
241
+ segmentStrokeWidth : 2,
242
+ animation : true,
243
+ animationSteps : 100,
244
+ animationEasing : "easeOutBounce",
245
+ animateRotate : true,
246
+ animateScale : false,
247
+ onAnimationComplete : null
248
+ };
249
+
250
+ var config = (options)? mergeChartConfig(chart.Pie.defaults,options) : chart.Pie.defaults;
251
+
252
+ return new Pie(data,config,context);
253
+ };
254
+
255
+ this.Doughnut = function(data,options){
256
+
257
+ chart.Doughnut.defaults = {
258
+ segmentShowStroke : true,
259
+ segmentStrokeColor : "#fff",
260
+ segmentStrokeWidth : 2,
261
+ percentageInnerCutout : 50,
262
+ animation : true,
263
+ animationSteps : 100,
264
+ animationEasing : "easeOutBounce",
265
+ animateRotate : true,
266
+ animateScale : false,
267
+ onAnimationComplete : null
268
+ };
269
+
270
+ var config = (options)? mergeChartConfig(chart.Doughnut.defaults,options) : chart.Doughnut.defaults;
271
+
272
+ return new Doughnut(data,config,context);
273
+
274
+ };
275
+
276
+ this.Line = function(data,options){
277
+
278
+ chart.Line.defaults = {
279
+ scaleOverlay : false,
280
+ scaleOverride : false,
281
+ scaleSteps : null,
282
+ scaleStepWidth : null,
283
+ scaleStartValue : null,
284
+ scaleLineColor : "rgba(0,0,0,.1)",
285
+ scaleLineWidth : 1,
286
+ scaleShowLabels : true,
287
+ scaleLabel : "<%=value%>",
288
+ scaleFontFamily : "'Arial'",
289
+ scaleFontSize : 12,
290
+ scaleFontStyle : "normal",
291
+ scaleFontColor : "#666",
292
+ scaleShowGridLines : true,
293
+ scaleGridLineColor : "rgba(0,0,0,.05)",
294
+ scaleGridLineWidth : 1,
295
+ bezierCurve : true,
296
+ pointDot : true,
297
+ pointDotRadius : 4,
298
+ pointDotStrokeWidth : 2,
299
+ datasetStroke : true,
300
+ datasetStrokeWidth : 2,
301
+ datasetFill : true,
302
+ animation : true,
303
+ animationSteps : 60,
304
+ animationEasing : "easeOutQuart",
305
+ onAnimationComplete : null
306
+ };
307
+ var config = (options) ? mergeChartConfig(chart.Line.defaults,options) : chart.Line.defaults;
308
+
309
+ return new Line(data,config,context);
310
+ }
311
+
312
+ this.Bar = function(data,options){
313
+ chart.Bar.defaults = {
314
+ scaleOverlay : false,
315
+ scaleOverride : false,
316
+ scaleSteps : null,
317
+ scaleStepWidth : null,
318
+ scaleStartValue : null,
319
+ scaleLineColor : "rgba(0,0,0,.1)",
320
+ scaleLineWidth : 1,
321
+ scaleShowLabels : true,
322
+ scaleLabel : "<%=value%>",
323
+ scaleFontFamily : "'Arial'",
324
+ scaleFontSize : 12,
325
+ scaleFontStyle : "normal",
326
+ scaleFontColor : "#666",
327
+ scaleShowGridLines : true,
328
+ scaleGridLineColor : "rgba(0,0,0,.05)",
329
+ scaleGridLineWidth : 1,
330
+ barShowStroke : true,
331
+ barStrokeWidth : 2,
332
+ barValueSpacing : 5,
333
+ barDatasetSpacing : 1,
334
+ animation : true,
335
+ animationSteps : 60,
336
+ animationEasing : "easeOutQuart",
337
+ onAnimationComplete : null
338
+ };
339
+ var config = (options) ? mergeChartConfig(chart.Bar.defaults,options) : chart.Bar.defaults;
340
+
341
+ return new Bar(data,config,context);
342
+ }
343
+
344
+ var clear = function(c){
345
+ c.clearRect(0, 0, width, height);
346
+ };
347
+
348
+ var PolarArea = function(data,config,ctx){
349
+ var maxSize, scaleHop, calculatedScale, labelHeight, scaleHeight, valueBounds, labelTemplateString;
350
+
351
+
352
+ calculateDrawingSizes();
353
+
354
+ valueBounds = getValueBounds();
355
+
356
+ labelTemplateString = (config.scaleShowLabels)? config.scaleLabel : null;
357
+
358
+ //Check and set the scale
359
+ if (!config.scaleOverride){
360
+
361
+ calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
362
+ }
363
+ else {
364
+ calculatedScale = {
365
+ steps : config.scaleSteps,
366
+ stepValue : config.scaleStepWidth,
367
+ graphMin : config.scaleStartValue,
368
+ labels : []
369
+ }
370
+ for (var i=0; i<calculatedScale.steps; i++){
371
+ if(labelTemplateString){
372
+ calculatedScale.labels.push(tmpl(labelTemplateString,{value:(config.scaleStartValue + (config.scaleStepWidth * i)).toFixed(getDecimalPlaces (config.scaleStepWidth))}));
373
+ }
374
+ }
375
+ }
376
+
377
+ scaleHop = maxSize/(calculatedScale.steps);
378
+
379
+ //Wrap in an animation loop wrapper
380
+ animationLoop(config,drawScale,drawAllSegments,ctx);
381
+
382
+ function calculateDrawingSizes(){
383
+ maxSize = (Min([width,height])/2);
384
+ //Remove whatever is larger - the font size or line width.
385
+
386
+ maxSize -= Max([config.scaleFontSize*0.5,config.scaleLineWidth*0.5]);
387
+
388
+ labelHeight = config.scaleFontSize*2;
389
+ //If we're drawing the backdrop - add the Y padding to the label height and remove from drawing region.
390
+ if (config.scaleShowLabelBackdrop){
391
+ labelHeight += (2 * config.scaleBackdropPaddingY);
392
+ maxSize -= config.scaleBackdropPaddingY*1.5;
393
+ }
394
+
395
+ scaleHeight = maxSize;
396
+ //If the label height is less than 5, set it to 5 so we don't have lines on top of each other.
397
+ labelHeight = Default(labelHeight,5);
398
+ }
399
+ function drawScale(){
400
+ for (var i=0; i<calculatedScale.steps; i++){
401
+ //If the line object is there
402
+ if (config.scaleShowLine){
403
+ ctx.beginPath();
404
+ ctx.arc(width/2, height/2, scaleHop * (i + 1), 0, (Math.PI * 2), true);
405
+ ctx.strokeStyle = config.scaleLineColor;
406
+ ctx.lineWidth = config.scaleLineWidth;
407
+ ctx.stroke();
408
+ }
409
+
410
+ if (config.scaleShowLabels){
411
+ ctx.textAlign = "center";
412
+ ctx.font = config.scaleFontStyle + " " + config.scaleFontSize + "px " + config.scaleFontFamily;
413
+ var label = calculatedScale.labels[i];
414
+ //If the backdrop object is within the font object
415
+ if (config.scaleShowLabelBackdrop){
416
+ var textWidth = ctx.measureText(label).width;
417
+ ctx.fillStyle = config.scaleBackdropColor;
418
+ ctx.beginPath();
419
+ ctx.rect(
420
+ Math.round(width/2 - textWidth/2 - config.scaleBackdropPaddingX), //X
421
+ Math.round(height/2 - (scaleHop * (i + 1)) - config.scaleFontSize*0.5 - config.scaleBackdropPaddingY),//Y
422
+ Math.round(textWidth + (config.scaleBackdropPaddingX*2)), //Width
423
+ Math.round(config.scaleFontSize + (config.scaleBackdropPaddingY*2)) //Height
424
+ );
425
+ ctx.fill();
426
+ }
427
+ ctx.textBaseline = "middle";
428
+ ctx.fillStyle = config.scaleFontColor;
429
+ ctx.fillText(label,width/2,height/2 - (scaleHop * (i + 1)));
430
+ }
431
+ }
432
+ }
433
+ function drawAllSegments(animationDecimal){
434
+ var startAngle = -Math.PI/2,
435
+ angleStep = (Math.PI*2)/data.length,
436
+ scaleAnimation = 1,
437
+ rotateAnimation = 1;
438
+ if (config.animation) {
439
+ if (config.animateScale) {
440
+ scaleAnimation = animationDecimal;
441
+ }
442
+ if (config.animateRotate){
443
+ rotateAnimation = animationDecimal;
444
+ }
445
+ }
446
+
447
+ for (var i=0; i<data.length; i++){
448
+
449
+ ctx.beginPath();
450
+ ctx.arc(width/2,height/2,scaleAnimation * calculateOffset(data[i].value,calculatedScale,scaleHop),startAngle, startAngle + rotateAnimation*angleStep, false);
451
+ ctx.lineTo(width/2,height/2);
452
+ ctx.closePath();
453
+ ctx.fillStyle = data[i].color;
454
+ ctx.fill();
455
+
456
+ if(config.segmentShowStroke){
457
+ ctx.strokeStyle = config.segmentStrokeColor;
458
+ ctx.lineWidth = config.segmentStrokeWidth;
459
+ ctx.stroke();
460
+ }
461
+ startAngle += rotateAnimation*angleStep;
462
+ }
463
+ }
464
+ function getValueBounds() {
465
+ var upperValue = Number.MIN_VALUE;
466
+ var lowerValue = Number.MAX_VALUE;
467
+ for (var i=0; i<data.length; i++){
468
+ if (data[i].value > upperValue) {upperValue = data[i].value;}
469
+ if (data[i].value < lowerValue) {lowerValue = data[i].value;}
470
+ };
471
+
472
+ var maxSteps = Math.floor((scaleHeight / (labelHeight*0.66)));
473
+ var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
474
+
475
+ return {
476
+ maxValue : upperValue,
477
+ minValue : lowerValue,
478
+ maxSteps : maxSteps,
479
+ minSteps : minSteps
480
+ };
481
+
482
+
483
+ }
484
+ }
485
+
486
+ var Radar = function (data,config,ctx) {
487
+ var maxSize, scaleHop, calculatedScale, labelHeight, scaleHeight, valueBounds, labelTemplateString;
488
+
489
+ //If no labels are defined set to an empty array, so referencing length for looping doesn't blow up.
490
+ if (!data.labels) data.labels = [];
491
+
492
+ calculateDrawingSizes();
493
+
494
+ var valueBounds = getValueBounds();
495
+
496
+ labelTemplateString = (config.scaleShowLabels)? config.scaleLabel : null;
497
+
498
+ //Check and set the scale
499
+ if (!config.scaleOverride){
500
+
501
+ calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
502
+ }
503
+ else {
504
+ calculatedScale = {
505
+ steps : config.scaleSteps,
506
+ stepValue : config.scaleStepWidth,
507
+ graphMin : config.scaleStartValue,
508
+ labels : []
509
+ }
510
+ for (var i=0; i<calculatedScale.steps; i++){
511
+ if(labelTemplateString){
512
+ calculatedScale.labels.push(tmpl(labelTemplateString,{value:(config.scaleStartValue + (config.scaleStepWidth * i)).toFixed(getDecimalPlaces (config.scaleStepWidth))}));
513
+ }
514
+ }
515
+ }
516
+
517
+ scaleHop = maxSize/(calculatedScale.steps);
518
+
519
+ animationLoop(config,drawScale,drawAllDataPoints,ctx);
520
+
521
+ //Radar specific functions.
522
+ function drawAllDataPoints(animationDecimal){
523
+ var rotationDegree = (2*Math.PI)/data.datasets[0].data.length;
524
+
525
+ ctx.save();
526
+ //translate to the centre of the canvas.
527
+ ctx.translate(width/2,height/2);
528
+ ctx.rotate(rotationDegree);
529
+ //We accept multiple data sets for radar charts, so show loop through each set
530
+ for (var i=0; i<data.datasets.length; i++){
531
+ ctx.beginPath();
532
+
533
+ ctx.moveTo(0,animationDecimal*(-1*calculateOffset(data.datasets[i].data[0],calculatedScale,scaleHop)));
534
+ for (var j=1; j<data.datasets[i].data.length; j++){
535
+ ctx.rotate(rotationDegree);
536
+ ctx.lineTo(0,animationDecimal*(-1*calculateOffset(data.datasets[i].data[j],calculatedScale,scaleHop)));
537
+
538
+ }
539
+ ctx.closePath();
540
+
541
+
542
+ ctx.fillStyle = data.datasets[i].fillColor;
543
+ ctx.strokeStyle = data.datasets[i].strokeColor;
544
+ ctx.lineWidth = config.datasetStrokeWidth;
545
+ ctx.fill();
546
+ ctx.stroke();
547
+
548
+
549
+ if (config.pointDot){
550
+ ctx.fillStyle = data.datasets[i].pointColor;
551
+ ctx.strokeStyle = data.datasets[i].pointStrokeColor;
552
+ ctx.lineWidth = config.pointDotStrokeWidth;
553
+ for (var k=0; k<data.datasets[i].data.length; k++){
554
+ ctx.rotate(rotationDegree);
555
+ ctx.beginPath();
556
+ ctx.arc(0,animationDecimal*(-1*calculateOffset(data.datasets[i].data[k],calculatedScale,scaleHop)),config.pointDotRadius,2*Math.PI,false);
557
+ ctx.fill();
558
+ ctx.stroke();
559
+ }
560
+
561
+ }
562
+
563
+ }
564
+ ctx.restore();
565
+
566
+
567
+ }
568
+ function drawScale(){
569
+ var rotationDegree = (2*Math.PI)/data.datasets[0].data.length;
570
+ ctx.save();
571
+ ctx.translate(width / 2, height / 2);
572
+
573
+ if (config.angleShowLineOut){
574
+ ctx.strokeStyle = config.angleLineColor;
575
+ ctx.lineWidth = config.angleLineWidth;
576
+ for (var h=0; h<data.datasets[0].data.length; h++){
577
+
578
+ ctx.rotate(rotationDegree);
579
+ ctx.beginPath();
580
+ ctx.moveTo(0,0);
581
+ ctx.lineTo(0,-maxSize);
582
+ ctx.stroke();
583
+ }
584
+ }
585
+
586
+ for (var i=0; i<calculatedScale.steps; i++){
587
+ ctx.beginPath();
588
+
589
+ if(config.scaleShowLine){
590
+ ctx.strokeStyle = config.scaleLineColor;
591
+ ctx.lineWidth = config.scaleLineWidth;
592
+ ctx.moveTo(0,-scaleHop * (i+1));
593
+ for (var j=0; j<data.datasets[0].data.length; j++){
594
+ ctx.rotate(rotationDegree);
595
+ ctx.lineTo(0,-scaleHop * (i+1));
596
+ }
597
+ ctx.closePath();
598
+ ctx.stroke();
599
+
600
+ }
601
+
602
+ if (config.scaleShowLabels){
603
+ ctx.textAlign = 'center';
604
+ ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
605
+ ctx.textBaseline = "middle";
606
+
607
+ if (config.scaleShowLabelBackdrop){
608
+ var textWidth = ctx.measureText(calculatedScale.labels[i]).width;
609
+ ctx.fillStyle = config.scaleBackdropColor;
610
+ ctx.beginPath();
611
+ ctx.rect(
612
+ Math.round(- textWidth/2 - config.scaleBackdropPaddingX), //X
613
+ Math.round((-scaleHop * (i + 1)) - config.scaleFontSize*0.5 - config.scaleBackdropPaddingY),//Y
614
+ Math.round(textWidth + (config.scaleBackdropPaddingX*2)), //Width
615
+ Math.round(config.scaleFontSize + (config.scaleBackdropPaddingY*2)) //Height
616
+ );
617
+ ctx.fill();
618
+ }
619
+ ctx.fillStyle = config.scaleFontColor;
620
+ ctx.fillText(calculatedScale.labels[i],0,-scaleHop*(i+1));
621
+ }
622
+
623
+ }
624
+ for (var k=0; k<data.labels.length; k++){
625
+ ctx.font = config.pointLabelFontStyle + " " + config.pointLabelFontSize+"px " + config.pointLabelFontFamily;
626
+ ctx.fillStyle = config.pointLabelFontColor;
627
+ var opposite = Math.sin(rotationDegree*k) * (maxSize + config.pointLabelFontSize);
628
+ var adjacent = Math.cos(rotationDegree*k) * (maxSize + config.pointLabelFontSize);
629
+
630
+ if(rotationDegree*k == Math.PI || rotationDegree*k == 0){
631
+ ctx.textAlign = "center";
632
+ }
633
+ else if(rotationDegree*k > Math.PI){
634
+ ctx.textAlign = "right";
635
+ }
636
+ else{
637
+ ctx.textAlign = "left";
638
+ }
639
+
640
+ ctx.textBaseline = "middle";
641
+
642
+ ctx.fillText(data.labels[k],opposite,-adjacent);
643
+
644
+ }
645
+ ctx.restore();
646
+ };
647
+ function calculateDrawingSizes(){
648
+ maxSize = (Min([width,height])/2);
649
+
650
+ labelHeight = config.scaleFontSize*2;
651
+
652
+ var labelLength = 0;
653
+ for (var i=0; i<data.labels.length; i++){
654
+ ctx.font = config.pointLabelFontStyle + " " + config.pointLabelFontSize+"px " + config.pointLabelFontFamily;
655
+ var textMeasurement = ctx.measureText(data.labels[i]).width;
656
+ if(textMeasurement>labelLength) labelLength = textMeasurement;
657
+ }
658
+
659
+ //Figure out whats the largest - the height of the text or the width of what's there, and minus it from the maximum usable size.
660
+ maxSize -= Max([labelLength,((config.pointLabelFontSize/2)*1.5)]);
661
+
662
+ maxSize -= config.pointLabelFontSize;
663
+ maxSize = CapValue(maxSize, null, 0);
664
+ scaleHeight = maxSize;
665
+ //If the label height is less than 5, set it to 5 so we don't have lines on top of each other.
666
+ labelHeight = Default(labelHeight,5);
667
+ };
668
+ function getValueBounds() {
669
+ var upperValue = Number.MIN_VALUE;
670
+ var lowerValue = Number.MAX_VALUE;
671
+
672
+ for (var i=0; i<data.datasets.length; i++){
673
+ for (var j=0; j<data.datasets[i].data.length; j++){
674
+ if (data.datasets[i].data[j] > upperValue){upperValue = data.datasets[i].data[j]}
675
+ if (data.datasets[i].data[j] < lowerValue){lowerValue = data.datasets[i].data[j]}
676
+ }
677
+ }
678
+
679
+ var maxSteps = Math.floor((scaleHeight / (labelHeight*0.66)));
680
+ var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
681
+
682
+ return {
683
+ maxValue : upperValue,
684
+ minValue : lowerValue,
685
+ maxSteps : maxSteps,
686
+ minSteps : minSteps
687
+ };
688
+
689
+
690
+ }
691
+ }
692
+
693
+ var Pie = function(data,config,ctx){
694
+ var segmentTotal = 0;
695
+
696
+ //In case we have a canvas that is not a square. Minus 5 pixels as padding round the edge.
697
+ var pieRadius = Min([height/2,width/2]) - 5;
698
+
699
+ for (var i=0; i<data.length; i++){
700
+ segmentTotal += data[i].value;
701
+ }
702
+
703
+
704
+ animationLoop(config,null,drawPieSegments,ctx);
705
+
706
+ function drawPieSegments (animationDecimal){
707
+ var cumulativeAngle = -Math.PI/2,
708
+ scaleAnimation = 1,
709
+ rotateAnimation = 1;
710
+ if (config.animation) {
711
+ if (config.animateScale) {
712
+ scaleAnimation = animationDecimal;
713
+ }
714
+ if (config.animateRotate){
715
+ rotateAnimation = animationDecimal;
716
+ }
717
+ }
718
+ for (var i=0; i<data.length; i++){
719
+ var segmentAngle = rotateAnimation * ((data[i].value/segmentTotal) * (Math.PI*2));
720
+ ctx.beginPath();
721
+ ctx.arc(width/2,height/2,scaleAnimation * pieRadius,cumulativeAngle,cumulativeAngle + segmentAngle);
722
+ ctx.lineTo(width/2,height/2);
723
+ ctx.closePath();
724
+ ctx.fillStyle = data[i].color;
725
+ ctx.fill();
726
+
727
+ if(config.segmentShowStroke){
728
+ ctx.lineWidth = config.segmentStrokeWidth;
729
+ ctx.strokeStyle = config.segmentStrokeColor;
730
+ ctx.stroke();
731
+ }
732
+ cumulativeAngle += segmentAngle;
733
+ }
734
+ }
735
+ }
736
+
737
+ var Doughnut = function(data,config,ctx){
738
+ var segmentTotal = 0;
739
+
740
+ //In case we have a canvas that is not a square. Minus 5 pixels as padding round the edge.
741
+ var doughnutRadius = Min([height/2,width/2]) - 5;
742
+
743
+ var cutoutRadius = doughnutRadius * (config.percentageInnerCutout/100);
744
+
745
+ for (var i=0; i<data.length; i++){
746
+ segmentTotal += data[i].value;
747
+ }
748
+
749
+
750
+ animationLoop(config,null,drawPieSegments,ctx);
751
+
752
+
753
+ function drawPieSegments (animationDecimal){
754
+ var cumulativeAngle = -Math.PI/2,
755
+ scaleAnimation = 1,
756
+ rotateAnimation = 1;
757
+ if (config.animation) {
758
+ if (config.animateScale) {
759
+ scaleAnimation = animationDecimal;
760
+ }
761
+ if (config.animateRotate){
762
+ rotateAnimation = animationDecimal;
763
+ }
764
+ }
765
+ for (var i=0; i<data.length; i++){
766
+ var segmentAngle = rotateAnimation * ((data[i].value/segmentTotal) * (Math.PI*2));
767
+ ctx.beginPath();
768
+ ctx.arc(width/2,height/2,scaleAnimation * doughnutRadius,cumulativeAngle,cumulativeAngle + segmentAngle,false);
769
+ ctx.arc(width/2,height/2,scaleAnimation * cutoutRadius,cumulativeAngle + segmentAngle,cumulativeAngle,true);
770
+ ctx.closePath();
771
+ ctx.fillStyle = data[i].color;
772
+ ctx.fill();
773
+
774
+ if(config.segmentShowStroke){
775
+ ctx.lineWidth = config.segmentStrokeWidth;
776
+ ctx.strokeStyle = config.segmentStrokeColor;
777
+ ctx.stroke();
778
+ }
779
+ cumulativeAngle += segmentAngle;
780
+ }
781
+ }
782
+
783
+
784
+
785
+ }
786
+
787
+ var Line = function(data,config,ctx){
788
+ var maxSize, scaleHop, calculatedScale, labelHeight, scaleHeight, valueBounds, labelTemplateString, valueHop,widestXLabel, xAxisLength,yAxisPosX,xAxisPosY, rotateLabels = 0;
789
+
790
+ calculateDrawingSizes();
791
+
792
+ valueBounds = getValueBounds();
793
+ //Check and set the scale
794
+ labelTemplateString = (config.scaleShowLabels)? config.scaleLabel : "";
795
+ if (!config.scaleOverride){
796
+
797
+ calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
798
+ }
799
+ else {
800
+ calculatedScale = {
801
+ steps : config.scaleSteps,
802
+ stepValue : config.scaleStepWidth,
803
+ graphMin : config.scaleStartValue,
804
+ labels : []
805
+ }
806
+ for (var i=0; i<calculatedScale.steps; i++){
807
+ if(labelTemplateString){
808
+ calculatedScale.labels.push(tmpl(labelTemplateString,{value:(config.scaleStartValue + (config.scaleStepWidth * i)).toFixed(getDecimalPlaces (config.scaleStepWidth))}));
809
+ }
810
+ }
811
+ }
812
+
813
+ scaleHop = Math.floor(scaleHeight/calculatedScale.steps);
814
+ calculateXAxisSize();
815
+ animationLoop(config,drawScale,drawLines,ctx);
816
+
817
+ function drawLines(animPc){
818
+ for (var i=0; i<data.datasets.length; i++){
819
+ ctx.strokeStyle = data.datasets[i].strokeColor;
820
+ ctx.lineWidth = config.datasetStrokeWidth;
821
+ ctx.beginPath();
822
+ ctx.moveTo(yAxisPosX, xAxisPosY - animPc*(calculateOffset(data.datasets[i].data[0],calculatedScale,scaleHop)))
823
+
824
+ for (var j=1; j<data.datasets[i].data.length; j++){
825
+ if (config.bezierCurve){
826
+ ctx.bezierCurveTo(xPos(j-0.5),yPos(i,j-1),xPos(j-0.5),yPos(i,j),xPos(j),yPos(i,j));
827
+ }
828
+ else{
829
+ ctx.lineTo(xPos(j),yPos(i,j));
830
+ }
831
+ }
832
+ ctx.stroke();
833
+ if (config.datasetFill){
834
+ ctx.lineTo(yAxisPosX + (valueHop*(data.datasets[i].data.length-1)),xAxisPosY);
835
+ ctx.lineTo(yAxisPosX,xAxisPosY);
836
+ ctx.closePath();
837
+ ctx.fillStyle = data.datasets[i].fillColor;
838
+ ctx.fill();
839
+ }
840
+ else{
841
+ ctx.closePath();
842
+ }
843
+ if(config.pointDot){
844
+ ctx.fillStyle = data.datasets[i].pointColor;
845
+ ctx.strokeStyle = data.datasets[i].pointStrokeColor;
846
+ ctx.lineWidth = config.pointDotStrokeWidth;
847
+ for (var k=0; k<data.datasets[i].data.length; k++){
848
+ ctx.beginPath();
849
+ ctx.arc(yAxisPosX + (valueHop *k),xAxisPosY - animPc*(calculateOffset(data.datasets[i].data[k],calculatedScale,scaleHop)),config.pointDotRadius,0,Math.PI*2,true);
850
+ ctx.fill();
851
+ ctx.stroke();
852
+ }
853
+ }
854
+ }
855
+
856
+ function yPos(dataSet,iteration){
857
+ return xAxisPosY - animPc*(calculateOffset(data.datasets[dataSet].data[iteration],calculatedScale,scaleHop));
858
+ }
859
+ function xPos(iteration){
860
+ return yAxisPosX + (valueHop * iteration);
861
+ }
862
+ }
863
+ function drawScale(){
864
+ //X axis line
865
+ ctx.lineWidth = config.scaleLineWidth;
866
+ ctx.strokeStyle = config.scaleLineColor;
867
+ ctx.beginPath();
868
+ ctx.moveTo(width-widestXLabel/2+5,xAxisPosY);
869
+ ctx.lineTo(width-(widestXLabel/2)-xAxisLength-5,xAxisPosY);
870
+ ctx.stroke();
871
+
872
+
873
+ if (rotateLabels > 0){
874
+ ctx.save();
875
+ ctx.textAlign = "right";
876
+ }
877
+ else{
878
+ ctx.textAlign = "center";
879
+ }
880
+ ctx.fillStyle = config.scaleFontColor;
881
+ for (var i=0; i<data.labels.length; i++){
882
+ ctx.save();
883
+ if (rotateLabels > 0){
884
+ ctx.translate(yAxisPosX + i*valueHop,xAxisPosY + config.scaleFontSize);
885
+ ctx.rotate(-(rotateLabels * (Math.PI/180)));
886
+ ctx.fillText(data.labels[i], 0,0);
887
+ ctx.restore();
888
+ }
889
+
890
+ else{
891
+ ctx.fillText(data.labels[i], yAxisPosX + i*valueHop,xAxisPosY + config.scaleFontSize+3);
892
+ }
893
+
894
+ ctx.beginPath();
895
+ ctx.moveTo(yAxisPosX + i * valueHop, xAxisPosY+3);
896
+
897
+ //Check i isnt 0, so we dont go over the Y axis twice.
898
+ if(config.scaleShowGridLines && i>0){
899
+ ctx.lineWidth = config.scaleGridLineWidth;
900
+ ctx.strokeStyle = config.scaleGridLineColor;
901
+ ctx.lineTo(yAxisPosX + i * valueHop, 5);
902
+ }
903
+ else{
904
+ ctx.lineTo(yAxisPosX + i * valueHop, xAxisPosY+3);
905
+ }
906
+ ctx.stroke();
907
+ }
908
+
909
+ //Y axis
910
+ ctx.lineWidth = config.scaleLineWidth;
911
+ ctx.strokeStyle = config.scaleLineColor;
912
+ ctx.beginPath();
913
+ ctx.moveTo(yAxisPosX,xAxisPosY+5);
914
+ ctx.lineTo(yAxisPosX,5);
915
+ ctx.stroke();
916
+
917
+ ctx.textAlign = "right";
918
+ ctx.textBaseline = "middle";
919
+ for (var j=0; j<calculatedScale.steps; j++){
920
+ ctx.beginPath();
921
+ ctx.moveTo(yAxisPosX-3,xAxisPosY - ((j+1) * scaleHop));
922
+ if (config.scaleShowGridLines){
923
+ ctx.lineWidth = config.scaleGridLineWidth;
924
+ ctx.strokeStyle = config.scaleGridLineColor;
925
+ ctx.lineTo(yAxisPosX + xAxisLength + 5,xAxisPosY - ((j+1) * scaleHop));
926
+ }
927
+ else{
928
+ ctx.lineTo(yAxisPosX-0.5,xAxisPosY - ((j+1) * scaleHop));
929
+ }
930
+
931
+ ctx.stroke();
932
+
933
+ if (config.scaleShowLabels){
934
+ ctx.fillText(calculatedScale.labels[j],yAxisPosX-8,xAxisPosY - ((j+1) * scaleHop));
935
+ }
936
+ }
937
+
938
+
939
+ }
940
+ function calculateXAxisSize(){
941
+ var longestText = 1;
942
+ //if we are showing the labels
943
+ if (config.scaleShowLabels){
944
+ ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
945
+ for (var i=0; i<calculatedScale.labels.length; i++){
946
+ var measuredText = ctx.measureText(calculatedScale.labels[i]).width;
947
+ longestText = (measuredText > longestText)? measuredText : longestText;
948
+ }
949
+ //Add a little extra padding from the y axis
950
+ longestText +=10;
951
+ }
952
+ xAxisLength = width - longestText - widestXLabel;
953
+ valueHop = Math.floor(xAxisLength/(data.labels.length-1));
954
+
955
+ yAxisPosX = width-widestXLabel/2-xAxisLength;
956
+ xAxisPosY = scaleHeight + config.scaleFontSize/2;
957
+ }
958
+ function calculateDrawingSizes(){
959
+ maxSize = height;
960
+
961
+ //Need to check the X axis first - measure the length of each text metric, and figure out if we need to rotate by 45 degrees.
962
+ ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
963
+ widestXLabel = 1;
964
+ for (var i=0; i<data.labels.length; i++){
965
+ var textLength = ctx.measureText(data.labels[i]).width;
966
+ //If the text length is longer - make that equal to longest text!
967
+ widestXLabel = (textLength > widestXLabel)? textLength : widestXLabel;
968
+ }
969
+ if (width/data.labels.length < widestXLabel){
970
+ rotateLabels = 45;
971
+ if (width/data.labels.length < Math.cos(rotateLabels) * widestXLabel){
972
+ rotateLabels = 90;
973
+ maxSize -= widestXLabel;
974
+ }
975
+ else{
976
+ maxSize -= Math.sin(rotateLabels) * widestXLabel;
977
+ }
978
+ }
979
+ else{
980
+ maxSize -= config.scaleFontSize;
981
+ }
982
+
983
+ //Add a little padding between the x line and the text
984
+ maxSize -= 5;
985
+
986
+
987
+ labelHeight = config.scaleFontSize;
988
+
989
+ maxSize -= labelHeight;
990
+ //Set 5 pixels greater than the font size to allow for a little padding from the X axis.
991
+
992
+ scaleHeight = maxSize;
993
+
994
+ //Then get the area above we can safely draw on.
995
+
996
+ }
997
+ function getValueBounds() {
998
+ var upperValue = Number.MIN_VALUE;
999
+ var lowerValue = Number.MAX_VALUE;
1000
+ for (var i=0; i<data.datasets.length; i++){
1001
+ for (var j=0; j<data.datasets[i].data.length; j++){
1002
+ if ( data.datasets[i].data[j] > upperValue) { upperValue = data.datasets[i].data[j] };
1003
+ if ( data.datasets[i].data[j] < lowerValue) { lowerValue = data.datasets[i].data[j] };
1004
+ }
1005
+ };
1006
+
1007
+ var maxSteps = Math.floor((scaleHeight / (labelHeight*0.66)));
1008
+ var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
1009
+
1010
+ return {
1011
+ maxValue : upperValue,
1012
+ minValue : lowerValue,
1013
+ maxSteps : maxSteps,
1014
+ minSteps : minSteps
1015
+ };
1016
+
1017
+
1018
+ }
1019
+
1020
+
1021
+ }
1022
+
1023
+ var Bar = function(data,config,ctx){
1024
+ var maxSize, scaleHop, calculatedScale, labelHeight, scaleHeight, valueBounds, labelTemplateString, valueHop,widestXLabel, xAxisLength,yAxisPosX,xAxisPosY,barWidth, rotateLabels = 0;
1025
+
1026
+ calculateDrawingSizes();
1027
+
1028
+ valueBounds = getValueBounds();
1029
+ //Check and set the scale
1030
+ labelTemplateString = (config.scaleShowLabels)? config.scaleLabel : "";
1031
+ if (!config.scaleOverride){
1032
+
1033
+ calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
1034
+ }
1035
+ else {
1036
+ calculatedScale = {
1037
+ steps : config.scaleSteps,
1038
+ stepValue : config.scaleStepWidth,
1039
+ graphMin : config.scaleStartValue,
1040
+ labels : []
1041
+ }
1042
+ for (var i=0; i<calculatedScale.steps; i++){
1043
+ if(labelTemplateString){
1044
+ calculatedScale.labels.push(tmpl(labelTemplateString,{value:(config.scaleStartValue + (config.scaleStepWidth * i)).toFixed(getDecimalPlaces (config.scaleStepWidth))}));
1045
+ }
1046
+ }
1047
+ }
1048
+
1049
+ scaleHop = Math.floor(scaleHeight/calculatedScale.steps);
1050
+ calculateXAxisSize();
1051
+ animationLoop(config,drawScale,drawBars,ctx);
1052
+
1053
+ function drawBars(animPc){
1054
+ ctx.lineWidth = config.barStrokeWidth;
1055
+ for (var i=0; i<data.datasets.length; i++){
1056
+ ctx.fillStyle = data.datasets[i].fillColor;
1057
+ ctx.strokeStyle = data.datasets[i].strokeColor;
1058
+ for (var j=0; j<data.datasets[i].data.length; j++){
1059
+ var barOffset = yAxisPosX + config.barValueSpacing + valueHop*j + barWidth*i + config.barDatasetSpacing*i + config.barStrokeWidth*i;
1060
+
1061
+ ctx.beginPath();
1062
+ ctx.moveTo(barOffset, xAxisPosY);
1063
+ ctx.lineTo(barOffset, xAxisPosY - animPc*calculateOffset(data.datasets[i].data[j],calculatedScale,scaleHop)+(config.barStrokeWidth/2));
1064
+ ctx.lineTo(barOffset + barWidth, xAxisPosY - animPc*calculateOffset(data.datasets[i].data[j],calculatedScale,scaleHop)+(config.barStrokeWidth/2));
1065
+ ctx.lineTo(barOffset + barWidth, xAxisPosY);
1066
+ if(config.barShowStroke){
1067
+ ctx.stroke();
1068
+ }
1069
+ ctx.closePath();
1070
+ ctx.fill();
1071
+ }
1072
+ }
1073
+
1074
+ }
1075
+ function drawScale(){
1076
+ //X axis line
1077
+ ctx.lineWidth = config.scaleLineWidth;
1078
+ ctx.strokeStyle = config.scaleLineColor;
1079
+ ctx.beginPath();
1080
+ ctx.moveTo(width-widestXLabel/2+5,xAxisPosY);
1081
+ ctx.lineTo(width-(widestXLabel/2)-xAxisLength-5,xAxisPosY);
1082
+ ctx.stroke();
1083
+
1084
+
1085
+ if (rotateLabels > 0){
1086
+ ctx.save();
1087
+ ctx.textAlign = "right";
1088
+ }
1089
+ else{
1090
+ ctx.textAlign = "center";
1091
+ }
1092
+ ctx.fillStyle = config.scaleFontColor;
1093
+ for (var i=0; i<data.labels.length; i++){
1094
+ ctx.save();
1095
+ if (rotateLabels > 0){
1096
+ ctx.translate(yAxisPosX + i*valueHop,xAxisPosY + config.scaleFontSize);
1097
+ ctx.rotate(-(rotateLabels * (Math.PI/180)));
1098
+ ctx.fillText(data.labels[i], 0,0);
1099
+ ctx.restore();
1100
+ }
1101
+
1102
+ else{
1103
+ ctx.fillText(data.labels[i], yAxisPosX + i*valueHop + valueHop/2,xAxisPosY + config.scaleFontSize+3);
1104
+ }
1105
+
1106
+ ctx.beginPath();
1107
+ ctx.moveTo(yAxisPosX + (i+1) * valueHop, xAxisPosY+3);
1108
+
1109
+ //Check i isnt 0, so we dont go over the Y axis twice.
1110
+ ctx.lineWidth = config.scaleGridLineWidth;
1111
+ ctx.strokeStyle = config.scaleGridLineColor;
1112
+ ctx.lineTo(yAxisPosX + (i+1) * valueHop, 5);
1113
+ ctx.stroke();
1114
+ }
1115
+
1116
+ //Y axis
1117
+ ctx.lineWidth = config.scaleLineWidth;
1118
+ ctx.strokeStyle = config.scaleLineColor;
1119
+ ctx.beginPath();
1120
+ ctx.moveTo(yAxisPosX,xAxisPosY+5);
1121
+ ctx.lineTo(yAxisPosX,5);
1122
+ ctx.stroke();
1123
+
1124
+ ctx.textAlign = "right";
1125
+ ctx.textBaseline = "middle";
1126
+ for (var j=0; j<calculatedScale.steps; j++){
1127
+ ctx.beginPath();
1128
+ ctx.moveTo(yAxisPosX-3,xAxisPosY - ((j+1) * scaleHop));
1129
+ if (config.scaleShowGridLines){
1130
+ ctx.lineWidth = config.scaleGridLineWidth;
1131
+ ctx.strokeStyle = config.scaleGridLineColor;
1132
+ ctx.lineTo(yAxisPosX + xAxisLength + 5,xAxisPosY - ((j+1) * scaleHop));
1133
+ }
1134
+ else{
1135
+ ctx.lineTo(yAxisPosX-0.5,xAxisPosY - ((j+1) * scaleHop));
1136
+ }
1137
+
1138
+ ctx.stroke();
1139
+ if (config.scaleShowLabels){
1140
+ ctx.fillText(calculatedScale.labels[j],yAxisPosX-8,xAxisPosY - ((j+1) * scaleHop));
1141
+ }
1142
+ }
1143
+
1144
+
1145
+ }
1146
+ function calculateXAxisSize(){
1147
+ var longestText = 1;
1148
+ //if we are showing the labels
1149
+ if (config.scaleShowLabels){
1150
+ ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
1151
+ for (var i=0; i<calculatedScale.labels.length; i++){
1152
+ var measuredText = ctx.measureText(calculatedScale.labels[i]).width;
1153
+ longestText = (measuredText > longestText)? measuredText : longestText;
1154
+ }
1155
+ //Add a little extra padding from the y axis
1156
+ longestText +=10;
1157
+ }
1158
+ xAxisLength = width - longestText - widestXLabel;
1159
+ valueHop = Math.floor(xAxisLength/(data.labels.length));
1160
+
1161
+ barWidth = (valueHop - config.scaleGridLineWidth*2 - (config.barValueSpacing*2) - (config.barDatasetSpacing*data.datasets.length-1) - ((config.barStrokeWidth/2)*data.datasets.length-1))/data.datasets.length;
1162
+
1163
+ yAxisPosX = width-widestXLabel/2-xAxisLength;
1164
+ xAxisPosY = scaleHeight + config.scaleFontSize/2;
1165
+ }
1166
+ function calculateDrawingSizes(){
1167
+ maxSize = height;
1168
+
1169
+ //Need to check the X axis first - measure the length of each text metric, and figure out if we need to rotate by 45 degrees.
1170
+ ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
1171
+ widestXLabel = 1;
1172
+ for (var i=0; i<data.labels.length; i++){
1173
+ var textLength = ctx.measureText(data.labels[i]).width;
1174
+ //If the text length is longer - make that equal to longest text!
1175
+ widestXLabel = (textLength > widestXLabel)? textLength : widestXLabel;
1176
+ }
1177
+ if (width/data.labels.length < widestXLabel){
1178
+ rotateLabels = 45;
1179
+ if (width/data.labels.length < Math.cos(rotateLabels) * widestXLabel){
1180
+ rotateLabels = 90;
1181
+ maxSize -= widestXLabel;
1182
+ }
1183
+ else{
1184
+ maxSize -= Math.sin(rotateLabels) * widestXLabel;
1185
+ }
1186
+ }
1187
+ else{
1188
+ maxSize -= config.scaleFontSize;
1189
+ }
1190
+
1191
+ //Add a little padding between the x line and the text
1192
+ maxSize -= 5;
1193
+
1194
+
1195
+ labelHeight = config.scaleFontSize;
1196
+
1197
+ maxSize -= labelHeight;
1198
+ //Set 5 pixels greater than the font size to allow for a little padding from the X axis.
1199
+
1200
+ scaleHeight = maxSize;
1201
+
1202
+ //Then get the area above we can safely draw on.
1203
+
1204
+ }
1205
+ function getValueBounds() {
1206
+ var upperValue = Number.MIN_VALUE;
1207
+ var lowerValue = Number.MAX_VALUE;
1208
+ for (var i=0; i<data.datasets.length; i++){
1209
+ for (var j=0; j<data.datasets[i].data.length; j++){
1210
+ if ( data.datasets[i].data[j] > upperValue) { upperValue = data.datasets[i].data[j] };
1211
+ if ( data.datasets[i].data[j] < lowerValue) { lowerValue = data.datasets[i].data[j] };
1212
+ }
1213
+ };
1214
+
1215
+ var maxSteps = Math.floor((scaleHeight / (labelHeight*0.66)));
1216
+ var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
1217
+
1218
+ return {
1219
+ maxValue : upperValue,
1220
+ minValue : lowerValue,
1221
+ maxSteps : maxSteps,
1222
+ minSteps : minSteps
1223
+ };
1224
+
1225
+
1226
+ }
1227
+ }
1228
+
1229
+ function calculateOffset(val,calculatedScale,scaleHop){
1230
+ var outerValue = calculatedScale.steps * calculatedScale.stepValue;
1231
+ var adjustedValue = val - calculatedScale.graphMin;
1232
+ var scalingFactor = CapValue(adjustedValue/outerValue,1,0);
1233
+ return (scaleHop*calculatedScale.steps) * scalingFactor;
1234
+ }
1235
+
1236
+ function animationLoop(config,drawScale,drawData,ctx){
1237
+ var animFrameAmount = (config.animation)? 1/CapValue(config.animationSteps,Number.MAX_VALUE,1) : 1,
1238
+ easingFunction = animationOptions[config.animationEasing],
1239
+ percentAnimComplete =(config.animation)? 0 : 1;
1240
+
1241
+
1242
+
1243
+ if (typeof drawScale !== "function") drawScale = function(){};
1244
+
1245
+ requestAnimFrame(animLoop);
1246
+
1247
+ function animateFrame(){
1248
+ var easeAdjustedAnimationPercent =(config.animation)? CapValue(easingFunction(percentAnimComplete),null,0) : 1;
1249
+ clear(ctx);
1250
+ if(config.scaleOverlay){
1251
+ drawData(easeAdjustedAnimationPercent);
1252
+ drawScale();
1253
+ } else {
1254
+ drawScale();
1255
+ drawData(easeAdjustedAnimationPercent);
1256
+ }
1257
+ }
1258
+ function animLoop(){
1259
+ //We need to check if the animation is incomplete (less than 1), or complete (1).
1260
+ percentAnimComplete += animFrameAmount;
1261
+ animateFrame();
1262
+ //Stop the loop continuing forever
1263
+ if (percentAnimComplete <= 1){
1264
+ requestAnimFrame(animLoop);
1265
+ }
1266
+ else{
1267
+ if (typeof config.onAnimationComplete == "function") config.onAnimationComplete();
1268
+ }
1269
+
1270
+ }
1271
+
1272
+ }
1273
+
1274
+ //Declare global functions to be called within this namespace here.
1275
+
1276
+
1277
+ // shim layer with setTimeout fallback
1278
+ var requestAnimFrame = (function(){
1279
+ return window.requestAnimationFrame ||
1280
+ window.webkitRequestAnimationFrame ||
1281
+ window.mozRequestAnimationFrame ||
1282
+ window.oRequestAnimationFrame ||
1283
+ window.msRequestAnimationFrame ||
1284
+ function(callback) {
1285
+ window.setTimeout(callback, 1000 / 60);
1286
+ };
1287
+ })();
1288
+
1289
+ function calculateScale(drawingHeight,maxSteps,minSteps,maxValue,minValue,labelTemplateString){
1290
+ var graphMin,graphMax,graphRange,stepValue,numberOfSteps,valueRange,rangeOrderOfMagnitude,decimalNum;
1291
+
1292
+ valueRange = maxValue - minValue;
1293
+
1294
+ rangeOrderOfMagnitude = calculateOrderOfMagnitude(valueRange);
1295
+
1296
+ graphMin = Math.floor(minValue / (1 * Math.pow(10, rangeOrderOfMagnitude))) * Math.pow(10, rangeOrderOfMagnitude);
1297
+
1298
+ graphMax = Math.ceil(maxValue / (1 * Math.pow(10, rangeOrderOfMagnitude))) * Math.pow(10, rangeOrderOfMagnitude);
1299
+
1300
+ graphRange = graphMax - graphMin;
1301
+
1302
+ stepValue = Math.pow(10, rangeOrderOfMagnitude);
1303
+
1304
+ numberOfSteps = Math.round(graphRange / stepValue);
1305
+
1306
+ //Compare number of steps to the max and min for that size graph, and add in half steps if need be.
1307
+ while(numberOfSteps < minSteps || numberOfSteps > maxSteps) {
1308
+ if (numberOfSteps < minSteps){
1309
+ stepValue /= 2;
1310
+ numberOfSteps = Math.round(graphRange/stepValue);
1311
+ }
1312
+ else{
1313
+ stepValue *=2;
1314
+ numberOfSteps = Math.round(graphRange/stepValue);
1315
+ }
1316
+ };
1317
+
1318
+
1319
+
1320
+ //Create an array of all the labels by interpolating the string.
1321
+
1322
+ var labels = [];
1323
+
1324
+ if(labelTemplateString){
1325
+ //Fix floating point errors by setting to fixed the on the same decimal as the stepValue.
1326
+ for (var i=1; i<numberOfSteps+1; i++){
1327
+ labels.push(tmpl(labelTemplateString,{value:(graphMin + (stepValue*i)).toFixed(getDecimalPlaces (stepValue))}));
1328
+ }
1329
+ }
1330
+
1331
+ return {
1332
+ steps : numberOfSteps,
1333
+ stepValue : stepValue,
1334
+ graphMin : graphMin,
1335
+ labels : labels
1336
+
1337
+ }
1338
+
1339
+ function calculateOrderOfMagnitude(val){
1340
+ return Math.floor(Math.log(val) / Math.LN10);
1341
+ }
1342
+
1343
+
1344
+ }
1345
+
1346
+ //Max value from array
1347
+ function Max( array ){
1348
+ return Math.max.apply( Math, array );
1349
+ };
1350
+ //Min value from array
1351
+ function Min( array ){
1352
+ return Math.min.apply( Math, array );
1353
+ };
1354
+ //Default if undefined
1355
+ function Default(userDeclared,valueIfFalse){
1356
+ if(!userDeclared){
1357
+ return valueIfFalse;
1358
+ } else {
1359
+ return userDeclared;
1360
+ }
1361
+ };
1362
+ //Is a number function
1363
+ function isNumber(n) {
1364
+ return !isNaN(parseFloat(n)) && isFinite(n);
1365
+ }
1366
+ //Apply cap a value at a high or low number
1367
+ function CapValue(valueToCap, maxValue, minValue){
1368
+ if(isNumber(maxValue)) {
1369
+ if( valueToCap > maxValue ) {
1370
+ return maxValue;
1371
+ }
1372
+ }
1373
+ if(isNumber(minValue)){
1374
+ if ( valueToCap < minValue ){
1375
+ return minValue;
1376
+ }
1377
+ }
1378
+ return valueToCap;
1379
+ }
1380
+ function getDecimalPlaces (num){
1381
+ var numberOfDecimalPlaces;
1382
+ if (num%1!=0){
1383
+ return num.toString().split(".")[1].length
1384
+ }
1385
+ else{
1386
+ return 0;
1387
+ }
1388
+
1389
+ }
1390
+
1391
+ function mergeChartConfig(defaults,userDefined){
1392
+ var returnObj = {};
1393
+ for (var attrname in defaults) { returnObj[attrname] = defaults[attrname]; }
1394
+ for (var attrname in userDefined) { returnObj[attrname] = userDefined[attrname]; }
1395
+ return returnObj;
1396
+ }
1397
+
1398
+ //Javascript micro templating by John Resig - source at http://ejohn.org/blog/javascript-micro-templating/
1399
+ var cache = {};
1400
+
1401
+ function tmpl(str, data){
1402
+ // Figure out if we're getting a template, or if we need to
1403
+ // load the template - and be sure to cache the result.
1404
+ var fn = !/\W/.test(str) ?
1405
+ cache[str] = cache[str] ||
1406
+ tmpl(document.getElementById(str).innerHTML) :
1407
+
1408
+ // Generate a reusable function that will serve as a template
1409
+ // generator (and which will be cached).
1410
+ new Function("obj",
1411
+ "var p=[],print=function(){p.push.apply(p,arguments);};" +
1412
+
1413
+ // Introduce the data as local variables using with(){}
1414
+ "with(obj){p.push('" +
1415
+
1416
+ // Convert the template into pure JavaScript
1417
+ str
1418
+ .replace(/[\r\t\n]/g, " ")
1419
+ .split("<%").join("\t")
1420
+ .replace(/((^|%>)[^\t]*)'/g, "$1\r")
1421
+ .replace(/\t=(.*?)%>/g, "',$1,'")
1422
+ .split("\t").join("');")
1423
+ .split("%>").join("p.push('")
1424
+ .split("\r").join("\\'")
1425
+ + "');}return p.join('');");
1426
+
1427
+ // Provide some basic currying to the user
1428
+ return data ? fn( data ) : fn;
1429
+ };
1430
+ }