mirror of
https://github.com/livebook-dev/livebook.git
synced 2024-11-17 21:33:16 +08:00
7804ff1d82
* Set up Vega-Lite plots rendering * Automatically recognise VegaLite specification * Improve matching VegaLite result * Update naming * StringFormatter -> DefaultFormatter
42 lines
896 B
JavaScript
42 lines
896 B
JavaScript
import vegaEmbed from "vega-embed";
|
|
import { getAttributeOrThrow } from "../lib/attribute";
|
|
|
|
/**
|
|
* A hook used to render graphics according to the given
|
|
* Vega-Lite specification.
|
|
*
|
|
* The hook expects a `vega_lite:<id>` event with `{ spec }` payload,
|
|
* where `spec` is the graphic definition as an object.
|
|
*
|
|
* Configuration:
|
|
*
|
|
* * `data-id` - plot id
|
|
*
|
|
*/
|
|
const VegaLite = {
|
|
mounted() {
|
|
this.props = getProps(this);
|
|
this.state = {
|
|
container: null,
|
|
};
|
|
|
|
this.state.container = document.createElement("div");
|
|
this.el.appendChild(this.state.container);
|
|
|
|
this.handleEvent(`vega_lite:${this.props.id}`, ({ spec }) => {
|
|
vegaEmbed(this.state.container, spec, {});
|
|
});
|
|
},
|
|
|
|
updated() {
|
|
this.props = getProps(this);
|
|
},
|
|
};
|
|
|
|
function getProps(hook) {
|
|
return {
|
|
id: getAttributeOrThrow(hook.el, "data-id"),
|
|
};
|
|
}
|
|
|
|
export default VegaLite;
|