3 Copyright 2014 Olly Smith All rights reserved.
4 Licensed under the BSD-2-Clause License.
9 var $, Morris, minutesSpecHelper, secondsSpecHelper,
11 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
12 __hasProp = {}.hasOwnProperty,
13 __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; },
14 __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; };
16 Morris = window.Morris = {};
20 Morris.EventEmitter = (function() {
21 function EventEmitter() {}
23 EventEmitter.prototype.on = function(name, handler) {
24 if (this.handlers == null) {
27 if (this.handlers[name] == null) {
28 this.handlers[name] = [];
30 this.handlers[name].push(handler);
34 EventEmitter.prototype.fire = function() {
35 var args, handler, name, _i, _len, _ref, _results;
36 name = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
37 if ((this.handlers != null) && (this.handlers[name] != null)) {
38 _ref = this.handlers[name];
40 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
42 _results.push(handler.apply(null, args));
52 Morris.commas = function(num) {
53 var absnum, intnum, ret, strabsnum;
55 ret = num < 0 ? "-" : "";
56 absnum = Math.abs(num);
57 intnum = Math.floor(absnum).toFixed(0);
58 ret += intnum.replace(/(?=(?:\d{3})+$)(?!^)/g, ',');
59 strabsnum = absnum.toString();
60 if (strabsnum.length > intnum.length) {
61 ret += strabsnum.slice(intnum.length);
69 Morris.pad2 = function(number) {
70 return (number < 10 ? '0' : '') + number;
73 Morris.Grid = (function(_super) {
74 __extends(Grid, _super);
76 function Grid(options) {
77 this.hasToShow = __bind(this.hasToShow, this);
78 this.resizeHandler = __bind(this.resizeHandler, this);
80 if (typeof options.element === 'string') {
81 this.el = $(document.getElementById(options.element));
83 this.el = $(options.element);
85 if ((this.el == null) || this.el.length === 0) {
86 throw new Error("Graph container element not found");
88 if (this.el.css('position') === 'static') {
89 this.el.css('position', 'relative');
91 this.options = $.extend({}, this.gridDefaults, this.defaults || {}, options);
92 if (typeof this.options.units === 'string') {
93 this.options.postUnits = options.units;
95 this.raphael = new Raphael(this.el[0]);
96 this.elementWidth = null;
97 this.elementHeight = null;
99 this.selectFrom = null;
103 this.setData(this.options.data);
104 this.el.bind('mousemove', function(evt) {
105 var left, offset, right, width, x;
106 offset = _this.el.offset();
107 x = evt.pageX - offset.left;
108 if (_this.selectFrom) {
109 left = _this.data[_this.hitTest(Math.min(x, _this.selectFrom))]._x;
110 right = _this.data[_this.hitTest(Math.max(x, _this.selectFrom))]._x;
111 width = right - left;
112 return _this.selectionRect.attr({
117 return _this.fire('hovermove', x, evt.pageY - offset.top);
120 this.el.bind('mouseleave', function(evt) {
121 if (_this.selectFrom) {
122 _this.selectionRect.hide();
123 _this.selectFrom = null;
125 return _this.fire('hoverout');
127 this.el.bind('touchstart touchmove touchend', function(evt) {
129 touch = evt.originalEvent.touches[0] || evt.originalEvent.changedTouches[0];
130 offset = _this.el.offset();
131 return _this.fire('hovermove', touch.pageX - offset.left, touch.pageY - offset.top);
133 this.el.bind('click', function(evt) {
135 offset = _this.el.offset();
136 return _this.fire('gridclick', evt.pageX - offset.left, evt.pageY - offset.top);
138 if (this.options.rangeSelect) {
139 this.selectionRect = this.raphael.rect(0, 0, 0, this.el.innerHeight()).attr({
140 fill: this.options.rangeSelectColor,
143 this.el.bind('mousedown', function(evt) {
145 offset = _this.el.offset();
146 return _this.startRange(evt.pageX - offset.left);
148 this.el.bind('mouseup', function(evt) {
150 offset = _this.el.offset();
151 _this.endRange(evt.pageX - offset.left);
152 return _this.fire('hovermove', evt.pageX - offset.left, evt.pageY - offset.top);
155 if (this.options.resize) {
156 $(window).bind('resize', function(evt) {
157 if (_this.timeoutId != null) {
158 window.clearTimeout(_this.timeoutId);
160 return _this.timeoutId = window.setTimeout(_this.resizeHandler, 100);
163 this.el.css('-webkit-tap-highlight-color', 'rgba(0,0,0,0)');
169 Grid.prototype.gridDefaults = {
173 gridLineColor: '#aaa',
174 gridStrokeWidth: 0.5,
175 gridTextColor: '#888',
177 gridTextFamily: 'sans-serif',
178 gridTextWeight: 'normal',
190 goalStrokeWidth: 1.0,
191 goalLineColors: ['#666633', '#999966', '#cc6666', '#663333'],
193 eventStrokeWidth: 1.0,
194 eventLineColors: ['#005a04', '#ccffbb', '#3a5f0b', '#005502'],
196 rangeSelectColor: '#eef',
200 Grid.prototype.setData = function(data, redraw) {
201 var e, flatEvents, from, idx, index, maxGoal, minGoal, ret, row, step, to, total, y, ykey, ymax, ymin, yval, _i, _len, _ref, _ref1;
202 if (redraw == null) {
205 this.options.data = data;
206 if ((data == null) || data.length === 0) {
208 this.raphael.clear();
209 if (this.hover != null) {
214 ymax = this.cumulative ? 0 : null;
215 ymin = this.cumulative ? 0 : null;
216 if (this.options.goals.length > 0) {
217 minGoal = Math.min.apply(Math, this.options.goals);
218 maxGoal = Math.max.apply(Math, this.options.goals);
219 ymin = ymin != null ? Math.min(ymin, minGoal) : minGoal;
220 ymax = ymax != null ? Math.max(ymax, maxGoal) : maxGoal;
222 this.data = (function() {
223 var _i, _len, _results;
225 for (index = _i = 0, _len = data.length; _i < _len; index = ++_i) {
230 ret.label = row[this.options.xkey];
231 if (this.options.parseTime) {
232 ret.x = Morris.parseDate(ret.label);
233 if (this.options.dateFormat) {
234 ret.label = this.options.dateFormat(ret.x);
235 } else if (typeof ret.label === 'number') {
236 ret.label = new Date(ret.label).toString();
240 if (this.options.xLabelFormat) {
241 ret.label = this.options.xLabelFormat(ret);
245 ret.y = (function() {
246 var _j, _len1, _ref, _results1;
247 _ref = this.options.ykeys;
249 for (idx = _j = 0, _len1 = _ref.length; _j < _len1; idx = ++_j) {
252 if (typeof yval === 'string') {
253 yval = parseFloat(yval);
255 if ((yval != null) && typeof yval !== 'number') {
258 if ((yval != null) && this.hasToShow(idx)) {
259 if (this.cumulative) {
263 ymax = Math.max(yval, ymax);
264 ymin = Math.min(yval, ymin);
270 if (this.cumulative && (total != null)) {
271 ymax = Math.max(total, ymax);
272 ymin = Math.min(total, ymin);
274 _results1.push(yval);
282 if (this.options.parseTime) {
283 this.data = this.data.sort(function(a, b) {
284 return (a.x > b.x) - (b.x > a.x);
287 this.xmin = this.data[0].x;
288 this.xmax = this.data[this.data.length - 1].x;
290 if (this.options.events.length > 0) {
291 if (this.options.parseTime) {
292 _ref = this.options.events;
293 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
295 if (e instanceof Array) {
296 from = e[0], to = e[1];
297 this.events.push([Morris.parseDate(from), Morris.parseDate(to)]);
299 this.events.push(Morris.parseDate(e));
303 this.events = this.options.events;
305 flatEvents = $.map(this.events, function(e) {
308 this.xmax = Math.max(this.xmax, Math.max.apply(Math, flatEvents));
309 this.xmin = Math.min(this.xmin, Math.min.apply(Math, flatEvents));
311 if (this.xmin === this.xmax) {
315 this.ymin = this.yboundary('min', ymin);
316 this.ymax = this.yboundary('max', ymax);
317 if (this.ymin === this.ymax) {
323 if (((_ref1 = this.options.axes) === true || _ref1 === 'both' || _ref1 === 'y') || this.options.grid === true) {
324 if (this.options.ymax === this.gridDefaults.ymax && this.options.ymin === this.gridDefaults.ymin) {
325 this.grid = this.autoGridLines(this.ymin, this.ymax, this.options.numLines);
326 this.ymin = Math.min(this.ymin, this.grid[0]);
327 this.ymax = Math.max(this.ymax, this.grid[this.grid.length - 1]);
329 step = (this.ymax - this.ymin) / (this.options.numLines - 1);
330 this.grid = (function() {
331 var _j, _ref2, _ref3, _results;
333 for (y = _j = _ref2 = this.ymin, _ref3 = this.ymax; step > 0 ? _j <= _ref3 : _j >= _ref3; y = _j += step) {
342 return this.redraw();
346 Grid.prototype.yboundary = function(boundaryType, currentValue) {
347 var boundaryOption, suggestedValue;
348 boundaryOption = this.options["y" + boundaryType];
349 if (typeof boundaryOption === 'string') {
350 if (boundaryOption.slice(0, 4) === 'auto') {
351 if (boundaryOption.length > 5) {
352 suggestedValue = parseInt(boundaryOption.slice(5), 10);
353 if (currentValue == null) {
354 return suggestedValue;
356 return Math[boundaryType](currentValue, suggestedValue);
358 if (currentValue != null) {
365 return parseInt(boundaryOption, 10);
368 return boundaryOption;
372 Grid.prototype.autoGridLines = function(ymin, ymax, nlines) {
373 var gmax, gmin, grid, smag, span, step, unit, y, ymag;
375 ymag = Math.floor(Math.log(span) / Math.log(10));
376 unit = Math.pow(10, ymag);
377 gmin = Math.floor(ymin / unit) * unit;
378 gmax = Math.ceil(ymax / unit) * unit;
379 step = (gmax - gmin) / (nlines - 1);
380 if (unit === 1 && step > 1 && Math.ceil(step) !== step) {
381 step = Math.ceil(step);
382 gmax = gmin + step * (nlines - 1);
384 if (gmin < 0 && gmax > 0) {
385 gmin = Math.floor(ymin / step) * step;
386 gmax = Math.ceil(ymax / step) * step;
389 smag = Math.floor(Math.log(step) / Math.log(10));
393 for (y = _i = gmin; step > 0 ? _i <= gmax : _i >= gmax; y = _i += step) {
394 _results.push(parseFloat(y.toFixed(1 - smag)));
402 for (y = _i = gmin; step > 0 ? _i <= gmax : _i >= gmax; y = _i += step) {
411 Grid.prototype._calc = function() {
412 var angle, bottomOffsets, gridLine, h, i, w, yLabelWidths, _ref, _ref1;
414 h = this.el.height();
415 if (this.elementWidth !== w || this.elementHeight !== h || this.dirty) {
416 this.elementWidth = w;
417 this.elementHeight = h;
419 this.left = this.options.padding;
420 this.right = this.elementWidth - this.options.padding;
421 this.top = this.options.padding;
422 this.bottom = this.elementHeight - this.options.padding;
423 if ((_ref = this.options.axes) === true || _ref === 'both' || _ref === 'y') {
424 yLabelWidths = (function() {
425 var _i, _len, _ref1, _results;
428 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
429 gridLine = _ref1[_i];
430 _results.push(this.measureText(this.yAxisFormat(gridLine)).width);
434 if (!this.options.horizontal) {
435 this.left += Math.max.apply(Math, yLabelWidths);
437 this.bottom -= Math.max.apply(Math, yLabelWidths);
440 if ((_ref1 = this.options.axes) === true || _ref1 === 'both' || _ref1 === 'x') {
441 if (!this.options.horizontal) {
442 angle = -this.options.xLabelAngle;
446 bottomOffsets = (function() {
447 var _i, _ref2, _results;
449 for (i = _i = 0, _ref2 = this.data.length; 0 <= _ref2 ? _i < _ref2 : _i > _ref2; i = 0 <= _ref2 ? ++_i : --_i) {
450 _results.push(this.measureText(this.data[i].label, angle).height);
454 if (!this.options.horizontal) {
455 this.bottom -= Math.max.apply(Math, bottomOffsets);
457 this.left += Math.max.apply(Math, bottomOffsets);
460 this.width = Math.max(1, this.right - this.left);
461 this.height = Math.max(1, this.bottom - this.top);
462 if (!this.options.horizontal) {
463 this.dx = this.width / (this.xmax - this.xmin);
464 this.dy = this.height / (this.ymax - this.ymin);
465 this.yStart = this.bottom;
466 this.yEnd = this.top;
467 this.xStart = this.left;
468 this.xEnd = this.right;
469 this.xSize = this.width;
470 this.ySize = this.height;
472 this.dx = this.height / (this.xmax - this.xmin);
473 this.dy = this.width / (this.ymax - this.ymin);
474 this.yStart = this.left;
475 this.yEnd = this.right;
476 this.xStart = this.top;
477 this.xEnd = this.bottom;
478 this.xSize = this.height;
479 this.ySize = this.width;
487 Grid.prototype.transY = function(y) {
488 if (!this.options.horizontal) {
489 return this.bottom - (y - this.ymin) * this.dy;
491 return this.left + (y - this.ymin) * this.dy;
495 Grid.prototype.transX = function(x) {
496 if (this.data.length === 1) {
497 return (this.xStart + this.xEnd) / 2;
499 return this.xStart + (x - this.xmin) * this.dx;
503 Grid.prototype.redraw = function() {
504 this.raphael.clear();
514 Grid.prototype.measureText = function(text, angle) {
519 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);
525 Grid.prototype.yAxisFormat = function(label) {
526 return this.yLabelFormat(label, 0);
529 Grid.prototype.yLabelFormat = function(label, i) {
530 if (typeof this.options.yLabelFormat === 'function') {
531 return this.options.yLabelFormat(label, i);
533 return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits;
537 Grid.prototype.getYAxisLabelX = function() {
538 return this.left - this.options.padding / 2;
541 Grid.prototype.drawGrid = function() {
542 var basePos, lineY, pos, _i, _len, _ref, _ref1, _ref2, _results;
543 if (this.options.grid === false && ((_ref = this.options.axes) !== true && _ref !== 'both' && _ref !== 'y')) {
546 if (!this.options.horizontal) {
547 basePos = this.getYAxisLabelX();
549 basePos = this.getXAxisLabelY();
553 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
555 pos = this.transY(lineY);
556 if ((_ref2 = this.options.axes) === true || _ref2 === 'both' || _ref2 === 'y') {
557 if (!this.options.horizontal) {
558 this.drawYAxisLabel(basePos, pos, this.yAxisFormat(lineY));
560 this.drawXAxisLabel(pos, basePos, this.yAxisFormat(lineY));
563 if (this.options.grid) {
564 pos = Math.floor(pos) + 0.5;
565 if (!this.options.horizontal) {
566 _results.push(this.drawGridLine("M" + this.xStart + "," + pos + "H" + this.xEnd));
568 _results.push(this.drawGridLine("M" + pos + "," + this.xStart + "V" + this.xEnd));
571 _results.push(void 0);
577 Grid.prototype.drawGoals = function() {
578 var color, goal, i, _i, _len, _ref, _results;
579 _ref = this.options.goals;
581 for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
583 color = this.options.goalLineColors[i % this.options.goalLineColors.length];
584 _results.push(this.drawGoal(goal, color));
589 Grid.prototype.drawEvents = function() {
590 var color, event, i, _i, _len, _ref, _results;
593 for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
595 color = this.options.eventLineColors[i % this.options.eventLineColors.length];
596 _results.push(this.drawEvent(event, color));
601 Grid.prototype.drawGoal = function(goal, color) {
603 y = Math.floor(this.transY(goal)) + 0.5;
604 if (!this.options.horizontal) {
605 path = "M" + this.xStart + "," + y + "H" + this.xEnd;
607 path = "M" + y + "," + this.xStart + "V" + this.xEnd;
609 return this.raphael.path(path).attr('stroke', color).attr('stroke-width', this.options.goalStrokeWidth);
612 Grid.prototype.drawEvent = function(event, color) {
613 var from, path, to, x;
614 if (event instanceof Array) {
615 from = event[0], to = event[1];
616 from = Math.floor(this.transX(from)) + 0.5;
617 to = Math.floor(this.transX(to)) + 0.5;
618 if (!this.options.horizontal) {
619 return this.raphael.rect(from, this.yEnd, to - from, this.yStart - this.yEnd).attr({
624 return this.raphael.rect(this.yStart, from, this.yEnd - this.yStart, to - from).attr({
630 x = Math.floor(this.transX(event)) + 0.5;
631 if (!this.options.horizontal) {
632 path = "M" + x + "," + this.yStart + "V" + this.yEnd;
634 path = "M" + this.yStart + "," + x + "H" + this.yEnd;
636 return this.raphael.path(path).attr('stroke', color).attr('stroke-width', this.options.eventStrokeWidth);
640 Grid.prototype.drawYAxisLabel = function(xPos, yPos, text) {
641 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');
644 Grid.prototype.drawGridLine = function(path) {
645 return this.raphael.path(path).attr('stroke', this.options.gridLineColor).attr('stroke-width', this.options.gridStrokeWidth);
648 Grid.prototype.startRange = function(x) {
651 return this.selectionRect.attr({
657 Grid.prototype.endRange = function(x) {
659 if (this.selectFrom) {
660 start = Math.min(this.selectFrom, x);
661 end = Math.max(this.selectFrom, x);
662 this.options.rangeSelect.call(this.el, {
663 start: this.data[this.hitTest(start)].x,
664 end: this.data[this.hitTest(end)].x
666 return this.selectFrom = null;
670 Grid.prototype.resizeHandler = function() {
671 this.timeoutId = null;
672 this.raphael.setSize(this.el.width(), this.el.height());
673 return this.redraw();
676 Grid.prototype.hasToShow = function(i) {
677 return this.options.shown === true || this.options.shown[i] === true;
682 })(Morris.EventEmitter);
684 Morris.parseDate = function(date) {
685 var isecs, m, msecs, n, o, offsetmins, p, q, r, ret, secs;
686 if (typeof date === 'number') {
689 m = date.match(/^(\d+) Q(\d)$/);
690 n = date.match(/^(\d+)-(\d+)$/);
691 o = date.match(/^(\d+)-(\d+)-(\d+)$/);
692 p = date.match(/^(\d+) W(\d+)$/);
693 q = date.match(/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+)(Z|([+-])(\d\d):?(\d\d))?$/);
694 r = date.match(/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+):(\d+(\.\d+)?)(Z|([+-])(\d\d):?(\d\d))?$/);
696 return new Date(parseInt(m[1], 10), parseInt(m[2], 10) * 3 - 1, 1).getTime();
698 return new Date(parseInt(n[1], 10), parseInt(n[2], 10) - 1, 1).getTime();
700 return new Date(parseInt(o[1], 10), parseInt(o[2], 10) - 1, parseInt(o[3], 10)).getTime();
702 ret = new Date(parseInt(p[1], 10), 0, 1);
703 if (ret.getDay() !== 4) {
704 ret.setMonth(0, 1 + ((4 - ret.getDay()) + 7) % 7);
706 return ret.getTime() + parseInt(p[2], 10) * 604800000;
709 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();
713 offsetmins = parseInt(q[8], 10) * 60 + parseInt(q[9], 10);
715 offsetmins = 0 - offsetmins;
718 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);
721 secs = parseFloat(r[6]);
722 isecs = Math.floor(secs);
723 msecs = Math.round((secs - isecs) * 1000);
725 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();
729 offsetmins = parseInt(r[10], 10) * 60 + parseInt(r[11], 10);
731 offsetmins = 0 - offsetmins;
734 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);
737 return new Date(parseInt(date, 10), 0, 1).getTime();
741 Morris.Hover = (function() {
743 "class": 'morris-hover morris-default-style'
746 function Hover(options) {
747 if (options == null) {
750 this.options = $.extend({}, Morris.Hover.defaults, options);
751 this.el = $("<div class='" + this.options["class"] + "'></div>");
753 this.options.parent.append(this.el);
756 Hover.prototype.update = function(html, x, y, centre_y) {
762 return this.moveTo(x, y, centre_y);
766 Hover.prototype.html = function(content) {
767 return this.el.html(content);
770 Hover.prototype.moveTo = function(x, y, centre_y) {
771 var hoverHeight, hoverWidth, left, parentHeight, parentWidth, top;
772 parentWidth = this.options.parent.innerWidth();
773 parentHeight = this.options.parent.innerHeight();
774 hoverWidth = this.el.outerWidth();
775 hoverHeight = this.el.outerHeight();
776 left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth);
778 if (centre_y === true) {
779 top = y - hoverHeight / 2;
784 top = y - hoverHeight - 10;
787 if (top + hoverHeight > parentHeight) {
788 top = parentHeight / 2 - hoverHeight / 2;
793 top = parentHeight / 2 - hoverHeight / 2;
797 top: parseInt(top) + "px"
801 Hover.prototype.show = function() {
802 return this.el.show();
805 Hover.prototype.hide = function() {
806 return this.el.hide();
813 Morris.Line = (function(_super) {
814 __extends(Line, _super);
816 function Line(options) {
817 this.hilight = __bind(this.hilight, this);
818 this.onHoverOut = __bind(this.onHoverOut, this);
819 this.onHoverMove = __bind(this.onHoverMove, this);
820 this.onGridClick = __bind(this.onGridClick, this);
821 if (!(this instanceof Morris.Line)) {
822 return new Morris.Line(options);
824 Line.__super__.constructor.call(this, options);
827 Line.prototype.init = function() {
828 if (this.options.hideHover !== 'always') {
829 this.hover = new Morris.Hover({
832 this.on('hovermove', this.onHoverMove);
833 this.on('hoverout', this.onHoverOut);
834 return this.on('gridclick', this.onGridClick);
838 Line.prototype.defaults = {
841 lineColors: ['#0b62a4', '#7A92A3', '#4da74d', '#afd8f8', '#edc240', '#cb4b4b', '#9440ed'],
842 pointStrokeWidths: [1],
843 pointStrokeColors: ['#ffffff'],
853 trendLineColors: ['#689bc3', '#a2b3bf', '#64b764']
856 Line.prototype.calc = function() {
858 return this.generatePaths();
861 Line.prototype.calcPoints = function() {
862 var row, y, _i, _len, _ref, _results;
865 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
867 row._x = this.transX(row.x);
868 row._y = (function() {
869 var _j, _len1, _ref1, _results1;
872 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
875 _results1.push(this.transY(y));
882 _results.push(row._ymax = Math.min.apply(Math, [this.bottom].concat((function() {
883 var _j, _len1, _ref1, _results1;
886 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
898 Line.prototype.hitTest = function(x) {
899 var index, r, _i, _len, _ref;
900 if (this.data.length === 0) {
903 _ref = this.data.slice(1);
904 for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
906 if (x < (r._x + this.data[index]._x) / 2) {
913 Line.prototype.onGridClick = function(x, y) {
915 index = this.hitTest(x);
916 return this.fire('click', index, this.data[index].src, x, y);
919 Line.prototype.onHoverMove = function(x, y) {
921 index = this.hitTest(x);
922 return this.displayHoverForRow(index);
925 Line.prototype.onHoverOut = function() {
926 if (this.options.hideHover !== false) {
927 return this.displayHoverForRow(null);
931 Line.prototype.displayHoverForRow = function(index) {
934 (_ref = this.hover).update.apply(_ref, this.hoverContentForRow(index));
935 return this.hilight(index);
938 return this.hilight();
942 Line.prototype.hoverContentForRow = function(index) {
943 var content, j, row, y, _i, _len, _ref;
944 row = this.data[index];
945 content = $("<div class='morris-hover-row-label'>").text(row.label);
946 content = content.prop('outerHTML');
948 for (j = _i = 0, _len = _ref.length; _i < _len; j = ++_i) {
950 if (this.options.labels[j] === false) {
953 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>";
955 if (typeof this.options.hoverCallback === 'function') {
956 content = this.options.hoverCallback(index, this.options, content, row.src);
958 return [content, row._x, row._ymax];
961 Line.prototype.generatePaths = function() {
962 var coords, i, r, smooth;
963 return this.paths = (function() {
964 var _i, _ref, _ref1, _results;
966 for (i = _i = 0, _ref = this.options.ykeys.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
967 smooth = typeof this.options.smooth === "boolean" ? this.options.smooth : (_ref1 = this.options.ykeys[i], __indexOf.call(this.options.smooth, _ref1) >= 0);
968 coords = (function() {
969 var _j, _len, _ref2, _results1;
972 for (_j = 0, _len = _ref2.length; _j < _len; _j++) {
974 if (r._y[i] !== void 0) {
983 if (coords.length > 1) {
984 _results.push(Morris.Line.createPath(coords, smooth, this.bottom));
993 Line.prototype.draw = function() {
995 if ((_ref = this.options.axes) === true || _ref === 'both' || _ref === 'x') {
999 if (this.options.hideHover === false) {
1000 return this.displayHoverForRow(this.data.length - 1);
1004 Line.prototype.drawXAxis = function() {
1005 var drawLabel, l, labels, prevAngleMargin, prevLabelMargin, row, ypos, _i, _len, _results,
1007 ypos = this.bottom + this.options.padding / 2;
1008 prevLabelMargin = null;
1009 prevAngleMargin = null;
1010 drawLabel = function(labelText, xpos) {
1011 var label, labelBox, margin, offset, textBox;
1012 label = _this.drawXAxisLabel(_this.transX(xpos), ypos, labelText);
1013 textBox = label.getBBox();
1014 label.transform("r" + (-_this.options.xLabelAngle));
1015 labelBox = label.getBBox();
1016 label.transform("t0," + (labelBox.height / 2) + "...");
1017 if (_this.options.xLabelAngle !== 0) {
1018 offset = -0.5 * textBox.width * Math.cos(_this.options.xLabelAngle * Math.PI / 180.0);
1019 label.transform("t" + offset + ",0...");
1021 labelBox = label.getBBox();
1022 if (((prevLabelMargin == null) || prevLabelMargin >= labelBox.x + labelBox.width || (prevAngleMargin != null) && prevAngleMargin >= labelBox.x) && labelBox.x >= 0 && (labelBox.x + labelBox.width) < _this.el.width()) {
1023 if (_this.options.xLabelAngle !== 0) {
1024 margin = 1.25 * _this.options.gridTextSize / Math.sin(_this.options.xLabelAngle * Math.PI / 180.0);
1025 prevAngleMargin = labelBox.x - margin;
1027 return prevLabelMargin = labelBox.x - _this.options.xLabelMargin;
1029 return label.remove();
1032 if (this.options.parseTime) {
1033 if (this.data.length === 1 && this.options.xLabels === 'auto') {
1034 labels = [[this.data[0].label, this.data[0].x]];
1036 labels = Morris.labelSeries(this.xmin, this.xmax, this.width, this.options.xLabels, this.options.xLabelFormat);
1039 labels = (function() {
1040 var _i, _len, _ref, _results;
1043 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1045 _results.push([row.label, row.x]);
1052 for (_i = 0, _len = labels.length; _i < _len; _i++) {
1054 _results.push(drawLabel(l[0], l[1]));
1059 Line.prototype.drawSeries = function() {
1060 var i, _i, _j, _ref, _ref1, _results;
1061 this.seriesPoints = [];
1062 for (i = _i = _ref = this.options.ykeys.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) {
1063 if (this.hasToShow(i)) {
1064 if (this.options.trendLine !== false && this.options.trendLine === true || this.options.trendLine[i] === true) {
1065 this._drawTrendLine(i);
1067 this._drawLineFor(i);
1071 for (i = _j = _ref1 = this.options.ykeys.length - 1; _ref1 <= 0 ? _j <= 0 : _j >= 0; i = _ref1 <= 0 ? ++_j : --_j) {
1072 if (this.hasToShow(i)) {
1073 _results.push(this._drawPointFor(i));
1075 _results.push(void 0);
1081 Line.prototype._drawPointFor = function(index) {
1082 var circle, row, _i, _len, _ref, _results;
1083 this.seriesPoints[index] = [];
1086 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1089 if (row._y[index] != null) {
1090 circle = this.drawLinePoint(row._x, row._y[index], this.colorFor(row, index, 'point'), index);
1092 _results.push(this.seriesPoints[index].push(circle));
1097 Line.prototype._drawLineFor = function(index) {
1099 path = this.paths[index];
1100 if (path !== null) {
1101 return this.drawLinePath(path, this.colorFor(null, index, 'line'), index);
1105 Line.prototype._drawTrendLine = function(index) {
1106 var a, b, data, datapoints, path, sum_x, sum_xx, sum_xy, sum_y, val, x, y, _i, _len, _ref;
1113 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1126 a = (datapoints * sum_xy - sum_x * sum_y) / (datapoints * sum_xx - sum_x * sum_x);
1127 b = (sum_y / datapoints) - ((a * sum_x) / datapoints);
1129 data[0].x = this.transX(this.data[0].x);
1130 data[0].y = this.transY(this.data[0].x * a + b);
1131 data[1].x = this.transX(this.data[this.data.length - 1].x);
1132 data[1].y = this.transY(this.data[this.data.length - 1].x * a + b);
1133 path = Morris.Line.createPath(data, false, this.bottom);
1134 return path = this.raphael.path(path).attr('stroke', this.colorFor(null, index, 'trendLine')).attr('stroke-width', this.options.trendLineWidth);
1137 Line.createPath = function(coords, smooth, bottom) {
1138 var coord, g, grads, i, ix, lg, path, prevCoord, x1, x2, y1, y2, _i, _len;
1141 grads = Morris.Line.gradients(coords);
1146 for (i = _i = 0, _len = coords.length; _i < _len; i = ++_i) {
1148 if (coord.y != null) {
1149 if (prevCoord.y != null) {
1153 ix = (coord.x - prevCoord.x) / 4;
1154 x1 = prevCoord.x + ix;
1155 y1 = Math.min(bottom, prevCoord.y + ix * lg);
1157 y2 = Math.min(bottom, coord.y - ix * g);
1158 path += "C" + x1 + "," + y1 + "," + x2 + "," + y2 + "," + coord.x + "," + coord.y;
1160 path += "L" + coord.x + "," + coord.y;
1163 if (!smooth || (grads[i] != null)) {
1164 path += "M" + coord.x + "," + coord.y;
1173 Line.gradients = function(coords) {
1174 var coord, grad, i, nextCoord, prevCoord, _i, _len, _results;
1175 grad = function(a, b) {
1176 return (a.y - b.y) / (a.x - b.x);
1179 for (i = _i = 0, _len = coords.length; _i < _len; i = ++_i) {
1181 if (coord.y != null) {
1182 nextCoord = coords[i + 1] || {
1185 prevCoord = coords[i - 1] || {
1188 if ((prevCoord.y != null) && (nextCoord.y != null)) {
1189 _results.push(grad(prevCoord, nextCoord));
1190 } else if (prevCoord.y != null) {
1191 _results.push(grad(prevCoord, coord));
1192 } else if (nextCoord.y != null) {
1193 _results.push(grad(coord, nextCoord));
1195 _results.push(null);
1198 _results.push(null);
1204 Line.prototype.hilight = function(index) {
1205 var i, _i, _j, _ref, _ref1;
1206 if (this.prevHilight !== null && this.prevHilight !== index) {
1207 for (i = _i = 0, _ref = this.seriesPoints.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
1208 if (this.seriesPoints[i][this.prevHilight]) {
1209 this.seriesPoints[i][this.prevHilight].animate(this.pointShrinkSeries(i));
1213 if (index !== null && this.prevHilight !== index) {
1214 for (i = _j = 0, _ref1 = this.seriesPoints.length - 1; 0 <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
1215 if (this.seriesPoints[i][index]) {
1216 this.seriesPoints[i][index].animate(this.pointGrowSeries(i));
1220 return this.prevHilight = index;
1223 Line.prototype.colorFor = function(row, sidx, type) {
1224 if (typeof this.options.lineColors === 'function') {
1225 return this.options.lineColors.call(this, row, sidx, type);
1226 } else if (type === 'point') {
1227 return this.options.pointFillColors[sidx % this.options.pointFillColors.length] || this.options.lineColors[sidx % this.options.lineColors.length];
1228 } else if (type === 'line') {
1229 return this.options.lineColors[sidx % this.options.lineColors.length];
1231 return this.options.trendLineColors[sidx % this.options.trendLineColors.length];
1235 Line.prototype.drawXAxisLabel = function(xPos, yPos, text) {
1236 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);
1239 Line.prototype.drawLinePath = function(path, lineColor, lineIndex) {
1240 return this.raphael.path(path).attr('stroke', lineColor).attr('stroke-width', this.lineWidthForSeries(lineIndex));
1243 Line.prototype.drawLinePoint = function(xPos, yPos, pointColor, lineIndex) {
1244 return this.raphael.circle(xPos, yPos, this.pointSizeForSeries(lineIndex)).attr('fill', pointColor).attr('stroke-width', this.pointStrokeWidthForSeries(lineIndex)).attr('stroke', this.pointStrokeColorForSeries(lineIndex));
1247 Line.prototype.pointStrokeWidthForSeries = function(index) {
1248 return this.options.pointStrokeWidths[index % this.options.pointStrokeWidths.length];
1251 Line.prototype.pointStrokeColorForSeries = function(index) {
1252 return this.options.pointStrokeColors[index % this.options.pointStrokeColors.length];
1255 Line.prototype.lineWidthForSeries = function(index) {
1256 if (this.options.lineWidth instanceof Array) {
1257 return this.options.lineWidth[index % this.options.lineWidth.length];
1259 return this.options.lineWidth;
1263 Line.prototype.pointSizeForSeries = function(index) {
1264 if (this.options.pointSize instanceof Array) {
1265 return this.options.pointSize[index % this.options.pointSize.length];
1267 return this.options.pointSize;
1271 Line.prototype.pointGrowSeries = function(index) {
1272 if (this.pointSizeForSeries(index) === 0) {
1275 return Raphael.animation({
1276 r: this.pointSizeForSeries(index) + 3
1280 Line.prototype.pointShrinkSeries = function(index) {
1281 return Raphael.animation({
1282 r: this.pointSizeForSeries(index)
1290 Morris.labelSeries = function(dmin, dmax, pxwidth, specName, xLabelFormat) {
1291 var d, d0, ddensity, name, ret, s, spec, t, _i, _len, _ref;
1292 ddensity = 200 * (dmax - dmin) / pxwidth;
1293 d0 = new Date(dmin);
1294 spec = Morris.LABEL_SPECS[specName];
1295 if (spec === void 0) {
1296 _ref = Morris.AUTO_LABEL_ORDER;
1297 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1299 s = Morris.LABEL_SPECS[name];
1300 if (ddensity >= s.span) {
1306 if (spec === void 0) {
1307 spec = Morris.LABEL_SPECS["second"];
1310 spec = $.extend({}, spec, {
1316 while ((t = d.getTime()) <= dmax) {
1318 ret.push([spec.fmt(d), t]);
1325 minutesSpecHelper = function(interval) {
1327 span: interval * 60 * 1000,
1328 start: function(d) {
1329 return new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours());
1332 return "" + (Morris.pad2(d.getHours())) + ":" + (Morris.pad2(d.getMinutes()));
1335 return d.setUTCMinutes(d.getUTCMinutes() + interval);
1340 secondsSpecHelper = function(interval) {
1342 span: interval * 1000,
1343 start: function(d) {
1344 return new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes());
1347 return "" + (Morris.pad2(d.getHours())) + ":" + (Morris.pad2(d.getMinutes())) + ":" + (Morris.pad2(d.getSeconds()));
1350 return d.setUTCSeconds(d.getUTCSeconds() + interval);
1355 Morris.LABEL_SPECS = {
1358 start: function(d) {
1359 return new Date(d.getFullYear() - d.getFullYear() % 10, 0, 1);
1362 return "" + (d.getFullYear());
1365 return d.setFullYear(d.getFullYear() + 10);
1370 start: function(d) {
1371 return new Date(d.getFullYear(), 0, 1);
1374 return "" + (d.getFullYear());
1377 return d.setFullYear(d.getFullYear() + 1);
1382 start: function(d) {
1383 return new Date(d.getFullYear(), d.getMonth(), 1);
1386 return "" + (d.getFullYear()) + "-" + (Morris.pad2(d.getMonth() + 1));
1389 return d.setMonth(d.getMonth() + 1);
1394 start: function(d) {
1395 return new Date(d.getFullYear(), d.getMonth(), d.getDate());
1398 return "" + (d.getFullYear()) + "-" + (Morris.pad2(d.getMonth() + 1)) + "-" + (Morris.pad2(d.getDate()));
1401 return d.setDate(d.getDate() + 7);
1406 start: function(d) {
1407 return new Date(d.getFullYear(), d.getMonth(), d.getDate());
1410 return "" + (d.getFullYear()) + "-" + (Morris.pad2(d.getMonth() + 1)) + "-" + (Morris.pad2(d.getDate()));
1413 return d.setDate(d.getDate() + 1);
1416 "hour": minutesSpecHelper(60),
1417 "30min": minutesSpecHelper(30),
1418 "15min": minutesSpecHelper(15),
1419 "10min": minutesSpecHelper(10),
1420 "5min": minutesSpecHelper(5),
1421 "minute": minutesSpecHelper(1),
1422 "30sec": secondsSpecHelper(30),
1423 "15sec": secondsSpecHelper(15),
1424 "10sec": secondsSpecHelper(10),
1425 "5sec": secondsSpecHelper(5),
1426 "second": secondsSpecHelper(1)
1429 Morris.AUTO_LABEL_ORDER = ["decade", "year", "month", "week", "day", "hour", "30min", "15min", "10min", "5min", "minute", "30sec", "15sec", "10sec", "5sec", "second"];
1431 Morris.Area = (function(_super) {
1434 __extends(Area, _super);
1437 fillOpacity: 'auto',
1438 behaveLikeLine: false
1441 function Area(options) {
1443 if (!(this instanceof Morris.Area)) {
1444 return new Morris.Area(options);
1446 areaOptions = $.extend({}, areaDefaults, options);
1447 this.cumulative = !areaOptions.behaveLikeLine;
1448 if (areaOptions.fillOpacity === 'auto') {
1449 areaOptions.fillOpacity = areaOptions.behaveLikeLine ? .8 : 1;
1451 Area.__super__.constructor.call(this, areaOptions);
1454 Area.prototype.calcPoints = function() {
1455 var row, total, y, _i, _len, _ref, _results;
1458 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1460 row._x = this.transX(row.x);
1462 row._y = (function() {
1463 var _j, _len1, _ref1, _results1;
1466 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
1468 if (this.options.behaveLikeLine) {
1469 _results1.push(this.transY(y));
1472 _results1.push(this.transY(total));
1477 _results.push(row._ymax = Math.max.apply(Math, row._y));
1482 Area.prototype.drawSeries = function() {
1483 var i, range, _i, _j, _k, _len, _ref, _ref1, _results, _results1, _results2;
1484 this.seriesPoints = [];
1485 if (this.options.behaveLikeLine) {
1486 range = (function() {
1488 for (var _i = 0, _ref = this.options.ykeys.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
1492 range = (function() {
1494 for (var _j = _ref1 = this.options.ykeys.length - 1; _ref1 <= 0 ? _j <= 0 : _j >= 0; _ref1 <= 0 ? _j++ : _j--){ _results1.push(_j); }
1499 for (_k = 0, _len = range.length; _k < _len; _k++) {
1501 this._drawFillFor(i);
1502 this._drawLineFor(i);
1503 _results2.push(this._drawPointFor(i));
1508 Area.prototype._drawFillFor = function(index) {
1510 path = this.paths[index];
1511 if (path !== null) {
1512 path = path + ("L" + (this.transX(this.xmax)) + "," + this.bottom + "L" + (this.transX(this.xmin)) + "," + this.bottom + "Z");
1513 return this.drawFilledPath(path, this.fillForSeries(index));
1517 Area.prototype.fillForSeries = function(i) {
1519 color = Raphael.rgb2hsl(this.colorFor(this.data[i], i, 'line'));
1520 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));
1523 Area.prototype.drawFilledPath = function(path, fill) {
1524 return this.raphael.path(path).attr('fill', fill).attr('fill-opacity', this.options.fillOpacity).attr('stroke', 'none');
1531 Morris.Bar = (function(_super) {
1532 __extends(Bar, _super);
1534 function Bar(options) {
1535 this.onHoverOut = __bind(this.onHoverOut, this);
1536 this.onHoverMove = __bind(this.onHoverMove, this);
1537 this.onGridClick = __bind(this.onGridClick, this);
1538 if (!(this instanceof Morris.Bar)) {
1539 return new Morris.Bar(options);
1541 Bar.__super__.constructor.call(this, $.extend({}, options, {
1546 Bar.prototype.init = function() {
1547 this.cumulative = this.options.stacked;
1548 if (this.options.hideHover !== 'always') {
1549 this.hover = new Morris.Hover({
1552 this.on('hovermove', this.onHoverMove);
1553 this.on('hoverout', this.onHoverOut);
1554 return this.on('gridclick', this.onGridClick);
1558 Bar.prototype.defaults = {
1561 barColors: ['#0b62a4', '#7a92a3', '#4da74d', '#afd8f8', '#edc240', '#cb4b4b', '#9440ed'],
1563 barRadius: [0, 0, 0, 0],
1569 Bar.prototype.calc = function() {
1572 if (this.options.hideHover === false) {
1573 return (_ref = this.hover).update.apply(_ref, this.hoverContentForRow(this.data.length - 1));
1577 Bar.prototype.calcBars = function() {
1578 var idx, row, y, _i, _len, _ref, _results;
1581 for (idx = _i = 0, _len = _ref.length; _i < _len; idx = ++_i) {
1583 row._x = this.xStart + this.xSize * (idx + 0.5) / this.data.length;
1584 _results.push(row._y = (function() {
1585 var _j, _len1, _ref1, _results1;
1588 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
1591 _results1.push(this.transY(y));
1593 _results1.push(null);
1602 Bar.prototype.draw = function() {
1604 if ((_ref = this.options.axes) === true || _ref === 'both' || _ref === 'x') {
1607 return this.drawSeries();
1610 Bar.prototype.drawXAxis = function() {
1611 var angle, basePos, i, label, labelBox, margin, maxSize, offset, prevAngleMargin, prevLabelMargin, row, size, startPos, textBox, _i, _ref, _results;
1612 if (!this.options.horizontal) {
1613 basePos = this.getXAxisLabelY();
1615 basePos = this.getYAxisLabelX();
1617 prevLabelMargin = null;
1618 prevAngleMargin = null;
1620 for (i = _i = 0, _ref = this.data.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
1621 row = this.data[this.data.length - 1 - i];
1622 if (!this.options.horizontal) {
1623 label = this.drawXAxisLabel(row._x, basePos, row.label);
1625 label = this.drawYAxisLabel(basePos, row._x - 0.5 * this.options.gridTextSize, row.label);
1627 if (!this.options.horizontal) {
1628 angle = this.options.xLabelAngle;
1632 textBox = label.getBBox();
1633 label.transform("r" + (-angle));
1634 labelBox = label.getBBox();
1635 label.transform("t0," + (labelBox.height / 2) + "...");
1637 offset = -0.5 * textBox.width * Math.cos(angle * Math.PI / 180.0);
1638 label.transform("t" + offset + ",0...");
1640 if (!this.options.horizontal) {
1641 startPos = labelBox.x;
1642 size = labelBox.width;
1643 maxSize = this.el.width();
1645 startPos = labelBox.y;
1646 size = labelBox.height;
1647 maxSize = this.el.height();
1649 if (((prevLabelMargin == null) || prevLabelMargin >= startPos + size || (prevAngleMargin != null) && prevAngleMargin >= startPos) && startPos >= 0 && (startPos + size) < maxSize) {
1651 margin = 1.25 * this.options.gridTextSize / Math.sin(angle * Math.PI / 180.0);
1652 prevAngleMargin = startPos - margin;
1654 if (!this.options.horizontal) {
1655 _results.push(prevLabelMargin = startPos - this.options.xLabelMargin);
1657 _results.push(prevLabelMargin = startPos);
1660 _results.push(label.remove());
1666 Bar.prototype.getXAxisLabelY = function() {
1667 return this.bottom + (this.options.xAxisLabelTopPadding || this.options.padding / 2);
1670 Bar.prototype.drawSeries = function() {
1671 var barWidth, bottom, groupWidth, i, idx, lastTop, left, leftPadding, numBars, row, sidx, size, spaceLeft, top, ypos, zeroPos, _i, _ref;
1672 groupWidth = this.xSize / this.options.data.length;
1673 if (this.options.stacked) {
1677 for (i = _i = 0, _ref = this.options.ykeys.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
1678 if (this.hasToShow(i)) {
1683 barWidth = (groupWidth * this.options.barSizeRatio - this.options.barGap * (numBars - 1)) / numBars;
1684 if (this.options.barSize) {
1685 barWidth = Math.min(barWidth, this.options.barSize);
1687 spaceLeft = groupWidth - barWidth * numBars - this.options.barGap * (numBars - 1);
1688 leftPadding = spaceLeft / 2;
1689 zeroPos = this.ymin <= 0 && this.ymax >= 0 ? this.transY(0) : null;
1690 return this.bars = (function() {
1691 var _j, _len, _ref1, _results;
1694 for (idx = _j = 0, _len = _ref1.length; _j < _len; idx = ++_j) {
1697 _results.push((function() {
1698 var _k, _len1, _ref2, _results1;
1701 for (sidx = _k = 0, _len1 = _ref2.length; _k < _len1; sidx = ++_k) {
1703 if (!this.hasToShow(sidx)) {
1706 if (ypos !== null) {
1708 top = Math.min(ypos, zeroPos);
1709 bottom = Math.max(ypos, zeroPos);
1712 bottom = this.bottom;
1714 left = this.xStart + idx * groupWidth + leftPadding;
1715 if (!this.options.stacked) {
1716 left += sidx * (barWidth + this.options.barGap);
1718 size = bottom - top;
1719 if (this.options.verticalGridCondition && this.options.verticalGridCondition(row.x)) {
1720 if (!this.options.horizontal) {
1721 this.drawBar(this.xStart + idx * groupWidth, this.yEnd, groupWidth, this.ySize, this.options.verticalGridColor, this.options.verticalGridOpacity, this.options.barRadius);
1723 this.drawBar(this.yStart, this.xStart + idx * groupWidth, this.ySize, groupWidth, this.options.verticalGridColor, this.options.verticalGridOpacity, this.options.barRadius);
1726 if (this.options.stacked) {
1729 if (!this.options.horizontal) {
1730 this.drawBar(left, top, barWidth, size, this.colorFor(row, sidx, 'bar'), this.options.barOpacity, this.options.barRadius);
1731 _results1.push(lastTop += size);
1733 this.drawBar(top, left, size, barWidth, this.colorFor(row, sidx, 'bar'), this.options.barOpacity, this.options.barRadius);
1734 _results1.push(lastTop -= size);
1737 _results1.push(null);
1747 Bar.prototype.colorFor = function(row, sidx, type) {
1749 if (typeof this.options.barColors === 'function') {
1757 key: this.options.ykeys[sidx],
1758 label: this.options.labels[sidx]
1760 return this.options.barColors.call(this, r, s, type);
1762 return this.options.barColors[sidx % this.options.barColors.length];
1766 Bar.prototype.hitTest = function(x, y) {
1768 if (this.data.length === 0) {
1771 if (!this.options.horizontal) {
1776 pos = Math.max(Math.min(pos, this.xEnd), this.xStart);
1777 return Math.min(this.data.length - 1, Math.floor((pos - this.xStart) / (this.xSize / this.data.length)));
1780 Bar.prototype.onGridClick = function(x, y) {
1782 index = this.hitTest(x, y);
1783 return this.fire('click', index, this.data[index].src, x, y);
1786 Bar.prototype.onHoverMove = function(x, y) {
1788 index = this.hitTest(x, y);
1789 return (_ref = this.hover).update.apply(_ref, this.hoverContentForRow(index));
1792 Bar.prototype.onHoverOut = function() {
1793 if (this.options.hideHover !== false) {
1794 return this.hover.hide();
1798 Bar.prototype.hoverContentForRow = function(index) {
1799 var content, j, row, x, y, _i, _len, _ref;
1800 row = this.data[index];
1801 content = $("<div class='morris-hover-row-label'>").text(row.label);
1802 content = content.prop('outerHTML');
1804 for (j = _i = 0, _len = _ref.length; _i < _len; j = ++_i) {
1806 if (this.options.labels[j] === false) {
1809 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>";
1811 if (typeof this.options.hoverCallback === 'function') {
1812 content = this.options.hoverCallback(index, this.options, content, row.src);
1814 if (!this.options.horizontal) {
1815 x = this.left + (index + 0.5) * this.width / this.data.length;
1816 return [content, x];
1818 x = this.left + 0.5 * this.width;
1819 y = this.top + (index + 0.5) * this.height / this.data.length;
1820 return [content, x, y, true];
1824 Bar.prototype.drawXAxisLabel = function(xPos, yPos, text) {
1826 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);
1829 Bar.prototype.drawBar = function(xPos, yPos, width, height, barColor, opacity, radiusArray) {
1830 var maxRadius, path;
1831 maxRadius = Math.max.apply(Math, radiusArray);
1832 if (maxRadius === 0 || maxRadius > height) {
1833 path = this.raphael.rect(xPos, yPos, width, height);
1835 path = this.raphael.path(this.roundedRect(xPos, yPos, width, height, radiusArray));
1837 return path.attr('fill', barColor).attr('fill-opacity', opacity).attr('stroke', 'none');
1840 Bar.prototype.roundedRect = function(x, y, w, h, r) {
1844 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"];
1851 Morris.Donut = (function(_super) {
1852 __extends(Donut, _super);
1854 Donut.prototype.defaults = {
1855 colors: ['#0B62A4', '#3980B5', '#679DC6', '#95BBD7', '#B0CCE1', '#095791', '#095085', '#083E67', '#052C48', '#042135'],
1856 backgroundColor: '#FFFFFF',
1857 labelColor: '#000000',
1858 formatter: Morris.commas,
1862 function Donut(options) {
1863 this.resizeHandler = __bind(this.resizeHandler, this);
1864 this.select = __bind(this.select, this);
1865 this.click = __bind(this.click, this);
1867 if (!(this instanceof Morris.Donut)) {
1868 return new Morris.Donut(options);
1870 this.options = $.extend({}, this.defaults, options);
1871 if (typeof options.element === 'string') {
1872 this.el = $(document.getElementById(options.element));
1874 this.el = $(options.element);
1876 if (this.el === null || this.el.length === 0) {
1877 throw new Error("Graph placeholder not found.");
1879 if (options.data === void 0 || options.data.length === 0) {
1882 this.raphael = new Raphael(this.el[0]);
1883 if (this.options.resize) {
1884 $(window).bind('resize', function(evt) {
1885 if (_this.timeoutId != null) {
1886 window.clearTimeout(_this.timeoutId);
1888 return _this.timeoutId = window.setTimeout(_this.resizeHandler, 100);
1891 this.setData(options.data);
1894 Donut.prototype.redraw = function() {
1895 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;
1896 this.raphael.clear();
1897 cx = this.el.width() / 2;
1898 cy = this.el.height() / 2;
1899 w = (Math.min(cx, cy) - 10) / 3;
1902 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1907 C = 1.9999 * Math.PI - min * this.data.length;
1911 _ref1 = this.values;
1912 for (i = _j = 0, _len1 = _ref1.length; _j < _len1; i = ++_j) {
1914 next = last + min + C * (value / total);
1915 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);
1917 this.segments.push(seg);
1918 seg.on('hover', this.select);
1919 seg.on('click', this.click);
1923 this.text1 = this.drawEmptyDonutLabel(cx, cy - 10, this.options.labelColor, 15, 800);
1924 this.text2 = this.drawEmptyDonutLabel(cx, cy + 10, this.options.labelColor, 14);
1925 max_value = Math.max.apply(Math, this.values);
1927 _ref2 = this.values;
1929 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
1931 if (value === max_value) {
1935 _results.push(idx += 1);
1940 Donut.prototype.setData = function(data) {
1943 this.values = (function() {
1944 var _i, _len, _ref, _results;
1947 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1949 _results.push(parseFloat(row.value));
1953 return this.redraw();
1956 Donut.prototype.click = function(idx) {
1957 return this.fire('click', idx, this.data[idx]);
1960 Donut.prototype.select = function(idx) {
1961 var row, s, segment, _i, _len, _ref;
1962 _ref = this.segments;
1963 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1967 segment = this.segments[idx];
1969 row = this.data[idx];
1970 return this.setLabels(row.label, this.options.formatter(row.value, row));
1973 Donut.prototype.setLabels = function(label1, label2) {
1974 var inner, maxHeightBottom, maxHeightTop, maxWidth, text1bbox, text1scale, text2bbox, text2scale;
1975 inner = (Math.min(this.el.width() / 2, this.el.height() / 2) - 10) * 2 / 3;
1976 maxWidth = 1.8 * inner;
1977 maxHeightTop = inner / 2;
1978 maxHeightBottom = inner / 3;
1983 text1bbox = this.text1.getBBox();
1984 text1scale = Math.min(maxWidth / text1bbox.width, maxHeightTop / text1bbox.height);
1986 transform: "S" + text1scale + "," + text1scale + "," + (text1bbox.x + text1bbox.width / 2) + "," + (text1bbox.y + text1bbox.height)
1992 text2bbox = this.text2.getBBox();
1993 text2scale = Math.min(maxWidth / text2bbox.width, maxHeightBottom / text2bbox.height);
1994 return this.text2.attr({
1995 transform: "S" + text2scale + "," + text2scale + "," + (text2bbox.x + text2bbox.width / 2) + "," + text2bbox.y
1999 Donut.prototype.drawEmptyDonutLabel = function(xPos, yPos, color, fontSize, fontWeight) {
2001 text = this.raphael.text(xPos, yPos, '').attr('font-size', fontSize).attr('fill', color);
2002 if (fontWeight != null) {
2003 text.attr('font-weight', fontWeight);
2008 Donut.prototype.resizeHandler = function() {
2009 this.timeoutId = null;
2010 this.raphael.setSize(this.el.width(), this.el.height());
2011 return this.redraw();
2016 })(Morris.EventEmitter);
2018 Morris.DonutSegment = (function(_super) {
2019 __extends(DonutSegment, _super);
2021 function DonutSegment(cx, cy, inner, outer, p0, p1, color, backgroundColor, index, raphael) {
2027 this.backgroundColor = backgroundColor;
2029 this.raphael = raphael;
2030 this.deselect = __bind(this.deselect, this);
2031 this.select = __bind(this.select, this);
2032 this.sin_p0 = Math.sin(p0);
2033 this.cos_p0 = Math.cos(p0);
2034 this.sin_p1 = Math.sin(p1);
2035 this.cos_p1 = Math.cos(p1);
2036 this.is_long = (p1 - p0) > Math.PI ? 1 : 0;
2037 this.path = this.calcSegment(this.inner + 3, this.inner + this.outer - 5);
2038 this.selectedPath = this.calcSegment(this.inner + 3, this.inner + this.outer);
2039 this.hilight = this.calcArc(this.inner);
2042 DonutSegment.prototype.calcArcPoints = function(r) {
2043 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];
2046 DonutSegment.prototype.calcSegment = function(r1, r2) {
2047 var ix0, ix1, iy0, iy1, ox0, ox1, oy0, oy1, _ref, _ref1;
2048 _ref = this.calcArcPoints(r1), ix0 = _ref[0], iy0 = _ref[1], ix1 = _ref[2], iy1 = _ref[3];
2049 _ref1 = this.calcArcPoints(r2), ox0 = _ref1[0], oy0 = _ref1[1], ox1 = _ref1[2], oy1 = _ref1[3];
2050 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";
2053 DonutSegment.prototype.calcArc = function(r) {
2054 var ix0, ix1, iy0, iy1, _ref;
2055 _ref = this.calcArcPoints(r), ix0 = _ref[0], iy0 = _ref[1], ix1 = _ref[2], iy1 = _ref[3];
2056 return ("M" + ix0 + "," + iy0) + ("A" + r + "," + r + ",0," + this.is_long + ",0," + ix1 + "," + iy1);
2059 DonutSegment.prototype.render = function() {
2061 this.arc = this.drawDonutArc(this.hilight, this.color);
2062 return this.seg = this.drawDonutSegment(this.path, this.color, this.backgroundColor, function() {
2063 return _this.fire('hover', _this.index);
2065 return _this.fire('click', _this.index);
2069 DonutSegment.prototype.drawDonutArc = function(path, color) {
2070 return this.raphael.path(path).attr({
2077 DonutSegment.prototype.drawDonutSegment = function(path, fillColor, strokeColor, hoverFunction, clickFunction) {
2078 return this.raphael.path(path).attr({
2080 stroke: strokeColor,
2082 }).hover(hoverFunction).click(clickFunction);
2085 DonutSegment.prototype.select = function() {
2086 if (!this.selected) {
2088 path: this.selectedPath
2093 return this.selected = true;
2097 DonutSegment.prototype.deselect = function() {
2098 if (this.selected) {
2105 return this.selected = false;
2109 return DonutSegment;
2111 })(Morris.EventEmitter);