morris.js 72 KB

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