DougBurke / hvega

Create Vega-Lite visualizations from Haskell.
BSD 3-Clause "New" or "Revised" License
52 stars 11 forks source link

Add `embed` function #202

Open dschrempf opened 1 year ago

dschrempf commented 1 year ago

Hi! I am referring to the Graphics.Vega.VegaLite.Output module. I suggest extracting and also exporting an embed function that allows embedding of visualizations into html files. That is (this uses blaze-html, but we could stick to using plain text):

embed :: VegaLite -> H.Html
embed x = mconcat [ H.div mempty H.! A.id "vis", H.script script H.! A.type_ "text/javascript"]
  where
    script = H.toHtml $ T.unlines [ "var spec = " <> spec <> ";", "vegaEmbed('#vis', spec);"]
    spec = A.encodeToLazyText $ fromVL x

Then, toHtmlWith could reuse this function, and visualizations can directly be embedded into other HTML files.

Further, one could also export a vegaHeader function that includes the java script parts of vega-embed.

EDIT: I just noticed that we need to provide a name so that we can embed more than one visualization in one HTML file.

DougBurke commented 1 year ago

You can see I've been ignoring the embedding stuff (see how long #107 has been waiting for me to look at it). I don't have much time/energy for hvega so I'd need someone to champion this/write the code.