Chart.js 117 KB

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