Skip to content
/ vegagoja Public

Go package to render Vega and Vega Lite visualizations as SVGs

License

Notifications You must be signed in to change notification settings

xo/vegagoja

Repository files navigation

vegagoja

vegagoja renders Vega and Vega-Lite visualizations as SVGs using the goja JavaScript runtime. Developed for use by usql for rendering charts.

Overview | TODO | About

Unit Tests Go Reference Discord Discussion

Overview

Install in the usual Go fashion:

$ go get github.com/xo/vegagoja@latest

Then use like the following:

package vegagoja_test

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/xo/vegagoja"
)

func Example() {
	vega := vegagoja.New(
		vegagoja.WithDemoData(),
	)
	data, err := vega.Render(context.Background(), candlestickSpec)
	if err != nil {
		log.Fatal(err)
	}
	if err := os.WriteFile("candestick.svg", []byte(data), 0o644); err != nil {
		log.Fatal(err)
	}
	// Output:
}

const candlestickSpec = `{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "width": 400,
  "description": "A candlestick chart inspired by an example in Protovis (http://mbostock.github.io/protovis/ex/candlestick.html)",
  "data": {"url": "data/ohlc.json"},
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "title": "Date in 2009",
      "axis": {
        "format": "%m/%d",
        "labelAngle": -45,
        "title": "Date in 2009"
      }
    },
    "y": {
      "type": "quantitative",
      "scale": {"zero": false},
      "axis": {"title": "Price"}
    },
    "color": {
      "condition": {
        "test": "datum.open < datum.close",
        "value": "#06982d"
      },
      "value": "#ae1325"
    }
  },
  "layer": [
    {
      "mark": "rule",
      "encoding": {
        "y": {"field": "low"},
        "y2": {"field": "high"}
      }
    },
    {
      "mark": "bar",
      "encoding": {
        "y": {"field": "open"},
        "y2": {"field": "close"}
      }
    }
  ]
}`

TODO

About

vegagoja was written primarily to support these projects:

  • usql - a universal command-line interface for SQL databases

Users of this package may find the github.com/xo/resvg package helpful in rendering generated SVGs.