import React, { createRef, useEffect, useRef, useState } from 'react'
// import {getArticleList} from '../api/blog'
import * as echarts from 'echarts';
import { Button, Drawer } from 'antd';
export default function Test02() {
const options = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data: ['销量']
},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [
{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
};
const chartRef = useRef<HTMLInputElement>(null);
const [chart, setChart] = useState<echarts.ECharts>();
const handleResize = () => {
chart?.resize();
};
const initChart = () => {
if (chart) {
window.removeEventListener("resize", handleResize);
chart?.dispose();
}
const newChart = echarts?.init(chartRef?.current as HTMLElement);
newChart.setOption(options, true);
window.addEventListener("resize", handleResize);
setChart(newChart);
};
const [open, setOpen] = useState(false);
const showDrawer = () => {
setOpen(true);
setTimeout(()=>
{
initChart()
}
, 0)
};
const onClose = () => {
setOpen(false);
};
return (
<div>
<Button type="primary" onClick={showDrawer}>
Open
</Button>
<Drawer title="Basic Drawer" placement="right" onClose={onClose} open={open}>
<div ref={chartRef} style={{ height: 400 }}></div>
</Drawer>
</div>
)
}
E chart 初始化