ankane / vega-ruby

Interactive charts for Ruby, powered by Vega and Vega-Lite
BSD 3-Clause "New" or "Revised" License
251 stars 9 forks source link

How to configure a spec and data separately? #2

Closed pmackay closed 3 years ago

pmackay commented 3 years ago

The docs describe how to make API calls to set the data and then build up the chart configuration. Separately a whole chart can be configured using a Vega spec, which includes a data location.

Is there a way to configure a chart using a Vega spec, but make an API call to independently set the data?

Thanks!

ankane commented 3 years ago

Hey @pmackay, if I understand correctly, there's not a way to load an existing Vega spec into a Vega.lite or Vega.start object. Can you share more about the use case?

pmackay commented 3 years ago

The use case is that Airtable has add-on apps, including one to render charts using Vega based on the data in a Base. These are configured with a Vega JSON config that typically has all the config settings except the data source. I would like to recreate those charts in a Rails app!

Sounds like perhaps the best way is to define a separate JSON endpoint that can serve the data from the Rails app and set that as the URL in the config thats passed to the call to Vega.display?

ankane commented 3 years ago

Yeah, you can add a data key to the spec hash directly (spec[:data] = {url: "..."} for a URL) and pass it to Vega.render for Rails (or Vega.display for iRuby) without the need to create a Vega.lite or Vega.start object.

pmackay commented 3 years ago

Great, I have this working, thanks for help!