/* * ECharts 组件基础部分 * 传入 option 和渲染方式 renderer * */ import React, { PureComponent } from "react"; import * as echarts from "echarts"; import "zrender/lib/svg/svg"; import { debounce } from "./index"; // 一个节流函数 export default class Chart extends PureComponent { constructor(props) { super(props); this.state = { width: "100%", height: "100%", }; this.chart = null; } async componentDidMount() { // 初始化图表 await this.initChart(this.el); // 将传入的配置(包含数据)注入 this.setOption(this.props.option); // 监听屏幕缩放,重新绘制 echart 图表 window.addEventListener("resize", debounce(this.resize, 100)); } componentDidUpdate() { // 每次更新组件都重置 this.setOption(this.props.option); } componentWillUnmount() { // 组件卸载前卸载图表 this.dispose(); } render() { const { width, height } = this.state; return (