echarts 渲染图表出错 Cannot read properties of undefined (reading ‘$echarts‘)

[Vue warn]: Error in mounted hook: "TypeError: Cannot read properties of undefined (reading '$echarts')"

 

报错的原因是没能指向 echarts。

<script>
export default {
  name: '',
  components: {},
  props: {
    id: {
      type: String,
      default: 'radar3',
    },
    width: {
      type: String,
      default: '100vw',
    },
    height: {
      type: String,
      default: '50vh',
    },
  },
  data() {
    return {};
  },
  computed: {},
  watch: {},
  created() {
    //
  },
  mounted() {
    //
    this.myCharts();
  },
  methods: {
    myCharts() {
      const myChart = this.$echarts.init(document.getElementById(this.id));

      const data = [80, 70, 30, 85, 25];
      const indicatorname = [
        '政治品德修养',
        '社会发展能力',
        '美劳素质拓展',
        '身心健康发展',
        '学业发展能力',
      ];
      const maxdata = [100, 100, 100, 100, 100];

      function contains(arrays, obj) {
        let i = arrays.length;
        while (i--) {
          if (arrays[i] === obj) {
            return i;
          }
        }
        return false;
      }

      const indicator = [];
      for (let i = 0; i < indicatorname.length; i++) {
        indicator.push({
          name: indicatorname[i],
          max: maxdata[i],
        });
      }

      function innerdata(i) {
        const innerData = [];
        for (let j = 0; j < data.length; j++) {
          innerData.push(100 - 20 * i);
        }
        return innerData;
      }

      const that = this;       // 1.重新指向

      const optionData = getData(data);

      function getData(Data) {
        const res = {
          series: [
            {
              type: 'radar',
              symbolSize: 10,
              symbol: 'circle',
              areaStyle: {
                color: '#39B2FF',
                opacity: 0.3,
              },
              lineStyle: {
                color: that.$echarts.graphic.LinearGradient(   // 2.指向
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: '#00A2FF',
                    },
                    {
                      offset: 1,
                      color: '#0060FF',
                    },
                  ],
                  false,
                ),
                width: 3,
              },
              itemStyle: {
                color: '#fff ',
                borderColor: that.$echarts.graphic.LinearGradient(   //3. 指向
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: '#00DEFF',
                    },
                    {
                      offset: 1,
                      color: '#1598FF',
                    },
                  ],
                  false,
                ),
                borderWidth: 4,
                opacity: 1,
              },
              label: {
                show: false,
              },
              data: [
                {
                  value: Data,
                },
              ],
              z: 100,
            },
          ],
        };
        for (let i = 0; i < Data.length; i++) {
          res.series.push({
            type: 'radar',
            data: [
              {
                value: innerdata(i),
              },
            ],
            symbol: 'none',
            lineStyle: {
              width: 0,
            },
            itemStyle: {
              color: '#fff',
            },
            areaStyle: {
              color: '#fff',
              shadowColor: 'rgba(14,122,191,0.15)',
              shadowBlur: 30,
              shadowOffsetY: 20,
            },
          });
        }
        return res;
      }

      const option = {
        backgroundColor: '#fff',
        tooltip: {
          formatter() {
            let html = '';
            for (let i = 0; i < data.length; i++) {
              html += `${indicatorname[i]} : ${data[i]}%<br>`;
            }
            return html;
          },
        },
        radar: {
          indicator,
          splitArea: {
            show: true,
            areaStyle: {
              color: '#fff',
              shadowColor: 'rgba(14,122,191,0.19)',
              shadowBlur: 30,
              shadowOffsetY: 20,
            },
          },
          splitLine: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          name: {
            textStyle: {
              rich: {
                a: {
                  fontSize: '17',
                  color: '#333',
                  align: 'left',
                  lineHeight: '30',
                  fontWeight: 'bold',
                },
                b: {
                  fontSize: '15',
                  color: '#666',
                  align: 'left',
                },
              },
            },

            formatter(params, index) {
              const i = contains(indicatorname, params);
              const percent = (data[i] / 100) * 100;
              // return "{a|" + percent + "%}\n" + "{b|" + params + "}";
              return `{a|${percent}%}\n"{b|${params}}`;
            },
          },
        },
        series: optionData.series,
      };
      myChart.setOption(option);
    },
  },
};
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在您的问题中,出现了一个错误信息 "Cannot read properties of undefined (reading 'getAttribute')",这是因为在使用`getAttribute`方法时,对应的属性未定义。此方法是一个函数,它接受一个参数,即您要查询的属性的名称。中的情况,这个错误可能是由于在调用`echarts.init()`时,DOM元素尚未挂载导致的。可以尝试将`echarts.init()`函数放在mounted钩子函数中而不是created钩子函数中,或者在渲染时使用`$nextTick`将echarts渲染函数包裹起来。这些方法可能有助于解决您遇到的问题。 此外,在不显示echarts图表时,我们还可以使用echarts官方提供的方法来销毁echarts实例以释放内存并提高网页性能,例如使用`echartsInstance.dispose()`。 这个方法可以帮助您在不需要显示图表时释放内存资源。 综上所述,要解决您遇到的错误信息 "Cannot read properties of undefined (reading 'getAttribute')",您可以检查一下调用`getAttribute`方法的属性是否正确定义,并尝试将`echarts.init()`函数放在正确的钩子函数中,或者使用`echartsInstance.dispose()`来销毁echarts实例。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [js中的getAttribute方法使用示例](https://download.csdn.net/download/weixin_38617602/13044321)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [“TypeError: Cannot read properties of undefined (reading ‘getAttribute‘)](https://blog.csdn.net/qq_56580072/article/details/127551260)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值