The task here is to apply interactivity in a value-adding way to a data with a regression.
Interactivity might be regarded unnecessary. However, the few seconds that a user spends more on your message when trying out this interactivity might make the difference, if he or she buys in on your message.
This solutions copies much of the Altair documentation example, see Altair - Multi-Line Tooltip.
This solution continues a previous example using Linear Regression
Some adaptations might be worth mentioning.
First, the original chart already consists out of 4 layers. Adding this multi-line interactivity adds another 5 layers.
chart = chart + selectors + rules + regrText + modelText + testText
The layers for ‘nearest’ and selectors and rules were copied unchanged, with minor or obvious modifications.
A noteworthy feature is the positioning of the text using an existing layer. For instance, the values of the prediction are positioned by using the layer for the regression.
regrText = myRegression.mark_text( align=”left”, dx=10, dy=15, fontSize=16, fontWeight=200, color=’green’, ).encode(text=alt.condition(nearest, “Profit_y:Q”, alt.value(“ “)))
This implies that the text to be displayed must be available in the dataframe that is used with the original layers.