Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Chart.js 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*!
  2. * Chart.js
  3. * http://chartjs.org/
  4. *
  5. * Copyright 2013 Nick Downie
  6. * Released under the MIT license
  7. * https://github.com/nnnick/Chart.js/blob/master/LICENSE.md
  8. */
  9. //Define the global Chart Variable as a class.
  10. window.Chart = function(context){
  11. var chart = this;
  12. //Easing functions adapted from Robert Penner's easing equations
  13. //http://www.robertpenner.com/easing/
  14. var animationOptions = {
  15. linear : function (t){
  16. return t;
  17. },
  18. easeInQuad: function (t) {
  19. return t*t;
  20. },
  21. easeOutQuad: function (t) {
  22. return -1 *t*(t-2);
  23. },
  24. easeInOutQuad: function (t) {
  25. if ((t/=1/2) < 1) return 1/2*t*t;
  26. return -1/2 * ((--t)*(t-2) - 1);
  27. },
  28. easeInCubic: function (t) {
  29. return t*t*t;
  30. },
  31. easeOutCubic: function (t) {
  32. return 1*((t=t/1-1)*t*t + 1);
  33. },
  34. easeInOutCubic: function (t) {
  35. if ((t/=1/2) < 1) return 1/2*t*t*t;
  36. return 1/2*((t-=2)*t*t + 2);
  37. },
  38. easeInQuart: function (t) {
  39. return t*t*t*t;
  40. },
  41. easeOutQuart: function (t) {
  42. return -1 * ((t=t/1-1)*t*t*t - 1);
  43. },
  44. easeInOutQuart: function (t) {
  45. if ((t/=1/2) < 1) return 1/2*t*t*t*t;
  46. return -1/2 * ((t-=2)*t*t*t - 2);
  47. },
  48. easeInQuint: function (t) {
  49. return 1*(t/=1)*t*t*t*t;
  50. },
  51. easeOutQuint: function (t) {
  52. return 1*((t=t/1-1)*t*t*t*t + 1);
  53. },
  54. easeInOutQuint: function (t) {
  55. if ((t/=1/2) < 1) return 1/2*t*t*t*t*t;
  56. return 1/2*((t-=2)*t*t*t*t + 2);
  57. },
  58. easeInSine: function (t) {
  59. return -1 * Math.cos(t/1 * (Math.PI/2)) + 1;
  60. },
  61. easeOutSine: function (t) {
  62. return 1 * Math.sin(t/1 * (Math.PI/2));
  63. },
  64. easeInOutSine: function (t) {
  65. return -1/2 * (Math.cos(Math.PI*t/1) - 1);
  66. },
  67. easeInExpo: function (t) {
  68. return (t==0) ? 1 : 1 * Math.pow(2, 10 * (t/1 - 1));
  69. },
  70. easeOutExpo: function (t) {
  71. return (t==1) ? 1 : 1 * (-Math.pow(2, -10 * t/1) + 1);
  72. },
  73. easeInOutExpo: function (t) {
  74. if (t==0) return 0;
  75. if (t==1) return 1;
  76. if ((t/=1/2) < 1) return 1/2 * Math.pow(2, 10 * (t - 1));
  77. return 1/2 * (-Math.pow(2, -10 * --t) + 2);
  78. },
  79. easeInCirc: function (t) {
  80. if (t>=1) return t;
  81. return -1 * (Math.sqrt(1 - (t/=1)*t) - 1);
  82. },
  83. easeOutCirc: function (t) {
  84. return 1 * Math.sqrt(1 - (t=t/1-1)*t);
  85. },
  86. easeInOutCirc: function (t) {
  87. if ((t/=1/2) < 1) return -1/2 * (Math.sqrt(1 - t*t) - 1);
  88. return 1/2 * (Math.sqrt(1 - (t-=2)*t) + 1);
  89. },
  90. easeInElastic: function (t) {
  91. var s=1.70158;var p=0;var a=1;
  92. if (t==0) return 0; if ((t/=1)==1) return 1; if (!p) p=1*.3;
  93. if (a < Math.abs(1)) { a=1; var s=p/4; }
  94. else var s = p/(2*Math.PI) * Math.asin (1/a);
  95. return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*1-s)*(2*Math.PI)/p ));
  96. },
  97. easeOutElastic: function (t) {
  98. var s=1.70158;var p=0;var a=1;
  99. if (t==0) return 0; if ((t/=1)==1) return 1; if (!p) p=1*.3;
  100. if (a < Math.abs(1)) { a=1; var s=p/4; }
  101. else var s = p/(2*Math.PI) * Math.asin (1/a);
  102. return a*Math.pow(2,-10*t) * Math.sin( (t*1-s)*(2*Math.PI)/p ) + 1;
  103. },
  104. easeInOutElastic: function (t) {
  105. var s=1.70158;var p=0;var a=1;
  106. if (t==0) return 0; if ((t/=1/2)==2) return 1; if (!p) p=1*(.3*1.5);
  107. if (a < Math.abs(1)) { a=1; var s=p/4; }
  108. else var s = p/(2*Math.PI) * Math.asin (1/a);
  109. if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*1-s)*(2*Math.PI)/p ));
  110. return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*1-s)*(2*Math.PI)/p )*.5 + 1;
  111. },
  112. easeInBack: function (t) {
  113. var s = 1.70158;
  114. return 1*(t/=1)*t*((s+1)*t - s);
  115. },
  116. easeOutBack: function (t) {
  117. var s = 1.70158;
  118. return 1*((t=t/1-1)*t*((s+1)*t + s) + 1);
  119. },
  120. easeInOutBack: function (t) {
  121. var s = 1.70158;
  122. if ((t/=1/2) < 1) return 1/2*(t*t*(((s*=(1.525))+1)*t - s));
  123. return 1/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2);
  124. },
  125. easeInBounce: function (t) {
  126. return 1 - animationOptions.easeOutBounce (1-t);
  127. },
  128. easeOutBounce: function (t) {
  129. if ((t/=1) < (1/2.75)) {
  130. return 1*(7.5625*t*t);
  131. } else if (t < (2/2.75)) {
  132. return 1*(7.5625*(t-=(1.5/2.75))*t + .75);
  133. } else if (t < (2.5/2.75)) {
  134. return 1*(7.5625*(t-=(2.25/2.75))*t + .9375);
  135. } else {
  136. return 1*(7.5625*(t-=(2.625/2.75))*t + .984375);
  137. }
  138. },
  139. easeInOutBounce: function (t) {
  140. if (t < 1/2) return animationOptions.easeInBounce (t*2) * .5;
  141. return animationOptions.easeOutBounce (t*2-1) * .5 + 1*.5;
  142. }
  143. };
  144. //Variables global to the chart
  145. var width = context.canvas.width;
  146. var height = context.canvas.height;
  147. //High pixel density displays - multiply the size of the canvas height/width by the device pixel ratio, then scale.
  148. if (window.devicePixelRatio) {
  149. context.canvas.style.width = width + "px";
  150. context.canvas.style.height = height + "px";
  151. context.canvas.height = height * window.devicePixelRatio;
  152. context.canvas.width = width * window.devicePixelRatio;
  153. context.scale(window.devicePixelRatio, window.devicePixelRatio);
  154. }
  155. this.PolarArea = function(data,options){
  156. chart.PolarArea.defaults = {
  157. scaleOverlay : true,
  158. scaleOverride : false,
  159. scaleSteps : null,
  160. scaleStepWidth : null,
  161. scaleStartValue : null,
  162. scaleShowLine : true,
  163. scaleLineColor : "rgba(0,0,0,.1)",
  164. scaleLineWidth : 1,
  165. scaleShowLabels : true,
  166. scaleLabel : "<%=value%>",
  167. scaleFontFamily : "'Arial'",
  168. scaleFontSize : 12,
  169. scaleFontStyle : "normal",
  170. scaleFontColor : "#666",
  171. scaleShowLabelBackdrop : true,
  172. scaleBackdropColor : "rgba(255,255,255,0.75)",
  173. scaleBackdropPaddingY : 2,
  174. scaleBackdropPaddingX : 2,
  175. segmentShowStroke : true,
  176. segmentStrokeColor : "#fff",
  177. segmentStrokeWidth : 2,
  178. animation : true,
  179. animationSteps : 100,
  180. animationEasing : "easeOutBounce",
  181. animateRotate : true,
  182. animateScale : false,
  183. onAnimationComplete : null
  184. };
  185. var config = (options)? mergeChartConfig(chart.PolarArea.defaults,options) : chart.PolarArea.defaults;
  186. return new PolarArea(data,config,context);
  187. };
  188. this.Radar = function(data,options){
  189. chart.Radar.defaults = {
  190. scaleOverlay : false,
  191. scaleOverride : false,
  192. scaleSteps : null,
  193. scaleStepWidth : null,
  194. scaleStartValue : null,
  195. scaleShowLine : true,
  196. scaleLineColor : "rgba(0,0,0,.1)",
  197. scaleLineWidth : 1,
  198. scaleShowLabels : false,
  199. scaleLabel : "<%=value%>",
  200. scaleFontFamily : "'Arial'",
  201. scaleFontSize : 12,
  202. scaleFontStyle : "normal",
  203. scaleFontColor : "#666",
  204. scaleShowLabelBackdrop : true,
  205. scaleBackdropColor : "rgba(255,255,255,0.75)",
  206. scaleBackdropPaddingY : 2,
  207. scaleBackdropPaddingX : 2,
  208. angleShowLineOut : true,
  209. angleLineColor : "rgba(0,0,0,.1)",
  210. angleLineWidth : 1,
  211. pointLabelFontFamily : "'Arial'",
  212. pointLabelFontStyle : "normal",
  213. pointLabelFontSize : 12,
  214. pointLabelFontColor : "#666",
  215. pointDot : true,
  216. pointDotRadius : 3,
  217. pointDotStrokeWidth : 1,
  218. datasetStroke : true,
  219. datasetStrokeWidth : 2,
  220. datasetFill : true,
  221. animation : true,
  222. animationSteps : 60,
  223. animationEasing : "easeOutQuart",
  224. onAnimationComplete : null
  225. };
  226. var config = (options)? mergeChartConfig(chart.Radar.defaults,options) : chart.Radar.defaults;
  227. return new Radar(data,config,context);
  228. };
  229. this.Pie = function(data,options){
  230. chart.Pie.defaults = {
  231. segmentShowStroke : true,
  232. segmentStrokeColor : "#fff",
  233. segmentStrokeWidth : 2,
  234. animation : true,
  235. animationSteps : 100,
  236. animationEasing : "easeOutBounce",
  237. animateRotate : true,
  238. animateScale : false,
  239. onAnimationComplete : null
  240. };
  241. var config = (options)? mergeChartConfig(chart.Pie.defaults,options) : chart.Pie.defaults;
  242. return new Pie(data,config,context);
  243. };
  244. this.Doughnut = function(data,options){
  245. chart.Doughnut.defaults = {
  246. segmentShowStroke : true,
  247. segmentStrokeColor : "#2c3e50",
  248. segmentStrokeWidth : 1,
  249. percentageInnerCutout : 90,
  250. animation : true,
  251. animationSteps : 100,
  252. animationEasing : "easeOutBounce",
  253. animateRotate : true,
  254. animateScale : false,
  255. onAnimationComplete : null
  256. };
  257. var config = (options)? mergeChartConfig(chart.Doughnut.defaults,options) : chart.Doughnut.defaults;
  258. return new Doughnut(data,config,context);
  259. };
  260. this.Line = function(data,options){
  261. chart.Line.defaults = {
  262. scaleOverlay : false,
  263. scaleOverride : false,
  264. scaleSteps : null,
  265. scaleStepWidth : null,
  266. scaleStartValue : null,
  267. scaleLineColor : "rgba(0,0,0,.1)",
  268. scaleLineWidth : 1,
  269. scaleShowLabels : true,
  270. scaleLabel : "<%=value%>",
  271. scaleFontFamily : "'Arial'",
  272. scaleFontSize : 12,
  273. scaleFontStyle : "normal",
  274. scaleFontColor : "#666",
  275. scaleShowGridLines : true,
  276. scaleGridLineColor : "rgba(0,0,0,.05)",
  277. scaleGridLineWidth : 1,
  278. bezierCurve : true,
  279. pointDot : true,
  280. pointDotRadius : 4,
  281. pointDotStrokeWidth : 2,
  282. datasetStroke : true,
  283. datasetStrokeWidth : 2,
  284. datasetFill : true,
  285. animation : true,
  286. animationSteps : 60,
  287. animationEasing : "easeOutQuart",
  288. onAnimationComplete : null
  289. };
  290. var config = (options) ? mergeChartConfig(chart.Line.defaults,options) : chart.Line.defaults;
  291. return new Line(data,config,context);
  292. }
  293. this.Bar = function(data,options){
  294. chart.Bar.defaults = {
  295. scaleOverlay : false,
  296. scaleOverride : false,
  297. scaleSteps : null,
  298. scaleStepWidth : null,
  299. scaleStartValue : null,
  300. scaleLineColor : "rgba(0,0,0,.1)",
  301. scaleLineWidth : 1,
  302. scaleShowLabels : true,
  303. scaleLabel : "<%=value%>",
  304. scaleFontFamily : "'Arial'",
  305. scaleFontSize : 12,
  306. scaleFontStyle : "normal",
  307. scaleFontColor : "#666",
  308. scaleShowGridLines : true,
  309. scaleGridLineColor : "rgba(0,0,0,.05)",
  310. scaleGridLineWidth : 1,
  311. barShowStroke : true,
  312. barStrokeWidth : 2,
  313. barValueSpacing : 5,
  314. barDatasetSpacing : 1,
  315. animation : true,
  316. animationSteps : 60,
  317. animationEasing : "easeOutQuart",
  318. onAnimationComplete : null
  319. };
  320. var config = (options) ? mergeChartConfig(chart.Bar.defaults,options) : chart.Bar.defaults;
  321. return new Bar(data,config,context);
  322. }
  323. var clear = function(c){
  324. c.clearRect(0, 0, width, height);
  325. };
  326. var PolarArea = function(data,config,ctx){
  327. var maxSize, scaleHop, calculatedScale, labelHeight, scaleHeight, valueBounds, labelTemplateString;
  328. calculateDrawingSizes();
  329. valueBounds = getValueBounds();
  330. labelTemplateString = (config.scaleShowLabels)? config.scaleLabel : null;
  331. //Check and set the scale
  332. if (!config.scaleOverride){
  333. calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
  334. }
  335. else {
  336. calculatedScale = {
  337. steps : config.scaleSteps,
  338. stepValue : config.scaleStepWidth,
  339. graphMin : config.scaleStartValue,
  340. labels : []
  341. }
  342. populateLabels(labelTemplateString, calculatedScale.labels,calculatedScale.steps,config.scaleStartValue,config.scaleStepWidth);
  343. }
  344. scaleHop = maxSize/(calculatedScale.steps);
  345. //Wrap in an animation loop wrapper
  346. animationLoop(config,drawScale,drawAllSegments,ctx);
  347. function calculateDrawingSizes(){
  348. maxSize = (Min([width,height])/2);
  349. //Remove whatever is larger - the font size or line width.
  350. maxSize -= Max([config.scaleFontSize*0.5,config.scaleLineWidth*0.5]);
  351. labelHeight = config.scaleFontSize*2;
  352. //If we're drawing the backdrop - add the Y padding to the label height and remove from drawing region.
  353. if (config.scaleShowLabelBackdrop){
  354. labelHeight += (2 * config.scaleBackdropPaddingY);
  355. maxSize -= config.scaleBackdropPaddingY*1.5;
  356. }
  357. scaleHeight = maxSize;
  358. //If the label height is less than 5, set it to 5 so we don't have lines on top of each other.
  359. labelHeight = Default(labelHeight,5);
  360. }
  361. function drawScale(){
  362. for (var i=0; i<calculatedScale.steps; i++){
  363. //If the line object is there
  364. if (config.scaleShowLine){
  365. ctx.beginPath();
  366. ctx.arc(width/2, height/2, scaleHop * (i + 1), 0, (Math.PI * 2), true);
  367. ctx.strokeStyle = config.scaleLineColor;
  368. ctx.lineWidth = config.scaleLineWidth;
  369. ctx.stroke();
  370. }
  371. if (config.scaleShowLabels){
  372. ctx.textAlign = "center";
  373. ctx.font = config.scaleFontStyle + " " + config.scaleFontSize + "px " + config.scaleFontFamily;
  374. var label = calculatedScale.labels[i];
  375. //If the backdrop object is within the font object
  376. if (config.scaleShowLabelBackdrop){
  377. var textWidth = ctx.measureText(label).width;
  378. ctx.fillStyle = config.scaleBackdropColor;
  379. ctx.beginPath();
  380. ctx.rect(
  381. Math.round(width/2 - textWidth/2 - config.scaleBackdropPaddingX), //X
  382. Math.round(height/2 - (scaleHop * (i + 1)) - config.scaleFontSize*0.5 - config.scaleBackdropPaddingY),//Y
  383. Math.round(textWidth + (config.scaleBackdropPaddingX*2)), //Width
  384. Math.round(config.scaleFontSize + (config.scaleBackdropPaddingY*2)) //Height
  385. );
  386. ctx.fill();
  387. }
  388. ctx.textBaseline = "middle";
  389. ctx.fillStyle = config.scaleFontColor;
  390. ctx.fillText(label,width/2,height/2 - (scaleHop * (i + 1)));
  391. }
  392. }
  393. }
  394. function drawAllSegments(animationDecimal){
  395. var startAngle = -Math.PI/2,
  396. angleStep = (Math.PI*2)/data.length,
  397. scaleAnimation = 1,
  398. rotateAnimation = 1;
  399. if (config.animation) {
  400. if (config.animateScale) {
  401. scaleAnimation = animationDecimal;
  402. }
  403. if (config.animateRotate){
  404. rotateAnimation = animationDecimal;
  405. }
  406. }
  407. for (var i=0; i<data.length; i++){
  408. ctx.beginPath();
  409. ctx.arc(width/2,height/2,scaleAnimation * calculateOffset(data[i].value,calculatedScale,scaleHop),startAngle, startAngle + rotateAnimation*angleStep, false);
  410. ctx.lineTo(width/2,height/2);
  411. ctx.closePath();
  412. ctx.fillStyle = data[i].color;
  413. ctx.fill();
  414. if(config.segmentShowStroke){
  415. ctx.strokeStyle = config.segmentStrokeColor;
  416. ctx.lineWidth = config.segmentStrokeWidth;
  417. ctx.stroke();
  418. }
  419. startAngle += rotateAnimation*angleStep;
  420. }
  421. }
  422. function getValueBounds() {
  423. var upperValue = Number.MIN_VALUE;
  424. var lowerValue = Number.MAX_VALUE;
  425. for (var i=0; i<data.length; i++){
  426. if (data[i].value > upperValue) {upperValue = data[i].value;}
  427. if (data[i].value < lowerValue) {lowerValue = data[i].value;}
  428. };
  429. var maxSteps = Math.floor((scaleHeight / (labelHeight*0.66)));
  430. var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
  431. return {
  432. maxValue : upperValue,
  433. minValue : lowerValue,
  434. maxSteps : maxSteps,
  435. minSteps : minSteps
  436. };
  437. }
  438. }
  439. var Radar = function (data,config,ctx) {
  440. var maxSize, scaleHop, calculatedScale, labelHeight, scaleHeight, valueBounds, labelTemplateString;
  441. //If no labels are defined set to an empty array, so referencing length for looping doesn't blow up.
  442. if (!data.labels) data.labels = [];
  443. calculateDrawingSizes();
  444. var valueBounds = getValueBounds();
  445. labelTemplateString = (config.scaleShowLabels)? config.scaleLabel : null;
  446. //Check and set the scale
  447. if (!config.scaleOverride){
  448. calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
  449. }
  450. else {
  451. calculatedScale = {
  452. steps : config.scaleSteps,
  453. stepValue : config.scaleStepWidth,
  454. graphMin : config.scaleStartValue,
  455. labels : []
  456. }
  457. populateLabels(labelTemplateString, calculatedScale.labels,calculatedScale.steps,config.scaleStartValue,config.scaleStepWidth);
  458. }
  459. scaleHop = maxSize/(calculatedScale.steps);
  460. animationLoop(config,drawScale,drawAllDataPoints,ctx);
  461. //Radar specific functions.
  462. function drawAllDataPoints(animationDecimal){
  463. var rotationDegree = (2*Math.PI)/data.datasets[0].data.length;
  464. ctx.save();
  465. //translate to the centre of the canvas.
  466. ctx.translate(width/2,height/2);
  467. //We accept multiple data sets for radar charts, so show loop through each set
  468. for (var i=0; i<data.datasets.length; i++){
  469. ctx.beginPath();
  470. ctx.moveTo(0,animationDecimal*(-1*calculateOffset(data.datasets[i].data[0],calculatedScale,scaleHop)));
  471. for (var j=1; j<data.datasets[i].data.length; j++){
  472. ctx.rotate(rotationDegree);
  473. ctx.lineTo(0,animationDecimal*(-1*calculateOffset(data.datasets[i].data[j],calculatedScale,scaleHop)));
  474. }
  475. ctx.closePath();
  476. ctx.fillStyle = data.datasets[i].fillColor;
  477. ctx.strokeStyle = data.datasets[i].strokeColor;
  478. ctx.lineWidth = config.datasetStrokeWidth;
  479. ctx.fill();
  480. ctx.stroke();
  481. if (config.pointDot){
  482. ctx.fillStyle = data.datasets[i].pointColor;
  483. ctx.strokeStyle = data.datasets[i].pointStrokeColor;
  484. ctx.lineWidth = config.pointDotStrokeWidth;
  485. for (var k=0; k<data.datasets[i].data.length; k++){
  486. ctx.rotate(rotationDegree);
  487. ctx.beginPath();
  488. ctx.arc(0,animationDecimal*(-1*calculateOffset(data.datasets[i].data[k],calculatedScale,scaleHop)),config.pointDotRadius,2*Math.PI,false);
  489. ctx.fill();
  490. ctx.stroke();
  491. }
  492. }
  493. ctx.rotate(rotationDegree);
  494. }
  495. ctx.restore();
  496. }
  497. function drawScale(){
  498. var rotationDegree = (2*Math.PI)/data.datasets[0].data.length;
  499. ctx.save();
  500. ctx.translate(width / 2, height / 2);
  501. if (config.angleShowLineOut){
  502. ctx.strokeStyle = config.angleLineColor;
  503. ctx.lineWidth = config.angleLineWidth;
  504. for (var h=0; h<data.datasets[0].data.length; h++){
  505. ctx.rotate(rotationDegree);
  506. ctx.beginPath();
  507. ctx.moveTo(0,0);
  508. ctx.lineTo(0,-maxSize);
  509. ctx.stroke();
  510. }
  511. }
  512. for (var i=0; i<calculatedScale.steps; i++){
  513. ctx.beginPath();
  514. if(config.scaleShowLine){
  515. ctx.strokeStyle = config.scaleLineColor;
  516. ctx.lineWidth = config.scaleLineWidth;
  517. ctx.moveTo(0,-scaleHop * (i+1));
  518. for (var j=0; j<data.datasets[0].data.length; j++){
  519. ctx.rotate(rotationDegree);
  520. ctx.lineTo(0,-scaleHop * (i+1));
  521. }
  522. ctx.closePath();
  523. ctx.stroke();
  524. }
  525. if (config.scaleShowLabels){
  526. ctx.textAlign = 'center';
  527. ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
  528. ctx.textBaseline = "middle";
  529. if (config.scaleShowLabelBackdrop){
  530. var textWidth = ctx.measureText(calculatedScale.labels[i]).width;
  531. ctx.fillStyle = config.scaleBackdropColor;
  532. ctx.beginPath();
  533. ctx.rect(
  534. Math.round(- textWidth/2 - config.scaleBackdropPaddingX), //X
  535. Math.round((-scaleHop * (i + 1)) - config.scaleFontSize*0.5 - config.scaleBackdropPaddingY),//Y
  536. Math.round(textWidth + (config.scaleBackdropPaddingX*2)), //Width
  537. Math.round(config.scaleFontSize + (config.scaleBackdropPaddingY*2)) //Height
  538. );
  539. ctx.fill();
  540. }
  541. ctx.fillStyle = config.scaleFontColor;
  542. ctx.fillText(calculatedScale.labels[i],0,-scaleHop*(i+1));
  543. }
  544. }
  545. for (var k=0; k<data.labels.length; k++){
  546. ctx.font = config.pointLabelFontStyle + " " + config.pointLabelFontSize+"px " + config.pointLabelFontFamily;
  547. ctx.fillStyle = config.pointLabelFontColor;
  548. var opposite = Math.sin(rotationDegree*k) * (maxSize + config.pointLabelFontSize);
  549. var adjacent = Math.cos(rotationDegree*k) * (maxSize + config.pointLabelFontSize);
  550. if(rotationDegree*k == Math.PI || rotationDegree*k == 0){
  551. ctx.textAlign = "center";
  552. }
  553. else if(rotationDegree*k > Math.PI){
  554. ctx.textAlign = "right";
  555. }
  556. else{
  557. ctx.textAlign = "left";
  558. }
  559. ctx.textBaseline = "middle";
  560. ctx.fillText(data.labels[k],opposite,-adjacent);
  561. }
  562. ctx.restore();
  563. };
  564. function calculateDrawingSizes(){
  565. maxSize = (Min([width,height])/2);
  566. labelHeight = config.scaleFontSize*2;
  567. var labelLength = 0;
  568. for (var i=0; i<data.labels.length; i++){
  569. ctx.font = config.pointLabelFontStyle + " " + config.pointLabelFontSize+"px " + config.pointLabelFontFamily;
  570. var textMeasurement = ctx.measureText(data.labels[i]).width;
  571. if(textMeasurement>labelLength) labelLength = textMeasurement;
  572. }
  573. //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.
  574. maxSize -= Max([labelLength,((config.pointLabelFontSize/2)*1.5)]);
  575. maxSize -= config.pointLabelFontSize;
  576. maxSize = CapValue(maxSize, null, 0);
  577. scaleHeight = maxSize;
  578. //If the label height is less than 5, set it to 5 so we don't have lines on top of each other.
  579. labelHeight = Default(labelHeight,5);
  580. };
  581. function getValueBounds() {
  582. var upperValue = Number.MIN_VALUE;
  583. var lowerValue = Number.MAX_VALUE;
  584. for (var i=0; i<data.datasets.length; i++){
  585. for (var j=0; j<data.datasets[i].data.length; j++){
  586. if (data.datasets[i].data[j] > upperValue){upperValue = data.datasets[i].data[j]}
  587. if (data.datasets[i].data[j] < lowerValue){lowerValue = data.datasets[i].data[j]}
  588. }
  589. }
  590. var maxSteps = Math.floor((scaleHeight / (labelHeight*0.66)));
  591. var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
  592. return {
  593. maxValue : upperValue,
  594. minValue : lowerValue,
  595. maxSteps : maxSteps,
  596. minSteps : minSteps
  597. };
  598. }
  599. }
  600. var Pie = function(data,config,ctx){
  601. var segmentTotal = 0;
  602. //In case we have a canvas that is not a square. Minus 5 pixels as padding round the edge.
  603. var pieRadius = Min([height/2,width/2]) - 5;
  604. for (var i=0; i<data.length; i++){
  605. segmentTotal += data[i].value;
  606. }
  607. animationLoop(config,null,drawPieSegments,ctx);
  608. function drawPieSegments (animationDecimal){
  609. var cumulativeAngle = -Math.PI/2,
  610. scaleAnimation = 1,
  611. rotateAnimation = 1;
  612. if (config.animation) {
  613. if (config.animateScale) {
  614. scaleAnimation = animationDecimal;
  615. }
  616. if (config.animateRotate){
  617. rotateAnimation = animationDecimal;
  618. }
  619. }
  620. for (var i=0; i<data.length; i++){
  621. var segmentAngle = rotateAnimation * ((data[i].value/segmentTotal) * (Math.PI*2));
  622. ctx.beginPath();
  623. ctx.arc(width/2,height/2,scaleAnimation * pieRadius,cumulativeAngle,cumulativeAngle + segmentAngle);
  624. ctx.lineTo(width/2,height/2);
  625. ctx.closePath();
  626. ctx.fillStyle = data[i].color;
  627. ctx.fill();
  628. if(config.segmentShowStroke){
  629. ctx.lineWidth = config.segmentStrokeWidth;
  630. ctx.strokeStyle = config.segmentStrokeColor;
  631. ctx.stroke();
  632. }
  633. cumulativeAngle += segmentAngle;
  634. }
  635. }
  636. }
  637. var Doughnut = function(data,config,ctx){
  638. var segmentTotal = 0;
  639. //In case we have a canvas that is not a square. Minus 5 pixels as padding round the edge.
  640. var doughnutRadius = Min([height/2,width/2]) - 5;
  641. var cutoutRadius = doughnutRadius * (config.percentageInnerCutout/100);
  642. for (var i=0; i<data.length; i++){
  643. segmentTotal += data[i].value;
  644. }
  645. animationLoop(config,null,drawPieSegments,ctx);
  646. function drawPieSegments (animationDecimal){
  647. var cumulativeAngle = -Math.PI/2,
  648. scaleAnimation = 1,
  649. rotateAnimation = 1;
  650. if (config.animation) {
  651. if (config.animateScale) {
  652. scaleAnimation = animationDecimal;
  653. }
  654. if (config.animateRotate){
  655. rotateAnimation = animationDecimal;
  656. }
  657. }
  658. for (var i=0; i<data.length; i++){
  659. var segmentAngle = rotateAnimation * ((data[i].value/segmentTotal) * (Math.PI*2));
  660. ctx.beginPath();
  661. ctx.arc(width/2,height/2,scaleAnimation * doughnutRadius,cumulativeAngle,cumulativeAngle + segmentAngle,false);
  662. ctx.arc(width/2,height/2,scaleAnimation * cutoutRadius,cumulativeAngle + segmentAngle,cumulativeAngle,true);
  663. ctx.closePath();
  664. ctx.fillStyle = data[i].color;
  665. ctx.fill();
  666. if(config.segmentShowStroke){
  667. ctx.lineWidth = config.segmentStrokeWidth;
  668. ctx.strokeStyle = config.segmentStrokeColor;
  669. ctx.stroke();
  670. }
  671. cumulativeAngle += segmentAngle;
  672. }
  673. }
  674. }
  675. var Line = function(data,config,ctx){
  676. var maxSize, scaleHop, calculatedScale, labelHeight, scaleHeight, valueBounds, labelTemplateString, valueHop,widestXLabel, xAxisLength,yAxisPosX,xAxisPosY, rotateLabels = 0;
  677. calculateDrawingSizes();
  678. valueBounds = getValueBounds();
  679. //Check and set the scale
  680. labelTemplateString = (config.scaleShowLabels)? config.scaleLabel : "";
  681. if (!config.scaleOverride){
  682. calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
  683. }
  684. else {
  685. calculatedScale = {
  686. steps : config.scaleSteps,
  687. stepValue : config.scaleStepWidth,
  688. graphMin : config.scaleStartValue,
  689. labels : []
  690. }
  691. populateLabels(labelTemplateString, calculatedScale.labels,calculatedScale.steps,config.scaleStartValue,config.scaleStepWidth);
  692. }
  693. scaleHop = Math.floor(scaleHeight/calculatedScale.steps);
  694. calculateXAxisSize();
  695. animationLoop(config,drawScale,drawLines,ctx);
  696. function drawLines(animPc){
  697. for (var i=0; i<data.datasets.length; i++){
  698. ctx.strokeStyle = data.datasets[i].strokeColor;
  699. ctx.lineWidth = config.datasetStrokeWidth;
  700. ctx.beginPath();
  701. ctx.moveTo(yAxisPosX, xAxisPosY - animPc*(calculateOffset(data.datasets[i].data[0],calculatedScale,scaleHop)))
  702. for (var j=1; j<data.datasets[i].data.length; j++){
  703. if (config.bezierCurve){
  704. ctx.bezierCurveTo(xPos(j-0.5),yPos(i,j-1),xPos(j-0.5),yPos(i,j),xPos(j),yPos(i,j));
  705. }
  706. else{
  707. ctx.lineTo(xPos(j),yPos(i,j));
  708. }
  709. }
  710. ctx.stroke();
  711. if (config.datasetFill){
  712. ctx.lineTo(yAxisPosX + (valueHop*(data.datasets[i].data.length-1)),xAxisPosY);
  713. ctx.lineTo(yAxisPosX,xAxisPosY);
  714. ctx.closePath();
  715. ctx.fillStyle = data.datasets[i].fillColor;
  716. ctx.fill();
  717. }
  718. else{
  719. ctx.closePath();
  720. }
  721. if(config.pointDot){
  722. ctx.fillStyle = data.datasets[i].pointColor;
  723. ctx.strokeStyle = data.datasets[i].pointStrokeColor;
  724. ctx.lineWidth = config.pointDotStrokeWidth;
  725. for (var k=0; k<data.datasets[i].data.length; k++){
  726. ctx.beginPath();
  727. ctx.arc(yAxisPosX + (valueHop *k),xAxisPosY - animPc*(calculateOffset(data.datasets[i].data[k],calculatedScale,scaleHop)),config.pointDotRadius,0,Math.PI*2,true);
  728. ctx.fill();
  729. ctx.stroke();
  730. }
  731. }
  732. }
  733. function yPos(dataSet,iteration){
  734. return xAxisPosY - animPc*(calculateOffset(data.datasets[dataSet].data[iteration],calculatedScale,scaleHop));
  735. }
  736. function xPos(iteration){
  737. return yAxisPosX + (valueHop * iteration);
  738. }
  739. }
  740. function drawScale(){
  741. //X axis line
  742. ctx.lineWidth = config.scaleLineWidth;
  743. ctx.strokeStyle = config.scaleLineColor;
  744. ctx.beginPath();
  745. ctx.moveTo(width-widestXLabel/2+5,xAxisPosY);
  746. ctx.lineTo(width-(widestXLabel/2)-xAxisLength-5,xAxisPosY);
  747. ctx.stroke();
  748. if (rotateLabels > 0){
  749. ctx.save();
  750. ctx.textAlign = "right";
  751. }
  752. else{
  753. ctx.textAlign = "center";
  754. }
  755. ctx.fillStyle = config.scaleFontColor;
  756. for (var i=0; i<data.labels.length; i++){
  757. ctx.save();
  758. if (rotateLabels > 0){
  759. ctx.translate(yAxisPosX + i*valueHop,xAxisPosY + config.scaleFontSize);
  760. ctx.rotate(-(rotateLabels * (Math.PI/180)));
  761. ctx.fillText(data.labels[i], 0,0);
  762. ctx.restore();
  763. }
  764. else{
  765. ctx.fillText(data.labels[i], yAxisPosX + i*valueHop,xAxisPosY + config.scaleFontSize+3);
  766. }
  767. ctx.beginPath();
  768. ctx.moveTo(yAxisPosX + i * valueHop, xAxisPosY+3);
  769. //Check i isnt 0, so we dont go over the Y axis twice.
  770. if(config.scaleShowGridLines && i>0){
  771. ctx.lineWidth = config.scaleGridLineWidth;
  772. ctx.strokeStyle = config.scaleGridLineColor;
  773. ctx.lineTo(yAxisPosX + i * valueHop, 5);
  774. }
  775. else{
  776. ctx.lineTo(yAxisPosX + i * valueHop, xAxisPosY+3);
  777. }
  778. ctx.stroke();
  779. }
  780. //Y axis
  781. ctx.lineWidth = config.scaleLineWidth;
  782. ctx.strokeStyle = config.scaleLineColor;
  783. ctx.beginPath();
  784. ctx.moveTo(yAxisPosX,xAxisPosY+5);
  785. ctx.lineTo(yAxisPosX,5);
  786. ctx.stroke();
  787. ctx.textAlign = "right";
  788. ctx.textBaseline = "middle";
  789. for (var j=0; j<calculatedScale.steps; j++){
  790. ctx.beginPath();
  791. ctx.moveTo(yAxisPosX-3,xAxisPosY - ((j+1) * scaleHop));
  792. if (config.scaleShowGridLines){
  793. ctx.lineWidth = config.scaleGridLineWidth;
  794. ctx.strokeStyle = config.scaleGridLineColor;
  795. ctx.lineTo(yAxisPosX + xAxisLength + 5,xAxisPosY - ((j+1) * scaleHop));
  796. }
  797. else{
  798. ctx.lineTo(yAxisPosX-0.5,xAxisPosY - ((j+1) * scaleHop));
  799. }
  800. ctx.stroke();
  801. if (config.scaleShowLabels){
  802. ctx.fillText(calculatedScale.labels[j],yAxisPosX-8,xAxisPosY - ((j+1) * scaleHop));
  803. }
  804. }
  805. }
  806. function calculateXAxisSize(){
  807. var longestText = 1;
  808. //if we are showing the labels
  809. if (config.scaleShowLabels){
  810. ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
  811. for (var i=0; i<calculatedScale.labels.length; i++){
  812. var measuredText = ctx.measureText(calculatedScale.labels[i]).width;
  813. longestText = (measuredText > longestText)? measuredText : longestText;
  814. }
  815. //Add a little extra padding from the y axis
  816. longestText +=10;
  817. }
  818. xAxisLength = width - longestText - widestXLabel;
  819. valueHop = Math.floor(xAxisLength/(data.labels.length-1));
  820. yAxisPosX = width-widestXLabel/2-xAxisLength;
  821. xAxisPosY = scaleHeight + config.scaleFontSize/2;
  822. }
  823. function calculateDrawingSizes(){
  824. maxSize = height;
  825. //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.
  826. ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
  827. widestXLabel = 1;
  828. for (var i=0; i<data.labels.length; i++){
  829. var textLength = ctx.measureText(data.labels[i]).width;
  830. //If the text length is longer - make that equal to longest text!
  831. widestXLabel = (textLength > widestXLabel)? textLength : widestXLabel;
  832. }
  833. if (width/data.labels.length < widestXLabel){
  834. rotateLabels = 45;
  835. if (width/data.labels.length < Math.cos(rotateLabels) * widestXLabel){
  836. rotateLabels = 90;
  837. maxSize -= widestXLabel;
  838. }
  839. else{
  840. maxSize -= Math.sin(rotateLabels) * widestXLabel;
  841. }
  842. }
  843. else{
  844. maxSize -= config.scaleFontSize;
  845. }
  846. //Add a little padding between the x line and the text
  847. maxSize -= 5;
  848. labelHeight = config.scaleFontSize;
  849. maxSize -= labelHeight;
  850. //Set 5 pixels greater than the font size to allow for a little padding from the X axis.
  851. scaleHeight = maxSize;
  852. //Then get the area above we can safely draw on.
  853. }
  854. function getValueBounds() {
  855. var upperValue = Number.MIN_VALUE;
  856. var lowerValue = Number.MAX_VALUE;
  857. for (var i=0; i<data.datasets.length; i++){
  858. for (var j=0; j<data.datasets[i].data.length; j++){
  859. if ( data.datasets[i].data[j] > upperValue) { upperValue = data.datasets[i].data[j] };
  860. if ( data.datasets[i].data[j] < lowerValue) { lowerValue = data.datasets[i].data[j] };
  861. }
  862. };
  863. var maxSteps = Math.floor((scaleHeight / (labelHeight*0.66)));
  864. var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
  865. return {
  866. maxValue : upperValue,
  867. minValue : lowerValue,
  868. maxSteps : maxSteps,
  869. minSteps : minSteps
  870. };
  871. }
  872. }
  873. var Bar = function(data,config,ctx){
  874. var maxSize, scaleHop, calculatedScale, labelHeight, scaleHeight, valueBounds, labelTemplateString, valueHop,widestXLabel, xAxisLength,yAxisPosX,xAxisPosY,barWidth, rotateLabels = 0;
  875. calculateDrawingSizes();
  876. valueBounds = getValueBounds();
  877. //Check and set the scale
  878. labelTemplateString = (config.scaleShowLabels)? config.scaleLabel : "";
  879. if (!config.scaleOverride){
  880. calculatedScale = calculateScale(scaleHeight,valueBounds.maxSteps,valueBounds.minSteps,valueBounds.maxValue,valueBounds.minValue,labelTemplateString);
  881. }
  882. else {
  883. calculatedScale = {
  884. steps : config.scaleSteps,
  885. stepValue : config.scaleStepWidth,
  886. graphMin : config.scaleStartValue,
  887. labels : []
  888. }
  889. populateLabels(labelTemplateString, calculatedScale.labels,calculatedScale.steps,config.scaleStartValue,config.scaleStepWidth);
  890. }
  891. scaleHop = Math.floor(scaleHeight/calculatedScale.steps);
  892. calculateXAxisSize();
  893. animationLoop(config,drawScale,drawBars,ctx);
  894. function drawBars(animPc){
  895. ctx.lineWidth = config.barStrokeWidth;
  896. for (var i=0; i<data.datasets.length; i++){
  897. ctx.fillStyle = data.datasets[i].fillColor;
  898. ctx.strokeStyle = data.datasets[i].strokeColor;
  899. for (var j=0; j<data.datasets[i].data.length; j++){
  900. var barOffset = yAxisPosX + config.barValueSpacing + valueHop*j + barWidth*i + config.barDatasetSpacing*i + config.barStrokeWidth*i;
  901. ctx.beginPath();
  902. ctx.moveTo(barOffset, xAxisPosY);
  903. ctx.lineTo(barOffset, xAxisPosY - animPc*calculateOffset(data.datasets[i].data[j],calculatedScale,scaleHop)+(config.barStrokeWidth/2));
  904. ctx.lineTo(barOffset + barWidth, xAxisPosY - animPc*calculateOffset(data.datasets[i].data[j],calculatedScale,scaleHop)+(config.barStrokeWidth/2));
  905. ctx.lineTo(barOffset + barWidth, xAxisPosY);
  906. if(config.barShowStroke){
  907. ctx.stroke();
  908. }
  909. ctx.closePath();
  910. ctx.fill();
  911. }
  912. }
  913. }
  914. function drawScale(){
  915. //X axis line
  916. ctx.lineWidth = config.scaleLineWidth;
  917. ctx.strokeStyle = config.scaleLineColor;
  918. ctx.beginPath();
  919. ctx.moveTo(width-widestXLabel/2+5,xAxisPosY);
  920. ctx.lineTo(width-(widestXLabel/2)-xAxisLength-5,xAxisPosY);
  921. ctx.stroke();
  922. if (rotateLabels > 0){
  923. ctx.save();
  924. ctx.textAlign = "right";
  925. }
  926. else{
  927. ctx.textAlign = "center";
  928. }
  929. ctx.fillStyle = config.scaleFontColor;
  930. for (var i=0; i<data.labels.length; i++){
  931. ctx.save();
  932. if (rotateLabels > 0){
  933. ctx.translate(yAxisPosX + i*valueHop,xAxisPosY + config.scaleFontSize);
  934. ctx.rotate(-(rotateLabels * (Math.PI/180)));
  935. ctx.fillText(data.labels[i], 0,0);
  936. ctx.restore();
  937. }
  938. else{
  939. ctx.fillText(data.labels[i], yAxisPosX + i*valueHop + valueHop/2,xAxisPosY + config.scaleFontSize+3);
  940. }
  941. ctx.beginPath();
  942. ctx.moveTo(yAxisPosX + (i+1) * valueHop, xAxisPosY+3);
  943. //Check i isnt 0, so we dont go over the Y axis twice.
  944. ctx.lineWidth = config.scaleGridLineWidth;
  945. ctx.strokeStyle = config.scaleGridLineColor;
  946. ctx.lineTo(yAxisPosX + (i+1) * valueHop, 5);
  947. ctx.stroke();
  948. }
  949. //Y axis
  950. ctx.lineWidth = config.scaleLineWidth;
  951. ctx.strokeStyle = config.scaleLineColor;
  952. ctx.beginPath();
  953. ctx.moveTo(yAxisPosX,xAxisPosY+5);
  954. ctx.lineTo(yAxisPosX,5);
  955. ctx.stroke();
  956. ctx.textAlign = "right";
  957. ctx.textBaseline = "middle";
  958. for (var j=0; j<calculatedScale.steps; j++){
  959. ctx.beginPath();
  960. ctx.moveTo(yAxisPosX-3,xAxisPosY - ((j+1) * scaleHop));
  961. if (config.scaleShowGridLines){
  962. ctx.lineWidth = config.scaleGridLineWidth;
  963. ctx.strokeStyle = config.scaleGridLineColor;
  964. ctx.lineTo(yAxisPosX + xAxisLength + 5,xAxisPosY - ((j+1) * scaleHop));
  965. }
  966. else{
  967. ctx.lineTo(yAxisPosX-0.5,xAxisPosY - ((j+1) * scaleHop));
  968. }
  969. ctx.stroke();
  970. if (config.scaleShowLabels){
  971. ctx.fillText(calculatedScale.labels[j],yAxisPosX-8,xAxisPosY - ((j+1) * scaleHop));
  972. }
  973. }
  974. }
  975. function calculateXAxisSize(){
  976. var longestText = 1;
  977. //if we are showing the labels
  978. if (config.scaleShowLabels){
  979. ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
  980. for (var i=0; i<calculatedScale.labels.length; i++){
  981. var measuredText = ctx.measureText(calculatedScale.labels[i]).width;
  982. longestText = (measuredText > longestText)? measuredText : longestText;
  983. }
  984. //Add a little extra padding from the y axis
  985. longestText +=10;
  986. }
  987. xAxisLength = width - longestText - widestXLabel;
  988. valueHop = Math.floor(xAxisLength/(data.labels.length));
  989. barWidth = (valueHop - config.scaleGridLineWidth*2 - (config.barValueSpacing*2) - (config.barDatasetSpacing*data.datasets.length-1) - ((config.barStrokeWidth/2)*data.datasets.length-1))/data.datasets.length;
  990. yAxisPosX = width-widestXLabel/2-xAxisLength;
  991. xAxisPosY = scaleHeight + config.scaleFontSize/2;
  992. }
  993. function calculateDrawingSizes(){
  994. maxSize = height;
  995. //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.
  996. ctx.font = config.scaleFontStyle + " " + config.scaleFontSize+"px " + config.scaleFontFamily;
  997. widestXLabel = 1;
  998. for (var i=0; i<data.labels.length; i++){
  999. var textLength = ctx.measureText(data.labels[i]).width;
  1000. //If the text length is longer - make that equal to longest text!
  1001. widestXLabel = (textLength > widestXLabel)? textLength : widestXLabel;
  1002. }
  1003. if (width/data.labels.length < widestXLabel){
  1004. rotateLabels = 45;
  1005. if (width/data.labels.length < Math.cos(rotateLabels) * widestXLabel){
  1006. rotateLabels = 90;
  1007. maxSize -= widestXLabel;
  1008. }
  1009. else{
  1010. maxSize -= Math.sin(rotateLabels) * widestXLabel;
  1011. }
  1012. }
  1013. else{
  1014. maxSize -= config.scaleFontSize;
  1015. }
  1016. //Add a little padding between the x line and the text
  1017. maxSize -= 5;
  1018. labelHeight = config.scaleFontSize;
  1019. maxSize -= labelHeight;
  1020. //Set 5 pixels greater than the font size to allow for a little padding from the X axis.
  1021. scaleHeight = maxSize;
  1022. //Then get the area above we can safely draw on.
  1023. }
  1024. function getValueBounds() {
  1025. var upperValue = Number.MIN_VALUE;
  1026. var lowerValue = Number.MAX_VALUE;
  1027. for (var i=0; i<data.datasets.length; i++){
  1028. for (var j=0; j<data.datasets[i].data.length; j++){
  1029. if ( data.datasets[i].data[j] > upperValue) { upperValue = data.datasets[i].data[j] };
  1030. if ( data.datasets[i].data[j] < lowerValue) { lowerValue = data.datasets[i].data[j] };
  1031. }
  1032. };
  1033. var maxSteps = Math.floor((scaleHeight / (labelHeight*0.66)));
  1034. var minSteps = Math.floor((scaleHeight / labelHeight*0.5));
  1035. return {
  1036. maxValue : upperValue,
  1037. minValue : lowerValue,
  1038. maxSteps : maxSteps,
  1039. minSteps : minSteps
  1040. };
  1041. }
  1042. }
  1043. function calculateOffset(val,calculatedScale,scaleHop){
  1044. var outerValue = calculatedScale.steps * calculatedScale.stepValue;
  1045. var adjustedValue = val - calculatedScale.graphMin;
  1046. var scalingFactor = CapValue(adjustedValue/outerValue,1,0);
  1047. return (scaleHop*calculatedScale.steps) * scalingFactor;
  1048. }
  1049. function animationLoop(config,drawScale,drawData,ctx){
  1050. var animFrameAmount = (config.animation)? 1/CapValue(config.animationSteps,Number.MAX_VALUE,1) : 1,
  1051. easingFunction = animationOptions[config.animationEasing],
  1052. percentAnimComplete =(config.animation)? 0 : 1;
  1053. if (typeof drawScale !== "function") drawScale = function(){};
  1054. requestAnimFrame(animLoop);
  1055. function animateFrame(){
  1056. var easeAdjustedAnimationPercent =(config.animation)? CapValue(easingFunction(percentAnimComplete),null,0) : 1;
  1057. clear(ctx);
  1058. if(config.scaleOverlay){
  1059. drawData(easeAdjustedAnimationPercent);
  1060. drawScale();
  1061. } else {
  1062. drawScale();
  1063. drawData(easeAdjustedAnimationPercent);
  1064. }
  1065. }
  1066. function animLoop(){
  1067. //We need to check if the animation is incomplete (less than 1), or complete (1).
  1068. percentAnimComplete += animFrameAmount;
  1069. animateFrame();
  1070. //Stop the loop continuing forever
  1071. if (percentAnimComplete <= 1){
  1072. requestAnimFrame(animLoop);
  1073. }
  1074. else{
  1075. if (typeof config.onAnimationComplete == "function") config.onAnimationComplete();
  1076. }
  1077. }
  1078. }
  1079. //Declare global functions to be called within this namespace here.
  1080. // shim layer with setTimeout fallback
  1081. var requestAnimFrame = (function(){
  1082. return window.requestAnimationFrame ||
  1083. window.webkitRequestAnimationFrame ||
  1084. window.mozRequestAnimationFrame ||
  1085. window.oRequestAnimationFrame ||
  1086. window.msRequestAnimationFrame ||
  1087. function(callback) {
  1088. window.setTimeout(callback, 1000 / 60);
  1089. };
  1090. })();
  1091. function calculateScale(drawingHeight,maxSteps,minSteps,maxValue,minValue,labelTemplateString){
  1092. var graphMin,graphMax,graphRange,stepValue,numberOfSteps,valueRange,rangeOrderOfMagnitude,decimalNum;
  1093. valueRange = maxValue - minValue;
  1094. rangeOrderOfMagnitude = calculateOrderOfMagnitude(valueRange);
  1095. graphMin = Math.floor(minValue / (1 * Math.pow(10, rangeOrderOfMagnitude))) * Math.pow(10, rangeOrderOfMagnitude);
  1096. graphMax = Math.ceil(maxValue / (1 * Math.pow(10, rangeOrderOfMagnitude))) * Math.pow(10, rangeOrderOfMagnitude);
  1097. graphRange = graphMax - graphMin;
  1098. stepValue = Math.pow(10, rangeOrderOfMagnitude);
  1099. numberOfSteps = Math.round(graphRange / stepValue);
  1100. //Compare number of steps to the max and min for that size graph, and add in half steps if need be.
  1101. while(numberOfSteps < minSteps || numberOfSteps > maxSteps) {
  1102. if (numberOfSteps < minSteps){
  1103. stepValue /= 2;
  1104. numberOfSteps = Math.round(graphRange/stepValue);
  1105. }
  1106. else{
  1107. stepValue *=2;
  1108. numberOfSteps = Math.round(graphRange/stepValue);
  1109. }
  1110. };
  1111. var labels = [];
  1112. populateLabels(labelTemplateString, labels, numberOfSteps, graphMin, stepValue);
  1113. return {
  1114. steps : numberOfSteps,
  1115. stepValue : stepValue,
  1116. graphMin : graphMin,
  1117. labels : labels
  1118. }
  1119. function calculateOrderOfMagnitude(val){
  1120. return Math.floor(Math.log(val) / Math.LN10);
  1121. }
  1122. }
  1123. //Populate an array of all the labels by interpolating the string.
  1124. function populateLabels(labelTemplateString, labels, numberOfSteps, graphMin, stepValue) {
  1125. if (labelTemplateString) {
  1126. //Fix floating point errors by setting to fixed the on the same decimal as the stepValue.
  1127. for (var i = 1; i < numberOfSteps + 1; i++) {
  1128. labels.push(tmpl(labelTemplateString, {value: (graphMin + (stepValue * i)).toFixed(getDecimalPlaces(stepValue))}));
  1129. }
  1130. }
  1131. }
  1132. //Max value from array
  1133. function Max( array ){
  1134. return Math.max.apply( Math, array );
  1135. };
  1136. //Min value from array
  1137. function Min( array ){
  1138. return Math.min.apply( Math, array );
  1139. };
  1140. //Default if undefined
  1141. function Default(userDeclared,valueIfFalse){
  1142. if(!userDeclared){
  1143. return valueIfFalse;
  1144. } else {
  1145. return userDeclared;
  1146. }
  1147. };
  1148. //Is a number function
  1149. function isNumber(n) {
  1150. return !isNaN(parseFloat(n)) && isFinite(n);
  1151. }
  1152. //Apply cap a value at a high or low number
  1153. function CapValue(valueToCap, maxValue, minValue){
  1154. if(isNumber(maxValue)) {
  1155. if( valueToCap > maxValue ) {
  1156. return maxValue;
  1157. }
  1158. }
  1159. if(isNumber(minValue)){
  1160. if ( valueToCap < minValue ){
  1161. return minValue;
  1162. }
  1163. }
  1164. return valueToCap;
  1165. }
  1166. function getDecimalPlaces (num){
  1167. var numberOfDecimalPlaces;
  1168. if (num%1!=0){
  1169. return num.toString().split(".")[1].length
  1170. }
  1171. else{
  1172. return 0;
  1173. }
  1174. }
  1175. function mergeChartConfig(defaults,userDefined){
  1176. var returnObj = {};
  1177. for (var attrname in defaults) { returnObj[attrname] = defaults[attrname]; }
  1178. for (var attrname in userDefined) { returnObj[attrname] = userDefined[attrname]; }
  1179. return returnObj;
  1180. }
  1181. //Javascript micro templating by John Resig - source at http://ejohn.org/blog/javascript-micro-templating/
  1182. var cache = {};
  1183. function tmpl(str, data){
  1184. // Figure out if we're getting a template, or if we need to
  1185. // load the template - and be sure to cache the result.
  1186. var fn = !/\W/.test(str) ?
  1187. cache[str] = cache[str] ||
  1188. tmpl(document.getElementById(str).innerHTML) :
  1189. // Generate a reusable function that will serve as a template
  1190. // generator (and which will be cached).
  1191. new Function("obj",
  1192. "var p=[],print=function(){p.push.apply(p,arguments);};" +
  1193. // Introduce the data as local variables using with(){}
  1194. "with(obj){p.push('" +
  1195. // Convert the template into pure JavaScript
  1196. str
  1197. .replace(/[\r\t\n]/g, " ")
  1198. .split("<%").join("\t")
  1199. .replace(/((^|%>)[^\t]*)'/g, "$1\r")
  1200. .replace(/\t=(.*?)%>/g, "',$1,'")
  1201. .split("\t").join("');")
  1202. .split("%>").join("p.push('")
  1203. .split("\r").join("\\'")
  1204. + "');}return p.join('');");
  1205. // Provide some basic currying to the user
  1206. return data ? fn( data ) : fn;
  1207. };
  1208. }