Skip to content

Commit 1b73af4

Browse files
committed
[default-card][ui] make chart components updatable
1 parent cbb4dd1 commit 1b73af4

File tree

3 files changed

+69
-42
lines changed

3 files changed

+69
-42
lines changed

metaflow/plugins/cards/card_modules/main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

metaflow/plugins/cards/ui/src/components/bar-chart.svelte

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
} from "chart.js";
1212
import type { ChartConfiguration } from "chart.js";
1313
import { COLORS_LIST } from "../constants";
14+
import { onMount } from "svelte";
1415
1516
Chart.register(
1617
BarElement,
@@ -24,29 +25,43 @@
2425
$: ({ config, data, labels } = componentData);
2526
2627
let el: HTMLCanvasElement;
28+
let chart: Chart;
2729
28-
const chartConfiguration: ChartConfiguration = config || {
29-
type: "bar",
30-
data: {
31-
labels,
32-
datasets: [
33-
{
34-
backgroundColor: COLORS_LIST[2],
35-
borderColor: COLORS_LIST[2],
36-
data: data || [],
37-
},
38-
],
39-
},
40-
options: {
41-
plugins: {
42-
legend: {
43-
display: false,
30+
const createChart = () => {
31+
const chartConfiguration: ChartConfiguration = config || {
32+
type: "bar",
33+
data: {
34+
labels,
35+
datasets: [
36+
{
37+
backgroundColor: COLORS_LIST[2],
38+
borderColor: COLORS_LIST[2],
39+
data: data || [],
40+
},
41+
],
42+
},
43+
options: {
44+
plugins: {
45+
legend: {
46+
display: false,
47+
},
4448
},
4549
},
46-
},
47-
};
50+
};
51+
chart = new Chart(el, chartConfiguration);
52+
}
53+
54+
55+
onMount(createChart);
4856
49-
$: el && new Chart(el, chartConfiguration);
57+
$: {
58+
if (chart) {
59+
const { data, labels,} = componentData;
60+
chart.data.labels = labels;
61+
chart.data.datasets[0].data = data || [];
62+
chart.update();
63+
}
64+
}
5065
</script>
5166

5267
<div data-component="bar-chart">

metaflow/plugins/cards/ui/src/components/line-chart.svelte

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<!-- render a line chart using chart.js, note, we do tree-shaking method, so register any components as needed -->
21
<script lang="ts">
32
import type * as types from "../types";
43
import {
@@ -11,6 +10,7 @@
1110
} from "chart.js";
1211
import type { ChartConfiguration } from "chart.js";
1312
import { COLORS_LIST } from "../constants";
13+
import { onMount } from "svelte";
1414
1515
Chart.register(
1616
LineElement,
@@ -21,32 +21,44 @@
2121
);
2222
2323
export let componentData: types.LineChartComponent;
24-
$: ({ config, data, labels } = componentData);
25-
2624
let el: HTMLCanvasElement;
25+
let chart: Chart;
2726
28-
const chartConfiguration: ChartConfiguration = config || {
29-
type: "line",
30-
data: {
31-
labels,
32-
datasets: [
33-
{
34-
backgroundColor: COLORS_LIST[2],
35-
borderColor: COLORS_LIST[2],
36-
data: data || [],
37-
},
38-
],
39-
},
40-
options: {
41-
plugins: {
42-
legend: {
43-
display: false,
27+
const createChart = () => {
28+
const { config, data, labels } = componentData;
29+
const chartConfiguration: ChartConfiguration = config || {
30+
type: "line",
31+
data: {
32+
labels,
33+
datasets: [
34+
{
35+
backgroundColor: COLORS_LIST[2],
36+
borderColor: COLORS_LIST[2],
37+
data: data || [],
38+
},
39+
],
40+
},
41+
options: {
42+
plugins: {
43+
legend: {
44+
display: false,
45+
},
4446
},
4547
},
46-
},
48+
};
49+
chart = new Chart(el, chartConfiguration);
4750
};
4851
49-
$: el && new Chart(el, chartConfiguration);
52+
onMount(createChart);
53+
54+
$: {
55+
if (chart) {
56+
const { data, labels,} = componentData;
57+
chart.data.labels = labels;
58+
chart.data.datasets[0].data = data || [];
59+
chart.update();
60+
}
61+
}
5062
</script>
5163

5264
<div data-component="line-chart">

0 commit comments

Comments
 (0)