ChartView.js
5.41 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
'use strict';
import React, {Component} from 'react';
import {
WebView,
StyleSheet,
View,
Text,
} from 'react-native';
export default class ChartView extends Component {
constructor(props) {
super(props)
}
render() {
let xString = this.props.xData.reduce((previous, current, index, array) => {
if (index==1) {
previous = `\'` + previous + `\'`;
}
return previous + ','+ `\'` + current+ `\'`;
});
// xString = '\'' + xString;
console.log(xString);
const HTML = getHTMLString(xString,this.props.yData.toString());
console.log(this.props.yData);
return(
<View style={styles.container}>
<Text style={styles.titleStyle}>{this.props.chartTitle}</Text>
<WebView
style={styles.chartStyle}
bounces={false}
automaticallyAdjustContentInsets={true}
source={{html:HTML}}
scalesPageToFit={true}
startInLoadingState={false}
javaScriptEnabled={true}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'white',
},
titleStyle: {
fontSize: 14,
color: '#b1b1b1',
marginTop: 20,
marginLeft: 15,
},
chartStyle: {
height: 175,
marginLeft: -45,
marginRight: -45,
marginBottom: 15,
},
});
function getHTMLString(xData,yData) {
const HTML = `
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="http://cdn.bootcss.com/echarts/3.0.0/echarts.min.js"></script>
<script type="text/javascript">
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
option = {
title: {
show: false,
},
xAxis: {
type: 'category',
boundaryGap: true,
axisLine: {
show: true,
onZero: false,
lineStyle: {
width: 1,
color: 'gray',
},
},
axisTick: {
show:false,
},
axisLabel: {
textStyle: {
color: '#b1b1b1',
fontSize: 24,
},
},
splitLine: {
show: false,
},
data:[`+xData+`]
},
yAxis: {
type: 'value',
axisLine: {
show: false,
onZero: false,
},
axisTick: {
show:false,
},
axisLabel: {
show: false,
},
},
tooltip: {
show: true,
showContent: true,
trigger: 'axis',
alwaysShowContent: 'true',
position: function (point,params,dom) {
return [point[0]/7*7-40,0];
},
backgroundColor: 'orange',
formatter: '{c}',
textStyle: {
fontSize: 24,
},
axisPointer: {
type: 'line',
lineStyle: {
opacity: 1,
},
},
extraCssText: 'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);',
padding:[10,20],
},
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;
}