morrisjs-rails 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  module Morrisjs
2
2
  module Rails
3
- VERSION = "0.4.1"
3
+ VERSION = "0.4.2"
4
4
  end
5
5
  end
@@ -107,6 +107,11 @@
107
107
  _this.fire('hover', touch.pageX - offset.left, touch.pageY - offset.top);
108
108
  return touch;
109
109
  });
110
+ this.el.bind('click', function(evt) {
111
+ var offset;
112
+ offset = _this.el.offset();
113
+ return _this.fire('gridclick', evt.pageX - offset.left, evt.pageY - offset.top);
114
+ });
110
115
  if (this.postInit) {
111
116
  this.postInit();
112
117
  }
@@ -138,7 +143,7 @@
138
143
  };
139
144
 
140
145
  Grid.prototype.setData = function(data, redraw) {
141
- var e, idx, index, maxGoal, minGoal, ret, row, total, ykey, ymax, ymin, yval;
146
+ var e, idx, index, maxGoal, minGoal, ret, row, step, total, y, ykey, ymax, ymin, yval;
142
147
  if (redraw == null) {
143
148
  redraw = true;
144
149
  }
@@ -247,11 +252,22 @@
247
252
  }
248
253
  this.ymax += 1;
249
254
  }
250
- this.yInterval = (this.ymax - this.ymin) / (this.options.numLines - 1);
251
- if (this.yInterval > 0 && this.yInterval < 1) {
252
- this.precision = -Math.floor(Math.log(this.yInterval) / Math.log(10));
253
- } else {
254
- this.precision = 0;
255
+ if (this.options.axes === true || this.options.grid === true) {
256
+ if (this.options.ymax === this.gridDefaults.ymax && this.options.ymin === this.gridDefaults.ymin) {
257
+ this.grid = this.autoGridLines(this.ymin, this.ymax, this.options.numLines);
258
+ this.ymin = Math.min(this.ymin, this.grid[0]);
259
+ this.ymax = Math.max(this.ymax, this.grid[this.grid.length - 1]);
260
+ } else {
261
+ step = (this.ymax - this.ymin) / (this.options.numLines - 1);
262
+ this.grid = (function() {
263
+ var _i, _ref, _ref1, _results;
264
+ _results = [];
265
+ for (y = _i = _ref = this.ymin, _ref1 = this.ymax; _ref <= _ref1 ? _i <= _ref1 : _i >= _ref1; y = _i += step) {
266
+ _results.push(y);
267
+ }
268
+ return _results;
269
+ }).call(this);
270
+ }
255
271
  }
256
272
  this.dirty = true;
257
273
  if (redraw) {
@@ -285,8 +301,47 @@
285
301
  }
286
302
  };
287
303
 
304
+ Grid.prototype.autoGridLines = function(ymin, ymax, nlines) {
305
+ var gmax, gmin, grid, smag, span, step, unit, y, ymag;
306
+ span = ymax - ymin;
307
+ ymag = Math.floor(Math.log(span) / Math.log(10));
308
+ unit = Math.pow(10, ymag);
309
+ gmin = Math.floor(ymin / unit) * unit;
310
+ gmax = Math.ceil(ymax / unit) * unit;
311
+ step = (gmax - gmin) / (nlines - 1);
312
+ if (unit === 1 && step > 1 && Math.ceil(step) !== step) {
313
+ step = Math.ceil(step);
314
+ gmax = gmin + step * (nlines - 1);
315
+ }
316
+ if (gmin < 0 && gmax > 0) {
317
+ gmin = Math.floor(ymin / step) * step;
318
+ gmax = Math.ceil(ymax / step) * step;
319
+ }
320
+ if (step < 1) {
321
+ smag = Math.floor(Math.log(step) / Math.log(10));
322
+ grid = (function() {
323
+ var _i, _results;
324
+ _results = [];
325
+ for (y = _i = gmin; gmin <= gmax ? _i <= gmax : _i >= gmax; y = _i += step) {
326
+ _results.push(parseFloat(y.toFixed(1 - smag)));
327
+ }
328
+ return _results;
329
+ })();
330
+ } else {
331
+ grid = (function() {
332
+ var _i, _results;
333
+ _results = [];
334
+ for (y = _i = gmin; gmin <= gmax ? _i <= gmax : _i >= gmax; y = _i += step) {
335
+ _results.push(y);
336
+ }
337
+ return _results;
338
+ })();
339
+ }
340
+ return grid;
341
+ };
342
+
288
343
  Grid.prototype._calc = function() {
289
- var h, maxYLabelWidth, w;
344
+ var gridLine, h, w, yLabelWidths;
290
345
  w = this.el.width();
291
346
  h = this.el.height();
292
347
  if (this.elementWidth !== w || this.elementHeight !== h || this.dirty) {
@@ -298,12 +353,21 @@
298
353
  this.top = this.options.padding;
299
354
  this.bottom = this.elementHeight - this.options.padding;
300
355
  if (this.options.axes) {
301
- maxYLabelWidth = Math.max(this.measureText(this.yAxisFormat(this.ymin), this.options.gridTextSize).width, this.measureText(this.yAxisFormat(this.ymax), this.options.gridTextSize).width);
302
- this.left += maxYLabelWidth;
356
+ yLabelWidths = (function() {
357
+ var _i, _len, _ref, _results;
358
+ _ref = this.grid;
359
+ _results = [];
360
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
361
+ gridLine = _ref[_i];
362
+ _results.push(this.measureText(this.yAxisFormat(gridLine), this.options.gridTextSize).width);
363
+ }
364
+ return _results;
365
+ }).call(this);
366
+ this.left += Math.max.apply(Math, yLabelWidths);
303
367
  this.bottom -= 1.5 * this.options.gridTextSize;
304
368
  }
305
- this.width = this.right - this.left;
306
- this.height = this.bottom - this.top;
369
+ this.width = Math.max(1, this.right - this.left);
370
+ this.height = Math.max(1, this.bottom - this.top);
307
371
  this.dx = this.width / (this.xmax - this.xmin);
308
372
  this.dy = this.height / (this.ymax - this.ymin);
309
373
  if (this.calc) {
@@ -367,18 +431,17 @@
367
431
  };
368
432
 
369
433
  Grid.prototype.drawGrid = function() {
370
- var firstY, lastY, lineY, v, y, _i, _ref, _results;
434
+ var lineY, y, _i, _len, _ref, _results;
371
435
  if (this.options.grid === false && this.options.axes === false) {
372
436
  return;
373
437
  }
374
- firstY = this.ymin;
375
- lastY = this.ymax;
438
+ _ref = this.grid;
376
439
  _results = [];
377
- for (lineY = _i = firstY, _ref = this.yInterval; firstY <= lastY ? _i <= lastY : _i >= lastY; lineY = _i += _ref) {
378
- v = parseFloat(lineY.toFixed(this.precision));
379
- y = this.transY(v);
440
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
441
+ lineY = _ref[_i];
442
+ y = this.transY(lineY);
380
443
  if (this.options.axes) {
381
- this.drawYAxisLabel(this.left - this.options.padding / 2, y, this.yAxisFormat(v));
444
+ this.drawYAxisLabel(this.left - this.options.padding / 2, y, this.yAxisFormat(lineY));
382
445
  }
383
446
  if (this.options.grid) {
384
447
  _results.push(this.drawGridLine("M" + this.left + "," + y + "H" + (this.left + this.width)));
@@ -562,6 +625,8 @@
562
625
  this.onHoverOut = __bind(this.onHoverOut, this);
563
626
 
564
627
  this.onHoverMove = __bind(this.onHoverMove, this);
628
+
629
+ this.onGridClick = __bind(this.onGridClick, this);
565
630
  if (!(this instanceof Morris.Line)) {
566
631
  return new Morris.Line(options);
567
632
  }
@@ -580,7 +645,8 @@
580
645
  parent: this.el
581
646
  });
582
647
  this.on('hovermove', this.onHoverMove);
583
- return this.on('hoverout', this.onHoverOut);
648
+ this.on('hoverout', this.onHoverOut);
649
+ return this.on('gridclick', this.onGridClick);
584
650
  }
585
651
  };
586
652
 
@@ -656,6 +722,12 @@
656
722
  return index;
657
723
  };
658
724
 
725
+ Line.prototype.onGridClick = function(x, y) {
726
+ var index;
727
+ index = this.hitTest(x, y);
728
+ return this.fire('click', index, this.options.data[index], x, y);
729
+ };
730
+
659
731
  Line.prototype.onHoverMove = function(x, y) {
660
732
  var index;
661
733
  index = this.hitTest(x, y);
@@ -682,15 +754,14 @@
682
754
  Line.prototype.hoverContentForRow = function(index) {
683
755
  var content, j, row, y, _i, _len, _ref;
684
756
  row = this.data[index];
757
+ content = "<div class='morris-hover-row-label'>" + row.label + "</div>";
758
+ _ref = row.y;
759
+ for (j = _i = 0, _len = _ref.length; _i < _len; j = ++_i) {
760
+ y = _ref[j];
761
+ content += "<div class='morris-hover-point' style='color: " + (this.colorFor(row, j, 'label')) + "'>\n " + this.options.labels[j] + ":\n " + (this.yLabelFormat(y)) + "\n</div>";
762
+ }
685
763
  if (typeof this.options.hoverCallback === 'function') {
686
- content = this.options.hoverCallback(index, this.options);
687
- } else {
688
- content = "<div class='morris-hover-row-label'>" + row.label + "</div>";
689
- _ref = row.y;
690
- for (j = _i = 0, _len = _ref.length; _i < _len; j = ++_i) {
691
- y = _ref[j];
692
- content += "<div class='morris-hover-point' style='color: " + (this.colorFor(row, j, 'label')) + "'>\n " + this.options.labels[j] + ":\n " + (this.yLabelFormat(y)) + "\n</div>";
693
- }
764
+ content = this.options.hoverCallback(index, this.options, content);
694
765
  }
695
766
  return [content, row._x, row._ymax];
696
767
  };
@@ -793,42 +864,42 @@
793
864
  };
794
865
 
795
866
  Line.prototype.drawSeries = function() {
796
- var circle, i, path, row, _i, _j, _ref, _ref1, _results;
867
+ var i, _i, _j, _ref, _ref1, _results;
868
+ this.seriesPoints = [];
797
869
  for (i = _i = _ref = this.options.ykeys.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) {
798
- path = this.paths[i];
799
- if (path !== null) {
800
- this.drawLinePath(path, this.colorFor(row, i, 'line'));
801
- }
870
+ this._drawLineFor(i);
802
871
  }
803
- this.seriesPoints = (function() {
804
- var _j, _ref1, _results;
805
- _results = [];
806
- for (i = _j = 0, _ref1 = this.options.ykeys.length; 0 <= _ref1 ? _j < _ref1 : _j > _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
807
- _results.push([]);
808
- }
809
- return _results;
810
- }).call(this);
811
872
  _results = [];
812
873
  for (i = _j = _ref1 = this.options.ykeys.length - 1; _ref1 <= 0 ? _j <= 0 : _j >= 0; i = _ref1 <= 0 ? ++_j : --_j) {
813
- _results.push((function() {
814
- var _k, _len, _ref2, _results1;
815
- _ref2 = this.data;
816
- _results1 = [];
817
- for (_k = 0, _len = _ref2.length; _k < _len; _k++) {
818
- row = _ref2[_k];
819
- if (row._y[i] != null) {
820
- circle = this.drawLinePoint(row._x, row._y[i], this.options.pointSize, this.colorFor(row, i, 'point'), i);
821
- } else {
822
- circle = null;
823
- }
824
- _results1.push(this.seriesPoints[i].push(circle));
825
- }
826
- return _results1;
827
- }).call(this));
874
+ _results.push(this._drawPointFor(i));
828
875
  }
829
876
  return _results;
830
877
  };
831
878
 
879
+ Line.prototype._drawPointFor = function(index) {
880
+ var circle, row, _i, _len, _ref, _results;
881
+ this.seriesPoints[index] = [];
882
+ _ref = this.data;
883
+ _results = [];
884
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
885
+ row = _ref[_i];
886
+ circle = null;
887
+ if (row._y[index] != null) {
888
+ circle = this.drawLinePoint(row._x, row._y[index], this.options.pointSize, this.colorFor(row, index, 'point'), index);
889
+ }
890
+ _results.push(this.seriesPoints[index].push(circle));
891
+ }
892
+ return _results;
893
+ };
894
+
895
+ Line.prototype._drawLineFor = function(index) {
896
+ var path;
897
+ path = this.paths[index];
898
+ if (path !== null) {
899
+ return this.drawLinePath(path, this.colorFor(null, index, 'line'));
900
+ }
901
+ };
902
+
832
903
  Line.createPath = function(coords, smooth, bottom) {
833
904
  var coord, g, grads, i, ix, lg, path, prevCoord, x1, x2, y1, y2, _i, _len;
834
905
  path = "";
@@ -994,7 +1065,7 @@
994
1065
  return "" + (Morris.pad2(d.getHours())) + ":" + (Morris.pad2(d.getMinutes()));
995
1066
  },
996
1067
  incr: function(d) {
997
- return d.setMinutes(d.getMinutes() + interval);
1068
+ return d.setUTCMinutes(d.getUTCMinutes() + interval);
998
1069
  }
999
1070
  };
1000
1071
  };
@@ -1009,7 +1080,7 @@
1009
1080
  return "" + (Morris.pad2(d.getHours())) + ":" + (Morris.pad2(d.getMinutes())) + ":" + (Morris.pad2(d.getSeconds()));
1010
1081
  },
1011
1082
  incr: function(d) {
1012
- return d.setSeconds(d.getSeconds() + interval);
1083
+ return d.setUTCSeconds(d.getUTCSeconds() + interval);
1013
1084
  }
1014
1085
  };
1015
1086
  };
@@ -1079,15 +1150,26 @@
1079
1150
  Morris.AUTO_LABEL_ORDER = ["decade", "year", "month", "day", "hour", "30min", "15min", "10min", "5min", "minute", "30sec", "15sec", "10sec", "5sec", "second"];
1080
1151
 
1081
1152
  Morris.Area = (function(_super) {
1153
+ var areaDefaults;
1082
1154
 
1083
1155
  __extends(Area, _super);
1084
1156
 
1157
+ areaDefaults = {
1158
+ fillOpacity: 'auto',
1159
+ behaveLikeLine: false
1160
+ };
1161
+
1085
1162
  function Area(options) {
1163
+ var areaOptions;
1086
1164
  if (!(this instanceof Morris.Area)) {
1087
1165
  return new Morris.Area(options);
1088
1166
  }
1089
- this.cumulative = true;
1090
- Area.__super__.constructor.call(this, options);
1167
+ areaOptions = $.extend({}, areaDefaults, options);
1168
+ this.cumulative = !areaOptions.behaveLikeLine;
1169
+ if (areaOptions.fillOpacity === 'auto') {
1170
+ areaOptions.fillOpacity = areaOptions.behaveLikeLine ? .8 : 1;
1171
+ }
1172
+ Area.__super__.constructor.call(this, areaOptions);
1091
1173
  }
1092
1174
 
1093
1175
  Area.prototype.calcPoints = function() {
@@ -1104,36 +1186,63 @@
1104
1186
  _results1 = [];
1105
1187
  for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
1106
1188
  y = _ref1[_j];
1107
- total += y || 0;
1108
- _results1.push(this.transY(total));
1189
+ if (this.options.behaveLikeLine) {
1190
+ _results1.push(this.transY(y));
1191
+ } else {
1192
+ total += y || 0;
1193
+ _results1.push(this.transY(total));
1194
+ }
1109
1195
  }
1110
1196
  return _results1;
1111
1197
  }).call(this);
1112
- _results.push(row._ymax = row._y[row._y.length - 1]);
1198
+ _results.push(row._ymax = Math.max.apply(Math, row._y));
1113
1199
  }
1114
1200
  return _results;
1115
1201
  };
1116
1202
 
1117
1203
  Area.prototype.drawSeries = function() {
1118
- var i, path, _i, _ref;
1119
- for (i = _i = _ref = this.options.ykeys.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) {
1120
- path = this.paths[i];
1121
- if (path !== null) {
1122
- path = path + ("L" + (this.transX(this.xmax)) + "," + this.bottom + "L" + (this.transX(this.xmin)) + "," + this.bottom + "Z");
1123
- this.drawFilledPath(path, this.fillForSeries(i));
1124
- }
1204
+ var i, range, _i, _j, _k, _len, _ref, _ref1, _results, _results1, _results2;
1205
+ this.seriesPoints = [];
1206
+ if (this.options.behaveLikeLine) {
1207
+ range = (function() {
1208
+ _results = [];
1209
+ for (var _i = 0, _ref = this.options.ykeys.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; 0 <= _ref ? _i++ : _i--){ _results.push(_i); }
1210
+ return _results;
1211
+ }).apply(this);
1212
+ } else {
1213
+ range = (function() {
1214
+ _results1 = [];
1215
+ for (var _j = _ref1 = this.options.ykeys.length - 1; _ref1 <= 0 ? _j <= 0 : _j >= 0; _ref1 <= 0 ? _j++ : _j--){ _results1.push(_j); }
1216
+ return _results1;
1217
+ }).apply(this);
1218
+ }
1219
+ _results2 = [];
1220
+ for (_k = 0, _len = range.length; _k < _len; _k++) {
1221
+ i = range[_k];
1222
+ this._drawFillFor(i);
1223
+ this._drawLineFor(i);
1224
+ _results2.push(this._drawPointFor(i));
1225
+ }
1226
+ return _results2;
1227
+ };
1228
+
1229
+ Area.prototype._drawFillFor = function(index) {
1230
+ var path;
1231
+ path = this.paths[index];
1232
+ if (path !== null) {
1233
+ path = path + ("L" + (this.transX(this.xmax)) + "," + this.bottom + "L" + (this.transX(this.xmin)) + "," + this.bottom + "Z");
1234
+ return this.drawFilledPath(path, this.fillForSeries(index));
1125
1235
  }
1126
- return Area.__super__.drawSeries.call(this);
1127
1236
  };
1128
1237
 
1129
1238
  Area.prototype.fillForSeries = function(i) {
1130
1239
  var color;
1131
1240
  color = Raphael.rgb2hsl(this.colorFor(this.data[i], i, 'line'));
1132
- return Raphael.hsl(color.h, Math.min(255, color.s * 0.75), Math.min(255, color.l * 1.25));
1241
+ return Raphael.hsl(color.h, Math.min(255, this.options.behaveLikeLine ? color.s * 0.9 : color.s * 0.75), Math.min(255, this.options.behaveLikeLine ? color.l * 1.2 : color.l * 1.25));
1133
1242
  };
1134
1243
 
1135
1244
  Area.prototype.drawFilledPath = function(path, fill) {
1136
- return this.raphael.path(path).attr('fill', fill).attr('stroke-width', 0);
1245
+ return this.raphael.path(path).attr('fill', fill).attr('fill-opacity', this.options.fillOpacity).attr('stroke-width', 0);
1137
1246
  };
1138
1247
 
1139
1248
  return Area;
@@ -1148,6 +1257,8 @@
1148
1257
  this.onHoverOut = __bind(this.onHoverOut, this);
1149
1258
 
1150
1259
  this.onHoverMove = __bind(this.onHoverMove, this);
1260
+
1261
+ this.onGridClick = __bind(this.onGridClick, this);
1151
1262
  if (!(this instanceof Morris.Bar)) {
1152
1263
  return new Morris.Bar(options);
1153
1264
  }
@@ -1163,7 +1274,8 @@
1163
1274
  parent: this.el
1164
1275
  });
1165
1276
  this.on('hovermove', this.onHoverMove);
1166
- return this.on('hoverout', this.onHoverOut);
1277
+ this.on('hoverout', this.onHoverOut);
1278
+ return this.on('gridclick', this.onGridClick);
1167
1279
  }
1168
1280
  };
1169
1281
 
@@ -1308,6 +1420,12 @@
1308
1420
  return Math.min(this.data.length - 1, Math.floor((x - this.left) / (this.width / this.data.length)));
1309
1421
  };
1310
1422
 
1423
+ Bar.prototype.onGridClick = function(x, y) {
1424
+ var index;
1425
+ index = this.hitTest(x, y);
1426
+ return this.fire('click', index, this.options.data[index], x, y);
1427
+ };
1428
+
1311
1429
  Bar.prototype.onHoverMove = function(x, y) {
1312
1430
  var index, _ref;
1313
1431
  index = this.hitTest(x, y);
@@ -1322,16 +1440,15 @@
1322
1440
 
1323
1441
  Bar.prototype.hoverContentForRow = function(index) {
1324
1442
  var content, j, row, x, y, _i, _len, _ref;
1443
+ row = this.data[index];
1444
+ content = "<div class='morris-hover-row-label'>" + row.label + "</div>";
1445
+ _ref = row.y;
1446
+ for (j = _i = 0, _len = _ref.length; _i < _len; j = ++_i) {
1447
+ y = _ref[j];
1448
+ content += "<div class='morris-hover-point' style='color: " + (this.colorFor(row, j, 'label')) + "'>\n " + this.options.labels[j] + ":\n " + (this.yLabelFormat(y)) + "\n</div>";
1449
+ }
1325
1450
  if (typeof this.options.hoverCallback === 'function') {
1326
- content = this.options.hoverCallback(index, this.options);
1327
- } else {
1328
- row = this.data[index];
1329
- content = "<div class='morris-hover-row-label'>" + row.label + "</div>";
1330
- _ref = row.y;
1331
- for (j = _i = 0, _len = _ref.length; _i < _len; j = ++_i) {
1332
- y = _ref[j];
1333
- content += "<div class='morris-hover-point' style='color: " + (this.colorFor(row, j, 'label')) + "'>\n " + this.options.labels[j] + ":\n " + (this.yLabelFormat(y)) + "\n</div>";
1334
- }
1451
+ content = this.options.hoverCallback(index, this.options, content);
1335
1452
  }
1336
1453
  x = this.left + (index + 0.5) * this.width / this.data.length;
1337
1454
  return [content, x];
@@ -1350,7 +1467,9 @@
1350
1467
 
1351
1468
  })(Morris.Grid);
1352
1469
 
1353
- Morris.Donut = (function() {
1470
+ Morris.Donut = (function(_super) {
1471
+
1472
+ __extends(Donut, _super);
1354
1473
 
1355
1474
  Donut.prototype.defaults = {
1356
1475
  colors: ['#0B62A4', '#3980B5', '#679DC6', '#95BBD7', '#B0CCE1', '#095791', '#095085', '#083E67', '#052C48', '#042135'],
@@ -1361,6 +1480,10 @@
1361
1480
 
1362
1481
  function Donut(options) {
1363
1482
  this.select = __bind(this.select, this);
1483
+
1484
+ this.click = __bind(this.click, this);
1485
+
1486
+ var row;
1364
1487
  if (!(this instanceof Morris.Donut)) {
1365
1488
  return new Morris.Donut(options);
1366
1489
  }
@@ -1377,35 +1500,46 @@
1377
1500
  return;
1378
1501
  }
1379
1502
  this.data = options.data;
1503
+ this.values = (function() {
1504
+ var _i, _len, _ref, _results;
1505
+ _ref = this.data;
1506
+ _results = [];
1507
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1508
+ row = _ref[_i];
1509
+ _results.push(parseFloat(row.value));
1510
+ }
1511
+ return _results;
1512
+ }).call(this);
1380
1513
  this.redraw();
1381
1514
  }
1382
1515
 
1383
1516
  Donut.prototype.redraw = function() {
1384
- var C, cx, cy, d, idx, last, max_value, min, next, seg, total, w, x, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _results;
1517
+ 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;
1385
1518
  this.el.empty();
1386
1519
  this.raphael = new Raphael(this.el[0]);
1387
1520
  cx = this.el.width() / 2;
1388
1521
  cy = this.el.height() / 2;
1389
1522
  w = (Math.min(cx, cy) - 10) / 3;
1390
1523
  total = 0;
1391
- _ref = this.data;
1524
+ _ref = this.values;
1392
1525
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1393
- x = _ref[_i];
1394
- total += x.value;
1526
+ value = _ref[_i];
1527
+ total += value;
1395
1528
  }
1396
1529
  min = 5 / (2 * w);
1397
1530
  C = 1.9999 * Math.PI - min * this.data.length;
1398
1531
  last = 0;
1399
1532
  idx = 0;
1400
1533
  this.segments = [];
1401
- _ref1 = this.data;
1402
- for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
1403
- d = _ref1[_j];
1404
- next = last + min + C * (d.value / total);
1405
- seg = new Morris.DonutSegment(cx, cy, w * 2, w, last, next, this.options.colors[idx % this.options.colors.length], this.options.backgroundColor, d, this.raphael);
1534
+ _ref1 = this.values;
1535
+ for (i = _j = 0, _len1 = _ref1.length; _j < _len1; i = ++_j) {
1536
+ value = _ref1[i];
1537
+ next = last + min + C * (value / total);
1538
+ seg = new Morris.DonutSegment(cx, cy, w * 2, w, last, next, this.options.colors[idx % this.options.colors.length], this.options.backgroundColor, idx, this.raphael);
1406
1539
  seg.render();
1407
1540
  this.segments.push(seg);
1408
1541
  seg.on('hover', this.select);
1542
+ seg.on('click', this.click);
1409
1543
  last = next;
1410
1544
  idx += 1;
1411
1545
  }
@@ -1413,20 +1547,20 @@
1413
1547
  this.text2 = this.drawEmptyDonutLabel(cx, cy + 10, this.options.labelColor, 14);
1414
1548
  max_value = Math.max.apply(null, (function() {
1415
1549
  var _k, _len2, _ref2, _results;
1416
- _ref2 = this.data;
1550
+ _ref2 = this.values;
1417
1551
  _results = [];
1418
1552
  for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
1419
- d = _ref2[_k];
1420
- _results.push(d.value);
1553
+ value = _ref2[_k];
1554
+ _results.push(value);
1421
1555
  }
1422
1556
  return _results;
1423
1557
  }).call(this));
1424
1558
  idx = 0;
1425
- _ref2 = this.data;
1559
+ _ref2 = this.values;
1426
1560
  _results = [];
1427
1561
  for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
1428
- d = _ref2[_k];
1429
- if (d.value === max_value) {
1562
+ value = _ref2[_k];
1563
+ if (value === max_value) {
1430
1564
  this.select(idx);
1431
1565
  break;
1432
1566
  }
@@ -1435,20 +1569,21 @@
1435
1569
  return _results;
1436
1570
  };
1437
1571
 
1572
+ Donut.prototype.click = function(idx) {
1573
+ return this.fire('click', idx, this.data[idx]);
1574
+ };
1575
+
1438
1576
  Donut.prototype.select = function(idx) {
1439
- var s, segment, _i, _len, _ref;
1577
+ var row, s, segment, _i, _len, _ref;
1440
1578
  _ref = this.segments;
1441
1579
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1442
1580
  s = _ref[_i];
1443
1581
  s.deselect();
1444
1582
  }
1445
- if (typeof idx === 'number') {
1446
- segment = this.segments[idx];
1447
- } else {
1448
- segment = idx;
1449
- }
1583
+ segment = this.segments[idx];
1450
1584
  segment.select();
1451
- return this.setLabels(segment.data.label, this.options.formatter(segment.data.value, segment.data));
1585
+ row = this.data[idx];
1586
+ return this.setLabels(row.label, this.options.formatter(row.value, row));
1452
1587
  };
1453
1588
 
1454
1589
  Donut.prototype.setLabels = function(label1, label2) {
@@ -1488,20 +1623,20 @@
1488
1623
 
1489
1624
  return Donut;
1490
1625
 
1491
- })();
1626
+ })(Morris.EventEmitter);
1492
1627
 
1493
1628
  Morris.DonutSegment = (function(_super) {
1494
1629
 
1495
1630
  __extends(DonutSegment, _super);
1496
1631
 
1497
- function DonutSegment(cx, cy, inner, outer, p0, p1, color, backgroundColor, data, raphael) {
1632
+ function DonutSegment(cx, cy, inner, outer, p0, p1, color, backgroundColor, index, raphael) {
1498
1633
  this.cx = cx;
1499
1634
  this.cy = cy;
1500
1635
  this.inner = inner;
1501
1636
  this.outer = outer;
1502
1637
  this.color = color;
1503
1638
  this.backgroundColor = backgroundColor;
1504
- this.data = data;
1639
+ this.index = index;
1505
1640
  this.raphael = raphael;
1506
1641
  this.deselect = __bind(this.deselect, this);
1507
1642
 
@@ -1538,7 +1673,9 @@
1538
1673
  var _this = this;
1539
1674
  this.arc = this.drawDonutArc(this.hilight, this.color);
1540
1675
  return this.seg = this.drawDonutSegment(this.path, this.color, this.backgroundColor, function() {
1541
- return _this.fire('hover', _this);
1676
+ return _this.fire('hover', _this.index);
1677
+ }, function() {
1678
+ return _this.fire('click', _this.index);
1542
1679
  });
1543
1680
  };
1544
1681
 
@@ -1550,12 +1687,12 @@
1550
1687
  });
1551
1688
  };
1552
1689
 
1553
- DonutSegment.prototype.drawDonutSegment = function(path, fillColor, strokeColor, hoverFunction) {
1690
+ DonutSegment.prototype.drawDonutSegment = function(path, fillColor, strokeColor, hoverFunction, clickFunction) {
1554
1691
  return this.raphael.path(path).attr({
1555
1692
  fill: fillColor,
1556
1693
  stroke: strokeColor,
1557
1694
  'stroke-width': 3
1558
- }).hover(hoverFunction);
1695
+ }).hover(hoverFunction).click(clickFunction);
1559
1696
  };
1560
1697
 
1561
1698
  DonutSegment.prototype.select = function() {
@@ -0,0 +1,2 @@
1
+ .morris-hover{position:absolute;z-index:1000;}.morris-hover.morris-default-style{border-radius:10px;padding:6px;color:#666;background:rgba(255, 255, 255, 0.8);border:solid 2px rgba(230, 230, 230, 0.8);font-family:sans-serif;font-size:12px;text-align:center;}.morris-hover.morris-default-style .morris-hover-row-label{font-weight:bold;margin:0.25em 0;}
2
+ .morris-hover.morris-default-style .morris-hover-point{white-space:nowrap;margin:0.1em 0;}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morrisjs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-18 00:00:00.000000000 Z
12
+ date: 2013-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -37,6 +37,7 @@ files:
37
37
  - lib/morrisjs-rails/version.rb
38
38
  - lib/morrisjs-rails.rb
39
39
  - vendor/assets/javascripts/morris.js
40
+ - vendor/assets/stylesheets/morris.css
40
41
  - README.md
41
42
  homepage: https://github.com/beanieboi/morrisjs-rails
42
43
  licenses: []