mirror of
https://github.com/morrisjs/morris.js.git
synced 2024-11-13 07:11:12 +01:00
Updating datasets.
Use setData() function to update dataset.
This commit is contained in:
parent
acfec20bcf
commit
68aca2cde4
3 changed files with 83 additions and 65 deletions
|
@ -26,8 +26,32 @@ class Morris.Line
|
||||||
if @options.data is undefined or @options.data.length is 0
|
if @options.data is undefined or @options.data.length is 0
|
||||||
return
|
return
|
||||||
@el.addClass 'graph-initialised'
|
@el.addClass 'graph-initialised'
|
||||||
@precalc()
|
|
||||||
@redraw()
|
# the raphael drawing instance
|
||||||
|
@r = new Raphael(@el[0])
|
||||||
|
|
||||||
|
# Some instance variables for later
|
||||||
|
@pointGrow = Raphael.animation r: @options.pointSize + 3, 25, 'linear'
|
||||||
|
@pointShrink = Raphael.animation r: @options.pointSize, 25, 'linear'
|
||||||
|
@elementWidth = null
|
||||||
|
@elementHeight = null
|
||||||
|
@dirty = false
|
||||||
|
# column hilight events
|
||||||
|
@prevHilight = null
|
||||||
|
@el.mousemove (evt) =>
|
||||||
|
@updateHilight evt.pageX
|
||||||
|
if @options.hideHover
|
||||||
|
@el.mouseout (evt) =>
|
||||||
|
@hilight null
|
||||||
|
touchHandler = (evt) =>
|
||||||
|
touch = evt.originalEvent.touches[0] or evt.originalEvent.changedTouches[0]
|
||||||
|
@updateHilight touch.pageX
|
||||||
|
return touch
|
||||||
|
@el.bind 'touchstart', touchHandler
|
||||||
|
@el.bind 'touchmove', touchHandler
|
||||||
|
@el.bind 'touchend', touchHandler
|
||||||
|
|
||||||
|
@setData(@options.data)
|
||||||
|
|
||||||
# Default configuration
|
# Default configuration
|
||||||
#
|
#
|
||||||
|
@ -72,11 +96,11 @@ class Morris.Line
|
||||||
xLabels: 'auto'
|
xLabels: 'auto'
|
||||||
xLabelFormat: null
|
xLabelFormat: null
|
||||||
|
|
||||||
# Do any necessary pre-processing for a new dataset
|
# Pre-process data
|
||||||
#
|
#
|
||||||
precalc: ->
|
setData: (data, redraw = true) ->
|
||||||
# shallow copy & sort data
|
# shallow copy & sort data
|
||||||
@options.data = @options.data.slice(0)
|
@options.data = data.slice(0)
|
||||||
@options.data.sort (a, b) => (a[@options.xkey] < b[@options.xkey]) - (b[@options.xkey] < a[@options.xkey])
|
@options.data.sort (a, b) => (a[@options.xkey] < b[@options.xkey]) - (b[@options.xkey] < a[@options.xkey])
|
||||||
# extract labels
|
# extract labels
|
||||||
@columnLabels = $.map @options.data, (d) => d[@options.xkey]
|
@columnLabels = $.map @options.data, (d) => d[@options.xkey]
|
||||||
|
@ -133,25 +157,8 @@ class Morris.Line
|
||||||
else
|
else
|
||||||
@precision = 0
|
@precision = 0
|
||||||
|
|
||||||
# Some instance variables for later
|
@dirty = true
|
||||||
@pointGrow = Raphael.animation r: @options.pointSize + 3, 25, 'linear'
|
@redraw() if redraw
|
||||||
@pointShrink = Raphael.animation r: @options.pointSize, 25, 'linear'
|
|
||||||
@elementWidth = null
|
|
||||||
@elementHeight = null
|
|
||||||
# column hilight events
|
|
||||||
@prevHilight = null
|
|
||||||
@el.mousemove (evt) =>
|
|
||||||
@updateHilight evt.pageX
|
|
||||||
if @options.hideHover
|
|
||||||
@el.mouseout (evt) =>
|
|
||||||
@hilight null
|
|
||||||
touchHandler = (evt) =>
|
|
||||||
touch = evt.originalEvent.touches[0] or evt.originalEvent.changedTouches[0]
|
|
||||||
@updateHilight touch.pageX
|
|
||||||
return touch
|
|
||||||
@el.bind 'touchstart', touchHandler
|
|
||||||
@el.bind 'touchmove', touchHandler
|
|
||||||
@el.bind 'touchend', touchHandler
|
|
||||||
|
|
||||||
# Do any size-related calculations
|
# Do any size-related calculations
|
||||||
#
|
#
|
||||||
|
@ -159,7 +166,10 @@ class Morris.Line
|
||||||
w = @el.width()
|
w = @el.width()
|
||||||
h = @el.height()
|
h = @el.height()
|
||||||
|
|
||||||
if @elementWidth != w or @elementHeight != h
|
if @elementWidth != w or @elementHeight != h or @dirty
|
||||||
|
@elementWidth = w
|
||||||
|
@elementHeight = h
|
||||||
|
@dirty = false
|
||||||
# calculate grid dimensions
|
# calculate grid dimensions
|
||||||
@maxYLabelWidth = Math.max(
|
@maxYLabelWidth = Math.max(
|
||||||
@measureText(@yLabelFormat(@options.ymin), @options.gridTextSize).width,
|
@measureText(@yLabelFormat(@options.ymin), @options.gridTextSize).width,
|
||||||
|
@ -197,12 +207,7 @@ class Morris.Line
|
||||||
# Clear and redraw the graph
|
# Clear and redraw the graph
|
||||||
#
|
#
|
||||||
redraw: ->
|
redraw: ->
|
||||||
# remove child elements (get rid of old drawings)
|
@r.clear()
|
||||||
@el.empty()
|
|
||||||
|
|
||||||
# the raphael drawing instance
|
|
||||||
@r = new Raphael(@el[0])
|
|
||||||
|
|
||||||
@calc()
|
@calc()
|
||||||
@drawGrid()
|
@drawGrid()
|
||||||
@drawSeries()
|
@drawSeries()
|
||||||
|
|
79
morris.js
79
morris.js
|
@ -19,6 +19,9 @@
|
||||||
this.transY = __bind(this.transY, this);
|
this.transY = __bind(this.transY, this);
|
||||||
|
|
||||||
this.transX = __bind(this.transX, this);
|
this.transX = __bind(this.transX, this);
|
||||||
|
|
||||||
|
var touchHandler,
|
||||||
|
_this = this;
|
||||||
if (!(this instanceof Morris.Line)) {
|
if (!(this instanceof Morris.Line)) {
|
||||||
return new Morris.Line(options);
|
return new Morris.Line(options);
|
||||||
}
|
}
|
||||||
|
@ -38,8 +41,35 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.el.addClass('graph-initialised');
|
this.el.addClass('graph-initialised');
|
||||||
this.precalc();
|
this.r = new Raphael(this.el[0]);
|
||||||
this.redraw();
|
this.pointGrow = Raphael.animation({
|
||||||
|
r: this.options.pointSize + 3
|
||||||
|
}, 25, 'linear');
|
||||||
|
this.pointShrink = Raphael.animation({
|
||||||
|
r: this.options.pointSize
|
||||||
|
}, 25, 'linear');
|
||||||
|
this.elementWidth = null;
|
||||||
|
this.elementHeight = null;
|
||||||
|
this.dirty = false;
|
||||||
|
this.prevHilight = null;
|
||||||
|
this.el.mousemove(function(evt) {
|
||||||
|
return _this.updateHilight(evt.pageX);
|
||||||
|
});
|
||||||
|
if (this.options.hideHover) {
|
||||||
|
this.el.mouseout(function(evt) {
|
||||||
|
return _this.hilight(null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
touchHandler = function(evt) {
|
||||||
|
var touch;
|
||||||
|
touch = evt.originalEvent.touches[0] || evt.originalEvent.changedTouches[0];
|
||||||
|
_this.updateHilight(touch.pageX);
|
||||||
|
return touch;
|
||||||
|
};
|
||||||
|
this.el.bind('touchstart', touchHandler);
|
||||||
|
this.el.bind('touchmove', touchHandler);
|
||||||
|
this.el.bind('touchend', touchHandler);
|
||||||
|
this.setData(this.options.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
Line.prototype.defaults = {
|
Line.prototype.defaults = {
|
||||||
|
@ -78,10 +108,13 @@
|
||||||
xLabelFormat: null
|
xLabelFormat: null
|
||||||
};
|
};
|
||||||
|
|
||||||
Line.prototype.precalc = function() {
|
Line.prototype.setData = function(data, redraw) {
|
||||||
var d, series_data, touchHandler, ykey, ymax, ymin, _i, _j, _k, _len, _len1, _ref, _ref1, _ref2, _results,
|
var d, series_data, ykey, ymax, ymin, _i, _j, _k, _len, _len1, _ref, _ref1, _ref2, _results,
|
||||||
_this = this;
|
_this = this;
|
||||||
this.options.data = this.options.data.slice(0);
|
if (redraw == null) {
|
||||||
|
redraw = true;
|
||||||
|
}
|
||||||
|
this.options.data = data.slice(0);
|
||||||
this.options.data.sort(function(a, b) {
|
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[_this.options.xkey] < b[_this.options.xkey]) - (b[_this.options.xkey] < a[_this.options.xkey]);
|
||||||
});
|
});
|
||||||
|
@ -158,32 +191,10 @@
|
||||||
} else {
|
} else {
|
||||||
this.precision = 0;
|
this.precision = 0;
|
||||||
}
|
}
|
||||||
this.pointGrow = Raphael.animation({
|
this.dirty = true;
|
||||||
r: this.options.pointSize + 3
|
if (redraw) {
|
||||||
}, 25, 'linear');
|
return this.redraw();
|
||||||
this.pointShrink = Raphael.animation({
|
|
||||||
r: this.options.pointSize
|
|
||||||
}, 25, 'linear');
|
|
||||||
this.elementWidth = null;
|
|
||||||
this.elementHeight = null;
|
|
||||||
this.prevHilight = null;
|
|
||||||
this.el.mousemove(function(evt) {
|
|
||||||
return _this.updateHilight(evt.pageX);
|
|
||||||
});
|
|
||||||
if (this.options.hideHover) {
|
|
||||||
this.el.mouseout(function(evt) {
|
|
||||||
return _this.hilight(null);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
touchHandler = function(evt) {
|
|
||||||
var touch;
|
|
||||||
touch = evt.originalEvent.touches[0] || evt.originalEvent.changedTouches[0];
|
|
||||||
_this.updateHilight(touch.pageX);
|
|
||||||
return touch;
|
|
||||||
};
|
|
||||||
this.el.bind('touchstart', touchHandler);
|
|
||||||
this.el.bind('touchmove', touchHandler);
|
|
||||||
return this.el.bind('touchend', touchHandler);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Line.prototype.calc = function() {
|
Line.prototype.calc = function() {
|
||||||
|
@ -191,7 +202,10 @@
|
||||||
_this = this;
|
_this = this;
|
||||||
w = this.el.width();
|
w = this.el.width();
|
||||||
h = this.el.height();
|
h = this.el.height();
|
||||||
if (this.elementWidth !== w || this.elementHeight !== h) {
|
if (this.elementWidth !== w || this.elementHeight !== h || this.dirty) {
|
||||||
|
this.elementWidth = w;
|
||||||
|
this.elementHeight = h;
|
||||||
|
this.dirty = false;
|
||||||
this.maxYLabelWidth = Math.max(this.measureText(this.yLabelFormat(this.options.ymin), this.options.gridTextSize).width, this.measureText(this.yLabelFormat(this.options.ymax), this.options.gridTextSize).width);
|
this.maxYLabelWidth = Math.max(this.measureText(this.yLabelFormat(this.options.ymin), this.options.gridTextSize).width, this.measureText(this.yLabelFormat(this.options.ymax), this.options.gridTextSize).width);
|
||||||
this.left = this.maxYLabelWidth + this.options.marginLeft;
|
this.left = this.maxYLabelWidth + this.options.marginLeft;
|
||||||
this.width = this.el.width() - this.left - this.options.marginRight;
|
this.width = this.el.width() - this.left - this.options.marginRight;
|
||||||
|
@ -244,8 +258,7 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
Line.prototype.redraw = function() {
|
Line.prototype.redraw = function() {
|
||||||
this.el.empty();
|
this.r.clear();
|
||||||
this.r = new Raphael(this.el[0]);
|
|
||||||
this.calc();
|
this.calc();
|
||||||
this.drawGrid();
|
this.drawGrid();
|
||||||
this.drawSeries();
|
this.drawSeries();
|
||||||
|
|
2
morris.min.js
vendored
2
morris.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue