2

Adding a Dynamic Reference Line to Viz-in-Tooltips

 2 years ago
source link: https://datavis.blog/2022/05/10/viz-in-tooltips-reference-line/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

For a recent visualisation I used a viz-in-tooltip with a reference line that dynamically updated as the user hovers the pointer across the chart to highlight the date currently being hovered over.

This video tutorial walks through how to customise the viz-in-tooltip to remove the default filtering and add the dynamic reference line.

Below are the same steps, with a few more details on how the viz-in-tooltip filtering works in Tableau.

Create the main view

For this tutorial, I’ll use a simple line chart of monthly sales, using the Tableau Superstore sample dataset that comes with Tableau:

Create the viz-in-tooltip view

We’ll use this simple view showing the total monthly profits as a bar chart with positive and negative values being coloured differently to easily differentiate them.

To encode the colour, I used this ad-hoc calculation:

SIGN(SUM([Profit]]))

The SIGN() function returns 1 if the number being passed to the function is positive, zero if the number is zero or -1 if the number is negative. A different colour is assigned to each of those results (excluding zero in this case as there were no months where the sum of profit was exactly zero).

Insert the viz-in-tooltip view into the tooltip

To add the viz-in-tooltip view to the main chart, edit the tooltips and select: 

Insert > Sheets > Profit

Doing this will insert some code into your tooltip that refers to the sheet being inserted along with sizing and filter information:

Default Viz-in-Tooltip

The result of this is that when you hover over a mark (a data point) in the main view, you will see a filtered version of the profit chart in the viz-in-tooltip:

By default, Tableau passes any dimension values in the view through to the viz-in-tooltip chart as a filter, which in this case includes the Order Date at a monthly level of aggregation, so we only see one month of data in the viz-in-tooltip.

On the profits chart you can see this filter (in italics) that has been automatically generated by Tableau:

Remove viz-in-tooltip filters.

To remove this default filtering, we can edit the code that was added to the tooltip when the sheet was inserted and remove the “<All Fields>” section at the end. This will ensure no filter values are sent to the viz-in-tooltip chart:

Before:

<Sheet name="Profit" maxwidth="300" maxheight="300" filter="<All Fields>">

After:

<Sheet name="Profit" maxwidth="300" maxheight="300" filter="">

Viz-in-tooltip with no filters

We now see all months of data in the viz-in-tooltip, which gives more context to the trend of profits, however, it’s not easy to see in the viz-in-tooltip chart which month is being hovered over in the main chart. 

Add a reference line

On the viz-in-tooltip chart, add a reference line on the date axis

Right click on the axis > Add Reference Line

In the popup that appears, in the Value drop-down, click “Create a New Parameter…

In the parameter dialog that shows, give the parameter a name and set the Display format to be Month Year, from the drop down list (it may show as “March 2001”) as the date is at the monthly granularity in the chart.

Click OK and then format the reference line as below to ensure it is easy to see in the viz-in-tooltip chart.

Ignore the current value, we’ll change that in the next step.

Add a Parameter Action

We now need a way to update the reference line when we hover over the Sales chart. On the sales view, go to the Worksheet menu and select Actions:

Select Add Action > Change Parameter

Then update the following settings: 

Now when you hover over a mark in the sales view the viz-in-tooltip will update the reference line in the viz-in-tooltip to match the month you hover over:

Adjust the size of the viz-in-tooltip

If required you can also update the size of the viz-in-tooltip chart by changing the below values in the tooltip, as I did in this case to: 450 px width and 250 px height:

<Sheet name="Profit" maxwidth="450" maxheight="250" filter="">

If you want to learn a lot more about tooltips, take a look at this video tutorial by Tim Ngwena.

Workbook

Interact with or download the workbook from my Tableau Public profile here.

Thanks for reading!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK