Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

升级兼容 #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
},
"homepage": "https://github.com/somonus/react-native-echarts#readme",
"dependencies": {
"echarts": "3.2.3"
"echarts": "^4.2.0-rc.1"
}
}
18 changes: 9 additions & 9 deletions src/components/Container/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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 {
render() {
return (
<View style={[styles.container, {width: this.props.width}]}>
{this.props.children}
</View>
);
}
render() {
return (
<View style={[styles.container, {width: this.props.width}]}>
{this.props.children}
</View>
);
}
}
59 changes: 22 additions & 37 deletions src/components/Echarts/echarts.min.js

Large diffs are not rendered by default.

70 changes: 33 additions & 37 deletions src/components/Echarts/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
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);
}


componentWillReceiveProps(nextProps) {
if(nextProps.option !== this.props.option) {
this.refs.chart.reload();
// 预防过渡渲染
shouldComponentUpdate(nextProps = {}, nextState) {
const thisProps = this.props || {};
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])) {
return true;
}
}
return false;
}
}

setNewOption(option) {
this.refs.chart.postMessage(JSON.stringify(option));
}

render() {
return (
<View style={{flex: 1, height: this.props.height || 400,}}>
<WebView
ref="chart"
scrollEnabled = {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}
/>
</View>
);
}
render() {
const messageFn = this.props.onMessage || null;
return (
<View style={{ flex: 1, height: this.props.height || 400 }}>
<WebView
ref="chart"
scrollEnabled={false}
scalesPageToFit={false}
injectedJavaScript={renderChart(this.props)}
style={{
height: this.props.height || 400,
}}
onMessage={messageFn}
source={require("./tpl.html")}
/>
</View>
);
}
}
42 changes: 15 additions & 27 deletions src/components/Echarts/renderChart.js
Original file line number Diff line number Diff line change
@@ -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';
return `
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;
}
seen.push(val);
const height = `${props.height || 400}px`;
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'), null, {renderer: 'svg'});
myChart.on('legendselectchanged',function(params){
params = params || {};
if(typeof window.postMessage === 'function'){
window.postMessage(JSON.stringify(params))
}
return val;
});
window.postMessage(paramsString);
});
`
})
myChart.setOption(${JSON.stringify(props.option)});
}, 100)
`;
}
75 changes: 19 additions & 56 deletions src/components/Echarts/tpl.html

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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 {

setNewOption(option) {
this.chart.setNewOption(option);
}
setNewOption(option) {
this.chart.setNewOption(option);
}

render() {
return (
<Container width={this.props.width}>
<Echarts {...this.props} ref={e => this.chart = e}/>
</Container>
);
}
render() {
return (
<Container width={this.props.width}>
<Echarts {...this.props} ref={e => this.chart = e}/>
</Container>
);
}
}
13 changes: 0 additions & 13 deletions src/util/toString.js

This file was deleted.