echarts dataZoom 组件报Cannot read properties of undefined (reading ‘type‘)

文章讲述了在Vue项目中使用ECharts库创建柱状图时,遇到dataZoom组件拖拽不生效的问题,通过将myChart变量改为var或let类型解决了控制台报错,并实现了图表的联动交互。
摘要由CSDN通过智能技术生成

需求要做一个可以拖拽范围的柱状图,效果如下,希望拖动滑块,柱状图联动交互。
在这里插入图片描述
我在使用dataZoom组件进行拖拽的时候,控制台报错,且拖动柱状图无反应。
在这里插入图片描述
查了一些资料,原因是我使用了this.myChart来接收数据,应该使用let或者var定义myChart,就不会出现错误了。
错误代码如下:

data() {
    return {
      myChart: null
    }
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      this.myChart = echarts.init(document.getElementById('chart'));
      const option = this.formatBarOptions()
      this.myChart.setOption(option)
    },
    formatBarOptions () {
      const dataCount = 10;
      const data = this.generateData(dataCount);
      console.log(data)
      const option = {
        // title: {
        //   text: echarts.format.addCommas(dataCount) + ' Data',
        //   left: 10
        // },
        // toolbox: {
        //   feature: {
        //     dataZoom: {
        //       yAxisIndex: false
        //     },
        //     saveAsImage: {
        //       pixelRatio: 2
        //     }
        //   }
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          top: 10,
          bottom: 90
        },
        dataZoom: [
          {
            type: 'slider',
            start: 10,
            end: 60,
            handleSize: 30, // 控制手柄的尺寸 number | string
            height: 15, // 控制滑动条高度 number | string
            handleStyle: {
              borderCap: 'butt'
            }
          }
        ],
        xAxis: {
          data: data.categoryData,
          silent: false,
          splitLine: {
            show: false
          },
          splitArea: {
            show: false
          }
        },
        yAxis: {
          show: false,
          splitArea: {
            show: false
          }
        },
        series: [
          {
            type: 'bar',
            data: data.valueData,
            // Set `large` for large data amount
            large: true
          }
        ]
      }
      return option;
    },
    generateData (count) {
      let baseValue = Math.random() * 1000;
      let time = +new Date(2011, 0, 1);
      let smallBaseValue;
      function next(idx) {
        smallBaseValue =
          idx % 30 === 0
            ? Math.random() * 700
            : smallBaseValue + Math.random() * 500 - 250;
        baseValue += Math.random() * 20 - 10;
        return Math.max(0, Math.round(baseValue + smallBaseValue) + 3000);
      }
      const categoryData = [];
      const valueData = [];
      for (let i = 0; i < count; i++) {
        categoryData.push(`${2024 - i}`);
        valueData.push(next(i).toFixed(2));
        time += 1000;
      }
      return {
        categoryData: categoryData.reverse(),
        valueData: valueData
      };
    }
  }

更改myChart提到外面用var定义,OK,不报错了。

import { defineComponent } from 'vue';
import * as echarts from 'echarts';
import { TooltipComponent, TitleComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';

echarts.use([TooltipComponent, TitleComponent, CanvasRenderer]);
var myChart = null;
export default defineComponent({
  name: 'ChartBar',
  data() {
    return {
      myChart: null
    }
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      myChart = echarts.init(document.getElementById('chart'));
      const option = this.formatBarOptions()
      myChart.setOption(option)
    },
    formatBarOptions () {
      const dataCount = 10;
      const data = this.generateData(dataCount);
      console.log(data)
      const option = {
        // title: {
        //   text: echarts.format.addCommas(dataCount) + ' Data',
        //   left: 10
        // },
        // toolbox: {
        //   feature: {
        //     dataZoom: {
        //       yAxisIndex: false
        //     },
        //     saveAsImage: {
        //       pixelRatio: 2
        //     }
        //   }
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          top: 10,
          bottom: 90
        },
        dataZoom: [
          {
            type: 'slider',
            start: 10,
            end: 60,
            handleSize: 30, // 控制手柄的尺寸 number | string
            height: 15, // 控制滑动条高度 number | string
            handleStyle: {
              borderCap: 'butt'
            }
          }
        ],
        xAxis: {
          data: data.categoryData,
          silent: false,
          splitLine: {
            show: false
          },
          splitArea: {
            show: false
          }
        },
        yAxis: {
          show: false,
          splitArea: {
            show: false
          }
        },
        series: [
          {
            type: 'bar',
            data: data.valueData,
            // Set `large` for large data amount
            large: true
          }
        ]
      }
      return option;
    },
    generateData (count) {
      let baseValue = Math.random() * 1000;
      let time = +new Date(2011, 0, 1);
      let smallBaseValue;
      function next(idx) {
        smallBaseValue =
          idx % 30 === 0
            ? Math.random() * 700
            : smallBaseValue + Math.random() * 500 - 250;
        baseValue += Math.random() * 20 - 10;
        return Math.max(0, Math.round(baseValue + smallBaseValue) + 3000);
      }
      const categoryData = [];
      const valueData = [];
      for (let i = 0; i < count; i++) {
        categoryData.push(`${2024 - i}`);
        valueData.push(next(i).toFixed(2));
        time += 1000;
      }
      return {
        categoryData: categoryData.reverse(),
        valueData: valueData
      };
    }
  }
});

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

misstianyun

打赏1元鼓励作者

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值