morris.js 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  1. /* @license
  2. morris.js v0.5.0
  3. Copyright 2014 Olly Smith All rights reserved.
  4. Licensed under the BSD-2-Clause License.
  5. */
  6. (function() {
  7. var $, Morris, minutesSpecHelper, secondsSpecHelper,
  8. __slice = [].slice,
  9. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  10. __hasProp = {}.hasOwnProperty,
  11. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  12. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  13. Morris = window.Morris = {};
  14. $ = jQuery;
  15. Morris.EventEmitter = (function() {
  16. function EventEmitter() {}
  17. EventEmitter.prototype.on = function(name, handler) {
  18. if (this.handlers == null) {
  19. this.handlers = {};
  20. }
  21. if (this.handlers[name] == null) {
  22. this.handlers[name] = [];
  23. }
  24. this.handlers[name].push(handler);
  25. return this;
  26. };
  27. EventEmitter.prototype.fire = function() {
  28. var args, handler, name, _i, _len, _ref, _results;
  29. name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  30. if ((this.handlers != null) && (this.handlers[name] != null)) {
  31. _ref = this.handlers[name];
  32. _results = [];
  33. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  34. handler = _ref[_i];
  35. _results.push(handler.apply(null, args));
  36. }
  37. return _results;
  38. }
  39. };
  40. return EventEmitter;
  41. })();
  42. Morris.commas = function(num) {
  43. var absnum, intnum, ret, strabsnum;
  44. if (num != null) {
  45. ret = num < 0 ? "-" : "";
  46. absnum = Math.abs(num);
  47. intnum = Math.floor(absnum).toFixed(0);
  48. ret += intnum.replace(/(?=(?:\d{3})+$)(?!^)/g, ',');
  49. strabsnum = absnum.toString();
  50. if (strabsnum.length > intnum.length) {
  51. ret += strabsnum.slice(intnum.length);
  52. }
  53. return ret;
  54. } else {
  55. return '-';
  56. }
  57. };
  58. Morris.pad2 = function(number) {
  59. return (number < 10 ? '0' : '') + number;
  60. };
  61. Morris.Grid = (function(_super) {
  62. __extends(Grid, _super);
  63. function Grid(options) {
  64. this.resizeHandler = __bind(this.resizeHandler, this);
  65. var _this = this;
  66. if (typeof options.element === 'string') {
  67. this.el = $(document.getElementById(options.element));
  68. } else {
  69. this.el = $(options.element);
  70. }
  71. if ((this.el == null) || this.el.length === 0) {
  72. throw new Error("Graph container element not found");
  73. }
  74. if (this.el.css('position') === 'static') {
  75. this.el.css('position', 'relative');
  76. }
  77. this.options = $.extend({}, this.gridDefaults, this.defaults || {}, options);
  78. if (typeof this.options.units === 'string') {
  79. this.options.postUnits = options.units;
  80. }
  81. this.raphael = new Raphael(this.el[0]);
  82. this.elementWidth = null;
  83. this.elementHeight = null;
  84. this.dirty = false;
  85. this.selectFrom = null;
  86. if (this.init) {
  87. this.init();
  88. }
  89. this.setData(this.options.data);
  90. this.el.bind('mousemove', function(evt) {
  91. var left, offset, right, width, x;
  92. offset = _this.el.offset();
  93. x = evt.pageX - offset.left;
  94. if (_this.selectFrom) {
  95. left = _this.data[_this.hitTest(Math.min(x, _this.selectFrom))]._x;
  96. right = _this.data[_this.hitTest(Math.max(x, _this.selectFrom))]._x;
  97. width = right - left;
  98. return _this.selectionRect.attr({
  99. x: left,
  100. width: width
  101. });
  102. } else {
  103. return _this.fire('hovermove', x, evt.pageY - offset.top);
  104. }
  105. });
  106. this.el.bind('mouseleave', function(evt) {
  107. if (_this.selectFrom) {
  108. _this.selectionRect.hide();
  109. _this.selectFrom = null;
  110. }
  111. return _this.fire('hoverout');
  112. });
  113. this.el.bind('touchstart touchmove touchend', function(evt) {
  114. var offset, touch;
  115. touch = evt.originalEvent.touches[0] || evt.originalEvent.changedTouches[0];
  116. offset = _this.el.offset();
  117. return _this.fire('hovermove', touch.pageX - offset.left, touch.pageY - offset.top);
  118. });
  119. this.el.bind('click', function(evt) {
  120. var offset;
  121. offset = _this.el.offset();
  122. return _this.fire('gridclick', evt.pageX - offset.left, evt.pageY - offset.top);
  123. });
  124. if (this.options.rangeSelect) {
  125. this.selectionRect = this.raphael.rect(0, 0, 0, this.el.innerHeight()).attr({
  126. fill: this.options.rangeSelectColor,
  127. stroke: false
  128. }).toBack().hide();
  129. this.el.bind('mousedown', function(evt) {
  130. var offset;
  131. offset = _this.el.offset();
  132. return _this.startRange(evt.pageX - offset.left);
  133. });
  134. this.el.bind('mouseup', function(evt) {
  135. var offset;
  136. offset = _this.el.offset();
  137. _this.endRange(evt.pageX - offset.left);
  138. return _this.fire('hovermove', evt.pageX - offset.left, evt.pageY - offset.top);
  139. });
  140. }
  141. if (this.options.resize) {
  142. $(window).bind('resize', function(evt) {
  143. if (_this.timeoutId != null) {
  144. window.clearTimeout(_this.timeoutId);
  145. }
  146. return _this.timeoutId = window.setTimeout(_this.resizeHandler, 100);
  147. });
  148. }
  149. this.el.css('-webkit-tap-highlight-color', 'rgba(0,0,0,0)');
  150. if (this.postInit) {
  151. this.postInit();
  152. }
  153. }
  154. Grid.prototype.gridDefaults = {
  155. dateFormat: null,
  156. axes: true,
  157. grid: true,
  158. gridLineColor: '#aaa',
  159. gridStrokeWidth: 0.5,
  160. gridTextColor: '#888',
  161. gridTextSize: 12,
  162. gridTextFamily: 'sans-serif',
  163. gridTextWeight: 'normal',
  164. hideHover: false,
  165. yLabelFormat: null,
  166. xLabelAngle: 0,
  167. numLines: 5,
  168. padding: 25,
  169. parseTime: true,
  170. postUnits: '',
  171. preUnits: '',
  172. ymax: 'auto',
  173. ymin: 'auto 0',
  174. goals: [],
  175. goalStrokeWidth: 1.0,
  176. goalLineColors: ['#666633', '#999966', '#cc6666', '#663333'],
  177. events: [],
  178. eventStrokeWidth: 1.0,
  179. eventLineColors: ['#005a04', '#ccffbb', '#3a5f0b', '#005502'],
  180. rangeSelect: null,
  181. rangeSelectColor: '#eef',
  182. resize: false
  183. };
  184. Grid.prototype.setData = function(data, redraw) {
  185. var e, idx, index, maxGoal, minGoal, ret, row, step, total, y, ykey, ymax, ymin, yval, _ref;
  186. if (redraw == null) {
  187. redraw = true;
  188. }
  189. this.options.data = data;
  190. if ((data == null) || data.length === 0) {
  191. this.data = [];
  192. this.raphael.clear();
  193. if (this.hover != null) {
  194. this.hover.hide();
  195. }
  196. return;
  197. }
  198. ymax = this.cumulative ? 0 : null;
  199. ymin = this.cumulative ? 0 : null;
  200. if (this.options.goals.length > 0) {
  201. minGoal = Math.min.apply(Math, this.options.goals);
  202. maxGoal = Math.max.apply(Math, this.options.goals);
  203. ymin = ymin != null ? Math.min(ymin, minGoal) : minGoal;
  204. ymax = ymax != null ? Math.max(ymax, maxGoal) : maxGoal;
  205. }
  206. this.data = (function() {
  207. var _i, _len, _results;
  208. _results = [];
  209. for (index = _i = 0, _len = data.length; _i < _len; index = ++_i) {
  210. row = data[index];
  211. ret = {
  212. src: row
  213. };
  214. ret.label = row[this.options.xkey];
  215. if (this.options.parseTime) {
  216. ret.x = Morris.parseDate(ret.label);
  217. if (this.options.dateFormat) {
  218. ret.label = this.options.dateFormat(ret.x);
  219. } else if (typeof ret.label === 'number') {
  220. ret.label = new Date(ret.label).toString();
  221. }
  222. } else {
  223. ret.x = index;
  224. if (this.options.xLabelFormat) {
  225. ret.label = this.options.xLabelFormat(ret);
  226. }
  227. }
  228. total = 0;
  229. ret.y = (function() {
  230. var _j, _len1, _ref, _results1;
  231. _ref = this.options.ykeys;
  232. _results1 = [];
  233. for (idx = _j = 0, _len1 = _ref.length; _j < _len1; idx = ++_j) {
  234. ykey = _ref[idx];
  235. yval = row[ykey];
  236. if (typeof yval === 'string') {
  237. yval = parseFloat(yval);
  238. }
  239. if ((yval != null) && typeof yval !== 'number') {
  240. yval = null;
  241. }
  242. if (yval != null) {
  243. if (this.cumulative) {
  244. total += yval;
  245. } else {
  246. if (ymax != null) {
  247. ymax = Math.max(yval, ymax);
  248. ymin = Math.min(yval, ymin);
  249. } else {
  250. ymax = ymin = yval;
  251. }
  252. }
  253. }
  254. if (this.cumulative && (total != null)) {
  255. ymax = Math.max(total, ymax);
  256. ymin = Math.min(total, ymin);
  257. }
  258. _results1.push(yval);
  259. }
  260. return _results1;
  261. }).call(this);
  262. _results.push(ret);
  263. }
  264. return _results;
  265. }).call(this);
  266. if (this.options.parseTime) {
  267. this.data = this.data.sort(function(a, b) {
  268. return (a.x > b.x) - (b.x > a.x);
  269. });
  270. }
  271. this.xmin = this.data[0].x;
  272. this.xmax = this.data[this.data.length - 1].x;
  273. this.events = [];
  274. if (this.options.events.length > 0) {
  275. if (this.options.parseTime) {
  276. this.events = (function() {
  277. var _i, _len, _ref, _results;
  278. _ref = this.options.events;
  279. _results = [];
  280. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  281. e = _ref[_i];
  282. _results.push(Morris.parseDate(e));
  283. }
  284. return _results;
  285. }).call(this);
  286. } else {
  287. this.events = this.options.events;
  288. }
  289. this.xmax = Math.max(this.xmax, Math.max.apply(Math, this.events));
  290. this.xmin = Math.min(this.xmin, Math.min.apply(Math, this.events));
  291. }
  292. if (this.xmin === this.xmax) {
  293. this.xmin -= 1;
  294. this.xmax += 1;
  295. }
  296. this.ymin = this.yboundary('min', ymin);
  297. this.ymax = this.yboundary('max', ymax);
  298. if (this.ymin === this.ymax) {
  299. if (ymin) {
  300. this.ymin -= 1;
  301. }
  302. this.ymax += 1;
  303. }
  304. if (((_ref = this.options.axes) === true || _ref === 'both' || _ref === 'y') || this.options.grid === true) {
  305. if (this.options.ymax === this.gridDefaults.ymax && this.options.ymin === this.gridDefaults.ymin) {
  306. this.grid = this.autoGridLines(this.ymin, this.ymax, this.options.numLines);
  307. this.ymin = Math.min(this.ymin, this.grid[0]);
  308. this.ymax = Math.max(this.ymax, this.grid[this.grid.length - 1]);
  309. } else {
  310. step = (this.ymax - this.ymin) / (this.options.numLines - 1);
  311. this.grid = (function() {
  312. var _i, _ref1, _ref2, _results;
  313. _results = [];
  314. for (y = _i = _ref1 = this.ymin, _ref2 = this.ymax; step > 0 ? _i <= _ref2 : _i >= _ref2; y = _i += step) {
  315. _results.push(y);
  316. }
  317. return _results;
  318. }).call(this);
  319. }
  320. }
  321. this.dirty = true;
  322. if (redraw) {
  323. return this.redraw();
  324. }
  325. };
  326. Grid.prototype.yboundary = function(boundaryType, currentValue) {
  327. var boundaryOption, suggestedValue;
  328. boundaryOption = this.options["y" + boundaryType];
  329. if (typeof boundaryOption === 'string') {
  330. if (boundaryOption.slice(0, 4) === 'auto') {
  331. if (boundaryOption.length > 5) {
  332. suggestedValue = parseInt(boundaryOption.slice(5), 10);
  333. if (currentValue == null) {
  334. return suggestedValue;
  335. }
  336. return Math[boundaryType](currentValue, suggestedValue);
  337. } else {
  338. if (currentValue != null) {
  339. return currentValue;
  340. } else {
  341. return 0;
  342. }
  343. }
  344. } else {
  345. return parseInt(boundaryOption, 10);
  346. }
  347. } else {
  348. return boundaryOption;
  349. }
  350. };
  351. Grid.prototype.autoGridLines = function(ymin, ymax, nlines) {
  352. var gmax, gmin, grid, smag, span, step, unit, y, ymag;
  353. span = ymax - ymin;
  354. ymag = Math.floor(Math.log(span) / Math.log(10));
  355. unit = Math.pow(10, ymag);
  356. gmin = Math.floor(ymin / unit) * unit;
  357. gmax = Math.ceil(ymax / unit) * unit;
  358. step = (gmax - gmin) / (nlines - 1);
  359. if (unit === 1 && step > 1 && Math.ceil(step) !== step) {
  360. step = Math.ceil(step);
  361. gmax = gmin + step * (nlines - 1);
  362. }
  363. if (gmin < 0 && gmax > 0) {
  364. gmin = Math.floor(ymin / step) * step;
  365. gmax = Math.ceil(ymax / step) * step;
  366. }
  367. if (step < 1) {
  368. smag = Math.floor(Math.log(step) / Math.log(10));
  369. grid = (function() {
  370. var _i, _results;
  371. _results = [];
  372. for (y = _i = gmin; step > 0 ? _i <= gmax : _i >= gmax; y = _i += step) {
  373. _results.push(parseFloat(y.toFixed(1 - smag)));
  374. }
  375. return _results;
  376. })();
  377. } else {
  378. grid = (function() {
  379. var _i, _results;
  380. _results = [];
  381. for (y = _i = gmin; step > 0 ? _i <= gmax : _i >= gmax; y = _i += step) {
  382. _results.push(y);
  383. }
  384. return _results;
  385. })();
  386. }
  387. return grid;
  388. };
  389. Grid.prototype._calc = function() {
  390. var bottomOffsets, gridLine, h, i, w, yLabelWidths, _ref, _ref1;
  391. w = this.el.width();
  392. h = this.el.height();
  393. if (this.elementWidth !== w || this.elementHeight !== h || this.dirty) {
  394. this.elementWidth = w;
  395. this.elementHeight = h;
  396. this.dirty = false;
  397. this.left = this.options.padding;
  398. this.right = this.elementWidth - this.options.padding;
  399. this.top = this.options.padding;
  400. this.bottom = this.elementHeight - this.options.padding;
  401. if ((_ref = this.options.axes) === true || _ref === 'both' || _ref === 'y') {
  402. yLabelWidths = (function() {
  403. var _i, _len, _ref1, _results;
  404. _ref1 = this.grid;
  405. _results = [];
  406. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  407. gridLine = _ref1[_i];
  408. _results.push(this.measureText(this.yAxisFormat(gridLine)).width);
  409. }
  410. return _results;
  411. }).call(this);
  412. this.left += Math.max.apply(Math, yLabelWidths);
  413. }
  414. if ((_ref1 = this.options.axes) === true || _ref1 === 'both' || _ref1 === 'x') {
  415. bottomOffsets = (function() {
  416. var _i, _ref2, _results;
  417. _results = [];
  418. for (i = _i = 0, _ref2 = this.data.length; 0 <= _ref2 ? _i < _ref2 : _i > _ref2; i = 0 <= _ref2 ? ++_i : --_i) {
  419. _results.push(this.measureText(this.data[i].text, -this.options.xLabelAngle).height);
  420. }
  421. return _results;
  422. }).call(this);
  423. this.bottom -= Math.max.apply(Math, bottomOffsets);
  424. }
  425. this.width = Math.max(1, this.right - this.left);
  426. this.height = Math.max(1, this.bottom - this.top);
  427. this.dx = this.width / (this.xmax - this.xmin);
  428. this.dy = this.height / (this.ymax - this.ymin);
  429. if (this.calc) {
  430. return this.calc();
  431. }
  432. }
  433. };
  434. Grid.prototype.transY = function(y) {
  435. return this.bottom - (y - this.ymin) * this.dy;
  436. };
  437. Grid.prototype.transX = function(x) {
  438. if (this.data.length === 1) {
  439. return (this.left + this.right) / 2;
  440. } else {
  441. return this.left + (x - this.xmin) * this.dx;
  442. }
  443. };
  444. Grid.prototype.redraw = function() {
  445. this.raphael.clear();
  446. this._calc();
  447. this.drawGrid();
  448. this.drawGoals();
  449. this.drawEvents();
  450. if (this.draw) {
  451. return this.draw();
  452. }
  453. };
  454. Grid.prototype.measureText = function(text, angle) {
  455. var ret, tt;
  456. if (angle == null) {
  457. angle = 0;
  458. }
  459. tt = this.raphael.text(100, 100, text).attr('font-size', this.options.gridTextSize).attr('font-family', this.options.gridTextFamily).attr('font-weight', this.options.gridTextWeight).rotate(angle);
  460. ret = tt.getBBox();
  461. tt.remove();
  462. return ret;
  463. };
  464. Grid.prototype.yAxisFormat = function(label) {
  465. return this.yLabelFormat(label);
  466. };
  467. Grid.prototype.yLabelFormat = function(label) {
  468. if (typeof this.options.yLabelFormat === 'function') {
  469. return this.options.yLabelFormat(label);
  470. } else {
  471. return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits;
  472. }
  473. };
  474. Grid.prototype.drawGrid = function() {
  475. var lineY, y, _i, _len, _ref, _ref1, _ref2, _results;
  476. if (this.options.grid === false && ((_ref = this.options.axes) !== true && _ref !== 'both' && _ref !== 'y')) {
  477. return;
  478. }
  479. _ref1 = this.grid;
  480. _results = [];
  481. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  482. lineY = _ref1[_i];
  483. y = this.transY(lineY);
  484. if ((_ref2 = this.options.axes) === true || _ref2 === 'both' || _ref2 === 'y') {
  485. this.drawYAxisLabel(this.left - this.options.padding / 2, y, this.yAxisFormat(lineY));
  486. }
  487. if (this.options.grid) {
  488. _results.push(this.drawGridLine("M" + this.left + "," + y + "H" + (this.left + this.width)));
  489. } else {
  490. _results.push(void 0);
  491. }
  492. }
  493. return _results;
  494. };
  495. Grid.prototype.drawGoals = function() {
  496. var color, goal, i, _i, _len, _ref, _results;
  497. _ref = this.options.goals;
  498. _results = [];
  499. for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
  500. goal = _ref[i];
  501. color = this.options.goalLineColors[i % this.options.goalLineColors.length];
  502. _results.push(this.drawGoal(goal, color));
  503. }
  504. return _results;
  505. };
  506. Grid.prototype.drawEvents = function() {
  507. var color, event, i, _i, _len, _ref, _results;
  508. _ref = this.events;
  509. _results = [];
  510. for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
  511. event = _ref[i];
  512. color = this.options.eventLineColors[i % this.options.eventLineColors.length];
  513. _results.push(this.drawEvent(event, color));
  514. }
  515. return _results;
  516. };
  517. Grid.prototype.drawGoal = function(goal, color) {
  518. return this.raphael.path("M" + this.left + "," + (this.transY(goal)) + "H" + this.right).attr('stroke', color).attr('stroke-width', this.options.goalStrokeWidth);
  519. };
  520. Grid.prototype.drawEvent = function(event, color) {
  521. return this.raphael.path("M" + (this.transX(event)) + "," + this.bottom + "V" + this.top).attr('stroke', color).attr('stroke-width', this.options.eventStrokeWidth);
  522. };
  523. Grid.prototype.drawYAxisLabel = function(xPos, yPos, text) {
  524. return this.raphael.text(xPos, yPos, text).attr('font-size', this.options.gridTextSize).attr('font-family', this.options.gridTextFamily).attr('font-weight', this.options.gridTextWeight).attr('fill', this.options.gridTextColor).attr('text-anchor', 'end');
  525. };
  526. Grid.prototype.drawGridLine = function(path) {
  527. return this.raphael.path(path).attr('stroke', this.options.gridLineColor).attr('stroke-width', this.options.gridStrokeWidth);
  528. };
  529. Grid.prototype.startRange = function(x) {
  530. this.hover.hide();
  531. this.selectFrom = x;
  532. return this.selectionRect.attr({
  533. x: x,
  534. width: 0
  535. }).show();
  536. };
  537. Grid.prototype.endRange = function(x) {
  538. var end, start;
  539. if (this.selectFrom) {
  540. start = Math.min(this.selectFrom, x);
  541. end = Math.max(this.selectFrom, x);
  542. this.options.rangeSelect.call(this.el, {
  543. start: this.data[this.hitTest(start)].x,
  544. end: this.data[this.hitTest(end)].x
  545. });
  546. return this.selectFrom = null;
  547. }
  548. };
  549. Grid.prototype.resizeHandler = function() {
  550. this.timeoutId = null;
  551. this.raphael.setSize(this.el.width(), this.el.height());
  552. return this.redraw();
  553. };
  554. return Grid;
  555. })(Morris.EventEmitter);
  556. Morris.parseDate = function(date) {
  557. var isecs, m, msecs, n, o, offsetmins, p, q, r, ret, secs;
  558. if (typeof date === 'number') {
  559. return date;
  560. }
  561. m = date.match(/^(\d+) Q(\d)$/);
  562. n = date.match(/^(\d+)-(\d+)$/);
  563. o = date.match(/^(\d+)-(\d+)-(\d+)$/);
  564. p = date.match(/^(\d+) W(\d+)$/);
  565. q = date.match(/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+)(Z|([+-])(\d\d):?(\d\d))?$/);
  566. r = date.match(/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+):(\d+(\.\d+)?)(Z|([+-])(\d\d):?(\d\d))?$/);
  567. if (m) {
  568. return new Date(parseInt(m[1], 10), parseInt(m[2], 10) * 3 - 1, 1).getTime();
  569. } else if (n) {
  570. return new Date(parseInt(n[1], 10), parseInt(n[2], 10) - 1, 1).getTime();
  571. } else if (o) {
  572. return new Date(parseInt(o[1], 10), parseInt(o[2], 10) - 1, parseInt(o[3], 10)).getTime();
  573. } else if (p) {
  574. ret = new Date(parseInt(p[1], 10), 0, 1);
  575. if (ret.getDay() !== 4) {
  576. ret.setMonth(0, 1 + ((4 - ret.getDay()) + 7) % 7);
  577. }
  578. return ret.getTime() + parseInt(p[2], 10) * 604800000;
  579. } else if (q) {
  580. if (!q[6]) {
  581. return new Date(parseInt(q[1], 10), parseInt(q[2], 10) - 1, parseInt(q[3], 10), parseInt(q[4], 10), parseInt(q[5], 10)).getTime();
  582. } else {
  583. offsetmins = 0;
  584. if (q[6] !== 'Z') {
  585. offsetmins = parseInt(q[8], 10) * 60 + parseInt(q[9], 10);
  586. if (q[7] === '+') {
  587. offsetmins = 0 - offsetmins;
  588. }
  589. }
  590. return Date.UTC(parseInt(q[1], 10), parseInt(q[2], 10) - 1, parseInt(q[3], 10), parseInt(q[4], 10), parseInt(q[5], 10) + offsetmins);
  591. }
  592. } else if (r) {
  593. secs = parseFloat(r[6]);
  594. isecs = Math.floor(secs);
  595. msecs = Math.round((secs - isecs) * 1000);
  596. if (!r[8]) {
  597. return new Date(parseInt(r[1], 10), parseInt(r[2], 10) - 1, parseInt(r[3], 10), parseInt(r[4], 10), parseInt(r[5], 10), isecs, msecs).getTime();
  598. } else {
  599. offsetmins = 0;
  600. if (r[8] !== 'Z') {
  601. offsetmins = parseInt(r[10], 10) * 60 + parseInt(r[11], 10);
  602. if (r[9] === '+') {
  603. offsetmins = 0 - offsetmins;
  604. }
  605. }
  606. return Date.UTC(parseInt(r[1], 10), parseInt(r[2], 10) - 1, parseInt(r[3], 10), parseInt(r[4], 10), parseInt(r[5], 10) + offsetmins, isecs, msecs);
  607. }
  608. } else {
  609. return new Date(parseInt(date, 10), 0, 1).getTime();
  610. }
  611. };
  612. Morris.Hover = (function() {
  613. Hover.defaults = {
  614. "class": 'morris-hover morris-default-style'
  615. };
  616. function Hover(options) {
  617. if (options == null) {
  618. options = {};
  619. }
  620. this.options = $.extend({}, Morris.Hover.defaults, options);
  621. this.el = $("<div class='" + this.options["class"] + "'></div>");
  622. this.el.hide();
  623. this.options.parent.append(this.el);
  624. }
  625. Hover.prototype.update = function(html, x, y) {
  626. if (!html) {
  627. return this.hide();
  628. } else {
  629. this.html(html);
  630. this.show();
  631. return this.moveTo(x, y);
  632. }
  633. };
  634. Hover.prototype.html = function(content) {
  635. return this.el.html(content);
  636. };
  637. Hover.prototype.moveTo = function(x, y) {
  638. var hoverHeight, hoverWidth, left, parentHeight, parentWidth, top;
  639. parentWidth = this.options.parent.innerWidth();
  640. parentHeight = this.options.parent.innerHeight();
  641. hoverWidth = this.el.outerWidth();
  642. hoverHeight = this.el.outerHeight();
  643. left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth);
  644. if (y != null) {
  645. top = y - hoverHeight - 10;
  646. if (top < 0) {
  647. top = y + 10;
  648. if (top + hoverHeight > parentHeight) {
  649. top = parentHeight / 2 - hoverHeight / 2;
  650. }
  651. }
  652. } else {
  653. top = parentHeight / 2 - hoverHeight / 2;
  654. }
  655. return this.el.css({
  656. left: left + "px",
  657. top: parseInt(top) + "px"
  658. });
  659. };
  660. Hover.prototype.show = function() {
  661. return this.el.show();
  662. };
  663. Hover.prototype.hide = function() {
  664. return this.el.hide();
  665. };
  666. return Hover;
  667. })();
  668. Morris.Line = (function(_super) {
  669. __extends(Line, _super);
  670. function Line(options) {
  671. this.hilight = __bind(this.hilight, this);
  672. this.onHoverOut = __bind(this.onHoverOut, this);
  673. this.onHoverMove = __bind(this.onHoverMove, this);
  674. this.onGridClick = __bind(this.onGridClick, this);
  675. if (!(this instanceof Morris.Line)) {
  676. return new Morris.Line(options);
  677. }
  678. Line.__super__.constructor.call(this, options);
  679. }
  680. Line.prototype.init = function() {
  681. if (this.options.hideHover !== 'always') {
  682. this.hover = new Morris.Hover({
  683. parent: this.el
  684. });
  685. this.on('hovermove', this.onHoverMove);
  686. this.on('hoverout', this.onHoverOut);
  687. return this.on('gridclick', this.onGridClick);
  688. }
  689. };
  690. Line.prototype.defaults = {
  691. lineWidth: 3,
  692. pointSize: 4,
  693. lineColors: ['#0b62a4', '#7A92A3', '#4da74d', '#afd8f8', '#edc240', '#cb4b4b', '#9440ed'],
  694. pointStrokeWidths: [1],
  695. pointStrokeColors: ['#ffffff'],
  696. pointFillColors: [],
  697. smooth: true,
  698. xLabels: 'auto',
  699. xLabelFormat: null,
  700. xLabelMargin: 24,
  701. hideHover: false
  702. };
  703. Line.prototype.calc = function() {
  704. this.calcPoints();
  705. return this.generatePaths();
  706. };
  707. Line.prototype.calcPoints = function() {
  708. var row, y, _i, _len, _ref, _results;
  709. _ref = this.data;
  710. _results = [];
  711. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  712. row = _ref[_i];
  713. row._x = this.transX(row.x);
  714. row._y = (function() {
  715. var _j, _len1, _ref1, _results1;
  716. _ref1 = row.y;
  717. _results1 = [];
  718. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  719. y = _ref1[_j];
  720. if (y != null) {
  721. _results1.push(this.transY(y));
  722. } else {
  723. _results1.push(y);
  724. }
  725. }
  726. return _results1;
  727. }).call(this);
  728. _results.push(row._ymax = Math.min.apply(Math, [this.bottom].concat((function() {
  729. var _j, _len1, _ref1, _results1;
  730. _ref1 = row._y;
  731. _results1 = [];
  732. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  733. y = _ref1[_j];
  734. if (y != null) {
  735. _results1.push(y);
  736. }
  737. }
  738. return _results1;
  739. })())));
  740. }
  741. return _results;
  742. };
  743. Line.prototype.hitTest = function(x) {
  744. var index, r, _i, _len, _ref;
  745. if (this.data.length === 0) {
  746. return null;
  747. }
  748. _ref = this.data.slice(1);
  749. for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
  750. r = _ref[index];
  751. if (x < (r._x + this.data[index]._x) / 2) {
  752. break;
  753. }
  754. }
  755. return index;
  756. };
  757. Line.prototype.onGridClick = function(x, y) {
  758. var index;
  759. index = this.hitTest(x);
  760. return this.fire('click', index, this.data[index].src, x, y);
  761. };
  762. Line.prototype.onHoverMove = function(x, y) {
  763. var index;
  764. index = this.hitTest(x);
  765. return this.displayHoverForRow(index);
  766. };
  767. Line.prototype.onHoverOut = function() {
  768. if (this.options.hideHover !== false) {
  769. return this.displayHoverForRow(null);
  770. }
  771. };
  772. Line.prototype.displayHoverForRow = function(index) {
  773. var _ref;
  774. if (index != null) {
  775. (_ref = this.hover).update.apply(_ref, this.hoverContentForRow(index));
  776. return this.hilight(index);
  777. } else {
  778. this.hover.hide();
  779. return this.hilight();
  780. }
  781. };
  782. Line.prototype.hoverContentForRow = function(index) {
  783. var content, j, row, y, _i, _len, _ref;
  784. row = this.data[index];
  785. content = "<div class='morris-hover-row-label'>" + row.label + "</div>";
  786. _ref = row.y;
  787. for (j = _i = 0, _len = _ref.length; _i < _len; j = ++_i) {
  788. y = _ref[j];
  789. content += "<div class='morris-hover-point' style='color: " + (this.colorFor(row, j, 'label')) + "'>\n " + this.options.labels[j] + ":\n " + (this.yLabelFormat(y)) + "\n</div>";
  790. }
  791. if (typeof this.options.hoverCallback === 'function') {
  792. content = this.options.hoverCallback(index, this.options, content, row.src);
  793. }
  794. return [content, row._x, row._ymax];
  795. };
  796. Line.prototype.generatePaths = function() {
  797. var coords, i, r, smooth;
  798. return this.paths = (function() {
  799. var _i, _ref, _ref1, _results;
  800. _results = [];
  801. for (i = _i = 0, _ref = this.options.ykeys.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
  802. smooth = typeof this.options.smooth === "boolean" ? this.options.smooth : (_ref1 = this.options.ykeys[i], __indexOf.call(this.options.smooth, _ref1) >= 0);
  803. coords = (function() {
  804. var _j, _len, _ref2, _results1;
  805. _ref2 = this.data;
  806. _results1 = [];
  807. for (_j = 0, _len = _ref2.length; _j < _len; _j++) {
  808. r = _ref2[_j];
  809. if (r._y[i] !== void 0) {
  810. _results1.push({
  811. x: r._x,
  812. y: r._y[i]
  813. });
  814. }
  815. }
  816. return _results1;
  817. }).call(this);
  818. if (coords.length > 1) {
  819. _results.push(Morris.Line.createPath(coords, smooth, this.bottom));
  820. } else {
  821. _results.push(null);
  822. }
  823. }
  824. return _results;
  825. }).call(this);
  826. };
  827. Line.prototype.draw = function() {
  828. var _ref;
  829. if ((_ref = this.options.axes) === true || _ref === 'both' || _ref === 'x') {
  830. this.drawXAxis();
  831. }
  832. this.drawSeries();
  833. if (this.options.hideHover === false) {
  834. return this.displayHoverForRow(this.data.length - 1);
  835. }
  836. };
  837. Line.prototype.drawXAxis = function() {
  838. var drawLabel, l, labels, prevAngleMargin, prevLabelMargin, row, ypos, _i, _len, _results,
  839. _this = this;
  840. ypos = this.bottom + this.options.padding / 2;
  841. prevLabelMargin = null;
  842. prevAngleMargin = null;
  843. drawLabel = function(labelText, xpos) {
  844. var label, labelBox, margin, offset, textBox;
  845. label = _this.drawXAxisLabel(_this.transX(xpos), ypos, labelText);
  846. textBox = label.getBBox();
  847. label.transform("r" + (-_this.options.xLabelAngle));
  848. labelBox = label.getBBox();
  849. label.transform("t0," + (labelBox.height / 2) + "...");
  850. if (_this.options.xLabelAngle !== 0) {
  851. offset = -0.5 * textBox.width * Math.cos(_this.options.xLabelAngle * Math.PI / 180.0);
  852. label.transform("t" + offset + ",0...");
  853. }
  854. labelBox = label.getBBox();
  855. if (((prevLabelMargin == null) || prevLabelMargin >= labelBox.x + labelBox.width || (prevAngleMargin != null) && prevAngleMargin >= labelBox.x) && labelBox.x >= 0 && (labelBox.x + labelBox.width) < _this.el.width()) {
  856. if (_this.options.xLabelAngle !== 0) {
  857. margin = 1.25 * _this.options.gridTextSize / Math.sin(_this.options.xLabelAngle * Math.PI / 180.0);
  858. prevAngleMargin = labelBox.x - margin;
  859. }
  860. return prevLabelMargin = labelBox.x - _this.options.xLabelMargin;
  861. } else {
  862. return label.remove();
  863. }
  864. };
  865. if (this.options.parseTime) {
  866. if (this.data.length === 1 && this.options.xLabels === 'auto') {
  867. labels = [[this.data[0].label, this.data[0].x]];
  868. } else {
  869. labels = Morris.labelSeries(this.xmin, this.xmax, this.width, this.options.xLabels, this.options.xLabelFormat);
  870. }
  871. } else {
  872. labels = (function() {
  873. var _i, _len, _ref, _results;
  874. _ref = this.data;
  875. _results = [];
  876. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  877. row = _ref[_i];
  878. _results.push([row.label, row.x]);
  879. }
  880. return _results;
  881. }).call(this);
  882. }
  883. labels.reverse();
  884. _results = [];
  885. for (_i = 0, _len = labels.length; _i < _len; _i++) {
  886. l = labels[_i];
  887. _results.push(drawLabel(l[0], l[1]));
  888. }
  889. return _results;
  890. };
  891. Line.prototype.drawSeries = function() {
  892. var i, _i, _j, _ref, _ref1, _results;
  893. this.seriesPoints = [];
  894. for (i = _i = _ref = this.options.ykeys.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) {
  895. this._drawLineFor(i);
  896. }
  897. _results = [];
  898. for (i = _j = _ref1 = this.options.ykeys.length - 1; _ref1 <= 0 ? _j <= 0 : _j >= 0; i = _ref1 <= 0 ? ++_j : --_j) {
  899. _results.push(this._drawPointFor(i));
  900. }
  901. return _results;
  902. };
  903. Line.prototype._drawPointFor = function(index) {
  904. var circle, row, _i, _len, _ref, _results;
  905. this.seriesPoints[index] = [];
  906. _ref = this.data;
  907. _results = [];
  908. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  909. row = _ref[_i];
  910. circle = null;
  911. if (row._y[index] != null) {
  912. circle = this.drawLinePoint(row._x, row._y[index], this.colorFor(row, index, 'point'), index);
  913. }
  914. _results.push(this.seriesPoints[index].push(circle));
  915. }
  916. return _results;
  917. };
  918. Line.prototype._drawLineFor = function(index) {
  919. var path;
  920. path = this.paths[index];
  921. if (path !== null) {
  922. return this.drawLinePath(path, this.colorFor(null, index, 'line'), index);
  923. }
  924. };
  925. Line.createPath = function(coords, smooth, bottom) {
  926. var coord, g, grads, i, ix, lg, path, prevCoord, x1, x2, y1, y2, _i, _len;
  927. path = "";
  928. if (smooth) {
  929. grads = Morris.Line.gradients(coords);
  930. }
  931. prevCoord = {
  932. y: null
  933. };
  934. for (i = _i = 0, _len = coords.length; _i < _len; i = ++_i) {
  935. coord = coords[i];
  936. if (coord.y != null) {
  937. if (prevCoord.y != null) {
  938. if (smooth) {
  939. g = grads[i];
  940. lg = grads[i - 1];
  941. ix = (coord.x - prevCoord.x) / 4;
  942. x1 = prevCoord.x + ix;
  943. y1 = Math.min(bottom, prevCoord.y + ix * lg);
  944. x2 = coord.x - ix;
  945. y2 = Math.min(bottom, coord.y - ix * g);
  946. path += "C" + x1 + "," + y1 + "," + x2 + "," + y2 + "," + coord.x + "," + coord.y;
  947. } else {
  948. path += "L" + coord.x + "," + coord.y;
  949. }
  950. } else {
  951. if (!smooth || (grads[i] != null)) {
  952. path += "M" + coord.x + "," + coord.y;
  953. }
  954. }
  955. }
  956. prevCoord = coord;
  957. }
  958. return path;
  959. };
  960. Line.gradients = function(coords) {
  961. var coord, grad, i, nextCoord, prevCoord, _i, _len, _results;
  962. grad = function(a, b) {
  963. return (a.y - b.y) / (a.x - b.x);
  964. };
  965. _results = [];
  966. for (i = _i = 0, _len = coords.length; _i < _len; i = ++_i) {
  967. coord = coords[i];
  968. if (coord.y != null) {
  969. nextCoord = coords[i + 1] || {
  970. y: null
  971. };
  972. prevCoord = coords[i - 1] || {
  973. y: null
  974. };
  975. if ((prevCoord.y != null) && (nextCoord.y != null)) {
  976. _results.push(grad(prevCoord, nextCoord));
  977. } else if (prevCoord.y != null) {
  978. _results.push(grad(prevCoord, coord));
  979. } else if (nextCoord.y != null) {
  980. _results.push(grad(coord, nextCoord));
  981. } else {
  982. _results.push(null);
  983. }
  984. } else {
  985. _results.push(null);
  986. }
  987. }
  988. return _results;
  989. };
  990. Line.prototype.hilight = function(index) {
  991. var i, _i, _j, _ref, _ref1;
  992. if (this.prevHilight !== null && this.prevHilight !== index) {
  993. for (i = _i = 0, _ref = this.seriesPoints.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
  994. if (this.seriesPoints[i][this.prevHilight]) {
  995. this.seriesPoints[i][this.prevHilight].animate(this.pointShrinkSeries(i));
  996. }
  997. }
  998. }
  999. if (index !== null && this.prevHilight !== index) {
  1000. for (i = _j = 0, _ref1 = this.seriesPoints.length - 1; 0 <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
  1001. if (this.seriesPoints[i][index]) {
  1002. this.seriesPoints[i][index].animate(this.pointGrowSeries(i));
  1003. }
  1004. }
  1005. }
  1006. return this.prevHilight = index;
  1007. };
  1008. Line.prototype.colorFor = function(row, sidx, type) {
  1009. if (typeof this.options.lineColors === 'function') {
  1010. return this.options.lineColors.call(this, row, sidx, type);
  1011. } else if (type === 'point') {
  1012. return this.options.pointFillColors[sidx % this.options.pointFillColors.length] || this.options.lineColors[sidx % this.options.lineColors.length];
  1013. } else {
  1014. return this.options.lineColors[sidx % this.options.lineColors.length];
  1015. }
  1016. };
  1017. Line.prototype.drawXAxisLabel = function(xPos, yPos, text) {
  1018. return this.raphael.text(xPos, yPos, text).attr('font-size', this.options.gridTextSize).attr('font-family', this.options.gridTextFamily).attr('font-weight', this.options.gridTextWeight).attr('fill', this.options.gridTextColor);
  1019. };
  1020. Line.prototype.drawLinePath = function(path, lineColor, lineIndex) {
  1021. return this.raphael.path(path).attr('stroke', lineColor).attr('stroke-width', this.lineWidthForSeries(lineIndex));
  1022. };
  1023. Line.prototype.drawLinePoint = function(xPos, yPos, pointColor, lineIndex) {
  1024. return this.raphael.circle(xPos, yPos, this.pointSizeForSeries(lineIndex)).attr('fill', pointColor).attr('stroke-width', this.pointStrokeWidthForSeries(lineIndex)).attr('stroke', this.pointStrokeColorForSeries(lineIndex));
  1025. };
  1026. Line.prototype.pointStrokeWidthForSeries = function(index) {
  1027. return this.options.pointStrokeWidths[index % this.options.pointStrokeWidths.length];
  1028. };
  1029. Line.prototype.pointStrokeColorForSeries = function(index) {
  1030. return this.options.pointStrokeColors[index % this.options.pointStrokeColors.length];
  1031. };
  1032. Line.prototype.lineWidthForSeries = function(index) {
  1033. if (this.options.lineWidth instanceof Array) {
  1034. return this.options.lineWidth[index % this.options.lineWidth.length];
  1035. } else {
  1036. return this.options.lineWidth;
  1037. }
  1038. };
  1039. Line.prototype.pointSizeForSeries = function(index) {
  1040. if (this.options.pointSize instanceof Array) {
  1041. return this.options.pointSize[index % this.options.pointSize.length];
  1042. } else {
  1043. return this.options.pointSize;
  1044. }
  1045. };
  1046. Line.prototype.pointGrowSeries = function(index) {
  1047. return Raphael.animation({
  1048. r: this.pointSizeForSeries(index) + 3
  1049. }, 25, 'linear');
  1050. };
  1051. Line.prototype.pointShrinkSeries = function(index) {
  1052. return Raphael.animation({
  1053. r: this.pointSizeForSeries(index)
  1054. }, 25, 'linear');
  1055. };
  1056. return Line;
  1057. })(Morris.Grid);
  1058. Morris.labelSeries = function(dmin, dmax, pxwidth, specName, xLabelFormat) {
  1059. var d, d0, ddensity, name, ret, s, spec, t, _i, _len, _ref;
  1060. ddensity = 200 * (dmax - dmin) / pxwidth;
  1061. d0 = new Date(dmin);
  1062. spec = Morris.LABEL_SPECS[specName];
  1063. if (spec === void 0) {
  1064. _ref = Morris.AUTO_LABEL_ORDER;
  1065. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1066. name = _ref[_i];
  1067. s = Morris.LABEL_SPECS[name];
  1068. if (ddensity >= s.span) {
  1069. spec = s;
  1070. break;
  1071. }
  1072. }
  1073. }
  1074. if (spec === void 0) {
  1075. spec = Morris.LABEL_SPECS["second"];
  1076. }
  1077. if (xLabelFormat) {
  1078. spec = $.extend({}, spec, {
  1079. fmt: xLabelFormat
  1080. });
  1081. }
  1082. d = spec.start(d0);
  1083. ret = [];
  1084. while ((t = d.getTime()) <= dmax) {
  1085. if (t >= dmin) {
  1086. ret.push([spec.fmt(d), t]);
  1087. }
  1088. spec.incr(d);
  1089. }
  1090. return ret;
  1091. };
  1092. minutesSpecHelper = function(interval) {
  1093. return {
  1094. span: interval * 60 * 1000,
  1095. start: function(d) {
  1096. return new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours());
  1097. },
  1098. fmt: function(d) {
  1099. return "" + (Morris.pad2(d.getHours())) + ":" + (Morris.pad2(d.getMinutes()));
  1100. },
  1101. incr: function(d) {
  1102. return d.setUTCMinutes(d.getUTCMinutes() + interval);
  1103. }
  1104. };
  1105. };
  1106. secondsSpecHelper = function(interval) {
  1107. return {
  1108. span: interval * 1000,
  1109. start: function(d) {
  1110. return new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes());
  1111. },
  1112. fmt: function(d) {
  1113. return "" + (Morris.pad2(d.getHours())) + ":" + (Morris.pad2(d.getMinutes())) + ":" + (Morris.pad2(d.getSeconds()));
  1114. },
  1115. incr: function(d) {
  1116. return d.setUTCSeconds(d.getUTCSeconds() + interval);
  1117. }
  1118. };
  1119. };
  1120. Morris.LABEL_SPECS = {
  1121. "decade": {
  1122. span: 172800000000,
  1123. start: function(d) {
  1124. return new Date(d.getFullYear() - d.getFullYear() % 10, 0, 1);
  1125. },
  1126. fmt: function(d) {
  1127. return "" + (d.getFullYear());
  1128. },
  1129. incr: function(d) {
  1130. return d.setFullYear(d.getFullYear() + 10);
  1131. }
  1132. },
  1133. "year": {
  1134. span: 17280000000,
  1135. start: function(d) {
  1136. return new Date(d.getFullYear(), 0, 1);
  1137. },
  1138. fmt: function(d) {
  1139. return "" + (d.getFullYear());
  1140. },
  1141. incr: function(d) {
  1142. return d.setFullYear(d.getFullYear() + 1);
  1143. }
  1144. },
  1145. "month": {
  1146. span: 2419200000,
  1147. start: function(d) {
  1148. return new Date(d.getFullYear(), d.getMonth(), 1);
  1149. },
  1150. fmt: function(d) {
  1151. return "" + (d.getFullYear()) + "-" + (Morris.pad2(d.getMonth() + 1));
  1152. },
  1153. incr: function(d) {
  1154. return d.setMonth(d.getMonth() + 1);
  1155. }
  1156. },
  1157. "week": {
  1158. span: 604800000,
  1159. start: function(d) {
  1160. return new Date(d.getFullYear(), d.getMonth(), d.getDate());
  1161. },
  1162. fmt: function(d) {
  1163. return "" + (d.getFullYear()) + "-" + (Morris.pad2(d.getMonth() + 1)) + "-" + (Morris.pad2(d.getDate()));
  1164. },
  1165. incr: function(d) {
  1166. return d.setDate(d.getDate() + 7);
  1167. }
  1168. },
  1169. "day": {
  1170. span: 86400000,
  1171. start: function(d) {
  1172. return new Date(d.getFullYear(), d.getMonth(), d.getDate());
  1173. },
  1174. fmt: function(d) {
  1175. return "" + (d.getFullYear()) + "-" + (Morris.pad2(d.getMonth() + 1)) + "-" + (Morris.pad2(d.getDate()));
  1176. },
  1177. incr: function(d) {
  1178. return d.setDate(d.getDate() + 1);
  1179. }
  1180. },
  1181. "hour": minutesSpecHelper(60),
  1182. "30min": minutesSpecHelper(30),
  1183. "15min": minutesSpecHelper(15),
  1184. "10min": minutesSpecHelper(10),
  1185. "5min": minutesSpecHelper(5),
  1186. "minute": minutesSpecHelper(1),
  1187. "30sec": secondsSpecHelper(30),
  1188. "15sec": secondsSpecHelper(15),
  1189. "10sec": secondsSpecHelper(10),
  1190. "5sec": secondsSpecHelper(5),
  1191. "second": secondsSpecHelper(1)
  1192. };
  1193. Morris.AUTO_LABEL_ORDER = ["decade", "year", "month", "week", "day", "hour", "30min", "15min", "10min", "5min", "minute", "30sec", "15sec", "10sec", "5sec", "second"];
  1194. Morris.Area = (function(_super) {
  1195. var areaDefaults;
  1196. __extends(Area, _super);
  1197. areaDefaults = {
  1198. fillOpacity: 'auto',
  1199. behaveLikeLine: false
  1200. };
  1201. function Area(options) {
  1202. var areaOptions;
  1203. if (!(this instanceof Morris.Area)) {
  1204. return new Morris.Area(options);
  1205. }
  1206. areaOptions = $.extend({}, areaDefaults, options);
  1207. this.cumulative = !areaOptions.behaveLikeLine;
  1208. if (areaOptions.fillOpacity === 'auto') {
  1209. areaOptions.fillOpacity = areaOptions.behaveLikeLine ? .8 : 1;
  1210. }
  1211. Area.__super__.constructor.call(this, areaOptions);
  1212. }
  1213. Area.prototype.calcPoints = function() {
  1214. var row, total, y, _i, _len, _ref, _results;
  1215. _ref = this.data;
  1216. _results = [];
  1217. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1218. row = _ref[_i];
  1219. row._x = this.transX(row.x);
  1220. total = 0;
  1221. row._y = (function() {
  1222. var _j, _len1, _ref1, _results1;
  1223. _ref1 = row.y;
  1224. _results1 = [];
  1225. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  1226. y = _ref1[_j];
  1227. if (this.options.behaveLikeLine) {
  1228. _results1.push(this.transY(y));
  1229. } else {
  1230. total += y || 0;
  1231. _results1.push(this.transY(total));
  1232. }
  1233. }
  1234. return _results1;
  1235. }).call(this);
  1236. _results.push(row._ymax = Math.max.apply(Math, row._y));
  1237. }
  1238. return _results;
  1239. };
  1240. Area.prototype.drawSeries = function() {
  1241. var i, range, _i, _j, _k, _len, _ref, _ref1, _results, _results1, _results2;
  1242. this.seriesPoints = [];
  1243. if (this.options.behaveLikeLine) {
  1244. range = (function() {
  1245. _results = [];
  1246. for (var _i = 0, _ref = this.options.ykeys.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
  1247. return _results;
  1248. }).apply(this);
  1249. } else {
  1250. range = (function() {
  1251. _results1 = [];
  1252. for (var _j = _ref1 = this.options.ykeys.length - 1; _ref1 <= 0 ? _j <= 0 : _j >= 0; _ref1 <= 0 ? _j++ : _j--){ _results1.push(_j); }
  1253. return _results1;
  1254. }).apply(this);
  1255. }
  1256. _results2 = [];
  1257. for (_k = 0, _len = range.length; _k < _len; _k++) {
  1258. i = range[_k];
  1259. this._drawFillFor(i);
  1260. this._drawLineFor(i);
  1261. _results2.push(this._drawPointFor(i));
  1262. }
  1263. return _results2;
  1264. };
  1265. Area.prototype._drawFillFor = function(index) {
  1266. var path;
  1267. path = this.paths[index];
  1268. if (path !== null) {
  1269. path = path + ("L" + (this.transX(this.xmax)) + "," + this.bottom + "L" + (this.transX(this.xmin)) + "," + this.bottom + "Z");
  1270. return this.drawFilledPath(path, this.fillForSeries(index));
  1271. }
  1272. };
  1273. Area.prototype.fillForSeries = function(i) {
  1274. var color;
  1275. color = Raphael.rgb2hsl(this.colorFor(this.data[i], i, 'line'));
  1276. return Raphael.hsl(color.h, this.options.behaveLikeLine ? color.s * 0.9 : color.s * 0.75, Math.min(0.98, this.options.behaveLikeLine ? color.l * 1.2 : color.l * 1.25));
  1277. };
  1278. Area.prototype.drawFilledPath = function(path, fill) {
  1279. return this.raphael.path(path).attr('fill', fill).attr('fill-opacity', this.options.fillOpacity).attr('stroke', 'none');
  1280. };
  1281. return Area;
  1282. })(Morris.Line);
  1283. Morris.Bar = (function(_super) {
  1284. __extends(Bar, _super);
  1285. function Bar(options) {
  1286. this.onHoverOut = __bind(this.onHoverOut, this);
  1287. this.onHoverMove = __bind(this.onHoverMove, this);
  1288. this.onGridClick = __bind(this.onGridClick, this);
  1289. if (!(this instanceof Morris.Bar)) {
  1290. return new Morris.Bar(options);
  1291. }
  1292. Bar.__super__.constructor.call(this, $.extend({}, options, {
  1293. parseTime: false
  1294. }));
  1295. }
  1296. Bar.prototype.init = function() {
  1297. this.cumulative = this.options.stacked;
  1298. if (this.options.hideHover !== 'always') {
  1299. this.hover = new Morris.Hover({
  1300. parent: this.el
  1301. });
  1302. this.on('hovermove', this.onHoverMove);
  1303. this.on('hoverout', this.onHoverOut);
  1304. return this.on('gridclick', this.onGridClick);
  1305. }
  1306. };
  1307. Bar.prototype.defaults = {
  1308. barSizeRatio: 0.75,
  1309. barGap: 3,
  1310. barColors: ['#0b62a4', '#7a92a3', '#4da74d', '#afd8f8', '#edc240', '#cb4b4b', '#9440ed'],
  1311. barOpacity: 1.0,
  1312. barRadius: [0, 0, 0, 0],
  1313. xLabelMargin: 50
  1314. };
  1315. Bar.prototype.calc = function() {
  1316. var _ref;
  1317. this.calcBars();
  1318. if (this.options.hideHover === false) {
  1319. return (_ref = this.hover).update.apply(_ref, this.hoverContentForRow(this.data.length - 1));
  1320. }
  1321. };
  1322. Bar.prototype.calcBars = function() {
  1323. var idx, row, y, _i, _len, _ref, _results;
  1324. _ref = this.data;
  1325. _results = [];
  1326. for (idx = _i = 0, _len = _ref.length; _i < _len; idx = ++_i) {
  1327. row = _ref[idx];
  1328. row._x = this.left + this.width * (idx + 0.5) / this.data.length;
  1329. _results.push(row._y = (function() {
  1330. var _j, _len1, _ref1, _results1;
  1331. _ref1 = row.y;
  1332. _results1 = [];
  1333. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  1334. y = _ref1[_j];
  1335. if (y != null) {
  1336. _results1.push(this.transY(y));
  1337. } else {
  1338. _results1.push(null);
  1339. }
  1340. }
  1341. return _results1;
  1342. }).call(this));
  1343. }
  1344. return _results;
  1345. };
  1346. Bar.prototype.draw = function() {
  1347. var _ref;
  1348. if ((_ref = this.options.axes) === true || _ref === 'both' || _ref === 'x') {
  1349. this.drawXAxis();
  1350. }
  1351. return this.drawSeries();
  1352. };
  1353. Bar.prototype.drawXAxis = function() {
  1354. var i, label, labelBox, margin, offset, prevAngleMargin, prevLabelMargin, row, textBox, ypos, _i, _ref, _results;
  1355. ypos = this.bottom + (this.options.xAxisLabelTopPadding || this.options.padding / 2);
  1356. prevLabelMargin = null;
  1357. prevAngleMargin = null;
  1358. _results = [];
  1359. for (i = _i = 0, _ref = this.data.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
  1360. row = this.data[this.data.length - 1 - i];
  1361. label = this.drawXAxisLabel(row._x, ypos, row.label);
  1362. textBox = label.getBBox();
  1363. label.transform("r" + (-this.options.xLabelAngle));
  1364. labelBox = label.getBBox();
  1365. label.transform("t0," + (labelBox.height / 2) + "...");
  1366. if (this.options.xLabelAngle !== 0) {
  1367. offset = -0.5 * textBox.width * Math.cos(this.options.xLabelAngle * Math.PI / 180.0);
  1368. label.transform("t" + offset + ",0...");
  1369. }
  1370. if (((prevLabelMargin == null) || prevLabelMargin >= labelBox.x + labelBox.width || (prevAngleMargin != null) && prevAngleMargin >= labelBox.x) && labelBox.x >= 0 && (labelBox.x + labelBox.width) < this.el.width()) {
  1371. if (this.options.xLabelAngle !== 0) {
  1372. margin = 1.25 * this.options.gridTextSize / Math.sin(this.options.xLabelAngle * Math.PI / 180.0);
  1373. prevAngleMargin = labelBox.x - margin;
  1374. }
  1375. _results.push(prevLabelMargin = labelBox.x - this.options.xLabelMargin);
  1376. } else {
  1377. _results.push(label.remove());
  1378. }
  1379. }
  1380. return _results;
  1381. };
  1382. Bar.prototype.drawSeries = function() {
  1383. var barWidth, bottom, groupWidth, idx, lastTop, left, leftPadding, numBars, row, sidx, size, spaceLeft, top, ypos, zeroPos;
  1384. groupWidth = this.width / this.options.data.length;
  1385. numBars = this.options.stacked ? 1 : this.options.ykeys.length;
  1386. barWidth = (groupWidth * this.options.barSizeRatio - this.options.barGap * (numBars - 1)) / numBars;
  1387. if (this.options.barSize) {
  1388. barWidth = Math.min(barWidth, this.options.barSize);
  1389. }
  1390. spaceLeft = groupWidth - barWidth * numBars - this.options.barGap * (numBars - 1);
  1391. leftPadding = spaceLeft / 2;
  1392. zeroPos = this.ymin <= 0 && this.ymax >= 0 ? this.transY(0) : null;
  1393. return this.bars = (function() {
  1394. var _i, _len, _ref, _results;
  1395. _ref = this.data;
  1396. _results = [];
  1397. for (idx = _i = 0, _len = _ref.length; _i < _len; idx = ++_i) {
  1398. row = _ref[idx];
  1399. lastTop = 0;
  1400. _results.push((function() {
  1401. var _j, _len1, _ref1, _results1;
  1402. _ref1 = row._y;
  1403. _results1 = [];
  1404. for (sidx = _j = 0, _len1 = _ref1.length; _j < _len1; sidx = ++_j) {
  1405. ypos = _ref1[sidx];
  1406. if (ypos !== null) {
  1407. if (zeroPos) {
  1408. top = Math.min(ypos, zeroPos);
  1409. bottom = Math.max(ypos, zeroPos);
  1410. } else {
  1411. top = ypos;
  1412. bottom = this.bottom;
  1413. }
  1414. left = this.left + idx * groupWidth + leftPadding;
  1415. if (!this.options.stacked) {
  1416. left += sidx * (barWidth + this.options.barGap);
  1417. }
  1418. size = bottom - top;
  1419. if (this.options.verticalGridCondition && this.options.verticalGridCondition(row.x)) {
  1420. this.drawBar(this.left + idx * groupWidth, this.top, groupWidth, Math.abs(this.top - this.bottom), this.options.verticalGridColor, this.options.verticalGridOpacity, this.options.barRadius);
  1421. }
  1422. if (this.options.stacked) {
  1423. top -= lastTop;
  1424. }
  1425. this.drawBar(left, top, barWidth, size, this.colorFor(row, sidx, 'bar'), this.options.barOpacity, this.options.barRadius);
  1426. _results1.push(lastTop += size);
  1427. } else {
  1428. _results1.push(null);
  1429. }
  1430. }
  1431. return _results1;
  1432. }).call(this));
  1433. }
  1434. return _results;
  1435. }).call(this);
  1436. };
  1437. Bar.prototype.colorFor = function(row, sidx, type) {
  1438. var r, s;
  1439. if (typeof this.options.barColors === 'function') {
  1440. r = {
  1441. x: row.x,
  1442. y: row.y[sidx],
  1443. label: row.label
  1444. };
  1445. s = {
  1446. index: sidx,
  1447. key: this.options.ykeys[sidx],
  1448. label: this.options.labels[sidx]
  1449. };
  1450. return this.options.barColors.call(this, r, s, type);
  1451. } else {
  1452. return this.options.barColors[sidx % this.options.barColors.length];
  1453. }
  1454. };
  1455. Bar.prototype.hitTest = function(x) {
  1456. if (this.data.length === 0) {
  1457. return null;
  1458. }
  1459. x = Math.max(Math.min(x, this.right), this.left);
  1460. return Math.min(this.data.length - 1, Math.floor((x - this.left) / (this.width / this.data.length)));
  1461. };
  1462. Bar.prototype.onGridClick = function(x, y) {
  1463. var index;
  1464. index = this.hitTest(x);
  1465. return this.fire('click', index, this.data[index].src, x, y);
  1466. };
  1467. Bar.prototype.onHoverMove = function(x, y) {
  1468. var index, _ref;
  1469. index = this.hitTest(x);
  1470. return (_ref = this.hover).update.apply(_ref, this.hoverContentForRow(index));
  1471. };
  1472. Bar.prototype.onHoverOut = function() {
  1473. if (this.options.hideHover !== false) {
  1474. return this.hover.hide();
  1475. }
  1476. };
  1477. Bar.prototype.hoverContentForRow = function(index) {
  1478. var content, j, row, x, y, _i, _len, _ref;
  1479. row = this.data[index];
  1480. content = "<div class='morris-hover-row-label'>" + row.label + "</div>";
  1481. _ref = row.y;
  1482. for (j = _i = 0, _len = _ref.length; _i < _len; j = ++_i) {
  1483. y = _ref[j];
  1484. content += "<div class='morris-hover-point' style='color: " + (this.colorFor(row, j, 'label')) + "'>\n " + this.options.labels[j] + ":\n " + (this.yLabelFormat(y)) + "\n</div>";
  1485. }
  1486. if (typeof this.options.hoverCallback === 'function') {
  1487. content = this.options.hoverCallback(index, this.options, content, row.src);
  1488. }
  1489. x = this.left + (index + 0.5) * this.width / this.data.length;
  1490. return [content, x];
  1491. };
  1492. Bar.prototype.drawXAxisLabel = function(xPos, yPos, text) {
  1493. var label;
  1494. return label = this.raphael.text(xPos, yPos, text).attr('font-size', this.options.gridTextSize).attr('font-family', this.options.gridTextFamily).attr('font-weight', this.options.gridTextWeight).attr('fill', this.options.gridTextColor);
  1495. };
  1496. Bar.prototype.drawBar = function(xPos, yPos, width, height, barColor, opacity, radiusArray) {
  1497. var maxRadius, path;
  1498. maxRadius = Math.max.apply(Math, radiusArray);
  1499. if (maxRadius === 0 || maxRadius > height) {
  1500. path = this.raphael.rect(xPos, yPos, width, height);
  1501. } else {
  1502. path = this.raphael.path(this.roundedRect(xPos, yPos, width, height, radiusArray));
  1503. }
  1504. return path.attr('fill', barColor).attr('fill-opacity', opacity).attr('stroke', 'none');
  1505. };
  1506. Bar.prototype.roundedRect = function(x, y, w, h, r) {
  1507. if (r == null) {
  1508. r = [0, 0, 0, 0];
  1509. }
  1510. return ["M", x, r[0] + y, "Q", x, y, x + r[0], y, "L", x + w - r[1], y, "Q", x + w, y, x + w, y + r[1], "L", x + w, y + h - r[2], "Q", x + w, y + h, x + w - r[2], y + h, "L", x + r[3], y + h, "Q", x, y + h, x, y + h - r[3], "Z"];
  1511. };
  1512. return Bar;
  1513. })(Morris.Grid);
  1514. Morris.Donut = (function(_super) {
  1515. __extends(Donut, _super);
  1516. Donut.prototype.defaults = {
  1517. colors: ['#0B62A4', '#3980B5', '#679DC6', '#95BBD7', '#B0CCE1', '#095791', '#095085', '#083E67', '#052C48', '#042135'],
  1518. backgroundColor: '#FFFFFF',
  1519. labelColor: '#000000',
  1520. formatter: Morris.commas,
  1521. resize: false
  1522. };
  1523. function Donut(options) {
  1524. this.resizeHandler = __bind(this.resizeHandler, this);
  1525. this.select = __bind(this.select, this);
  1526. this.click = __bind(this.click, this);
  1527. var _this = this;
  1528. if (!(this instanceof Morris.Donut)) {
  1529. return new Morris.Donut(options);
  1530. }
  1531. this.options = $.extend({}, this.defaults, options);
  1532. if (typeof options.element === 'string') {
  1533. this.el = $(document.getElementById(options.element));
  1534. } else {
  1535. this.el = $(options.element);
  1536. }
  1537. if (this.el === null || this.el.length === 0) {
  1538. throw new Error("Graph placeholder not found.");
  1539. }
  1540. if (options.data === void 0 || options.data.length === 0) {
  1541. return;
  1542. }
  1543. this.raphael = new Raphael(this.el[0]);
  1544. if (this.options.resize) {
  1545. $(window).bind('resize', function(evt) {
  1546. if (_this.timeoutId != null) {
  1547. window.clearTimeout(_this.timeoutId);
  1548. }
  1549. return _this.timeoutId = window.setTimeout(_this.resizeHandler, 100);
  1550. });
  1551. }
  1552. this.setData(options.data);
  1553. }
  1554. Donut.prototype.redraw = function() {
  1555. var C, cx, cy, i, idx, last, max_value, min, next, seg, total, value, w, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _results;
  1556. this.raphael.clear();
  1557. cx = this.el.width() / 2;
  1558. cy = this.el.height() / 2;
  1559. w = (Math.min(cx, cy) - 10) / 3;
  1560. total = 0;
  1561. _ref = this.values;
  1562. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1563. value = _ref[_i];
  1564. total += value;
  1565. }
  1566. min = 5 / (2 * w);
  1567. C = 1.9999 * Math.PI - min * this.data.length;
  1568. last = 0;
  1569. idx = 0;
  1570. this.segments = [];
  1571. _ref1 = this.values;
  1572. for (i = _j = 0, _len1 = _ref1.length; _j < _len1; i = ++_j) {
  1573. value = _ref1[i];
  1574. next = last + min + C * (value / total);
  1575. seg = new Morris.DonutSegment(cx, cy, w * 2, w, last, next, this.data[i].color || this.options.colors[idx % this.options.colors.length], this.options.backgroundColor, idx, this.raphael);
  1576. seg.render();
  1577. this.segments.push(seg);
  1578. seg.on('hover', this.select);
  1579. seg.on('click', this.click);
  1580. last = next;
  1581. idx += 1;
  1582. }
  1583. this.text1 = this.drawEmptyDonutLabel(cx, cy - 10, this.options.labelColor, 15, 800);
  1584. this.text2 = this.drawEmptyDonutLabel(cx, cy + 10, this.options.labelColor, 14);
  1585. max_value = Math.max.apply(Math, this.values);
  1586. idx = 0;
  1587. _ref2 = this.values;
  1588. _results = [];
  1589. for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
  1590. value = _ref2[_k];
  1591. if (value === max_value) {
  1592. this.select(idx);
  1593. break;
  1594. }
  1595. _results.push(idx += 1);
  1596. }
  1597. return _results;
  1598. };
  1599. Donut.prototype.setData = function(data) {
  1600. var row;
  1601. this.data = data;
  1602. this.values = (function() {
  1603. var _i, _len, _ref, _results;
  1604. _ref = this.data;
  1605. _results = [];
  1606. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1607. row = _ref[_i];
  1608. _results.push(parseFloat(row.value));
  1609. }
  1610. return _results;
  1611. }).call(this);
  1612. return this.redraw();
  1613. };
  1614. Donut.prototype.click = function(idx) {
  1615. return this.fire('click', idx, this.data[idx]);
  1616. };
  1617. Donut.prototype.select = function(idx) {
  1618. var row, s, segment, _i, _len, _ref;
  1619. _ref = this.segments;
  1620. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1621. s = _ref[_i];
  1622. s.deselect();
  1623. }
  1624. segment = this.segments[idx];
  1625. segment.select();
  1626. row = this.data[idx];
  1627. return this.setLabels(row.label, this.options.formatter(row.value, row));
  1628. };
  1629. Donut.prototype.setLabels = function(label1, label2) {
  1630. var inner, maxHeightBottom, maxHeightTop, maxWidth, text1bbox, text1scale, text2bbox, text2scale;
  1631. inner = (Math.min(this.el.width() / 2, this.el.height() / 2) - 10) * 2 / 3;
  1632. maxWidth = 1.8 * inner;
  1633. maxHeightTop = inner / 2;
  1634. maxHeightBottom = inner / 3;
  1635. this.text1.attr({
  1636. text: label1,
  1637. transform: ''
  1638. });
  1639. text1bbox = this.text1.getBBox();
  1640. text1scale = Math.min(maxWidth / text1bbox.width, maxHeightTop / text1bbox.height);
  1641. this.text1.attr({
  1642. transform: "S" + text1scale + "," + text1scale + "," + (text1bbox.x + text1bbox.width / 2) + "," + (text1bbox.y + text1bbox.height)
  1643. });
  1644. this.text2.attr({
  1645. text: label2,
  1646. transform: ''
  1647. });
  1648. text2bbox = this.text2.getBBox();
  1649. text2scale = Math.min(maxWidth / text2bbox.width, maxHeightBottom / text2bbox.height);
  1650. return this.text2.attr({
  1651. transform: "S" + text2scale + "," + text2scale + "," + (text2bbox.x + text2bbox.width / 2) + "," + text2bbox.y
  1652. });
  1653. };
  1654. Donut.prototype.drawEmptyDonutLabel = function(xPos, yPos, color, fontSize, fontWeight) {
  1655. var text;
  1656. text = this.raphael.text(xPos, yPos, '').attr('font-size', fontSize).attr('fill', color);
  1657. if (fontWeight != null) {
  1658. text.attr('font-weight', fontWeight);
  1659. }
  1660. return text;
  1661. };
  1662. Donut.prototype.resizeHandler = function() {
  1663. this.timeoutId = null;
  1664. this.raphael.setSize(this.el.width(), this.el.height());
  1665. return this.redraw();
  1666. };
  1667. return Donut;
  1668. })(Morris.EventEmitter);
  1669. Morris.DonutSegment = (function(_super) {
  1670. __extends(DonutSegment, _super);
  1671. function DonutSegment(cx, cy, inner, outer, p0, p1, color, backgroundColor, index, raphael) {
  1672. this.cx = cx;
  1673. this.cy = cy;
  1674. this.inner = inner;
  1675. this.outer = outer;
  1676. this.color = color;
  1677. this.backgroundColor = backgroundColor;
  1678. this.index = index;
  1679. this.raphael = raphael;
  1680. this.deselect = __bind(this.deselect, this);
  1681. this.select = __bind(this.select, this);
  1682. this.sin_p0 = Math.sin(p0);
  1683. this.cos_p0 = Math.cos(p0);
  1684. this.sin_p1 = Math.sin(p1);
  1685. this.cos_p1 = Math.cos(p1);
  1686. this.is_long = (p1 - p0) > Math.PI ? 1 : 0;
  1687. this.path = this.calcSegment(this.inner + 3, this.inner + this.outer - 5);
  1688. this.selectedPath = this.calcSegment(this.inner + 3, this.inner + this.outer);
  1689. this.hilight = this.calcArc(this.inner);
  1690. }
  1691. DonutSegment.prototype.calcArcPoints = function(r) {
  1692. return [this.cx + r * this.sin_p0, this.cy + r * this.cos_p0, this.cx + r * this.sin_p1, this.cy + r * this.cos_p1];
  1693. };
  1694. DonutSegment.prototype.calcSegment = function(r1, r2) {
  1695. var ix0, ix1, iy0, iy1, ox0, ox1, oy0, oy1, _ref, _ref1;
  1696. _ref = this.calcArcPoints(r1), ix0 = _ref[0], iy0 = _ref[1], ix1 = _ref[2], iy1 = _ref[3];
  1697. _ref1 = this.calcArcPoints(r2), ox0 = _ref1[0], oy0 = _ref1[1], ox1 = _ref1[2], oy1 = _ref1[3];
  1698. return ("M" + ix0 + "," + iy0) + ("A" + r1 + "," + r1 + ",0," + this.is_long + ",0," + ix1 + "," + iy1) + ("L" + ox1 + "," + oy1) + ("A" + r2 + "," + r2 + ",0," + this.is_long + ",1," + ox0 + "," + oy0) + "Z";
  1699. };
  1700. DonutSegment.prototype.calcArc = function(r) {
  1701. var ix0, ix1, iy0, iy1, _ref;
  1702. _ref = this.calcArcPoints(r), ix0 = _ref[0], iy0 = _ref[1], ix1 = _ref[2], iy1 = _ref[3];
  1703. return ("M" + ix0 + "," + iy0) + ("A" + r + "," + r + ",0," + this.is_long + ",0," + ix1 + "," + iy1);
  1704. };
  1705. DonutSegment.prototype.render = function() {
  1706. var _this = this;
  1707. this.arc = this.drawDonutArc(this.hilight, this.color);
  1708. return this.seg = this.drawDonutSegment(this.path, this.color, this.backgroundColor, function() {
  1709. return _this.fire('hover', _this.index);
  1710. }, function() {
  1711. return _this.fire('click', _this.index);
  1712. });
  1713. };
  1714. DonutSegment.prototype.drawDonutArc = function(path, color) {
  1715. return this.raphael.path(path).attr({
  1716. stroke: color,
  1717. 'stroke-width': 2,
  1718. opacity: 0
  1719. });
  1720. };
  1721. DonutSegment.prototype.drawDonutSegment = function(path, fillColor, strokeColor, hoverFunction, clickFunction) {
  1722. return this.raphael.path(path).attr({
  1723. fill: fillColor,
  1724. stroke: strokeColor,
  1725. 'stroke-width': 3
  1726. }).hover(hoverFunction).click(clickFunction);
  1727. };
  1728. DonutSegment.prototype.select = function() {
  1729. if (!this.selected) {
  1730. this.seg.animate({
  1731. path: this.selectedPath
  1732. }, 150, '<>');
  1733. this.arc.animate({
  1734. opacity: 1
  1735. }, 150, '<>');
  1736. return this.selected = true;
  1737. }
  1738. };
  1739. DonutSegment.prototype.deselect = function() {
  1740. if (this.selected) {
  1741. this.seg.animate({
  1742. path: this.path
  1743. }, 150, '<>');
  1744. this.arc.animate({
  1745. opacity: 0
  1746. }, 150, '<>');
  1747. return this.selected = false;
  1748. }
  1749. };
  1750. return DonutSegment;
  1751. })(Morris.EventEmitter);
  1752. }).call(this);