Skip to content

Commit

Permalink
feat: expose resulting Vega View (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Cabrera <angel_cabrera@apple.com>
  • Loading branch information
cabreraalex and Alex Cabrera authored May 20, 2021
1 parent 58e524d commit e933cf3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
38 changes: 12 additions & 26 deletions packages/sample-project/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import { Vega, VegaLite } from "svelte-vega";
import type { VisualizationSpec } from "svelte-vega";
import type { VisualizationSpec, View } from "svelte-vega";
let dataVL = {
const data = {
table: [
{ category: "A", amount: 28 },
{ category: "B", amount: 55 },
Expand All @@ -14,6 +14,8 @@
{ category: "H", amount: 87 },
],
};
let viewVL: View;
let specVL: VisualizationSpec = {
$schema: "https://vega.github.io/schema/vega-lite/v5.json",
description: "A simple bar chart with embedded data.",
Expand All @@ -27,18 +29,7 @@
},
};
const dataV = {
table: [
{ category: "A", amount: 28 },
{ category: "B", amount: 55 },
{ category: "C", amount: 43 },
{ category: "D", amount: 91 },
{ category: "E", amount: 81 },
{ category: "F", amount: 53 },
{ category: "G", amount: 19 },
{ category: "H", amount: 87 },
],
};
let viewV: View;
const specV: VisualizationSpec = {
$schema: "https://vega.github.io/schema/vega/v5.json",
width: 400,
Expand All @@ -48,16 +39,6 @@
data: [
{
name: "table",
values: [
{ category: "A", amount: 28 },
{ category: "B", amount: 55 },
{ category: "C", amount: 43 },
{ category: "D", amount: 91 },
{ category: "E", amount: 81 },
{ category: "F", amount: 53 },
{ category: "G", amount: 19 },
{ category: "H", amount: 87 },
],
},
],
Expand Down Expand Up @@ -132,11 +113,15 @@
},
],
};
$: viewVL ? console.log("Vega-Lite view: ", viewVL.data("table")) : "";
$: viewV ? console.log("Vega view: ", viewV.data("table")) : "";
</script>

<main>
<Vega data={dataV} spec={specV} />
<VegaLite data={dataVL} spec={specVL} />
<Vega {data} spec={specV} bind:view={viewV} />
<VegaLite {data} spec={specVL} bind:view={viewVL} />
</main>

<style>
Expand All @@ -152,4 +137,5 @@
max-width: none;
}
}
</style>
5 changes: 4 additions & 1 deletion packages/svelte-vega/src/Vega.svelte
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<script lang="ts">
import type { EmbedOptions, Mode } from "vega-embed";
import type { SignalListeners, VisualizationSpec } from "./types";
import type { SignalListeners, VisualizationSpec, View } from "./types";
import VegaEmbed from "./VegaEmbed.svelte";
export let view: View | undefined;
export let spec: VisualizationSpec;
export let options: EmbedOptions = {};
export let data: Record<string, unknown> = {};
export let signalListeners: SignalListeners = {};
const mode = "vega" as Mode;
$: vegaOptions = { ...options, mode: mode };
</script>

<VegaEmbed
{spec}
{data}
{signalListeners}
options={vegaOptions}
bind:view
on:onNewView
on:onError
/>
18 changes: 11 additions & 7 deletions packages/svelte-vega/src/VegaEmbed.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script lang="ts">
import { createEventDispatcher, onDestroy } from "svelte";
import type { EmbedOptions, Result } from "vega-embed";
import type { SignalListeners, View, VisualizationSpec } from "./types";
import { createEventDispatcher, onDestroy } from "svelte";
import vegaEmbed from "vega-embed";
import { WIDTH_HEIGHT } from "./constants";
import type { SignalListeners, View, VisualizationSpec } from "./types";
import {
addSignalListenersToView,
updateMultipleDatasetsInView,
Expand All @@ -15,6 +17,7 @@
export let options: EmbedOptions;
export let spec: VisualizationSpec;
export let view: View | undefined;
export let signalListeners: SignalListeners = {};
export let data: Record<string, unknown> = {};
Expand Down Expand Up @@ -55,7 +58,7 @@
newSignalListeners,
oldSignalListeners
);
const { view } = result;
view = result.view;
if (specChanges.width !== false) {
view.width(specChanges.width);
}
Expand All @@ -76,7 +79,7 @@
!shallowEqual(newSignalListeners, oldSignalListeners) &&
result !== undefined
) {
const { view } = result;
view = result.view;
if (oldSignalListeners) {
removeSignalListenersFromView(view, oldSignalListeners);
}
Expand All @@ -99,9 +102,8 @@
async function createView() {
clearView();
try {
console.info("Creating new view");
result = await vegaEmbed(chartContainer, spec, options);
const { view } = result;
view = result.view;
if (addSignalListenersToView(view, signalListeners)) {
view.runAsync();
}
Expand All @@ -115,6 +117,7 @@
if (result) {
result.finalize();
result = undefined;
view = undefined;
}
}
Expand All @@ -135,11 +138,12 @@
async function update() {
if (data && Object.keys(data).length > 0 && result !== undefined) {
const { view } = result;
view = result.view;
updateMultipleDatasetsInView(view, data);
await view.resize().runAsync();
}
}
</script>

<div bind:this={chartContainer} />
5 changes: 4 additions & 1 deletion packages/svelte-vega/src/VegaLite.svelte
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<script lang="ts">
import type { EmbedOptions, Mode } from "vega-embed";
import type { SignalListeners, VisualizationSpec } from "./types";
import type { SignalListeners, VisualizationSpec, View } from "./types";
import VegaEmbed from "./VegaEmbed.svelte";
export let view: View | undefined;
export let spec: VisualizationSpec;
export let options: EmbedOptions = {};
export let data: Record<string, unknown> = {};
export let signalListeners: SignalListeners = {};
const mode = "vega-lite" as Mode;
$: vegaLiteOptions = { ...options, mode: mode };
</script>

<VegaEmbed
{spec}
{data}
{signalListeners}
options={vegaLiteOptions}
bind:view
on:onNewView
on:onError
/>

0 comments on commit e933cf3

Please sign in to comment.