-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.9.php
More file actions
182 lines (148 loc) · 5.45 KB
/
6.9.php
File metadata and controls
182 lines (148 loc) · 5.45 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
<div class="container" style="margin-top:30px">
<div class="row">
<div class="col-sm-12">
<h1>Figure: <?= $get_figure ?></h1>
<div id="chartdiv"></div>
</div>
</div>
</div>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="regression.js"></script>
<script>
$(document).ready(function () {
var options = {
chart: {
type: 'line',
zoomType: 'x'
},
plotOptions: {
series: {
lineWidth: 0.5
}
},
xAxis: {
type: 'datetime',
categories: []
},
tooltip: {
valueDecimals: 2,
shared: true,
valueSuffix: 'mm'
},
title: {
text: 'Lake Superior'
},
yAxis: {
title: {
text: 'Units'
}
},
series: []
};
$.get('/csvs/6.9a.csv', function (data) {
// Split the lines
var lines = data.split('\n');
// Iterate over the lines and add categories or series
$.each(lines, function (lineNo, line) {
var items = line.split(',');
// header line contains categories
if (lineNo === 0) {
$.each(items, function (itemNo, item) {
if (itemNo === 1) {
lc = '#346cbc';
}
if (itemNo === 2) {
lc = '#ff2a21';
}
if (itemNo === 3) {
lc = '#00640c';
}
if (itemNo > 0) {
options.series.push({
name: item,
id: item,
color: lc,
type: 'line',
data: []
});
options.series.push({
name: item + ' Trend',
id: item + 'Trend',
color: lc,
type: 'line',
data: []
});
}
});
} else {
$.each(items, function (itemNo, item) {
if (itemNo === 1) {
seriesNo = 0
}
if (itemNo === 2) {
seriesNo = 2
}
if (itemNo === 3) {
seriesNo = 4
}
if (itemNo === 0) {
options.xAxis.categories.push(item);
} else if (parseFloat(item)) {
options.series[seriesNo].data.push(parseFloat(item));
} else if (item === "null") { /* adding nulls */
options.series[seriesNo].data.push(null);
}
});
}
});
var chart = new Highcharts.Chart('chartdiv', options);
/* add regression line dynamically */
chart.series[1].update({
type: 'line',
name: 'Precipitation Trend',
lineWidth: 1,
linkedTo: 'Precipitation',
marker: {enabled: false},
/* function returns data for trend-line */
data: (function () {
return fitOneDimensionalData(options.series[0].data);
})()
}, true); //true / false to redraw
chart.series[3].update({
type: 'line',
name: 'Evaporation Trend',
lineWidth: 1,
linkedTo: 'Evaporation',
marker: {enabled: false},
/* function returns data for trend-line */
data: (function () {
return fitOneDimensionalData(options.series[2].data);
})()
}, true); //true / false to redraw
chart.series[5].update({
name: 'Runoff Trend',
lineWidth: 1,
linkedTo: ':previous',
marker: {enabled: false},
/* function returns data for trend-line */
data: (function () {
return fitOneDimensionalData(options.series[4].data);
})()
}, true); //true / false to redraw
});
});
function fitOneDimensionalData(source_data) {
var trend_source_data = [];
for (var i = source_data.length; i-- > 0;) {
trend_source_data[i] = [i, source_data[i]]
}
var regression_data = fitData(trend_source_data).data;
var trend_line_data = [];
for (var i = regression_data.length; i-- > 0;) {
trend_line_data[i] = regression_data[i][1];
}
return trend_line_data;
}
</script>