-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (68 loc) · 2.13 KB
/
index.js
File metadata and controls
74 lines (68 loc) · 2.13 KB
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
import React, {
Component,
} from 'react';
import {
StyleSheet,
Platform
} from 'react-native';
import PropTypes from 'prop-types';
import { WebView } from 'react-native-webview';
/**
* 渲染图表脚本的模版,设置时将CONFIG参数替换成对应的值
* @type {[string]}
*/
var settingChartScript = `
Chart.defaults.global.defaultFontSize={DEFAULT_FONT_SIZE};
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart( ctx, {CONFIG} );
`;
export default class Chart extends Component {
static propTypes = {
/**
* 图表配置参数,对应chart.js中初始化需要的参数
* @type {[object]}
*/
chartConfiguration: PropTypes.object.isRequired,
defaultFontSize : PropTypes.number
}
constructor(props) {
super(props);
}
componentWillReceiveProps(nextProps) {
if( nextProps.chartConfiguration !== this.props.chartConfiguration
|| nextProps.defaultFontSize !== this.props.defaultFontSize ){
this.setChart(nextProps.chartConfiguration, nextProps.defaultFontSize );
}
}
setChart(chartConfiguration, defaultFontSize) {
if( !chartConfiguration || undefined == defaultFontSize || null == defaultFontSize ){
return ;
}
this.webview && this.webview.injectJavaScript(
settingChartScript.replace('{CONFIG}', JSON.stringify( chartConfiguration ))
.replace('{DEFAULT_FONT_SIZE}', defaultFontSize )
);
}
render() {
const defaultFontSize = this.props.defaultFontSize ? this.props.defaultFontSize : 12;
return ( < WebView style={{ flex : 1 }}
originWhitelist={["*"]}
ref = {
ref => this.webview = ref
}
injectedJavaScript = {
settingChartScript.replace( '{CONFIG}', JSON.stringify( this.props.chartConfiguration ))
.replace('{DEFAULT_FONT_SIZE}', defaultFontSize )
}
source= {Platform.OS == 'ios' ? require('./dist/index.html') : {uri: "file:///android_asset/dist/index.html"}}
onError = {
(error) => {
console.log(error)
}
}
// scalesPageToFit false for IOS and true for Android
scalesPageToFit={Platform.OS === 'ios' ? false : true}
/>
)
}
}