直接在react中使用原生的方法
// 引入 ECharts 主模块
import echarts from 'echarts/lib/echarts';
// // 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import 'echarts/lib/component/legend'
import 'echarts/lib/chart/pie';
const Model_echarts = (props) => {
let [main , setMain] = useState('')
const option = {
title: {
text: '某站点用户访问来源',
subtext: '纯属虚构',
left: 'center'
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b} : {c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 'left',
data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
series: [
{
name: '访问来源',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
data: [
{ value: 335, name: '直接访问' },
{ value: 310, name: '邮件营销' },
{ value: 234, name: '联盟广告' },
{ value: 135, name: '视频广告' },
{ value: 1548, name: '搜索引擎' }
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
useEffect(()=>{
var node = document.getElementById('main')
setMain(node)
} , [])
// 基于准备好的dom,初始化echarts实例
if(main !== ""){
var myChart = echarts.init(main);
myChart.resize({ height: '400px' })
myChart.setOption(option);
}
// 绘制图表
return ( <div id="main"></div> )
}
使用echarts-for-react控件
对于组件来说,只有一个API——getEchartsInstance(),用来获取Echarts的实例对象。获取到对象后就可以使用任意的Echarts API。
// render the echarts component below with rel
<ReactEcharts ref={(e) => { this.echarts_react = e }}
option={this.getOption()} />
let echarts_instance = this.echarts_react.getEchartsInstance();
// then you can use any API of echarts.
let base64 = echarts_instance.getDataURL();
或者写成函数组件的形式
<ReactEcharts ref={e => echartRef.current = e } option={options} />
let chartInstance = echartRef.current.getEchartsInstance()
chartInstance.resize({ height: '100%' })
// then get the `ReactEcharts` use this.echarts_react