-
Hey gang! Coming from tons of experience with React/Next and Vue (and Qt, and countless other things). Turned to Got started minimally with the Van+Vite example, have been having a blast with it! Been able to get going in minutes, and the docs are super helpful (if a bit UX-unfriendly to navigate for the API reference). However, I ran into my first fast block (and poked around van's minimal internals to see what's what and why):
import van from "vanjs-core";
import LibChart from "chart.js/auto";
import type { ChartConfiguration } from "chart.js/auto";
const { div, canvas } = van.tags;
export const Chart = (config: ChartConfiguration) => {
const chart = new LibChart(canvas({ id: "simChart" }), config);
return div(
chart.canvas,
);
} And I want to create a quick reactive UI graph with 2-dimensional array data like so: import "./style.css";
import van from "vanjs-core";
import { Chart } from "./chart";
const { div, h1, p, button, code } = van.tags;
const app = document.querySelector<HTMLDivElement>("#app")!;
const SimChart = (chartData: [number,number][]) => {
const unzip = (arr: [number, number][]) => {
const xs: number[] = [];
const ys: number[] = [];
arr.forEach(([x, y]) => {
xs.push(x);
ys.push(y);
});
return [xs, ys];
}
const [xList, yList] = unzip(van.val(chartData));
return Chart({
type: "line",
data: {
labels: xList,
datasets: [
{
label: "Global Cap (T)",
backgroundColor: "#0eee8c",
borderColor: "#0eee8c",
data: yList,
},
],
},
options: {
scales: {
x: {
title: {
display: true,
text: "Day",
},
},
y: {
title: {
display: true,
text: "Cap (T)"
},
beginAtZero: false,
ticks: {
callback: function(value, _index, _ticks) {
return (value as number).toFixed(3) + "T";
},
},
},
},
},
});
};
const Main = () => {
// data is x/y pairs of [day, cap in T]
// there are 60 days in a year
var data = van.state<[number,number][]>([]);
var growthRate = 1 + (0.05 / 60);
(() => {
var newData: [number,number][] = [[0, 100.0]];
for (let i = 1; i < 2; i++) {
newData.push([i, newData[i - 1][1] * growthRate]);
}
data.val = newData;
})();
const addData = () => {
let dataValToAdd: [number,number] = [data.val.length, data.val[data.val.length - 1][1] * growthRate];
data.val = [...data.val, dataValToAdd];
// console.log(`Added data: ${dataValToAdd} | data len = ${data.val.length}`);
};
return div(
h1(
"Sim 1980 Design Tool"
),
p(
{ class: "title-text" },
"Used for visualizing and tuning design data for Sim 1980."
),
button(
{ onclick: addData },
"Add Data"
),
SimChart(data.val),
p(),
code(
{ textContent: () => ("data = " + data.val) }
),
);
};
van.add(app, Main()); And this renders the initial data in the graph well, as you'd expect! The "Add Data" button also works to add data to the state, which does update the But I can't get the framework to reactively update that
I've looked extensively through the VanX Anything I should look at in particular for enabling this use case? I have a couple ideas for rewriting my chart component here to handle refresh-on-event, but wanted to introduce the underlying concept as a Q&A request! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
I took a glance at your code. I'm wondering whether we should replace SimChart(data.val) with () => SimChart(data.val) |
Beta Was this translation helpful? Give feedback.
I took a glance at your code.
I'm wondering whether we should replace
with