Best Practice: Filtering Table and Chart through Dropdown (Single Selection)
Learn how to filter a table or a chart using a single measure selected from a dropdown
widget.
Prerequisites
-
You've already added a table and a chart widget to your canvas and placed them on top of
each other.
-
To follow all functions of this sample use case, you've completed the exercise Best Practice: Switching between Chart and Table and can now enhance
your analytic application.
Context
In the dropdown widget, you load all the measures of your data source and set the default
filtering measure of the table to Gross Margin Plan. When the
application user selects a different measure in the dropdown, this filter is applied
to both table and chart.
Procedure
-
Click .
-
Change the name of the dropdown to
Dropdown_Measures.
You do this on the Styling Panel under Analytics Designer
Properties, or on the Layout panel
under .
-
Add a label to the dropdown that indicates to users that they can select measures through
the dropdown.
-
Click .
-
Place the text widget to the left of the dropdown.
-
Enter a text, Selected Measure, for
example.
- Optional:
Resize the text widget.
-
To let users select a value from the dropdown, add a script variable that acts as a global
variable, which can be accessed from anywhere in your application.
-
On the Layout panel under Scripting, click
(Add Script
Variable).
-
In the Script Variable panel under
Structure, enter
CurrentMeasureFilterSelection as name, leave
type as string and enter
[Account_BestRunJ_sold].[parentId].&[Gross_MarginActual]
as default value.
-
To close the Script Variable panel, click
Done.
This script variable definition lets Gross Margin appear as the default value
in the dropdown when running the application.
-
To define what will happen when users select a filter value from the dropdown, create a
script object. In this object, write a function that sets the measure filter
according to what the user has chosen from the dropdown.
-
On the Layout panel under
Scripting, click (Add Script Object).
-
To rename the folder, hover over ScriptObject_1,
click and enter Utils.
-
To rename the function, hover over function1,
click and enter setMeasureFilter.
-
Click on the function setMeasureFilter, and when the
Script Function panel opens, click
(Add Argument).
-
Enter selectedId as name of the argument, leave
type as string and click
Done.
-
To write the script for the function, hover over the setMeasureFilter
function, click and enter this
script in the script editor:
Sample Code
Table.getDataSource().removeDimensionFilter("Account_BestRunJ_sold");
if (CurrentMeasureFilterSelection !== "") {
Chart.removeMeasure(CurrentMeasureFilterSelection, Feed.ValueAxis);
}
Table.getDataSource().setDimensionFilter("Account_BestRunJ_sold",selectedId);
Chart.addMeasure(selectedId, Feed.ValueAxis);
With this script you define what happens to the table and the chart
when users select a measure from the dropdown:
You remove any already set dimensions of the table or measures of the
chart and add the captured value as the new dimension and measure of
the table as well as the chart.
Define how to pass the captured value to the setMeasureFilter
function by creating an onSelect function of the dropdown
widget.
-
To open the onSelect function, hover over the dropdown widget
in the Layout panel, click and
enter this script:
Sample Code
Utils.setMeasureFilter(Dropdown_Measures.getSelectedKey());
This script gets the selected value of the dropdown and passes it to the
setMeasureFilter function as parameter.
-
Define what will happen when the analytic application is first run by creating the
onInitialization function of the canvas itself.
-
Hover over the Canvas in the
Layout panel, click and select onInitialization.
-
Enter this script in the script editor:
Sample Code
var measures = Table.getDataSource().getMeasures();
var selectedKey = "";
if (measures.length > 0) {
for (var i=0;i<measures.length; i++){
// Measure
Dropdown_Measures.addItem(measures[i].id,measures[i].description);
if (selectedKey === "" && i === 0) {
selectedKey = measures[i].id;
Dropdown_Measures.setSelectedKey(selectedKey);
console.log(["selectedKey ", selectedKey]);
}
console.log(["CurrentMeasure ", measures]);
}
}
Utils.setMeasureFilter(selectedKey);
With this script you make sure that on initialization, you load all the available measures
of the table into your dropdown.
After doing that, you set the selected key to the first measure in that list and then you
set your measure filter to that first measure in your list.
-
Save the application and click Run Analytic
Application.
Results
When you run the application, it looks like this:
When you click on the dropdown, you can select a measure from all the
listed measures that will filter the results of the table or the chart.
You
can click on to switch to the chart and
select Gross Margin Plan, for example:
If you select
Discount, the chart looks like this: