use crate::Vegalite;
use anyhow::Error;
use serde_json;
use showata::ContentInfo;
use showata::Showable;
impl Showable for Vegalite {
fn to_content_info(&self) -> Result<ContentInfo, Error> {
let content = serde_json::to_string(self)?;
Ok(ContentInfo {
content,
mime_type: "application/vnd.vegalite.v4+json".into(),
})
}
fn to_html_page(&self) -> Result<String, Error> {
let dod = self.to_content_info()?;
let content = VEGA_EMBED_HTML_TEMPLATE.replace("{{ spec_as_json }}", &dod.content);
Ok(content)
}
}
const VEGA_EMBED_HTML_TEMPLATE: &str = r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Import Vega 5 & Vega-Lite 4 (does not have to be from CDN) -->
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@4"></script>
<!-- Import vega-embed -->
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
</head>
<body>
<div id="vis"></div>
<script type="text/javascript">
var spec = {{ spec_as_json }};
vegaEmbed('#vis', spec).then(function(result) {
// Access the Vega view instance (https://vega.github.io/vega/docs/api/view/) as result.view
}).catch(console.error);
</script>
</body>
</html>
"#;