vega / vegafusion

Serverside scaling for Vega and Altair visualizations
https://vegafusion.io
BSD 3-Clause "New" or "Revised" License
317 stars 17 forks source link

Support format expression function #409

Closed mattijn closed 11 months ago

mattijn commented 11 months ago

The following spec renders fine:

import altair as alt
url = 'https://gist.githubusercontent.com/mattijn/73efd6796132c997ae2dcad45e8b7873/raw/41512de3da5bbc372ac40d9ae18519a46ac5426c/doy_thresholds.csv'
chart = alt.Chart(url, height=10).mark_area(interpolate='monotone').encode(
    x=alt.X('doy:O', sort='ascending').bin(maxbins=20),
    y=alt.Y('sum(value):Q').axis(None).scale(range=[10, -10]),
    row=alt.Row('variable:O').title(None).header(labelAngle=0, labelAlign='left')
).configure_facet(
    spacing=0
).configure_view(
    stroke=None
)
chart
image

But when trying to extract the transformed data it errors:

chart.transformed_data()
ValueError: Pre-transform error: Requested variable (Variable { name: "source_0", namespace: Data }, [])
 requires transforms or signal expressions that are not yet supported

When looking to the spec, I'm not sure which transform is being utilised that is not yet supported.

jonmmease commented 11 months ago

Looking at the generated Vega spec, there is this transform

        {
          "type": "formula",
          "expr": "!isValid(datum[\"bin_maxbins_20_doy\"]) || !isFinite(+datum[\"bin_maxbins_20_doy\"]) ? \"null\" : format(datum[\"bin_maxbins_20_doy\"], \"\") + \" – \" + format(datum[\"bin_maxbins_20_doy_end\"], \"\")",
          "as": "bin_maxbins_20_doy_range"
        },

The issue is that the format expression function isn't implemented yet. I have an old Rust implementation of the Vega format function that I developed a while back for another project. I'll see if I can port that over to VegaFusion. Although, it may also be worth adding a special case for format(value, "") that just transforms this into toString(value), as this will be easier to support across SQL backends.

Also cross reference https://github.com/hex-inc/vegafusion/issues/336 for better error messages when something isn't supported.