javascript - 如何动画莫里斯条形图?

标签 javascript jquery morris.js

我正在尝试为莫里斯条形图设置动画,显示了莫里斯条形图,但我希望每个条形都有动画和不同的颜色。我的代码是:

success:function(response){
                    $('body').css('cursor', 'default');
                    if(response.status == 'success'){
                        var productValueCountList=response.productcountlist;
                        $('#productCount-bar').empty();
                        if(productValueCountList=='')
                        {vex.dialog.alert("No record found")
                            return false;
                        }
                        Morris.Bar({

                            element: 'productCount-bar',

                            data:productValueCountList,
                            xkey: 'productName',
                            ykeys: ['productCount'],
                            labels: ['Number of Product'],
                            barRatio: 0.3,
                            barSizeRatio:0.3,
                            xLabelAngle:25,
                            //seriesColors:['#85802b', '#00749F', '#73C774', '#C7754C'],
                            // barColors: ["#B21516", "#1531B2", "#1AB244", "#B29215"],
                            hideHover: 'auto'


                        });

在上面的代码中,productcountlist 是 JSON 数组。 请帮助我或给我另一个解决方案。

最佳答案

通过Raphael js的animate函数可以给morris图表添加动画,但是需要对代码做很多改动。

主要思想是我们需要创建一条路径,该路径将与 Morris 计算出的路径绑定(bind)。

我在下面展示了我是如何使用 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,感兴趣的可以看这里:https://pierresh.github.io/morris.js/

关于javascript - 如何动画莫里斯条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441502/

相关文章:

javascript - 如何注入(inject) app.run 函数中使用的模拟 Angular 服务?

javascript - 加载网页后如何触发javascript函数(不使用表单和提交按钮)

javascript - 无法从 VALUE_STRING 中反序列化 java.util.List 的实例

javascript - 未捕获的 ReferenceError : $ is not defined when using $(document). 就绪

javascript - 在 Firefox 中使用 morris.js 脚本不再回答任何错误

javascript - 我如何在react js中访问nextjs中_app.js文件中的上下文

Javascript 数组分组

javascript - 从 HTML 字符串中提取脚本标签

javascript - morris.js 未找到图形容器元素

morris.js - X 标签上的重复值