ChartView.js
6.29 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict';
import React, {Component} from 'react';
import {
WebView,
StyleSheet,
View,
Text,
Platform,
NativeModules,
Dimensions,
} from 'react-native';
export default class ChartView extends Component {
constructor(props) {
super(props)
}
render() {
let xString = '';
if (this.props.xData.length!==0) {
xString = this.props.xData.reduce((previous, current, index, array) => {
if (index==1) {
previous = `\'` + previous + `\'`;
}
return previous + ','+ `\'` + current+ `\'`;
});
}
let jsPath = NativeModules.RNNativeConfig.echartsPath;
if (jsPath === undefined || jsPath === '') {
jsPath = 'http://cdn.bootcss.com/echarts/3.0.0/echarts.min.js';
}
const HTML = getHTMLString(xString, this.props.yData.toString(), jsPath);
return(
<View style={styles.container}>
<Text style={styles.titleStyle}>{this.props.chartTitle}</Text>
<WebView
style={styles.chartStyle}
bounces={false}
automaticallyAdjustContentInsets={true}
source={{html:HTML, baseUrl:'/'}}
scalesPageToFit={true}
startInLoadingState={false}
javaScriptEnabled={true}
scrollEnabled={false}
/>
</View>
);
}
}
const fontSize = (Platform.OS === 'android') ? 14 : 14;
const width = Dimensions.get('window').width;
const initRatio = 465 / 250;
const height = width / initRatio;
const chartHeight = height + 75;
const styles = StyleSheet.create({
container: {
backgroundColor: 'white',
},
titleStyle: {
fontSize: 14,
color: '#b1b1b1',
marginTop: 15,
marginLeft: 15,
},
chartStyle: {
height: chartHeight,
// marginLeft: -45,
// marginRight: -45,
marginBottom: 15,
},
});
function getHTMLString(xData, yData, jsPath) {
const HTML = `
<!DOCTYPE html>
<html style="width: ` + width + `; height: ` + height + `; margin: 0px;">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=` + width + `,initial-scale=1.0,user-scalable=no"/>
</head>
<body style="width: ` + width + `; height: ` + height + `; margin: 0px;">
<div id="container" style="width: ` + width + `; height: ` + height + `; margin: 0px; min-height: 250px;"></div>
<script type="text/javascript" src="`+ jsPath +`"></script>
<script type="text/javascript">
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
option = {
title: {
show: false,
},
grid: {
left: 0,
top: 60,
right: 0,
width: ` + width + `,
height: '65%',
},
xAxis: {
type: 'category',
boundaryGap: true,
axisLine: {
show: true,
onZero: false,
},
axisTick: {
show:false,
},
axisLabel: {
textStyle: {
color: '#b1b1b1',
fontSize: ` + fontSize + `,
},
},
splitLine: {
show: false,
},
data:[` + xData + `]
},
yAxis: {
type: 'value',
axisLine: {
show: false,
onZero: false,
},
axisTick: {
show:false,
},
axisLabel: {
show: false,
},
splitLine: {
show: true,
},
},
tooltip: {
show: true,
showContent: true,
trigger: 'axis',
alwaysShowContent: 'true',
position: function (point,params,dom) {
return [point[0] / 7 * 7 - 20, 20];
},
backgroundColor: 'orange',
formatter: '{c}',
textStyle: {
fontSize: ` + fontSize + `,
},
axisPointer: {
type: 'line',
lineStyle: {
opacity: 1,
},
},
padding:[5, 10],
},
series: [
{
type:'line',
lineStyle: {
normal: {
color: 'orange',
},
},
smooth: true,
data:[` + yData + `],
},
]
};
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
</script>
</body>
</html>
`;
return HTML;
}