Skip to content

Commit

Permalink
[default-card][ui] make chart components updatable
Browse files Browse the repository at this point in the history
  • Loading branch information
valayDave committed Oct 25, 2023
1 parent ccaa239 commit 068db2f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 42 deletions.
4 changes: 2 additions & 2 deletions metaflow/plugins/cards/card_modules/main.js

Large diffs are not rendered by default.

53 changes: 34 additions & 19 deletions metaflow/plugins/cards/ui/src/components/bar-chart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
} from "chart.js";
import type { ChartConfiguration } from "chart.js";
import { COLORS_LIST } from "../constants";
import { onMount } from "svelte";
Chart.register(
BarElement,
Expand All @@ -24,29 +25,43 @@
$: ({ config, data, labels } = componentData);
let el: HTMLCanvasElement;
let chart: Chart;
const chartConfiguration: ChartConfiguration = config || {
type: "bar",
data: {
labels,
datasets: [
{
backgroundColor: COLORS_LIST[2],
borderColor: COLORS_LIST[2],
data: data || [],
},
],
},
options: {
plugins: {
legend: {
display: false,
const createChart = () => {
const chartConfiguration: ChartConfiguration = config || {
type: "bar",
data: {
labels,
datasets: [
{
backgroundColor: COLORS_LIST[2],
borderColor: COLORS_LIST[2],
data: data || [],
},
],
},
options: {
plugins: {
legend: {
display: false,
},
},
},
},
};
};
chart = new Chart(el, chartConfiguration);
}
onMount(createChart);
$: el && new Chart(el, chartConfiguration);
$: {
if (chart) {
const { data, labels,} = componentData;
chart.data.labels = labels;
chart.data.datasets[0].data = data || [];
chart.update();
}
}
</script>

<div data-component="bar-chart">
Expand Down
54 changes: 33 additions & 21 deletions metaflow/plugins/cards/ui/src/components/line-chart.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- render a line chart using chart.js, note, we do tree-shaking method, so register any components as needed -->
<script lang="ts">
import type * as types from "../types";
import {
Expand All @@ -11,6 +10,7 @@
} from "chart.js";
import type { ChartConfiguration } from "chart.js";
import { COLORS_LIST } from "../constants";
import { onMount } from "svelte";
Chart.register(
LineElement,
Expand All @@ -21,32 +21,44 @@
);
export let componentData: types.LineChartComponent;
$: ({ config, data, labels } = componentData);
let el: HTMLCanvasElement;
let chart: Chart;
const chartConfiguration: ChartConfiguration = config || {
type: "line",
data: {
labels,
datasets: [
{
backgroundColor: COLORS_LIST[2],
borderColor: COLORS_LIST[2],
data: data || [],
},
],
},
options: {
plugins: {
legend: {
display: false,
const createChart = () => {
const { config, data, labels } = componentData;
const chartConfiguration: ChartConfiguration = config || {
type: "line",
data: {
labels,
datasets: [
{
backgroundColor: COLORS_LIST[2],
borderColor: COLORS_LIST[2],
data: data || [],
},
],
},
options: {
plugins: {
legend: {
display: false,
},
},
},
},
};
chart = new Chart(el, chartConfiguration);
};
$: el && new Chart(el, chartConfiguration);
onMount(createChart);
$: {
if (chart) {
const { data, labels,} = componentData;
chart.data.labels = labels;
chart.data.datasets[0].data = data || [];
chart.update();
}
}
</script>

<div data-component="line-chart">
Expand Down

0 comments on commit 068db2f

Please sign in to comment.