简体   繁体   中英

How can we proramatically generate vega grammar JSON from vega-lite plot?

I am able to get vega json grammar on local installation of nodejs with vega-lite and vega-lite-api installed through npm as follows:

vl = require('vega-lite') 
vla = require('vega-lite-api') 
data = require('vega-datasets') 

const df2 = data['movies.json']()

const plot = vla.markBar().data(df2).encode(
    vla.x().fieldQ('IMDB_Rating').bin(true),
    vla.y().count()
  )

vega_lite_json = plot.toObject();
vega_json = vl.compile(plot.toObject()).spec;

However I am unable to make it work in Observablehq.

I am not able to do last line: vl.compile(...) . This is what I have did:

vegalite = require("@observablehq/vega-lite@0.2")
vegalite.compile(plot.toObject()).spec;

It says: TypeError: vegalite.compile is not a function Here is link to my observablehq notebook cell executing vegalie.compile(...) .

What I am doing wrong?

the vegalite compile option is located within the vl object, like this: vl.vegalite.compile(...)

Here's a notebook with the modified code: https://observablehq.com/@shan/untitled/6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM