1.安装及简单使用
1.1 安装
npm install echarts --save
安装完成后会放在 node_modules 目录下,我们可以直接在项目代码中使用 require(‘echarts’)
1.2 简单使用
柱形图
var echarts = require('echarts')
...
componentDidMount() {
this.echartsShow()
}
echartsShow(){
let option = {
color:['#ff7f50'], // 配置默认色板
title: { // 图表标题
x:'right', // 标题在x轴显示的位置(left\right\center\**px\...)
y:'top', // 标题在y轴显示的位置(top\bottom\center\**px\...)
text: '第一个 ECharts 实例'
},
backgroundColor: 'gray', // 背景色板
textStyle: {
fontSize: 18,
fontWeight: '700',
color: 'black' // 主标题文字颜色(x、y轴 文本样式)
},
tooltip: {},
legend: {},
xAxis: {
data: ['笔记本','键盘','鼠标','显示器','手机','iPad']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
}
echarts.init(document.getElementById('main')).setOption(option);
}
render() {
return (
<div style={{display:'flex'}}>
<div id='main' style={{width:'600px',height:'400px',margin:'100px auto'}}/>
</div>
)
}
效果如图:
饼图
componentDidMount() {
this.echartsShow()
}
echartsShow(){
let option = {
title: {
x:'left',
y:'bottom',
text: '第一个 ECharts 实例'
},
textStyle: {
fontSize: 18,
fontWeight: '700',
color: 'black'
},
series : [
{
name: '访问来源',
type: 'pie', // 设置图表类型为饼图
radius: '55%', // 饼图的半径,外半径为可视区尺寸(容器高宽中较小一项)的 55% 长度。
data:[ // 数据数组,name 为数据项名称,value 为数据项值
{value:235, name:'笔记本'},
{value:274, name:'键盘'},
{value:310, name:'鼠标'},
{value:335, name:'耳机'},
{value:400, name:'ipad'}
]
}
]
}
echarts.init(document.getElementById('main')).setOption(option);
}
render() {
return (
<div style={{display:'flex'}}>
<div id='main' style={{width:'600px',height:'400px',margin:'100px auto'}}/>
</div>
)
}