Skip to content

The Vega Visualization Library (vega)

The Vega Visualization Library

Module: vega

View source
vega

Module containing a function to display a relation R as a Vega chart, if supported by the client.

  • plot[R]: Given a relation representing a full JSON Vega schema, richly render it as a Vega chart. See the Vega docs (opens in a new tab) for more information on creating a Vega schema.

Example

def chart:width = 200
def chart:height = 200
def chart[:data, :[], 1] = {
  (:name, "table");
  :values, :[], {
    (1, :x, 1);
    (1, :y, 28);
    (2, :x, 3);
    (2, :y, 55);
    (3, :x, 5);
    (3, :y, 43)
  }
}
def chart[:scales, :[], 1] = {
  (:name, "xscale");
  (:domain, {(:data, "table"); (:field, "x")});
  (:range, "width");
}
def chart[:scales, :[], 2] = {
  (:name, "yscale");
  (:domain, {(:data, "table"); (:field, "y")});
  (:range, "height");
}
def chart[:marks, :[], 1] = {
  (:type, "line");
  (:from, :data, "table");
  (:encode, :enter, {
    (:x, {(:scale, "xscale"); (:field, "x")});
    (:y, {(:scale, "yscale"); (:field, "y")})
  })
}
 
def output = vega:plot[chart]

Outputs the custom Rel Vega MIME type along with the chart relation tuples, for the plot to be rendered by Vega in a client.

Note: a false value entered in one of the chart tuples (e.g. setting /:scales/:[]/1/:zero to false) will get compiled away and not exist in the specification at plot time. However, a false value loaded from JSON will not get compiled away - see the example below:

def chart = parse_json[\"\"\"
{
  "width": 200,
  "height": 200,
 
  "data": [
    {
      "name": "table"
    }
  ],
 
  "scales": [
    {
      "name": "xscale",
      "domain": {"data": "table", "field": "x"},
      "range": "width",
      "zero": false
    },
    {
      "name": "yscale",
      "domain": {"data": "table", "field": "y"},
      "range": "height"
    }
  ],
 
  "marks": [
    {
      "type": "line",
      "from": {"data":"table"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "x"},
          "y": {"scale": "yscale", "field": "y"}
        }
      }
    }
  ]
}
\"\"\"]
 
def data = {(1, 28); (3, 55); (5, 43)}
def chart[:data][:[]][1][:values][:[]][i] =
  {(:x, x); (:y, y)} from x,y where enumerate[data](i, x, y)
 
def output = vega:plot[chart]

Combines the Vega JSON specification without data, with data defined in Rel. Outputs the custom Rel Vega MIME type along with the chart relation tuples.

Checkout the vega-lite library.

Was this doc helpful?