-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighchartsReactNative.js
146 lines (125 loc) · 4.71 KB
/
HighchartsReactNative.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
144
145
146
import React from 'react';
import {
WebView,
Text,
View,
Dimensions,
StyleSheet,
Platform
} from 'react-native';
const win = Dimensions.get('window');
const path = '../highcharts-files/';
const highchartsLayout = (Platform.OS == 'ios') ? require('../highcharts-layout/index.html') : { uri: 'file:///android_asset/highcharts-layout/index.html' };
export default class HighchartsReactNative extends React.PureComponent {
constructor(props) {
super(props);
// extract width and height from user styles
const userStyles = StyleSheet.flatten(this.props.styles);
this.state = {
width: userStyles.width || win.width,
height: userStyles.height || win.height,
chartOptions: this.props.options,
};
// create script tag and apply all references
this.addHighchartsScripts = this.addHighchartsScripts.bind(this);
// catch rotation event
Dimensions.addEventListener('change', () => {
this.setState({
width: userStyles.width || Dimensions.get('window').width,
height: userStyles.height || Dimensions.get('window').height
});
});
}
componentDidUpdate() {
// send options for chart.update() as string to webview
this.webView.postMessage(
this.serialize(this.props.options, true)
);
}
/**
* Convert JSON to string. When is updated, functions (like events.load)
* is not wrapped in quotes.
*/
serialize(chartOptions, isUpdate) {
var hcFunctions = {},
serializedOptions,
i = 0;
serializedOptions = JSON.stringify(chartOptions, function (val, key) {
var fcId = '###HighchartsFunction' + i + '###';
// set reference to function for the later replacement
if (typeof key === 'function') {
hcFunctions[fcId] = key.toString();
i++;
return isUpdate ? key.toString() : fcId;
}
return key;
});
// replace ids with functions.
if (!isUpdate) {
Object.keys(hcFunctions).forEach(function (key) {
serializedOptions = serializedOptions.replace(
'"' + key + '"',
hcFunctions[key]
);
});
}
return serializedOptions;
}
// Create <scripts> with references to highcharts files
addHighchartsScripts() {
const highchartsInit = `
Highcharts.chart(
'container',
${this.serialize(this.props.options)},
${this.serialize(this.props.callback)}
)
`;
return `
var modules = ${this.serialize(this.props.modules) || '[]'},
moduleCounter = modules.length,
hcScript;
hcScript = document.createElement('script');
hcScript.setAttribute('src', '${path}highcharts.js');
hcScript.onload = function() {
if (moduleCounter === 0) {
${highchartsInit}
} else {
modules.forEach(function(scr) {
var moduleScript = document.createElement('script');
moduleScript.setAttribute('src', '${path}' + scr + '.js');
moduleScript.onload = function() {
moduleCounter--;
if (moduleCounter === 0) {
${highchartsInit}
}
};
document.body.appendChild(moduleScript);
});
};
};
document.body.appendChild(hcScript);
`;
}
render() {
// Create container for the chart
return <View style={[
this.props.styles,
{ width: this.state.width, height: this.state.height }
]}
>
<WebView
ref={(webView) => this.webView = webView}
source = {highchartsLayout}
injectedJavaScript={this.addHighchartsScripts()}
originWhitelist={["*"]}
automaticallyAdjustContentInsets={true}
allowFileAccess={true}
javaScriptEnabled={true}
domStorageEnabled={true}
scalesPageToFit={true}
scrollEnabled={false}
mixedContentMode='always'
/>
</View>;
}
}