使用React Native绘制图表(使用ECharts)
import React, { Component } from 'react';
import { View } from 'react-native';
import echarts from 'echarts';
class EChartsComponent extends Component {
constructor(props) {
super(props);
this.state = {
option: {
title: {
text: 'ECharts 示例图表'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
}
};
}
componentDidMount() {
this.renderChart();
}
renderChart = () => {
const chart = echarts.init(this.chartContainer);
chart.setOption(this.state.option);
}
render() {
return (
<View
style={{ height: 300, width: '100%' }}
ref={(c) => { this.chartContainer = c; }}
/>
);
}
}
export default EChartsComponent;
这段代码演示了如何在React Native中使用ECharts绘制一个简单的条形图。首先,在constructor
中定义了图表的配置option
,包括标题、工具提示、图例、X轴、Y轴和一系列数据。在componentDidMount
中,我们通过引用组件的DOM节点初始化ECharts实例,并应用配置好的option
来绘制图表。
评论已关闭