Skip to content

Commit 78133f7

Browse files
committed
添加一个主动刷新option的方法,界面不会闪动
1 parent 9ee6a04 commit 78133f7

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/components/Echarts/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@ import renderChart from './renderChart';
44
import echarts from './echarts.min';
55

66
export default class App extends Component {
7+
8+
constructor(props) {
9+
super(props);
10+
this.setNewOption = this.setNewOption.bind(this);
11+
}
12+
13+
714
componentWillReceiveProps(nextProps) {
815
if(nextProps.option !== this.props.option) {
916
this.refs.chart.reload();
1017
}
1118
}
1219

20+
setNewOption(option) {
21+
this.refs.chart.postMessage(JSON.stringify(option));
22+
}
23+
1324
render() {
1425
return (
1526
<View style={{flex: 1, height: this.props.height || 400,}}>

src/components/Echarts/renderChart.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export default function renderChart(props) {
99
document.getElementById('main').style.width = "${width}";
1010
var myChart = echarts.init(document.getElementById('main'));
1111
myChart.setOption(${toString(props.option)});
12+
window.document.addEventListener('message', function(e) {
13+
var option = JSON.parse(e.data);
14+
myChart.setOption(option);
15+
});
1216
myChart.on('click', function(params) {
1317
var seen = [];
1418
var paramsString = JSON.stringify(params, function(key, val) {

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import { WebView, View } from 'react-native';
33
import { Container, Echarts } from './components'
44

55
export default class App extends Component {
6+
7+
setNewOption(option) {
8+
this.chart.setNewOption(option);
9+
}
10+
611
render() {
712
return (
813
<Container width={this.props.width}>
9-
<Echarts {...this.props} />
14+
<Echarts {...this.props} ref={e => this.chart = e}/>
1015
</Container>
1116
);
1217
}

0 commit comments

Comments
 (0)