-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmint-trend.js
More file actions
226 lines (220 loc) · 6.67 KB
/
mint-trend.js
File metadata and controls
226 lines (220 loc) · 6.67 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import 'jquery';
import 'highcharts';
import 'highcharts/highcharts-more';
import 'highcharts/modules/data';
import 'highcharts/modules/exporting';
import 'highcharts/modules/drilldown';
import 'highcharts/modules/series-label';
import 'highcharts/modules/export-data';
/**
* `mint-trend`
* Mint Trend of Aggregation
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class MintTrend extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
}
.select {
position: relative;
display: inline-block;
margin-bottom: 15px;
width: 100%;
}
.select select {
display: inline-block;
width: 100%;
cursor: pointer;
padding: 10px 15px;
outline: 0;
border: 0;
border-radius: 0;
background: #e6e6e6;
color: #7b7b7b;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
.select select::-ms-expand {
display: none;
}
.select select:hover,
.select select:focus {
color: #000;
background: #ccc;
}
.select select:disabled {
opacity: 0.5;
pointer-events: none;
}
.select__arrow {
position: absolute;
top: 16px;
right: 15px;
width: 0;
height: 0;
pointer-events: none;
border-style: solid;
border-width: 8px 5px 0 5px;
border-color: #7b7b7b transparent transparent transparent;
}
.select select:hover ~ .select__arrow,
.select select:focus ~ .select__arrow {
border-top-color: #000;
}
.select select:disabled ~ .select__arrow {
border-top-color: #ccc;
}
</style>
<div class="select">
<select id="trend_datasets">
<option disabled="" selected="" value="">Please select one trend</option>
<option value="fldas01_fldas02_trend_chart" data-url="http://jonsnow.usc.edu:8081/mintmap/csv/fldas01_fldas02_trend_chart.csv">Precipitation: FLDAS Jan vs FLDAS Feb Trend</option>
<option value="fldas_chirps_trend_chart" data-url="http://jonsnow.usc.edu:8081/mintmap/csv/fldas_chirps_trend_chart.csv" data-connect-null="yes">Precipitation: FLDAS 2001(Jan and Feb) vs CHIRPS Yearly Trend</option>
<option value="fewschips_2018_yearly_trend" data-url="http://jonsnow.usc.edu:8081/mintmap/csv/chirps_trend_chart.csv">Precipitation: CHIRPS Yearly Trend</option>
</select>
<div class="select__arrow"></div>
</div>
<div id="trend_chart_panel">
</div>
<slot></slot>
`;
}
static get properties() {
return {
datasets: {
type: Object,
observer: '_configChanged',
value: {}
}
}
}
_configChanged(newObj, oldObj) {
// console.log(newObj);
if (JSON.stringify(newObj) !== JSON.stringify(oldObj)) {
this.initChart(newObj);
}
}
initChart(obj){
var self = this;
if (typeof obj.id === "string" && obj.id.length > 0) {
var trend = $(self.$.trend_datasets).find('option[value=' + obj.id + ']');
if (trend.length > 0) {
trend.prop('selected',true);
self.selectTrend(obj.id);
}
}
}
ready(){
super.ready();
var self = this;
$(self.$.trend_datasets).on('change',function (event) {
if ($(this).val().length > 0) {
self.selectTrend($(this).val())
}
})
}
selectTrend(optionValue){
var self = this;
var trend = $(self.$.trend_datasets).find('option[value=' + optionValue + ']');
// console.log(trend.data('url'));
if (trend.length === 0) {
alert('No such trend data!');
return;
}
var chartOption = {
chart: {
type: 'line',
zoomType: 'x'
},
title: {
text: trend.html()
},
tooltip: {
shared: true,
crosshairs: true,
useHTML: true,
headerFormat: 'Day Index: {point.key}<table>',
pointFormat: '<tr><td style="color: {series.color}">{series.name}: </td>' + '<td style="text-align: right"><b>{point.y}</b></td></tr>',
footerFormat: '</table>'
},
legend: {
align: 'left',
verticalAlign: 'top',
borderWidth: 0
},
xAxis: {
title: {
text: ""
},
gridLineWidth: 1,
labels: {
align: 'left',
}
},
yAxis: [{ // left y axis
title: {
text: null
},
labels: {
align: 'left',
x: 3,
y: 16,
format: '{value:.,0f}'
},
showFirstLabel: false
}],
plotOptions: {
series: {
cursor: 'pointer',
connectNulls: trend.data('connect-null') === 'yes' ? true : false,
marker: {
lineWidth: 1
}
}
},
series: []
};
var series = [];
$.ajax({
type: 'get',
url: trend.data('url'),
dataType: 'text',
success: function(csv){
csv = csv.replace(/null|"/g,'');
var lines = csv.split('\n');
lines.forEach(function(line, lineNo) {
var items = line.split(',');
if (lineNo == 0) {
items.forEach(function(item, itemNo) {
if (itemNo == 0) {
chartOption.tooltip.headerFormat = item + ': {point.key}<table>';
chartOption.xAxis.title.text = item;
}
if (itemNo > 0)
series.push({name:item,data: []});
});
}else {
items.forEach(function(item, itemNo) {
if (itemNo == 0) {
// series.name = item;
} else {
series[itemNo-1].data.push(parseFloat(item));
}
});
}
});
chartOption.series = series;
self.chart = Highcharts.chart(self.$.trend_chart_panel, chartOption);
}
});
}
}
window.customElements.define('mint-trend', MintTrend);