Sunday, March 31, 2019

Vega tooltips and format()

This took me an inordinately long time to figure out, so I'll document it here. When writing a Vega description of a chart and including a tooltip, the accessor methods for datum cannot be used inside of format(). For example, this doesn't work (it always displays 'NaN'):

"tooltip": {"signal": "{title: datum.​name, 'CO2eq': format(datum.​size, '.​2f') + ' Gigatons'}"}

This does:

"tooltip": {"signal": "{title: datum.​name, 'CO2eq': format(datum['size'], '.​2f') + ' Gigatons'}"}

Note the difference inside the format(), using datum['size'] instead of datum.size.