react-native-echarts/src/components/Echarts/index.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-10-21 07:05:57 +00:00
import React, { Component } from 'react';
2019-08-17 05:29:01 +00:00
import { View, StyleSheet, Platform } from 'react-native';
2016-10-21 07:54:29 +00:00
import renderChart from './renderChart';
2016-10-21 07:05:57 +00:00
import echarts from './echarts.min';
2019-08-17 05:29:01 +00:00
import WebView from "react-native-webview";
import getTpl from "./tpl" ;
2016-10-21 07:05:57 +00:00
export default class App extends Component {
constructor(props) {
super(props);
this.setNewOption = this.setNewOption.bind(this);
}
2019-09-21 10:44:55 +00:00
2016-11-03 02:43:21 +00:00
componentWillReceiveProps(nextProps) {
if(nextProps.option !== this.props.option) {
2019-09-21 10:44:55 +00:00
// this.refs.chart.reload();
if(Platform.OS === 'android'){
this.refs.chart.reload();
}else {
this.setNewOption(nextProps.option) ;
}
2016-11-03 02:43:21 +00:00
}
}
2019-09-21 10:44:55 +00:00
shouldComponentUpdate() {
return false ;
}
2016-11-03 02:43:21 +00:00
setNewOption(option) {
this.refs.chart.postMessage(JSON.stringify(option));
}
2016-10-21 07:05:57 +00:00
render() {
return (
2019-09-21 10:44:55 +00:00
<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'
}}
originWhitelist={['*']}
source={{ html:getTpl() }}
onMessage={event => this.props.onPress ? this.props.onPress(JSON.parse(event.nativeEvent.data)) : null}
/>
</View>
2016-10-21 07:05:57 +00:00
);
}
}