Google Data Studio – Toggle switch for line or bar chart
Let’s continue the exploration of Google Data Studio parameter feature with this new example. This time, we will combine parameter & related control with the new “IF” function, released last month (March 11, 2021). We will build a simple trend chart and create a control to switch between line and bar chart, designed like a toggle-switch.
As example, it could be useful to display bars for short series Vs draw line if there are a lot of data points.
In the example below, I choose to display data labels only on bar chart to get the exact value of each day. And, on the other hand, I draw just points on the line to see the main trend.
Ingredients for the toggle-switch
To get this result, we will take advantage to the following Data Studio features:
Data source parameter
Chart type parameter: To store and control the choice between line and bar chart
For the interactive selection of chart type
To display our data, we use combo chart but we will display only one visualization at a time: Line or chart
2 chart calculated fields as metrics
To display metric values only for the right and selected chart type, taking advantage to the new “IF” function
Data source parameter
Let’s create a parameter inside our data source to store the options we wish to propose through the slider.
Chart type is a parameter as a range of integers, from 1 to 2. Because sliders can only be used with numeric values, we need to create a parameter based on integers, then we map each integer with the right chart type.
- 1 for bar chart (default value)
- 2 for line chart
Now, we add the slider and we select the parameter Chart type as control field (Data tab) and adapt styles.
We decorate it with 2 icons – created for the report – and add them on the possible static positions of the slider value, side by side, above the slider.
At last, we draw a rectangle element with :
- Rounded angles to make it like the border of a toggle-switch widget
- Light color for the background
- Strong color (same than the slider) for the border
We send it to back and align it with the slider.
Our last action is the creation of the combo chart with the following configuration:
- Data tab
- Date as dimension
- Metric #1 for the bar chart visualization: Chart calculated field (In the example, I use revenue). The IF function allows to create a condition dependency with the parameter value, and keep the metric value only for the selected chart type. Otherwise, the value will be null, so not displayed.
- Metric #2 for the line chart visualization (See also formula below).
- Style tab
- Series #1 as bar, showing data labels
- Series #2 as line, showing points
- Line breaks for missing data
IF(Chart type = 1, Revenue, null)
IF(Chart type = 2, Revenue, null)
Let’s review the full configuration with the video below.
Here a demo report to test the toggle-switch by yourself 🙂