This commit is contained in:
Veveue 2018-09-27 11:37:51 +00:00 committed by GitHub
commit 5cbc886697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 130 additions and 195 deletions

14
.gitignore vendored
View File

@ -1,2 +1,12 @@
/node_modules logs/
*.sh npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
run/
.DS_Store
*.sw*
*.un~

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -26,6 +26,6 @@
}, },
"homepage": "https://github.com/somonus/react-native-echarts#readme", "homepage": "https://github.com/somonus/react-native-echarts#readme",
"dependencies": { "dependencies": {
"echarts": "3.2.3" "echarts": "^4.2.0-rc.1"
} }
} }

View File

@ -1,13 +1,13 @@
import React, { Component } from 'react'; import React, {Component} from 'react';
import { View } from 'react-native'; import {View} from 'react-native';
import styles from '../../style'; import styles from '../../style';
export default class App extends Component { export default class App extends Component {
render() { render() {
return ( return (
<View style={[styles.container, {width: this.props.width}]}> <View style={[styles.container, {width: this.props.width}]}>
{this.props.children} {this.props.children}
</View> </View>
); );
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -1,42 +1,38 @@
import React, { Component } from 'react'; import React, { Component } from "react";
import { WebView, View, StyleSheet, Platform } from 'react-native'; import { WebView, View } from "react-native";
import renderChart from './renderChart'; import renderChart from "./renderChart";
import echarts from './echarts.min';
export default class App extends Component { export default class App extends Component {
// 预防过渡渲染
constructor(props) { shouldComponentUpdate(nextProps = {}, nextState) {
super(props); const thisProps = this.props || {};
this.setNewOption = this.setNewOption.bind(this); if (Object.keys(thisProps).length !== Object.keys(nextProps).length) {
} return true;
}
for (const key in nextProps) {
componentWillReceiveProps(nextProps) { if (JSON.stringify(thisProps[key]) !== JSON.stringify(nextProps[key])) {
if(nextProps.option !== this.props.option) { return true;
this.refs.chart.reload(); }
}
return false;
} }
}
setNewOption(option) { render() {
this.refs.chart.postMessage(JSON.stringify(option)); const messageFn = this.props.onMessage || null;
} return (
<View style={{ flex: 1, height: this.props.height || 400 }}>
render() { <WebView
return ( ref="chart"
<View style={{flex: 1, height: this.props.height || 400,}}> scrollEnabled={false}
<WebView scalesPageToFit={false}
ref="chart" injectedJavaScript={renderChart(this.props)}
scrollEnabled = {false} style={{
injectedJavaScript = {renderChart(this.props)} height: this.props.height || 400,
style={{ }}
height: this.props.height || 400, onMessage={messageFn}
backgroundColor: this.props.backgroundColor || 'transparent' source={require("./tpl.html")}
}} />
scalesPageToFit={Platform.OS !== 'ios'} </View>
source={require('./tpl.html')} );
onMessage={event => this.props.onPress ? this.props.onPress(JSON.parse(event.nativeEvent.data)) : null} }
/>
</View>
);
}
} }

View File

@ -1,30 +1,18 @@
import echarts from './echarts.min';
import toString from '../../util/toString';
export default function renderChart(props) { export default function renderChart(props) {
const height = `${props.height || 400}px`; const height = `${props.height || 400}px`;
const width = props.width ? `${props.width}px` : 'auto'; const width = props.width ? `${props.width}px` : "auto";
return ` return `
document.getElementById('main').style.height = "${height}"; setTimeout(()=>{
document.getElementById('main').style.width = "${width}"; document.getElementById('main').style.height = "${height}";
var myChart = echarts.init(document.getElementById('main')); document.getElementById('main').style.width = "${width}";
myChart.setOption(${toString(props.option)}); var myChart = echarts.init(document.getElementById('main'), null, {renderer: 'svg'});
window.document.addEventListener('message', function(e) { myChart.on('legendselectchanged',function(params){
var option = JSON.parse(e.data); params = params || {};
myChart.setOption(option); if(typeof window.postMessage === 'function'){
}); window.postMessage(JSON.stringify(params))
myChart.on('click', function(params) {
var seen = [];
var paramsString = JSON.stringify(params, function(key, val) {
if (val != null && typeof val == "object") {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
} }
return val; })
}); myChart.setOption(${JSON.stringify(props.option)});
window.postMessage(paramsString); }, 100)
}); `;
`
} }

File diff suppressed because one or more lines are too long

View File

@ -1,18 +1,18 @@
import React, { Component } from 'react'; import React, {Component} from 'react';
import { WebView, View } from 'react-native'; import {WebView, View} from 'react-native';
import { Container, Echarts } from './components' import {Container, Echarts} from './components'
export default class App extends Component { export default class App extends Component {
setNewOption(option) { setNewOption(option) {
this.chart.setNewOption(option); this.chart.setNewOption(option);
} }
render() { render() {
return ( return (
<Container width={this.props.width}> <Container width={this.props.width}>
<Echarts {...this.props} ref={e => this.chart = e}/> <Echarts {...this.props} ref={e => this.chart = e}/>
</Container> </Container>
); );
} }
} }

View File

@ -1,13 +0,0 @@
export default function toString(obj) {
let result = JSON.stringify(obj, function(key, val) {
if (typeof val === 'function') {
return `~--demo--~${val}~--demo--~`;
}
return val;
});
do {
result = result.replace('\"~--demo--~', '').replace('~--demo--~\"', '').replace(/\\n/g, '').replace(/\\\"/g,"\"");//最后一个replace将release模式中莫名生成的\"转换成"
} while (result.indexOf('~--demo--~') >= 0);
return result;
}