一、npm安装 echarts 、echarts-for-react:
npm install echarts
--save
npm install
--save echarts
-for-react
二、引入模块(这里最好按需引入):
import echarts
from 'echarts/lib/echarts'
import 'echarts/lib/chart/bar';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import 'echarts/lib/component/legend';
import 'echarts/lib/component/markPoint';
import ReactEcharts
from 'echarts-for-react';
三、完整代码:
import React
,{Component
}from 'react'
import {Card
} from 'antd';
import echarts
from 'echarts/lib/echarts'
import 'echarts/lib/chart/pie';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import 'echarts/lib/component/legend';
import 'echarts/lib/component/markPoint';
import ReactEcharts
from 'echarts-for-react';
export default class Pie extends Component {
getOption = ()=>{
let option
= {
title
: {
text
: '某站点用户访问来源',
subtext
: '纯属虚构',
left
: 'left'
},
tooltip
: {
trigger
: 'item',
formatter
: '{a} <br/>{b} : {c} ({d}%)'
},
legend
: {
orient
: 'vertical',
left
: 'right',
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)'
}
}
}
]
};
return option
;
};
render() {
return (
<Card title
="我是标题">
<ReactEcharts option
={this.getOption()}/>
</Card
>
)
}
}
运行结果:
转载请注明原文地址:https://tech.qufami.com/read-7301.html