diff --git a/src/index.js b/src/index.js index ae3357a..e1e51b1 100644 --- a/src/index.js +++ b/src/index.js @@ -23,6 +23,12 @@ export default function createG2(__operation) { const { data: newData, width: newWidth, height: newHeight, plotCfg: newPlotCfg } = newProps; const { data: oldData, width: oldWidth, height: oldHeight, plotCfg: oldPlotCfg } = this.props; + if (oldData.length === 0 && newData.length > 0) { + this.destroyChart(); + this.initChart(newProps); + return; + } + if (newPlotCfg !== oldPlotCfg) { console.warn('plotCfg 不支持修改'); } @@ -40,8 +46,7 @@ export default function createG2(__operation) { } componentWillUnmount() { - this.chart.destroy(); - this.chart = null; + this.destroyChart(); this.chartId = null; } @@ -58,6 +63,13 @@ export default function createG2(__operation) { this.chart = chart; } + destroyChart() { + if (this.chart) { + this.chart.destroy(); + } + this.chart = null; + } + render() { return (
); }