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

基礎(chǔ)使用
native-echarts的使用方法基本和網(wǎng)頁端的Echarts使用方法一致。組件主要有三個屬性:
- option (object):圖表的相關(guān)配置和數(shù)據(jù)。詳見文檔: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:['銷量']
},
xAxis: {
data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
return (
<Echarts option={option} height={300} />
);
}
}
AppRegistry.registerComponent('app', () => app);

通過上面的代碼我們就可以在React Native里面顯示一個圖表了。但是我們會發(fā)現(xiàn)顯示的字體會偏小。我們需要適配下移動端的字體,我們需要在native-echarts文件下找到tpl.html文件,在head里面增加下面一句代碼:
<meta name="viewport" content="width=device-width, initial-scale=1"> 這樣字體大小就顯示正常了。
進(jìn)階使用:
在使用圖表時,如果我們需要使用圖表的點擊事件,比如點擊柱狀圖的某個柱子,獲取到該柱子的信息,再跳轉(zhuǎn)到詳情頁面,這該怎么做呢?組件本身是沒有這個屬性的,需要我們自己修改下代碼,傳遞下消息。具體代碼如下:
首先我們需要在renderChart.js文件中把需要的數(shù)據(jù)注入并傳遞出來(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>
);
}
}
最后在使用圖表的頁面中,修改下代碼來接受傳遞過來的消息:
<Echarts option={option} height={height} width={theme.screenWidth} handleMessage={this.handleMessage} />
在handleMessage方法中就可以寫自己的邏輯來處理傳遞過來數(shù)據(jù)了。
打包:
如果就這樣打包的話,IOS是可以正常打包并顯示的。但是在android端打包時會出錯。
解決方法:
將index.js中的代碼:source={require('./tpl.html')}修改為:
source= {Platform.OS === 'ios' ? require('./tpl.html') : { uri: 'file:///android_asset/tpl.html' }}
同時將tpl.html文件拷貝到安卓項目下面的app/src/main/assets文件夾中。
在執(zhí)行完react-native bundle命令后,需要手動將資源文件res/drawable-mdpi中生成的tpl.html文件刪除,再執(zhí)行cd android && ./gradlew assembleRelease命令,這樣就能成功打包了。
Q1
當(dāng)數(shù)據(jù)量比較大的時候,x軸的數(shù)據(jù)不顯示。這個是echarts自己的一個功能,解決辦法是設(shè)置xAxis-axisLabel-interval為0即可。
Q2
面積折線圖中面積顏色“不正“,也就是說和設(shè)置的顏色對不上。這個可能是react-native-echarts組件封裝的問題,解決辦法是設(shè)置areaStyle-normal-shadowColor為'#ffffff',同理可以設(shè)置lineStyle等。
Q3
打release包的時候報錯了,
\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打包的時候把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'這個地址加載,ios在項目目錄下建個文件夾,把tpl文件放里面去,使用uri:'文件名/tpl'加載。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
React封裝高階組件實現(xiàn)路由權(quán)限的控制詳解
這篇文章主要介紹了React封裝高階組件實現(xiàn)路由權(quán)限的控制,在React中,為了實現(xiàn)安全可靠的路由權(quán)限控制,可以通過多種方式來確保只有經(jīng)過授權(quán)的用戶才能訪問特定路徑下的資源,下面來介紹封裝高階組件控制的方法,需要的朋友可以參考下2025-02-02
聊聊ant?design?charts?獲取后端接口數(shù)據(jù)展示問題
今天在做項目的時候遇到幾個讓我很頭疼的問題,一個是通過后端接口成功訪問并又返回數(shù)據(jù),但拿不到數(shù)據(jù)值。其二是直接修改state中的data,console中數(shù)組發(fā)生變化但任然數(shù)據(jù)未顯示,這篇文章主要介紹了ant?design?charts?獲取后端接口數(shù)據(jù)展示,需要的朋友可以參考下2022-05-05
react中axios結(jié)合后端實現(xiàn)GET和POST請求方式
這篇文章主要介紹了react中axios結(jié)合后端實現(xiàn)GET和POST請求方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
適用于React?Native?旋轉(zhuǎn)木馬應(yīng)用程序介紹
這篇文章主要介紹了適用于React?Native?旋轉(zhuǎn)木馬應(yīng)用程序介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

