Is there a way to convert vega-lite-api code to vega-lite JSON code?

Viewed 156

Is there a way to convert vega-lite-api code into vega-lite json format ?

For example automatically turning :

vl.markBar().data('data/movies.json').encode(
  vl.x().fieldQ('IMDB_Rating').bin(true),
  vl.y().count()
)

Into

{
  "mark": "bar",
  "data": {"url": "data/movies.json"},
  "encoding": {
    "x": {
      "bin": true,
      "field": "IMDB_Rating",
      "type": "quantitative"
    },
    "y": {
      "aggregate": "count",
      "type": "quantitative"
    }
  }
}
1 Answers

Yep! Presuming...

var foo = vl.markBar().data('data/movies.json').encode(
  vl.x().fieldQ('IMDB_Rating').bin(true),
  vl.y().count()
)

...just use...

JSON.stringify(foo.toObject(), 0, 2)

Example here.

Related