echarts报错Uncaught (in promise) Error: Initialize failed: invalid dom.

x33g5p2x  于2022-03-29 转载在 其他  
字(0.7k)|赞(0)|评价(0)|浏览(1526)

在echart初始化函数外加个延时就可以:

加上延时会进入异步队列,同步可能获取不到dom

setTimeout(() => {
          init2();
        }, 1);


function init2() {
      var chartDom = document.getElementById("Node2");
      var myChart = echarts.init(chartDom);
      var option;

      option = {
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "0%",
          //   bottom:"5%",
          left: "center",
          textStyle: {
            color: "#ffffff",
          },
        },
        series: [
          {
            // name: "能耗",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "40",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: haoneng.value,
          },
        ],
      };

      option && myChart.setOption(option);
    }

相关文章