altair
altair copied to clipboard
Tooltip is not working with condition
Error message:
SchemaValidationError: Tooltip has no parameter named 'condition' Existing parameter names are: shorthand bin format title aggregate condition formatType type bandPosition field timeUnit See the help for Tooltip to read the full description of these parameters
Repro:
selection_chart.encoding.tooltip[0].condition = alt.condition(alt.datum.type1 > 0, 'type1', alt.value(''))
or
"tooltip": [alt.Tooltip('Date'), alt.Tooltip('type1:Q', condition=alt.condition(alt.datum.type1 > 0, 'type1', alt.value('')))]
I'm not sure why this fails but you should be able to achieve the same thing by calculating a new field using transform_calculate
and then using that one in the tooltip instead of type1
. Would that work for you?
@binste Thanks, that's good to know. Actually, I want to filter out zero/nan values in the tooltip and possibly sort the tooltip by values: https://github.com/vega/vega-lite/issues/9152, so I'm not sure whether condition
could satisfy this need (I doubt it will still show the field in the tooltip anyway, just with a different value?). I've had some conversation with @joelostblom here https://github.com/altair-viz/altair/issues/2393. Appreciate it if you could give some guidance for this!
I never tried setting a condition on a tooltip but I'd expect that it would still show an entry in the tooltip for that field but you can choose what value is shown but that's just a guess. I'm also curious to see if someone knows more in the Vega-Lite issue you opened :)
@binste Thanks. From your knowledge, would this be possible by modifying the altair side only? I'm trying to understand how altair passes these tooltip specification to the frontend, for now I only see it's a list of Tooltip
objects with shorthand
set to the field/column name:
[ 0:"Tooltip({ shorthand: 'Date' })" 1:"Tooltip({ shorthand: 'type1' })" 2:"Tooltip({ shorthand: 'type2' })" 3:"Tooltip({ shorthand: 'type3' })" 4:"Tooltip({ shorthand: 'type4' })" 5:"Tooltip({ shorthand: 'type5' })" 6:"Tooltip({ shorthand: 'type6' })" 7:"Tooltip({ shorthand: 'type7' })" 8:"Tooltip({ shorthand: 'type8' })" ]
Altair produces Vega-Lite specifications, see Altair Internals. These specifications are often passed to vega-embed to be rendered in a frontend such as when you display an Altair chart in a Jupyter notebook.
This means that you can't modify Altair to change the rendering as Altair is not involved in the rendering process at all. Hence, what you are looking for is only possible in Altair if it is possible in Vega-Lite.
I hope this helps.
@binste This is super helpful, thanks!
A QQ: what is the best approach to test local changes of vega-lite? At the beginning I followed the instructions in https://github.com/vega/editor - running yarn link
in vega-lite
and yarn link vega-lite
in editor
, but there's an error and I opened an issue here https://github.com/vega/editor/issues/1366. It seems related to https://github.com/vega/editor/issues/1346 but I've tried cloning an older version mentioned there but no luck.
Sorry I can't help you with that as I'm not very familiar with developing Vega-Lite. But using the editor in the way you described it is the best way I'm aware of.
It seems like although the tooltip encoding supports multiple fields there is no way to express this in a condition. This would need to be added in VL and the relevant issue tracking it is now https://github.com/vega/vega-lite/issues/8750. I'm closing this as there is no action to take in altair. The functionality will be available after it is implemented in VL.