-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTableau_View_Load.js
More file actions
271 lines (229 loc) · 7.87 KB
/
Tableau_View_Load.js
File metadata and controls
271 lines (229 loc) · 7.87 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
////////////////////////////////////////////////////////////////////////////////
// Global Variables
var viz, workbook, activeSheet;
////////////////////////////////////////////////////////////////////////////////
// 1 - Create a View
function initializeViz() {
var placeholderDiv = document.getElementById("tableauViz");
var url = "http://public.tableau.com/views/WorldIndicators/GDPpercapita";
var options = {
width: placeholderDiv.offsetWidth,
height: placeholderDiv.offsetHeight,
hideTabs: true,
hideToolbar: true,
onFirstInteractive: function () {
workbook = viz.getWorkbook();
activeSheet = workbook.getActiveSheet();
}
};
viz = new tableau.Viz(placeholderDiv, url, options);
}
// Create the viz after the page is done loading
// $(initializeViz);
////////////////////////////////////////////////////////////////////////////////
// 2 - Filter
function filterSingleValue() {
activeSheet.applyFilterAsync(
"Region",
"The Americas",
tableau.FilterUpdateType.REPLACE);
}
function addValuesToFilter() {
activeSheet.applyFilterAsync(
"Region",
["Europe", "Middle East"],
tableau.FilterUpdateType.ADD);
}
function removeValuesFromFilter() {
activeSheet.applyFilterAsync(
"Region",
"Europe",
tableau.FilterUpdateType.REMOVE);
}
function filterRangeOfValues() {
activeSheet.applyRangeFilterAsync(
"F: GDP per capita (curr $)",
{
min: 40000,
max: 60000
},
tableau.FilterUpdateType.REPLACE);
}
function clearFilters() {
activeSheet.clearFilterAsync("Region");
activeSheet.clearFilterAsync("F: GDP per capita (curr $)");
}
////////////////////////////////////////////////////////////////////////////////
// 3 - Switch Tabs
function switchToMapTab() {
workbook.activateSheetAsync("GDP per capita map");
}
////////////////////////////////////////////////////////////////////////////////
// 4 - Select
function selectSingleValue() {
workbook.getActiveSheet().selectMarksAsync(
"Region",
"Asia",
tableau.SelectionUpdateType.REPLACE);
}
function addValuesToSelection() {
workbook.getActiveSheet().selectMarksAsync(
"Region",
["Africa", "Oceania"],
tableau.SelectionUpdateType.ADD);
}
function removeFromSelection() {
// Remove all of the areas where the GDP is < 5000.
workbook.getActiveSheet().selectMarksAsync(
"AVG(F: GDP per capita (curr $))",
{
max: 5000
},
tableau.SelectionUpdateType.REMOVE);
}
function clearSelection() {
workbook.getActiveSheet().clearSelectedMarksAsync();
}
////////////////////////////////////////////////////////////////////////////////
// 5 - Chain Calls
function switchTabsThenFilterThenSelectMarks() {
workbook.activateSheetAsync("GDP per capita by region")
.then(function (newSheet) {
activeSheet = newSheet;
// It's important to return the promise so the next link in the chain
// won't be called until the filter completes.
return activeSheet.applyRangeFilterAsync(
"Date (year)",
{
min: new Date(Date.UTC(2002, 1, 1)),
max: new Date(Date.UTC(2008, 12, 31))
},
tableau.FilterUpdateType.REPLACE);
})
.then(function (filterFieldName) {
return activeSheet.selectMarksAsync(
"AGG(GDP per capita (weighted))",
{
min: 20000
},
tableau.SelectionUpdateType.REPLACE);
});
}
function triggerError() {
workbook.activateSheetAsync("GDP per capita by region")
.then(function (newSheet) {
// Do something that will cause an error: leave out required parameters.
return activeSheet.applyFilterAsync("Date (year)");
})
.otherwise(function (err) {
alert("We purposely triggered this error to show how error handling happens with chained calls.\n\n " + err);
});
}
////////////////////////////////////////////////////////////////////////////////
// 6 - Sheets
function getSheetsAlertText(sheets) {
var alertText = [];
for (var i = 0, len = sheets.length; i < len; i++) {
var sheet = sheets[i];
alertText.push(" Sheet " + i);
alertText.push(" (" + sheet.getSheetType() + ")");
alertText.push(" - " + sheet.getName());
alertText.push("\n");
}
return alertText.join("");
}
function querySheets() {
var sheets = workbook.getPublishedSheetsInfo();
var text = getSheetsAlertText(sheets);
text = "Sheets in the workbook:\n" + text;
alert(text);
}
function queryDashboard() {
// Notice that the filter is still applied on the "GDP per capita by region"
// worksheet in the dashboard, but the marks are not selected.
workbook.activateSheetAsync("GDP per Capita Dashboard")
.then(function (dashboard) {
var worksheets = dashboard.getWorksheets();
var text = getSheetsAlertText(worksheets);
text = "Worksheets in the dashboard:\n" + text;
alert(text);
});
}
function changeDashboardSize() {
workbook.activateSheetAsync("GDP per Capita Dashboard")
.then(function (dashboard) {
dashboard.changeSizeAsync({
behavior: tableau.SheetSizeBehavior.AUTOMATIC
});
});
}
function changeDashboard() {
var alertText = [
"After you click OK, the following things will happen: ",
" 1) Region will be set to Middle East.",
" 2) On the map, the year will be set to 2010.",
" 3) On the bottom worksheet, the filter will be cleared.",
" 4) On the bottom worksheet, the year 2010 will be selected."
];
alert(alertText.join("\n"));
var dashboard, mapSheet, graphSheet;
workbook.activateSheetAsync("GDP per Capita Dashboard")
.then(function (sheet) {
dashboard = sheet;
mapSheet = dashboard.getWorksheets().get("Map of GDP per capita");
graphSheet = dashboard.getWorksheets().get("GDP per capita by region");
return mapSheet.applyFilterAsync("Region", "Middle East", tableau.FilterUpdateType.REPLACE);
})
.then(function () {
// Do these two steps in parallel since they work on different sheets.
mapSheet.applyFilterAsync("YEAR(Date (year))", 2010, tableau.FilterUpdateType.REPLACE);
return graphSheet.clearFilterAsync("Date (year)");
})
.then(function () {
return graphSheet.selectMarksAsync("YEAR(Date (year))", 2010, tableau.SelectionUpdateType.REPLACE);
});
}
////////////////////////////////////////////////////////////////////////////////
// 7 - Control the Toolbar
function exportPDF() {
viz.showExportPDFDialog();
}
function exportImage() {
viz.showExportImageDialog();
}
function exportCrossTab() {
viz.showExportCrossTabDialog();
}
function exportData() {
viz.showExportDataDialog();
}
function revertAll() {
workbook.revertAllAsync();
}
////////////////////////////////////////////////////////////////////////////////
// 8 - Events
function listenToMarksSelection() {
viz.addEventListener(tableau.TableauEventName.MARKS_SELECTION, onMarksSelection);
}
function onMarksSelection(marksEvent) {
return marksEvent.getMarksAsync().then(reportSelectedMarks);
}
function reportSelectedMarks(marks) {
var html = [];
for (var markIndex = 0; markIndex < marks.length; markIndex++) {
var pairs = marks[markIndex].getPairs();
html.push("<b>Mark " + markIndex + ":</b><ul>");
for (var pairIndex = 0; pairIndex < pairs.length; pairIndex++) {
var pair = pairs[pairIndex];
html.push("<li><b>fieldName:</b> " + pair.fieldName);
html.push("<br/><b>formattedValue:</b> " + pair.formattedValue + "</li>");
}
html.push("</ul>");
}
var dialog = $("#dialog");
dialog.html(html.join(""));
dialog.dialog("open");
}
function removeMarksSelectionEventListener() {
viz.removeEventListener(tableau.TableauEventName.MARKS_SELECTION, onMarksSelection);
}