解决 echarts 使用setOption重新配置时不清除之前的配置项

解决 echarts 使用setOption重新配置时不清除之前的配置项

问题描述:

直接使用setOption重新配置,以前的配置项会重复合并,造成显示错误

   this.chart.setOption(
        {
          xAxis,
          yAxis,
          series,
          grid
        },
  
      );

解决方法:

使用replaceMerge属性来重新配置,并且为不想重复合并的配置项添加id属性

   this.chart.setOption(
        {
          xAxis,
          yAxis,
          series,
          grid
          // dataset: {
          //   source
          // }
          // height: 1000
        },
        { replaceMerge: ["xAxis", "yAxis", "series", "grid"] }
      );
//创建x轴
function generateXAxis(count) {
  // console.log(count);
  let xAxis = [];
  for (let i = 0; i < count; i++) {
    let id =
      Math.random()
        .toString()
        .substr(3, 3) + Date.now();
    xAxis.push({
      id: `xAxis-${id}`, //添加唯一id
      type: "category",
      gridIndex: i,
      show: i == count - 1
    });
  }
  return xAxis;
}
posted @ 2021-03-17 19:17  baifangzi  阅读(958)  评论(0编辑  收藏  举报