forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodesamples.js
More file actions
383 lines (321 loc) · 10.9 KB
/
codesamples.js
File metadata and controls
383 lines (321 loc) · 10.9 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
This file contains the code samples which will appear live in the web-page.
Each sample method name starts with _Report_ or _Page or _Embed depends on which section it appears.
Please keep this.
*/
// ---- Embed Code ----------------------------------------------------
function _Embed_BasicEmbed() {
// Read embed application token from textbox
var txtAccessToken = $('#txtAccessToken').val();
// Read embed URL from textbox
var txtEmbedUrl = $('#txtReportEmbed').val();
// Read report Id from textbox
var txtEmbedReportId = $('#txtEmbedReportId').val();
// Embed configuration used to describe the what and how to embed.
// This object is used when calling powerbi.embed.
// This also includes settings and options such as filters.
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
var config= {
type: 'report',
accessToken: txtAccessToken,
embedUrl: txtEmbedUrl,
id: txtEmbedReportId,
settings: {
filterPaneEnabled: true,
navContentPaneEnabled: true
}
};
// Grab the reference to the div HTML element that will host the report.
var reportContainer = $('#reportContainer')[0];
// Embed the report and display it within the div container.
var report = powerbi.embed(reportContainer, config);
// Report.off removes a given event handler if it exists.
report.off("loaded");
// Report.on will add an event handler which prints to Log window.
report.on("loaded", function() {
Log.logText("Loaded");
});
}
function _Embed_EmbedWithDefaultFilter() {
var txtAccessToken = $('#txtAccessToken').val();
var txtEmbedUrl = $('#txtReportEmbed').val();
var txtEmbedReportId = $('#txtEmbedReportId').val();
const filter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "Store",
column: "Chain"
},
operator: "In",
values: ["Lindseys"]
};
var embedConfiguration = {
type: 'report',
accessToken: txtAccessToken,
embedUrl: txtEmbedUrl,
id: txtEmbedReportId,
settings: {
filterPaneEnabled: false,
navContentPaneEnabled: false
},
filters: [filter]
};
var reportContainer = document.getElementById('reportContainer');
powerbi.embed(reportContainer, embedConfiguration);
}
// ---- Report Operations ----------------------------------------------------
function _Report_GetId() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Retrieve the report id.
var reportId = report.getId();
Log.logText(reportId);
}
function _Report_UpdateSettings() {
// The new settings that you want to apply to the report.
const newSettings = {
navContentPaneEnabled: true,
filterPaneEnabled: false
};
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Update the settings by passing in the new settings you have configured.
report.updateSettings(newSettings)
.then(function (result) {
$("#result").html(result);
})
.catch(function (error) {
$("#result").html(error);
});
}
function _Report_GetPages() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Retrieve the page collection and loop through to collect the
// page name and display name of each page and display the value.
report.getPages()
.then(function (pages) {
pages.forEach(function(page) {
var log = page.name + " - " + page.displayName;
Log.logText(log);
});
})
.catch(function (error) {
Log.log(error);
});
}
function _Report_SetPage() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// setPage will change the selected view to the page you indicate.
// This is the actual page name not the display name.
report.setPage("ReportSection2")
.then(function (result) {
Log.log(result);
})
.catch(function (errors) {
Log.log(errors);
});
// Report.off removes a given event handler if it exists.
report.off("pageChanged");
// Report.on will add an event handler which prints page
// name and display name to Log window.
report.on("pageChanged", function(event) {
var page = event.detail.newPage;
Log.logText(page.name + " - " + page.displayName);
});
}
function _Report_GetFilters() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Get the filters applied to the report.
report.getFilters()
.then(function (filters) {
Log.log(filters);
})
.catch(function (errors) {
Log.log(errors);
});
}
function _Report_SetFilters() {
// Build the filter you want to use. For more information, See Constructing
// Filters in https://github.com/Microsoft/PowerBI-JavaScript/wiki/Filters.
const filter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "Store",
column: "Chain"
},
operator: "In",
values: ["Lindseys"]
};
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Set the filter for the report.
// Pay attention that setFilters receives an array.
report.setFilters([filter])
.then(function (result) {
Log.log(result);
})
.catch(function (errors) {
Log.log(errors);
});
}
function _Report_RemoveFilters() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Remove the filters currently applied to the report.
report.removeFilters()
.then(function (result) {
Log.log(result);
})
.catch(function (errors) {
Log.log(errors);
});
}
function _Report_PrintCurrentReport() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Trigger the print dialog for your browser.
report.print()
.then(function (result) {
Log.log(result);
})
.catch(function (errors) {
Log.log(errors);
});
}
function _Report_Reload() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Reload the displayed report
report.reload()
.then(function (result) {
Log.log(result);
})
.catch(function (errors) {
Log.log(errors);
});
}
function _Report_FullScreen() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Displays the report in full screen mode.
report.fullscreen();
}
function _Report_ExitFullScreen() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Exits full screen mode.
report.exitFullscreen();
}
// ---- Page Operations ----------------------------------------------------
function _Page_SetActive() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Retrieve the page collection, and then set the second page to be active.
report.getPages()
.then(function (pages) {
pages[1].setActive().then(function (result) {
Log.log(result);
});
})
.catch(function (errors) {
Log.log(errors);
});
}
function _Page_GetFilters() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Retrieve the page collection and get the filters for the first page.
report.getPages()
.then(function (pages) {
pages[0].getFilters()
.then(function (filters) {
Log.log(filters);
})
.catch(function (errors) {
Log.log(errors);
});
})
.catch(function (errors) {
Log.log(errors);
});
}
function _Page_SetFilters() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Build the filter you want to use. For more information, see Constructing
// Filters in https://github.com/Microsoft/PowerBI-JavaScript/wiki/Filters.
const filter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "Store",
column: "Chain"
},
operator: "In",
values: ["Lindseys"]
};
// Retrieve the page collection and then set the filters for the first page.
// Pay attention that setFilters receives an array.
report.getPages()
.then(function (pages) {
pages[0].setFilters([filter])
.then(function (result) {
Log.log(result);
})
.catch(function (errors) {
Log.log(errors);
});
})
.catch(function (errors) {
Log.log(errors);
});
}
function _Page_RemoveFilters() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Retrieve the page collection and remove the filters for the first page.
report.getPages()
.then(function (pages) {
pages[0].removeFilters()
.then(function (result) {
Log.log(result);
})
.catch(function (errors) {
Log.log(errors);
});
})
.catch(function (errors) {
Log.log(errors);
});
}
// ---- Event Listener ----------------------------------------------------
function _Events_PageChanged() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Report.off removes a given event listener if it exists.
report.off("pageChanged");
// Report.on will add an event listener.
report.on("pageChanged", function(event) {
var page = event.detail.newPage;
Log.logText("Page changed to: " + page.name + " - " + page.displayName);
});
// Select Run and change to a different page.
// You should see an entry in the Log window.
Log.logText("Select different page to see events in Log window.");
}
function _Events_DataSelected() {
// Get a reference to the embedded report.
report = powerbi.embeds[0];
// Report.off removes a given event listener if it exists.
report.off("dataSelected");
// Report.on will add an event listener.
report.on("dataSelected", function(event) {
var data = event.detail;
Log.log(data);
});
// Select Run and select an element of a visualization.
// For example, a bar in a bar chart. You should see an entry in the Log window.
Log.logText("Select data to see events in Log window.");
}