xxxxxxxxxx
let table;
function preload() {
//my table is comma separated value "csv"
//and has a header specifying the columns labels: date, name, value
table = loadTable('bank-transactions3.csv', 'csv', 'header');
}
function setup() {
//create array
let tableArray = table.getArray();
//turn money into numbers
for (i = 0; i < tableArray.length; i++){
tableArray[i][2] = Number(tableArray[i][2]);
}
//sort
tableArray.sort(sortFunction);
//canvas
createCanvas(windowWidth, tableArray.length * 30 + 100);
fill(0);
background(255);
textSize(24);
text("Spending visualiser", 50, 50);
text(tableArray.length + " rows", 450, 50);
for (i = 0; i < tableArray.length -1; i++) {
textAlign(LEFT);
if (tableArray [i][0] !== tableArray [i + 1][0]){
text (tableArray [i][0], 50, i * 30 + 100);
line (0, i * 30 + 105, width, i * 30 + 105)
}
text (tableArray [i][1], 200, i * 30 + 100);
textAlign(RIGHT);
text (tableArray [i][2], 550, i * 30 + 100);
// text (i, 500, i * 30 + 100);
rect (580, i * 30 + 85, tableArray [i][2], 10)
}
}
function sortFunction(a, b) {
//change the number [x] to sort by a different part of the array
if (a[2] === b[2]) {
return 0;
}
else {
return (a[2] < b[2]) ? -1 : 1;
}
}