Merge pull request #86 from RebelBIrd/master

添加一个主动刷新option的方法
This commit is contained in:
Arron Zhu 2018-02-22 15:13:40 +08:00 committed by GitHub
commit 3767deeae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -4,12 +4,23 @@ import renderChart from './renderChart';
import echarts from './echarts.min'; import echarts from './echarts.min';
export default class App extends Component { export default class App extends Component {
constructor(props) {
super(props);
this.setNewOption = this.setNewOption.bind(this);
}
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if(nextProps.option !== this.props.option) { if(nextProps.option !== this.props.option) {
this.refs.chart.reload(); this.refs.chart.reload();
} }
} }
setNewOption(option) {
this.refs.chart.postMessage(JSON.stringify(option));
}
render() { render() {
return ( return (
<View style={{flex: 1, height: this.props.height || 400,}}> <View style={{flex: 1, height: this.props.height || 400,}}>

View File

@ -9,6 +9,10 @@ export default function renderChart(props) {
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'));
myChart.setOption(${toString(props.option)}); 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) { myChart.on('click', function(params) {
var seen = []; var seen = [];
var paramsString = JSON.stringify(params, function(key, val) { var paramsString = JSON.stringify(params, function(key, val) {

View File

@ -3,10 +3,15 @@ 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) {
this.chart.setNewOption(option);
}
render() { render() {
return ( return (
<Container width={this.props.width}> <Container width={this.props.width}>
<Echarts {...this.props} /> <Echarts {...this.props} ref={e => this.chart = e}/>
</Container> </Container>
); );
} }