Line chart working again.

This commit is contained in:
Olly Smith 2012-10-22 07:56:40 +01:00
parent 3b5ff1a47e
commit f6b1cfe7a0
5 changed files with 43 additions and 34 deletions

View File

@ -25,7 +25,7 @@ var day_data = [
{"period": "2012-09-15", "licensed": 3201, "sorned": 656}, {"period": "2012-09-15", "licensed": 3201, "sorned": 656},
{"period": "2012-09-10", "licensed": 3215, "sorned": 622} {"period": "2012-09-10", "licensed": 3215, "sorned": 622}
]; ];
line = Morris.Line({ Morris.Line({
element: 'graph', element: 'graph',
data: day_data, data: day_data,
xkey: 'period', xkey: 'period',

View File

@ -56,10 +56,14 @@ class Morris.Grid extends Morris.EventEmitter
# #
setData: (data, redraw = true) -> setData: (data, redraw = true) ->
# shallow copy data # shallow copy data
@options.data = data.slice() @options.data = $.map data, (row) =>
if @parseTime if @options.parseTime
$.extend {'__T': Morris.parseDate(row[@options.xkey])}, row
else
$.extend {}, row
if @options.parseTime
@options.data = @options.data.sort (a, b) => @options.data = @options.data.sort (a, b) =>
(a[@options.xkey] < b[@options.xkey]) - (b[@options.xkey] < a[@options.xkey]) (a['__T'] > b['__T']) - (b['__T'] > a['__T'])
# extract series data # extract series data
@series = [] @series = []
@ -74,9 +78,9 @@ class Morris.Grid extends Morris.EventEmitter
@series.push seriesData @series.push seriesData
# extract labels / x values # extract labels / x values
@columnLabels = $.map @options.data, (d) => d[@options.xkey] @columnLabels = $.map @options.data, (row) => row[@options.xkey]
if @options.parseTime if @options.parseTime
@xvals = $.map @columnLabels, (x) -> Morris.parseDate x @xvals = $.map @options.data, (row) -> row['__T']
if @options.dateFormat if @options.dateFormat
@columnLabels = $.map @xvals, (d) => @options.dateFormat d @columnLabels = $.map @xvals, (d) => @options.dateFormat d
else else

View File

@ -79,7 +79,7 @@ class Morris.Line extends Morris.Grid
@drawXAxis() @drawXAxis()
@drawSeries() @drawSeries()
@drawHover() @drawHover()
@hilight(if @options.hideHover then null else 0) @hilight(if @options.hideHover then null else @options.data.length - 1)
# draw the x-axis labels # draw the x-axis labels
# #
@ -111,8 +111,8 @@ class Morris.Line extends Morris.Grid
for l in Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelFormat) for l in Morris.labelSeries(@xmin, @xmax, @width, @options.xLabels, @options.xLabelFormat)
drawLabel(l[0], l[1]) drawLabel(l[0], l[1])
else else
for i in [0..@columnLabels.length] for i in [0...@columnLabels.length]
labelText = @columnLabels[@columnLabels.length - i - 1] labelText = @columnLabels[i]
drawLabel(labelText, i) drawLabel(labelText, i)
# draw the data series # draw the data series
@ -120,7 +120,7 @@ class Morris.Line extends Morris.Grid
# @private # @private
drawSeries: -> drawSeries: ->
for i in [@seriesCoords.length-1..0] for i in [@seriesCoords.length-1..0]
coords = @seriesCoords[i] coords = $.map @seriesCoords[i], (c) -> c
smooth = @options.smooth is true or smooth = @options.smooth is true or
$.inArray(@options.ykeys[i], @options.smooth) > -1 $.inArray(@options.ykeys[i], @options.smooth) > -1
if coords.length > 1 if coords.length > 1
@ -250,10 +250,9 @@ class Morris.Line extends Morris.Grid
# @private # @private
updateHilight: (x) => updateHilight: (x) =>
x -= @el.offset().left x -= @el.offset().left
for hoverIndex in [@hoverMargins.length..0] for hoverIndex in [0...@hoverMargins.length]
if hoverIndex == 0 || @hoverMargins[hoverIndex - 1] > x break if @hoverMargins[hoverIndex] > x
@hilight hoverIndex @hilight hoverIndex
break
# @private # @private
colorForSeries: (index) -> colorForSeries: (index) ->

View File

@ -334,10 +334,18 @@
if (redraw == null) { if (redraw == null) {
redraw = true; redraw = true;
} }
this.options.data = data.slice(); this.options.data = $.map(data, function(row) {
if (this.parseTime) { if (_this.options.parseTime) {
return $.extend({
'__T': Morris.parseDate(row[_this.options.xkey])
}, row);
} else {
return $.extend({}, row);
}
});
if (this.options.parseTime) {
this.options.data = this.options.data.sort(function(a, b) { this.options.data = this.options.data.sort(function(a, b) {
return (a[_this.options.xkey] < b[_this.options.xkey]) - (b[_this.options.xkey] < a[_this.options.xkey]); return (a['__T'] > b['__T']) - (b['__T'] > a['__T']);
}); });
} }
this.series = []; this.series = [];
@ -362,12 +370,12 @@
} }
this.series.push(seriesData); this.series.push(seriesData);
} }
this.columnLabels = $.map(this.options.data, function(d) { this.columnLabels = $.map(this.options.data, function(row) {
return d[_this.options.xkey]; return row[_this.options.xkey];
}); });
if (this.options.parseTime) { if (this.options.parseTime) {
this.xvals = $.map(this.columnLabels, function(x) { this.xvals = $.map(this.options.data, function(row) {
return Morris.parseDate(x); return row['__T'];
}); });
if (this.options.dateFormat) { if (this.options.dateFormat) {
this.columnLabels = $.map(this.xvals, function(d) { this.columnLabels = $.map(this.xvals, function(d) {
@ -681,7 +689,7 @@
this.drawXAxis(); this.drawXAxis();
this.drawSeries(); this.drawSeries();
this.drawHover(); this.drawHover();
return this.hilight(this.options.hideHover ? null : 0); return this.hilight(this.options.hideHover ? null : this.options.data.length - 1);
}; };
Line.prototype.drawXAxis = function() { Line.prototype.drawXAxis = function() {
@ -714,8 +722,8 @@
} }
} else { } else {
_results1 = []; _results1 = [];
for (i = _j = 0, _ref1 = this.columnLabels.length; 0 <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = 0 <= _ref1 ? ++_j : --_j) { for (i = _j = 0, _ref1 = this.columnLabels.length; 0 <= _ref1 ? _j < _ref1 : _j > _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
labelText = this.columnLabels[this.columnLabels.length - i - 1]; labelText = this.columnLabels[i];
_results1.push(drawLabel(labelText, i)); _results1.push(drawLabel(labelText, i));
} }
return _results1; return _results1;
@ -725,7 +733,9 @@
Line.prototype.drawSeries = function() { Line.prototype.drawSeries = function() {
var c, circle, coords, i, path, smooth, _i, _j, _ref, _ref1, _results; var c, circle, coords, i, path, smooth, _i, _j, _ref, _ref1, _results;
for (i = _i = _ref = this.seriesCoords.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) { for (i = _i = _ref = this.seriesCoords.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; i = _ref <= 0 ? ++_i : --_i) {
coords = this.seriesCoords[i]; coords = $.map(this.seriesCoords[i], function(c) {
return c;
});
smooth = this.options.smooth === true || $.inArray(this.options.ykeys[i], this.options.smooth) > -1; smooth = this.options.smooth === true || $.inArray(this.options.ykeys[i], this.options.smooth) > -1;
if (coords.length > 1) { if (coords.length > 1) {
path = this.createPath(coords, this.bottom, smooth); path = this.createPath(coords, this.bottom, smooth);
@ -877,18 +887,14 @@
}; };
Line.prototype.updateHilight = function(x) { Line.prototype.updateHilight = function(x) {
var hoverIndex, _i, _ref, _results; var hoverIndex, _i, _ref;
x -= this.el.offset().left; x -= this.el.offset().left;
_results = []; for (hoverIndex = _i = 0, _ref = this.hoverMargins.length; 0 <= _ref ? _i < _ref : _i > _ref; hoverIndex = 0 <= _ref ? ++_i : --_i) {
for (hoverIndex = _i = _ref = this.hoverMargins.length; _ref <= 0 ? _i <= 0 : _i >= 0; hoverIndex = _ref <= 0 ? ++_i : --_i) { if (this.hoverMargins[hoverIndex] > x) {
if (hoverIndex === 0 || this.hoverMargins[hoverIndex - 1] > x) {
this.hilight(hoverIndex);
break; break;
} else {
_results.push(void 0);
} }
} }
return _results; return this.hilight(hoverIndex);
}; };
Line.prototype.colorForSeries = function(index) { Line.prototype.colorForSeries = function(index) {

2
morris.min.js vendored

File diff suppressed because one or more lines are too long