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
*.sh
logs/
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",
"dependencies": {
"echarts": "3.2.3"
"echarts": "^4.2.0-rc.1"
}
}

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { View } from 'react-native';
import React, {Component} from 'react';
import {View} from 'react-native';
import styles from '../../style';
export default class App extends Component {

File diff suppressed because one or more lines are too long

View File

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

View File

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

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
import React, { Component } from 'react';
import { WebView, View } from 'react-native';
import { Container, Echarts } from './components'
import React, {Component} from 'react';
import {WebView, View} from 'react-native';
import {Container, Echarts} from './components'
export default class App extends Component {

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