vega / vl-convert

Utilities for converting Vega-Lite specs from the command line and Python
BSD 3-Clause "New" or "Revised" License
89 stars 9 forks source link

Add conversions to Vega scenegraph #122

Closed jonmmease closed 9 months ago

jonmmease commented 9 months ago

Adds Rust and Python functions for converting Vega and Vega-Lite specs to the Vega scene graph JSON format.

I didn't add CLI entry points as this isn't a standard file format. The main motivation here is to support testing in the development of alternative Vega renderers.

cc @lsh


Here are some interesting timing observations for a simple scatterplot based on https://altair-viz.github.io/gallery/scatter_tooltips.html

import altair as alt
import pandas as pd
alt.data_transformers.disable_max_rows()
from vega_datasets import data

source = pd.concat([data.cars()]*500, axis=0).reset_index(drop=True)
print(len(source))

chart = alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
).interactive()
chart_json = chart.to_dict()
# chart

The time to compute the scenegraph

%%time
png = vlc.vegalite_to_scenegraph(chart_json)
CPU times: user 4.94 s, sys: 1.1 s, total: 6.04 s
Wall time: 4.24 s

The time to render to PNG

%%time
png = vlc.vegalite_to_scenegraph(chart_json)
CPU times: user 7.48 s, sys: 1.12 s, total: 8.6 s
Wall time: 6.82 s

Ignoring any serialization overhead, this suggests that, for this example, Vega takes about 60% of the total render time to generate the scenegraph. The other 40% is taken by the Vega SVG renderer and resvg to render the SVG to PNG. If we had a GPU accelerated renderer, we could potentially shrink this 40% significantly, but a large chunk of the runtime is the generation of the scene graph, which a renderer wouldn't improve.