-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathindex.js
143 lines (129 loc) · 3.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React, {Component} from 'react';
import {View, StyleSheet, Platform} from 'react-native';
import {WebView as RNWebView} from 'react-native-webview';
import renderChart from './utils/renderChart';
import {toString} from './utils/utils';
import {index} from './tmp/templates';
import echarts from './lib/echarts.min';
import PropTypes from 'prop-types';
class Echarts extends Component {
constructor(props) {
super(props);
this.chartRef = React.createRef();
this.state = {
data: {},
isFirstLoad: true,
setOption: this.setOption
}
}
static getDerivedStateFromProps(props, state) {
if (state.isFirstLoad) {
return {
isFirstLoad: false
}
} else {
state.setOption(props.option);
return null
}
}
static defaultProps = {
backgroundColor: '#00000000',
onPress: () => {}
}
render() {
return (
<View style={{flexDirection: 'row', width: this.props.width}}>
<View style={{flex: 1, height: this.props.height || 400}}>
<RNWebView
ref={this.chartRef}
originWhitelist={['*']}
useWebKit={true} // ios使用最新webkit内核渲染
allowUniversalAccessFromFileURLs={true}
geolocationEnabled={true}
mixedContentMode={'always'}
renderLoading={this.props.renderLoading || (() => <View style={{backgroundColor: this.props.backgroundColor}} />)} // 设置空View,修复ioswebview闪白
style={{backgroundColor: this.props.backgroundColor}} // 设置背景色透明,修复android闪白
scrollEnabled={false}
onMessage={this._handleMessage}
javaScriptEnabled={true}
injectedJavaScript={renderChart(this.props)}
startInLoadingState={false}
source={{
baseUrl: '',
html: index()
}}
/>
</View>
</View>
);
}
_handleMessage = (event) => {
//event.persist()
if (!event) return null;
const data = JSON.parse(event.nativeEvent.data)
switch (data.types) {
case 'ON_PRESS':
this.props.onPress(JSON.parse(data.payload))
break;
case 'GET_IMAGE':
this.setState({data}, () => {
console.log(this.state.data)
this.emitImg()
})
break;
default:
break;
}
};
setOption = (option, notMerge = false, lazyUpdate = false) => {
let data = {
option: option,
notMerge: notMerge,
lazyUpdate: lazyUpdate
}
const run = `
// alert('optionsChange')
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption(${toString(data.option)},${data.notMerge.toString()},${data.lazyUpdate.toString()});
`
this.chartRef.current.injectJavaScript(run);
}
clear = () => {
const run = `
var myChart = echarts.init(document.getElementById('main'));
myChart.clear()
`
this.chartRef.current.injectJavaScript(run);
}
emitImg = () => {};
getImage = (callback) => {
const run = `
// alert('getimage')
var myChart = echarts.init(document.getElementById('main'));
var base64 = myChart.getDataURL();
window.ReactNativeWebView.postMessage(JSON.stringify({"types":"GET_IMAGE","payload": base64}));
`
this.chartRef.current.injectJavaScript(run);
let self = this;
this.emitImg = function () {
if (self.state.data.types === 'GET_IMAGE') {
let res = !self.state.data.payload ? null : self.state.data.payload;
callback(res)
} else {
callback(null)
}
};
}
componentWillUnmount() {
!!this.timer && clearTimeout(this.timer);
}
}
export {Echarts, echarts};
Echarts.propTypes = {
option: PropTypes.object,
backgroundColor: PropTypes.string,
width: PropTypes.number,
height: PropTypes.number,
renderLoading: PropTypes.func,
onPress: PropTypes.func
}