Converting from Vega#
Use the Vega functions for charts authored in Vega or already compiled to Vega from Vega-Lite. VlConvert parses and evaluates the specification directly, so the Vega-Lite theme and vl_version options do not apply.
Available outputs: SVG, PNG, JPEG, PDF, HTML, a Vega Editor URL, and an evaluated scenegraph.
Save this Vega specification:
chart.vg.json
{
"$schema": "https://vega.github.io/schema/vega/v6.json",
"width": 300,
"height": 180,
"padding": 5,
"data": [
{
"name": "table",
"values": [
{"category": "A", "value": 28},
{"category": "B", "value": 55},
{"category": "C", "value": 43},
{"category": "D", "value": 91},
{"category": "E", "value": 81}
]
}
],
"scales": [
{
"name": "xscale",
"type": "band",
"domain": {"data": "table", "field": "category"},
"range": "width",
"padding": 0.1
},
{
"name": "yscale",
"domain": {"data": "table", "field": "value"},
"nice": true,
"range": "height"
}
],
"axes": [
{"orient": "bottom", "scale": "xscale"},
{"orient": "left", "scale": "yscale"}
],
"marks": [
{
"type": "rect",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"scale": "xscale", "field": "category"},
"width": {"scale": "xscale", "band": 1},
"y": {"scale": "yscale", "field": "value"},
"y2": {"scale": "yscale", "value": 0}
},
"update": {"fill": {"value": "#4c78a8"}}
}
}
]
}
use vl_convert_rs::{VgOpts, VlConverter};
let spec = std::fs::read_to_string("chart.vg.json")?;
let converter = VlConverter::new();
let output = converter
.vega_to_svg(spec, VgOpts::default(), Default::default())
.await?;
std::fs::write("chart.svg", output.svg)?;
The SVG text is in output.svg, and Vega’s diagnostic messages are in output.logs.
The SVG conversion renders this chart:
Size, locale, and other per-conversion options work the same way as for Vega-Lite. See Image Size and Quality Recommendations, Number and Time Locales, and Conversion Overrides.