可以通过 Raphael js 的 animate 函数向莫里斯图添加动画,但需要对代码进行许多更改。
主要思想是我们需要创建一个straight将与 Morris 计算的路径绑定的路径。
下面我展示了我如何使用 Coffee 脚本:
drawLinePath: (path, lineColor, lineIndex) ->
straightPath = ''
for row, ii in @data
if straightPath == ''
straightPath = 'M'+row._x+','+@transY(@ymin)
else
straightPath += ','+row._x+','+@transY(@ymin)
rPath = @raphael.path(straightPath)
.attr('stroke', lineColor)
.attr('stroke-width', this.lineWidthForSeries(lineIndex))
do (rPath, path) =>
rPath.animate {path}, 500, '<>'
下面是生成的 JavaScript:
Line.prototype.drawLinePath = function(path, lineColor, lineIndex) {
var ii, rPath, row, straightPath, _i, _len, _ref,
_this = this;
straightPath = '';
_ref = this.data;
for (ii = _i = 0, _len = _ref.length; _i < _len; ii = ++_i) {
row = _ref[ii];
if (straightPath === '') {
straightPath = 'M' + row._x + ',' + this.transY(this.ymin);
} else {
straightPath += ',' + row._x + ',' + this.transY(this.ymin);
}
}
rPath = this.raphael.path(straightPath).attr('stroke', lineColor).attr('stroke-width', this.lineWidthForSeries(lineIndex));
return (function(rPath, path) {
return rPath.animate({
path: path
}, 500, '<>');
})(rPath, path);
};
由于我也需要这个功能,所以我对Morris做了一个fork a,有兴趣的可以在这里查看:https://pierresh.github.io/morris.js/ https://pierresh.github.io/morris.js/