react-native-echarts是react native結合百度echart的圖表,集成的一個圖表插件。
github地址:https://github.com/zhangxinagjunHJH/react-native-echarts
echart地址:http://echarts.baidu.com/examples/index.html 可以看到官方API很強大,圖表種類非常多
按照官網所寫,app在模擬器上運行正常,但是打包時出現圖表不顯示問題,下面先介紹按照和使用方法,再介紹解決問題方案。
- 安裝命令:
npm install native-echarts --save
- 頁面代碼:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import Echarts from 'native-echarts';
export default class app extends Component {
render() {
const option = {
title: {
text: 'ECharts demo'
},
tooltip: {},
legend: {
data:['銷量']
},
xAxis: {
data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
return (
<Echarts option={option} height={300} />
);
}
}
AppRegistry.registerComponent('app', () => app);
安裝后,可在模擬器上運行,顯示沒問題,但是在真機上一試,顯示不出圖表。
- 解決問題的方法:
把“\node_modules\native-echarts\src\components\Echarts\tpl.html”文件復制一份放在“android/app/src/main/assets”文件里,如果“android/app/src/main”下沒有“assets”文件,就建一個。修改后,在
android模擬器上運行就正常顯示圖表了。
