动态拼接series,为什么会报series.type should be specified?

作者站长头像
站长
· 阅读数 13
mounted() {
    this.$nextTick(() => {
      this.initChart()

    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // Use axis to trigger tooltip
            type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
          }
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          max: 24,
          interval: 2,
          splitNumber: 12//分割成几段
        },
        yAxis: {
          type: 'category',
          data: this.code
        },
        series: function () {
          let serie = []
          let colorArr = ['#208c23', '#ffd608', '#8b0100', '#636363'];
          for (var i = 0; i < this.list.length; i++) {
            let colors = '';
            if(list[i].paramValue=='running'){
              colors = colorArr[1];
            } else if(list[i].paramValue=='idle'){
              colors = colorArr[2];
            }else if(list[i].paramValue=='alert'){
              colors = colorArr[3];
            }else if(list[i].paramValue=='shutdown'){
              colors = colorArr[4];
            }
            serie.push(
              {
                name: list[i].paramValue,
                type: 'bar',
                itemStyle: {
                  color: colors
                },
                data:list[i].hours
              }
            )
          }
          return serie;
        }

      },true)
    }
  }

各位大佬们,我在动态拼接series时,报了以下这个错误,但是按网上说的,我加了type,请教一下各位动态拼接series,为什么会报series.type should be specified?

回复
1个回答
avatar
test
2024-07-16

原因是this.chart.setOption(option, true) 里面的 option 是一个 js 对象,里面的 series 也是一个js 对象,而不是一个 function, 你可以先计算出 series 的值,在赋值。

const series = {} // 计算 series
this.chart.setOption({/*其他配置项*/, series}, true)
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容