1. width 和 height 設(shè)置的不對的時候,會出現(xiàn)邊框線如圖所示:

image
解決辦法:Echarts/index.js文件中
<WebView
ref="chart"
scrollEnabled = {false}
injectedJavaScript = {renderChart(this.props)}
style={{
height: this.props.height || 400,
backgroundColor: 'rgba(0,0,0,0)', // ***********add by yourself*******************
}}
automaticallyAdjustContentInsets={true}
source={require('./tpl.html')}
/>
2. 折線區(qū)域圖設(shè)置為漸變色,去掉節(jié)點,變成平滑
series: [{
name: 'Clouds',
type: 'line',
// 變成平滑線
smooth:true,
//這句就是去掉點的
symbol:'none',
data: [5, 7, 13, 20, 30],
// 設(shè)置為填充樣式,把這句去掉的話,即為普通的折線圖
areaStyle: {
normal: {
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0, color: 'rgba(6,81,222, 0.5)' // 100% 處的顏色
},
{
offset: 0.5, color: 'rgba(6,81,222, 0.2)' // 50% 處的顏色
},
{
offset: 1, color: 'rgba(253,253,253,0.17)' // 0% 處的顏色
},
],
}
}
},
// 設(shè)置為漸變色, 此處設(shè)置的是針對網(wǎng)頁的
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(
0, 0, 0, 1,
[
{offset: 0, color: 'red'},
{offset: 0.5, color: 'pink'},
{offset: 1, color: '#ddd'}
]
)
}
}
}]
3. 環(huán)圖某一個項,設(shè)置為漸變色
data:[
{value:335, name:'正面消息', selected: true, itemStyle: normal: {
color: {
type: 'linear',
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0, color: 'rgba(250,79,125,1)' // 100% 處的顏色
},
{
offset: 1, color: 'rgba(244,39,71,1)' // 0% 處的顏色
},
]
},
}},
{value:180, name:'負(fù)面消息', itemStyle: {normal: 'green'}},
]
4.android 打包后,所有的圖表顯示不出來
1./node_modules/native-echarts/src/components/Echarts/ 目錄下的tpl.html 拷貝一份
2./android/app/src/main 創(chuàng)建 assets文件夾
3.把第一步拷貝的文件放到第二步創(chuàng)建的assets文件夾下
4.進(jìn)入Echarts文件(/node_modules/native-echarts/src/components/Echarts/index) 把WebView的source改為
source={{uri: 'file:///android_asset/tpl.html'}}
5.修改圖例顏色
react-native-echarts 修改圖例顏色首先要修改itemStyle的顏色
6.解決頻繁刷新
進(jìn)入Echarts文件(/node_modules/native-echarts/src/components/Echarts/index)
shouldComponentUpdate(nextProps, nextState) {
const thisProps = this.props || {}
nextProps = nextProps || {}
if (Object.keys(thisProps).length !== Object.keys(nextProps).length) {
return true
}
for (const key in nextProps) {
if (JSON.stringify(thisProps[key]) != JSON.stringify(nextProps[key])) {
// console.log('props', key, thisProps[key], nextProps[key])
return true
}
}
return false
}
componentWillReceiveProps(nextProps) {
if(nextProps.option !== this.props.option) {
// 解決數(shù)據(jù)改變時頁面閃爍的問題
this.refs.chart.injectJavaScript(renderChart(nextProps,false))
}
}
修改WebView的屬性injectedJavaScript
injectedJavaScript = {renderChart(this.props,true)}
進(jìn)入renderChart文件(/node_modules/native-echarts/src/components/Echarts/renderChart)
export default function renderChart(props, isFirst) {
const height = props.height || 400;
if (isFirst) {
return `
document.getElementById('main').style.height = "${height}px";
myChart = echarts.init(document.getElementById('main'));
myChart.setOption(${toString(props.option)});
`
} else {
return `
document.getElementById('main').style.height = "${height}px";
myChart.setOption(${toString(props.option)});
`
}
7. 折線圖節(jié)點添加點擊事件
進(jìn)入renderChart文件(/node_modules/native-echarts/src/components/Echarts/renderChart)
添加
myChart.on('click' , function (params) {
window.postMessage(params.dataIndex)
});
添加后
if (isFirst) {
return `
document.getElementById('main').style.height = "${height}px";
myChart = echarts.init(document.getElementById('main'));
myChart.setOption(${toString(props.option)});
myChart.on('click' , function (params) {
window.postMessage(params.dataIndex)
});
`
} else {
return `
document.getElementById('main').style.height = "${height}px";
myChart.setOption(${toString(props.option)});
myChart.on('click' , function (params) {
window.postMessage(params.dataIndex)
});
`
}
進(jìn)入Echart文件(/node_modules/native-echarts/src/components/Echarts/index)
WebView添加屬性
onMessage={this.onMessage}
添加事件
// 添加點擊事件
onMessage = (event) => {
if (this.props.onNodePress) {
console.log(event.nativeEvent)
this.props.onNodePress(event.nativeEvent.data)
}
}
最終修改后
進(jìn)入Echarts文件(/node_modules/native-echarts/src/components/Echarts/index)
import React, { Component } from 'react';
import { WebView, View, StyleSheet,Platform } from 'react-native';
import renderChart from './renderChart';
import renderChartNoFirst from './renderChart'
import echarts from './echarts.min';
export default class App extends Component {
// 預(yù)防過渡渲染
shouldComponentUpdate(nextProps, nextState) {
const thisProps = this.props || {}
nextProps = nextProps || {}
if (Object.keys(thisProps).length !== Object.keys(nextProps).length) {
return true
}
for (const key in nextProps) {
if (JSON.stringify(thisProps[key]) != JSON.stringify(nextProps[key])) {
// console.log('props', key, thisProps[key], nextProps[key])
return true
}
}
return false
}
componentWillReceiveProps(nextProps) {
if(nextProps.option !== this.props.option) {
// 解決數(shù)據(jù)改變時頁面閃爍的問題
this.refs.chart.injectJavaScript(renderChart(nextProps,false))
}
}
// 添加點擊事件
onMessage = (event) => {
if (this.props.onNodePress) {
console.log(event.nativeEvent)
this.props.onNodePress(event.nativeEvent.data)
}
}
render() {
if (Platform.OS == 'android'){
return (
<View style={{flex: 1, height: this.props.height || 400,}}>
<WebView
ref="chart"
scrollEnabled = {false}
injectedJavaScript = {renderChart(this.props,true)}
onMessage={this.onMessage}
style={{
height: this.props.height || 400,
backgroundColor: 'rgba(0,0,0,0)',
}}
//source={require('./tpl.html')}
// 解決安卓打包不顯示問題
source={{uri: 'file:///android_asset/tpl.html'}}
/>
</View>
);
}else{
return (
<View style={{flex: 1, height: this.props.height || 400,}}>
<WebView
ref="chart"
scrollEnabled = {false}
scalesPageToFit={false}
injectedJavaScript = {renderChart(this.props,true)}
onMessage={this.onMessage}
style={{
height: this.props.height || 400,
backgroundColor: 'rgba(0,0,0,0)',
}}
source={require('./tpl.html')}
/>
</View>
);
}
}
}
進(jìn)入renderChart文件(/node_modules/native-echarts/src/components/Echarts/renderChart)
import echarts from './echarts.min';
import toString from '../../util/toString';
var myChart = null;
export default function renderChart(props, isFirst) {
const height = props.height || 400;
if (isFirst) {
return `
document.getElementById('main').style.height = "${height}px";
myChart = echarts.init(document.getElementById('main'));
myChart.setOption(${toString(props.option)});
myChart.on('click' , function (params) {
window.postMessage(params.dataIndex)
});
`
} else {
return `
document.getElementById('main').style.height = "${height}px";
myChart.setOption(${toString(props.option)});
myChart.on('click' , function (params) {
window.postMessage(params.dataIndex)
});
`
}
}
使用
onChartNodePress(data) {
console.log(data)
this.setState({
selectedNodeIndex: data,
})
}
<Echarts option={option} width={width} height={177}
onNodePress={(data) => this.onChartNodePress(data)}
/>
android 雙擊圖表會縮小
/node_modules/native-echarts/src/components/Echarts/index.js
Line 24
scalesPageToFit={false}
替換為
scalesPageToFit={Platform.OS === 'android'}