-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatergaugestationsfilter.js
145 lines (121 loc) · 4.44 KB
/
watergaugestationsfilter.js
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
(function() {
// Create the connector object
var myConnector = tableau.makeConnector();
// Define the schema, Structure definition includes columns, alias and data type
myConnector.getSchema = function(schemaCallback) {
var cols = [{
id: "Nr", //not included in datasource, placeholder for artificial running number
dataType: tableau.dataTypeEnum.string
}, {
id: "uuid",
dataType: tableau.dataTypeEnum.string
}, {
id: "number",
dataType: tableau.dataTypeEnum.string
}, {
id: "shortname",
alias: "shortname wassergepelstaton",
dataType: tableau.dataTypeEnum.string
}, {
id: "longname",
alias: "longname wassergepelstaton",
dataType: tableau.dataTypeEnum.string
}, {
id: "km",
alias: "km",
dataType: tableau.dataTypeEnum.float
}, {
id: "agency",
dataType: tableau.dataTypeEnum.string
}, {
id: "longitude",
alias: "longitude wassergepelstaton",
dataType: tableau.dataTypeEnum.float
}, {
id: "latitude",
alias: "longitude wassergepelstaton",
dataType: tableau.dataTypeEnum.float
}, {
id: "watershort",
alias: "water shortname",
dataType: tableau.dataTypeEnum.string
}, {
id: "waterlong",
alias: "water longname",
dataType: tableau.dataTypeEnum.string
}];
var tableSchema = {
id: "watergaugeFeed", //needs to be one word
alias: "Wasserpegel Germany updated every 5 Minutes",
columns: cols
};
schemaCallback([tableSchema]);
};
// Download the masterdata data (Watergauge in Germany)
myConnector.getData = function(table, doneCallback) {
var waterObj = JSON.parse(tableau.connectionData),
dateString = "waters=" + waterObj.water + "&km=" + waterObj.distance + "&radius=" + waterObj.radius,
apiCall = "https://www.pegelonline.wsv.de/webservices/rest-api/v2/stations.json?" + dateString;
console.log(apiCall);
if (waterObj.water == '666'){
apiCall = 'https://www.pegelonline.wsv.de/webservices/rest-api/v2/stations.json'
};
console.log(apiCall);
$.getJSON(apiCall, function(result) {
var anzahl = result.length,
tableData = [];
// Iterate over the JSON object
for (var i = 0, len = anzahl; i < len; i++) {
tableData.push({
"Nr": i, //running number given by js
"uuid": result[i].uuid,
"number": result[i].number,
"shortname": result[i].shortname,
"longname": result[i].longname,
"km": result[i].km,
"agency": result[i].agency,
"longitude": result[i].longitude,
"latitude": result[i].latitude,
"watershort": result[i].water.shortname,
"waterlong": result[i].water.longname
});
}
table.appendRows(tableData);
doneCallback();
});
};
tableau.registerConnector(myConnector);
// Create event listeners for when the user submits the form
$(document).ready(function() {
$("#submitButton").click(function() {
// When nothing selected-> {water: '666', distance: '50', radius: '10'}
var sel1_value = $('#sel1').val() ;
var radio2_on_boolean = $("#inlineRadio2").is(":checked");
// Disable geographic search when 'All waters' is selected
console.log(radio2_on_boolean);
if (sel1_value == '666'){
var waterObj = {
water: $('#sel1').val().trim(),
distance: '0',
radius: '0'
};
} else if (radio2_on_boolean==true) {
var waterObj = {
water: $('#sel1').val().trim(),
distance: $('#customRange1').val().trim(),
radius: $('#customRange2').val().trim()
};
} else if (radio2_on_boolean==false) {
var waterObj = {
water: $('#sel1').val().trim(),
distance: '0',
radius: '0'
}; ;
}
console.log(waterObj);
tableau.connectionData = JSON.stringify(waterObj); // Use this variable to pass data to your getSchema and getData functions
tableau.connectionName = "Wassergauge Germany"; // This will be the data source name in Tableau
tableau.submit(); // This sends the connector object to Tableau
});
});
})();