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"}}
      }
    }
  ]
}

Send a JSON body with the Vega specification in spec to the endpoint for the output you need: /vega/svg, /vega/png, /vega/jpeg, /vega/pdf, /vega/html, /vega/url, or /vega/scenegraph. All are POST endpoints. For example, save this body as request.json:

request.json
{
  "spec": {
    "$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"
            }
          }
        }
      }
    ]
  }
}
$ curl http://127.0.0.1:3000/vega/svg \
>   -H 'Content-Type: application/json' \
>   --data-binary @request.json \
>   --output chart.svg

The request fields match the Vega-Lite endpoints, minus theme and vl_version. See Server API for the schemas.

The SVG conversion renders this chart:

Vega bar chart with five bars labeled A through E

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.