使用eCharts重复播放动画

时间:2018-12-15 13:44:26

标签: javascript animation echarts

我正在使用eCharts绘制一些散点图。我有几个数据系列,我想要的是每个序列都按照我选择的动画顺序显示。相反,发生的是我选择的第一个动画系列是动画,但随后的动画恢复为默认动画,尽管(据我所知)我仍在为它们定义动画。

以下是我可以做的最短的完整示例...

var chartOptions = function(seriesData) {
  let option = {
    xAxis: {
      min: 0,
      max: 11
    },
    yAxis: {
      min: 0,
      max: 11
    },
    series: [{
      name: "Series 1",
      type: 'scatter',
      data: seriesData,
      color: "red",
      animationDelay: function(idx) {
        return idx * 250;
      },
      animationEasingUpdate: "linear"
    }, ]
  };
  return option;
};

var nextSeries = function(seriesData) {
  let series = {
    series: [{
      name: "Next series",
      type: 'scatter',
      data: seriesData,
      color: "blue",
      animationDelay: function(idx) {
        return idx * 250;
      },
      animationEasingUpdate: "linear"
    }, ]
  };
  return series;
};

var series1 = [
  [1, 1],
  [2, 2],
  [3, 3],
  [4, 4],
  [5, 5]
];
var series2 = [
  [10, 10],
  [9, 9],
  [8, 8],
  [7, 7],
  [6, 6]
];
var series3 = [
  [10, 10],
  [1, 1],
  [8, 8],
  [2, 2],
  [6, 6]
];
var seriesAll = [series1, series2, series3]

var options = chartOptions(series1);
var chart1 = echarts.init(document.getElementById("ChartView"));
chart1.setOption(options);
var i = 0;
var timer = setInterval(function() {
  i++;
  if (i >= seriesAll.length) {
    clearInterval(timer)
  } else {
    var nextOptions = nextSeries(seriesAll[i])
    chart1.setOption(nextOptions);
  };
}, 2000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.1.0/echarts-en.js"></script>


<div id="ChartView" style="height:300px;max-width:300px;">
</div>

1 个答案:

答案 0 :(得分:1)

好吧,您可以先清除所有散点图,然后再绘制下一个系列,如下所示:

chart1.setOption(nextSeries([]));
chart1.setOption(nextOptions);

var chartOptions = function(seriesData) {
    let option = {
        xAxis: {
            min: 0,
            max: 11
        },
        yAxis: {
            min: 0,
            max: 11
        },
        series: [{
            name: "Series 1",
            type: 'scatter',
            data: seriesData,
            color: "red",
            animationDelay: function(idx) {
                return idx * 250;
            },
            animationEasingUpdate: "linear"
        }, ]
    };
    return option;
};

var nextSeries = function(seriesData) {
    let series = {
        series: [{
            name: "Next series",
            type: 'scatter',
            data: seriesData,
            color: "blue",
            animationDelay: function(idx) {
                return idx * 250;
            },
            animationEasingUpdate: "linear"
        }, ]
    };
    return series;
};

var series1 = [
    [1, 1],
    [2, 2],
    [3, 3],
    [4, 4],
    [5, 5]
];
var series2 = [
    [10, 10],
    [9, 9],
    [8, 8],
    [7, 7],
    [6, 6]
];
var series3 = [
    [10, 10],
    [1, 1],
    [8, 8],
    [2, 2],
    [6, 6]
];
var seriesAll = [series1, series2, series3]

var options = chartOptions(series1);
var chart1 = echarts.init(document.getElementById("ChartView"));
chart1.setOption(options);
var i = 0;
var timer = setInterval(function() {
    i++;
    if (i >= seriesAll.length) {
        clearInterval(timer)
    } else {
        var nextOptions = nextSeries(seriesAll[i])
        chart1.setOption(nextSeries([]));
        chart1.setOption(nextOptions);
    };
}, 2000);
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" > </script> 
<script src = "https://cdnjs.cloudflare.com/ajax/libs/echarts/4.1.0/echarts-en.js"> </script>
<div id = "ChartView" style = "height:300px;max-width:300px;" >
</div>