Forum Discussion
set value field in report powerbi by custom visual
- 8 years ago
This code snippet is going to address the issue:
let selectionIds = []; selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[0], 0) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[1], 0) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[0], 1) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[1], 1) .createSelectionId() ); selectionIds.forEach((selectionId) => { this.selectionManager.select(selectionId, true) });Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
Could you please sahre your capabilities.json?
As far as I understand the requirement, you sould use this code snippet to create a SelectionId for each category:
let dataViews = options.dataViews // options: VisualUpdateOptions
let categorical = dataViews[0].categorical;
let categoryIndex: number = 0; // Index of category
let i: number = 0; // this is index of your value in the category
let selectionId = host.createSelectionIdBuilder()
.withCategory(categorical.categories[categoryIndex], i)
.createSelectionId();After that, you should use selectionManager.select method to select categories:
this.selectionManager.select(selectionId, true).then((ids: ISelectionId[]) => {
//called when setting the selection has been completed successfully
});Please let me know if you have any questions.
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
Thank you v-viig,
i try your code and it runs.
I have tried to pass the array selectionId in selectionManager.select but it doesnt run.
I write my code
let selectionId = [];
selectionId [0] = host.createSelectionIdBuilder()
.withCategory(categorical.categories[0], 0)
.withCategory(categorical.categories[1], 0)
.createSelectionId();
selectionId [1] = host.createSelectionIdBuilder()
.withCategory(categorical.categories[0],1)
.withCategory(categorical.categories[1], 1)
.createSelectionId();
this.selectionManager.select(selectionId, true).then((ids: ISelectionId[]) => {
//called when setting the selection has been completed successfully
});
Where is wrong?
Thank you
Marco Baciga@24COnsulting
BI COnsultant
- v-viig8 years agoCommunity Champion
This code snippet is going to address the issue:
let selectionIds = []; selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[0], 0) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[1], 0) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[0], 1) .createSelectionId() ); selectionIds.push(host.createSelectionIdBuilder() .withCategory(categorical.categories[1], 1) .createSelectionId() ); selectionIds.forEach((selectionId) => { this.selectionManager.select(selectionId, true) });Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals
- marcobaciga8 years agoHelper I
- seregindv7 years agoFrequent Visitor
I tried this snippet under Power BI API 2.1, it works fine for the first row, then it continues to work only for that row, so I can deselect it, select again, etc. And another control (e.g. Donut chart) displays selected value highlighted while I would expect all other values filtered out and only selected one displayed. Then if I select another row, Donut chart displays "Can't display the visual" error.
Even if I deselect that another row and select the 1st row back, I still get that error.Under API 1.13 this snippet has no effect.
So what I want to know is how can I filter out unselected rows, not just highlight selected ones, so my slicer to work like HierarchySlicer or Sample Slicer.
Is the only way to achieve this is to use Interactivity Service?
Here is how I did this. First I added a check box for each of options.dataViews[0].categorical.categories[0].values both to control and this.checkboxes list so that i in snippet below represents row index and then when user checks or clears any checkbox, I clear selection and add each row index to it.
private onCheckBoxChange() { this.selectionManager.clear(); for (let i = 0; i < this.checkBoxes.length; i++) { const checkBox = this.checkBoxes[i]; if (checkBox.checked) { for (let category of this.updateOptions.dataViews[0].categorical.categories) { const selectionId = this.selectionIdBuidler .withCategory(category, i) .createSelectionId(); this.selectionManager.select(selectionId, true); } } } }selectionManager and selectionIdBuilder are created in constructor
constructor(options: VisualConstructorOptions) { this.selectionManager = options.host.createSelectionManager(); this.selectionIdBuidler = options.host.createSelectionIdBuilder(); ... }and the checkboxes in update method
public update(options: VisualUpdateOptions) { ... const values = options.dataViews[0].categorical.categories[0].values; for (let i in values) { const value = values[i]; const checkBox = document.createElement("input"); checkBox.type = "checkbox"; checkBox.addEventListener("change", () => this.onCheckBoxChange()); this.checkBoxes.push(checkBox); ... } ... }- v-viig7 years agoCommunity Champion
Do you want to imlement a Slicer to filter data out?
If so, please use JSON Filter.
Ignat Vilesov,
Software Engineer
Microsoft Power BI Custom Visuals