React Native使用百度Echarts顯示圖表的示例代碼
Echarts是百度推出的免費(fèi)開(kāi)源的圖表組件,功能豐富,涵蓋各行業(yè)圖表。相信很多同學(xué)在網(wǎng)頁(yè)端都使用過(guò)。今天我就來(lái)介紹下在React Native中如何使用Echarts來(lái)顯示各種圖表。
首先需要在我們的React Native項(xiàng)目中安裝native-echarts組件,該組件是兼容IOS和安卓雙平臺(tái)的。
安裝
npm install native-echarts --save
安裝完成后在node_modules文件夾下會(huì)多出一個(gè)文件夾叫native-echarts。
目錄結(jié)構(gòu)如下圖所示:

基礎(chǔ)使用
native-echarts的使用方法基本和網(wǎng)頁(yè)端的Echarts使用方法一致。組件主要有三個(gè)屬性:
- option (object):圖表的相關(guān)配置和數(shù)據(jù)。詳見(jiàn)文檔:ECharts Documentation
- width (number):圖表的寬度,默認(rèn)值是外部容器的寬度。
- height (number) :圖表的高度,默認(rèn)值是400。
示例代碼:
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:['銷(xiāo)量']
},
xAxis: {
data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
},
yAxis: {},
series: [{
name: '銷(xiāo)量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
return (
<Echarts option={option} height={300} />
);
}
}
AppRegistry.registerComponent('app', () => app);

通過(guò)上面的代碼我們就可以在React Native里面顯示一個(gè)圖表了。但是我們會(huì)發(fā)現(xiàn)顯示的字體會(huì)偏小。我們需要適配下移動(dòng)端的字體,我們需要在native-echarts文件下找到tpl.html文件,在head里面增加下面一句代碼:
<meta name="viewport" content="width=device-width, initial-scale=1"> 這樣字體大小就顯示正常了。
進(jìn)階使用:
在使用圖表時(shí),如果我們需要使用圖表的點(diǎn)擊事件,比如點(diǎn)擊柱狀圖的某個(gè)柱子,獲取到該柱子的信息,再跳轉(zhuǎn)到詳情頁(yè)面,這該怎么做呢?組件本身是沒(méi)有這個(gè)屬性的,需要我們自己修改下代碼,傳遞下消息。具體代碼如下:
首先我們需要在renderChart.js文件中把需要的數(shù)據(jù)注入并傳遞出來(lái)(window.postMessage):
import echarts from './echarts.min';
import toString from '../../util/toString';
export default function renderChart(props) {
const height = props.height || 400;
const width = props.width || 568;
return `
document.getElementById('main').style.height = "${height}px";
document.getElementById('main').style.width = "${width}px";
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption(${toString(props.option)});
myChart.on('click', function (params) {
var message = {};
message.event='click';
message.seriesName = params.seriesName;
message.name = params.name;
window.postMessage(JSON.stringify(message));
});
`
}
然后在index.js中做處理(handleMessage):
import React, { Component } from 'react';
import { WebView, View, StyleSheet, Platform } from 'react-native';
import renderChart from './renderChart';
import echarts from './echarts.min';
export default class App extends Component {
componentWillReceiveProps(nextProps) {
if(JSON.stringify(nextProps.option) !== JSON.stringify(this.props.option)) {
this.refs.chart.reload();
}
}
handleMessage = (evt) => {
const message = JSON.parse(evt.nativeEvent.data)
this.props.handleMessage(message);
}
render() {
return (
<View style={{flex: 1, height: this.props.height,width: this.props.width }}>
<WebView
ref="chart"
scrollEnabled = {false}
injectedJavaScript = {renderChart(this.props)}
style={{
height: this.props.height|| 400,
width: this.props.width || 568,
}}
onMessage={this.handleMessage}
source={require('./tpl.html')}
/>
</View>
);
}
}
最后在使用圖表的頁(yè)面中,修改下代碼來(lái)接受傳遞過(guò)來(lái)的消息:
<Echarts option={option} height={height} width={theme.screenWidth} handleMessage={this.handleMessage} />
在handleMessage方法中就可以寫(xiě)自己的邏輯來(lái)處理傳遞過(guò)來(lái)數(shù)據(jù)了。
打包:
如果就這樣打包的話(huà),IOS是可以正常打包并顯示的。但是在android端打包時(shí)會(huì)出錯(cuò)。
解決方法:
將index.js中的代碼:source={require('./tpl.html')}修改為:
source= {Platform.OS === 'ios' ? require('./tpl.html') : { uri: 'file:///android_asset/tpl.html' }}
同時(shí)將tpl.html文件拷貝到安卓項(xiàng)目下面的app/src/main/assets文件夾中。
在執(zhí)行完react-native bundle命令后,需要手動(dòng)將資源文件res/drawable-mdpi中生成的tpl.html文件刪除,再執(zhí)行cd android && ./gradlew assembleRelease命令,這樣就能成功打包了。
Q1
當(dāng)數(shù)據(jù)量比較大的時(shí)候,x軸的數(shù)據(jù)不顯示。這個(gè)是echarts自己的一個(gè)功能,解決辦法是設(shè)置xAxis-axisLabel-interval為0即可。
Q2
面積折線(xiàn)圖中面積顏色“不正“,也就是說(shuō)和設(shè)置的顏色對(duì)不上。這個(gè)可能是react-native-echarts組件封裝的問(wèn)題,解決辦法是設(shè)置areaStyle-normal-shadowColor為'#ffffff',同理可以設(shè)置lineStyle等。
Q3
打release包的時(shí)候報(bào)錯(cuò)了,
\android\app\src\main\res\drawable-mdpi\node_modules_nativeecharts_src_components_echarts_tpl.html
Error:Error: The file name must end with .xml or .png
原因:
release打包的時(shí)候把node_modules_nativeecharts_src_components_echarts_tpl.html打到了drawable下,這是不行的,要放到assets下。
解決辦法是
另外,release版本只能使用uri加載資源,android把tpl.html文件放在android/app/src/main/assets文件里,使用uri:'file:///android_asset/tpl.html'這個(gè)地址加載,ios在項(xiàng)目目錄下建個(gè)文件夾,把tpl文件放里面去,使用uri:'文件名/tpl'加載。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ReactNative實(shí)現(xiàn)Toast的示例
這篇文章主要介紹了ReactNative實(shí)現(xiàn)Toast的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
解決React報(bào)錯(cuò)Invalid hook call
這篇文章主要為大家介紹了React報(bào)錯(cuò)Invalid hook call解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React路由的history對(duì)象的插件history的使用解讀
這篇文章主要介紹了React路由的history對(duì)象的插件history的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
React+valtio響應(yīng)式狀態(tài)管理
Valtio是一個(gè)很輕量級(jí)的響應(yīng)式狀態(tài)管理庫(kù),使用外部狀態(tài)代理去驅(qū)動(dòng)React視圖來(lái)更新,本文主要介紹了React+valtio響應(yīng)式狀態(tài)管理,感興趣的可以了解一下2023-12-12
React前端解鏈表數(shù)據(jù)結(jié)構(gòu)示例詳解
這篇文章主要為大家介紹了React前端解鏈表數(shù)據(jù)結(jié)構(gòu)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

