Ecahrts结合React开发
1.在react项目中安装Echarts
npm install --save echarts-for-react
2.按需引入模块
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/line';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
3.初始化项目
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption(
title: {...},
grid: {...},
xAxis: {...},
grid: {...},
yAxis: {...},
series: [
{
...
}
],
});
4.入门示例
import React, { Component } from 'react';
import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/line';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
class EchartsTest extends Component {
componentDidMount() {
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption({
title: { text: 'ECharts 入门示例' },
tooltip: {},
xAxis: {
data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'line',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
render() {
return (
<div id="main" style={{ width: 400, height: 400 }}></div>
);
}
}
export default EchartsTest;