Chart.js 107 KB

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