Skip to content

Commit

Permalink
[default-card][ui] Fixes to chart/css/progess bar
Browse files Browse the repository at this point in the history
- pass down options in vega chart
- progress-bar min width for mf UI
- Fix bug when vega-chart spec changes.
- fix `embed` class CSS
  • Loading branch information
valayDave committed Jan 8, 2024
1 parent 6bc7e56 commit 947c2ec
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion metaflow/plugins/cards/card_modules/bundle.css

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions metaflow/plugins/cards/card_modules/main.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion metaflow/plugins/cards/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"dependencies": {
"@iconify/svelte": "^3.1.4",
"svelte-markdown": "^0.4.0",
"svelte-vega": "^2.1.0"
"svelte-vega": "^2.1.0",
"vega": "^5.26.1",
"vega-lite": "^5.16.3"
}
}
1 change: 1 addition & 0 deletions metaflow/plugins/cards/ui/src/components/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
/* if the embed class is present, we hide the aside, and we should center the main */
:global(.embed main) {
margin: 0 auto;
min-width: 80%
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
/* styling a progress bar is trickier than it should be. */
progress::-webkit-progress-bar {
background-color: white !important;
width: 100%;
min-width: 100%;
}
progress {
background-color: white;
Expand Down
6 changes: 3 additions & 3 deletions metaflow/plugins/cards/ui/src/components/vega-chart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import { Vega } from "svelte-vega";
export let componentData: VegaChartComponent
$:({data, spec} = componentData)
$:({data, spec, options} = componentData)
</script>
{#if data && spec}
<Vega data={data} spec={spec} />
<Vega data={data} spec={spec} options={options} />
{:else}
<Vega spec={spec} />
<Vega spec={spec} options={options}/>
{/if}


Expand Down
18 changes: 9 additions & 9 deletions metaflow/plugins/cards/ui/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const cardData: Writable<types.CardResponse | undefined> =
* changes that have come from the parent window.
*/
(window as any).metaflow_card_update = (
dataChanges: Record<string, types.CardComponent>,
dataChanges: Record<string, types.CardComponent>
) => {
console.log("metaflow_card_update", dataChanges, cardData?.update);

Expand All @@ -21,7 +21,7 @@ export const cardData: Writable<types.CardResponse | undefined> =

Object.values(dataChanges).forEach(
(change) =>
newData?.components && findAndMutateTree(newData.components, change),
newData?.components && findAndMutateTree(newData.components, change)
);

return newData;
Expand All @@ -31,37 +31,37 @@ export const cardData: Writable<types.CardResponse | undefined> =

const mutateChartElement = (
chart: types.VegaChartComponent,
newChart: types.VegaChartComponent,
newChart: types.VegaChartComponent
) => {
if (chart.data) {
chart.data = JSON.parse(JSON.stringify(newChart.data)) as Record<
string,
unknown
>;
}
const specHasChanged =
const specHasNotChanged =
JSON.stringify(newChart.spec) === JSON.stringify(chart.spec);

if (specHasChanged) {
if (!specHasNotChanged) {
chart.spec = JSON.parse(JSON.stringify(newChart.spec)) as VisualizationSpec;
}
};

// NOTE: this function mutates the object! Be careful with it.
const findAndMutateTree = (
components: types.CardComponent[],
newComponent: types.CardComponent,
newComponent: types.CardComponent
) => {
const componentIndex = components.findIndex(
(fcomp) => newComponent.id === fcomp?.id,
(fcomp) => newComponent.id === fcomp?.id
);

if (componentIndex > -1) {
if (components[componentIndex].type == "vegaChart") {
// if the component is a vegaChart, we need to merge the data
mutateChartElement(
components[componentIndex] as types.VegaChartComponent,
newComponent as types.VegaChartComponent,
newComponent as types.VegaChartComponent
);
} else {
Object.assign(components[componentIndex], newComponent);
Expand All @@ -84,7 +84,7 @@ export const setCardData: (cardDataId: string) => void = (cardDataId) => {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const data = JSON.parse(
atob((window as any).__MF_DATA__[cardDataId]),
atob((window as any).__MF_DATA__[cardDataId])
) as types.CardResponse;
cardData.set(data);
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions metaflow/plugins/cards/ui/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { VisualizationSpec } from "svelte-vega";

import type { EmbedOptions } from "vega-embed";

export type Route = [string, string];

export type Status = "success" | "error" | "idle" | "in-progress";
Expand Down Expand Up @@ -188,6 +190,7 @@ export interface VegaChartComponent {
id?: string;
spec: VisualizationSpec;
data: Record<string, unknown>;
options?: EmbedOptions;
}
// wrap all component options into a Component type
export type CardComponent =
Expand Down
6 changes: 0 additions & 6 deletions metaflow/plugins/cards/ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
"useDefineForClassFields": true,
"module": "ESNext",
"resolveJsonModule": true,
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable checkJs if you'd like to use dynamic types in JS.
* Note that setting allowJs false does not prevent the use
* of JS in `.svelte` files.
*/
"allowJs": true,
"checkJs": true,
"isolatedModules": true
Expand Down

0 comments on commit 947c2ec

Please sign in to comment.