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"
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -1,40 +1,36 @@
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) {
if (JSON.stringify(thisProps[key]) !== JSON.stringify(nextProps[key])) {
componentWillReceiveProps(nextProps) { return true;
if(nextProps.option !== this.props.option) {
this.refs.chart.reload();
} }
} }
return false;
setNewOption(option) {
this.refs.chart.postMessage(JSON.stringify(option));
} }
render() { render() {
const messageFn = this.props.onMessage || null;
return ( return (
<View style={{flex: 1, height: this.props.height || 400,}}> <View style={{ flex: 1, height: this.props.height || 400 }}>
<WebView <WebView
ref="chart" ref="chart"
scrollEnabled={false} scrollEnabled={false}
scalesPageToFit={false}
injectedJavaScript={renderChart(this.props)} injectedJavaScript={renderChart(this.props)}
style={{ style={{
height: this.props.height || 400, height: this.props.height || 400,
backgroundColor: this.props.backgroundColor || 'transparent'
}} }}
scalesPageToFit={Platform.OS !== 'ios'} onMessage={messageFn}
source={require('./tpl.html')} source={require("./tpl.html")}
onMessage={event => this.props.onPress ? this.props.onPress(JSON.parse(event.nativeEvent.data)) : null}
/> />
</View> </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 `
setTimeout(()=>{
document.getElementById('main').style.height = "${height}"; document.getElementById('main').style.height = "${height}";
document.getElementById('main').style.width = "${width}"; document.getElementById('main').style.width = "${width}";
var myChart = echarts.init(document.getElementById('main')); var myChart = echarts.init(document.getElementById('main'), null, {renderer: 'svg'});
myChart.setOption(${toString(props.option)}); myChart.on('legendselectchanged',function(params){
window.document.addEventListener('message', function(e) { params = params || {};
var option = JSON.parse(e.data); if(typeof window.postMessage === 'function'){
myChart.setOption(option); 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); })
} myChart.setOption(${JSON.stringify(props.option)});
return val; }, 100)
}); `;
window.postMessage(paramsString);
});
`
} }

File diff suppressed because one or more lines are too long

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;
}