I am loading a csv file using the p5.js function, loadTable in typescript. It seems that its not reading the csv file at all, maybe i am missing something in my approach. I have all the type definition in typescript for p5.js.
Here is the code, main.ts.
import * as p5 from "p5";
const plot_diagram = (p: p5) => {
let data: p5.Table;
p.preload = () => {
const CSV_FILE: string = "./sample.csv";
const CSV_TYPE: string = "csv";
const CSV_HEADER: string = "header";
data = (p as any).loadTable(CSV_FILE, CSV_TYPE, CSV_HEADER); //how to fix this?
console.log("data ", data.getRowCount()); // shows wrong object.
};
p.setup = () => {
p.createCanvas(640, 480);
p.noFill();
// how many rows?
console.log("row ", data.getRowCount());
// what are the columns?
console.log("col ", data.columns);
};
p.draw = () => {
p.background(200);
p.beginShape();
for (let i = 0; i < 5000; i++) {
p.vertex(
data.getNum(i, "row"),
data.getNum(i, "col ")
);
}
p.endShape();
};
};
export default plot_diagram;
yes, spectrum.ts is the main file i am working on.
To get the project running, uncomment, //import sketch from “~sketch/circle”;
and comment import sketch from “~sketch/spectrum”;
That will give you some circles. The spectrum file is basically supposed to create a x-y plot.
Only thing that i am struggling with is to load csv in typescript and have the data var with csv values.
Rest i can manage. once i have data var, i can run data.getNum and other functions on it.
Only clue i got so far is from this SO answer, but that only works because there is a type called SoundFile.
I added the code in sketch.ts in the above repo. Could it be that loadTable doesn’t work when running a local project? Perhaps i misunderstood its use case?
ah, i figure it out. The issue was not related to typescript. It was browser cors policy.
Perhaps a better error message is needed in loadTable function.