morris.js 64 KB

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