Skip to content

Commit 2c7a6a2

Browse files
authored
fixing multiple function from p5.Table
1 parent 9c9a3aa commit 2c7a6a2

File tree

1 file changed

+68
-44
lines changed

1 file changed

+68
-44
lines changed

src/io/p5.Table.js

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,31 @@ class Table {
5151
* let table;
5252
*
5353
* async function setup() {
54-
* // The table is comma separated value "csv"
55-
* // and has a header specifying the columns labels.
56-
* table = await loadTable('assets/mammals.csv', 'csv', 'header');
54+
* // Create a 300x300 canvas
55+
* createCanvas(300, 300);
5756
*
58-
* //add a row
57+
* // Load the CSV file from the assets folder with a header row
58+
* table = await loadTable('assets/mammals.csv', ',', 'header');
59+
*
60+
* // Add a new row for "Wolf"
5961
* let newRow = table.addRow();
6062
* newRow.setString('id', table.getRowCount() - 1);
6163
* newRow.setString('species', 'Canis Lupus');
62-
* newRow.setString('name', 'Wolf');
63-
*
64-
* //print the results
65-
* for (let r = 0; r < table.getRowCount(); r++)
66-
* for (let c = 0; c < table.getColumnCount(); c++)
67-
* print(table.getString(r, c));
64+
* newRow.setString('name', 'Wolf');
65+
*
66+
* // Set text properties
67+
* fill(0); // Text color: black
68+
* textSize(12); // Adjust text size as needed
69+
*
70+
* // Display the table data on the canvas
71+
* // Each cell is positioned based on its row and column
72+
* for (let r = 0; r < table.getRowCount(); r++) {
73+
* for (let c = 0; c < table.getColumnCount(); c++) {
74+
* let x = c * 50 + 10; // Horizontal spacing for each column
75+
* let y = r * 30 + 20; // Vertical spacing for each row
76+
* text(table.getString(r, c), x * c, y);
77+
* }
78+
* }
6879
*
6980
* describe('no image displayed');
7081
* }
@@ -500,7 +511,7 @@ class Table {
500511
const ret = [];
501512
if (typeof value === 'string') {
502513
for (let i = 0; i < this.rows.length; i++) {
503-
ret.push(this.rows[i].obj[value]);
514+
ret.push(this.rows[i].obj[this.columns.indexOf(value)]);
504515
}
505516
} else {
506517
for (let j = 0; j < this.rows.length; j++) {
@@ -530,13 +541,23 @@ class Table {
530541
* let table;
531542
*
532543
* async function setup() {
533-
* // The table is comma separated value "csv"
534-
* // and has a header specifying the columns labels.
535-
* table = await loadTable('assets/mammals.csv', 'csv', 'header');
544+
* // Create a 200x200 canvas
545+
* createCanvas(200, 200);
536546
*
547+
* // Load the CSV file with a header row
548+
* table = await loadTable('assets/mammals.csv', ',', 'header');
549+
*
550+
* // Clear all rows from the table
537551
* table.clearRows();
538-
* print(table.getRowCount() + ' total rows in table');
539-
* print(table.getColumnCount() + ' total columns in table');
552+
*
553+
* // Set text properties
554+
* fill(0); // Text color: black
555+
* textSize(12); // Adjust text size as needed
556+
*
557+
* // Display the number of rows and columns on the canvas
558+
* text(table.getRowCount() + ' total rows in table', 10, 30);
559+
* text(table.getColumnCount() + ' total columns in table', 10, 60);
560+
*
540561
* describe('no image displayed');
541562
* }
542563
* </code>
@@ -559,30 +580,35 @@ class Table {
559580
* @example
560581
* <div class="norender">
561582
* <code>
562-
* // Given the CSV file "mammals.csv"
563-
* // in the project's "assets" folder:
564-
* //
565-
* // id,species,name
566-
* // 0,Capra hircus,Goat
567-
* // 1,Panthera pardus,Leopard
568-
* // 2,Equus zebra,Zebra
569-
*
570583
* let table;
571584
*
572585
* async function setup() {
573-
* // The table is comma separated value "csv"
574-
* // and has a header specifying the columns labels.
575-
* table = await loadTable('assets/mammals.csv', 'csv', 'header');
586+
* // Create a 300x300 canvas
587+
* createCanvas(300, 300);
588+
*
589+
* // Load the CSV file with a header row
590+
* table = await loadTable('assets/mammals.csv', ',', 'header');
576591
*
592+
* // Add a new column 'carnivore' and set its values
577593
* table.addColumn('carnivore');
578594
* table.set(0, 'carnivore', 'no');
579595
* table.set(1, 'carnivore', 'yes');
580596
* table.set(2, 'carnivore', 'no');
581597
*
582-
* //print the results
583-
* for (let r = 0; r < table.getRowCount(); r++)
584-
* for (let c = 0; c < table.getColumnCount(); c++)
585-
* print(table.getString(r, c));
598+
* // Set text properties for drawing on the canvas
599+
* fill(0); // Text color: black
600+
* textSize(12); // Adjust text size as needed
601+
*
602+
* // Display the table data on the canvas in a grid format
603+
* // Here we calculate positions based on row and column indices.
604+
* for (let r = 0; r < table.getRowCount(); r++) {
605+
* for (let c = 0; c < table.getColumnCount(); c++) {
606+
* // Calculate x and y positions for each cell.
607+
* let x = c * 50 + 10; // Horizontal offset for each column
608+
* let y = r * 30 + 20; // Vertical offset for each row
609+
* text(table.getString(r, c), x * c, y);
610+
* }
611+
* }
586612
*
587613
* describe('no image displayed');
588614
* }
@@ -1267,24 +1293,22 @@ function table(p5, fn){
12671293
* @example
12681294
* <div class="norender">
12691295
* <code>
1270-
* // Given the CSV file "mammals.csv"
1271-
* // in the project's "assets" folder:
1272-
* //
1273-
* // id,species,name
1274-
* // 0,Capra hircus,Goat
1275-
* // 1,Panthera pardus,Leopard
1276-
* // 2,Equus zebra,Zebra
1277-
*
12781296
* let table;
12791297
*
12801298
* async function setup() {
1281-
* // The table is comma separated value "csv"
1282-
* // and has a header specifying the columns labels.
1283-
* table = await loadTable('assets/mammals.csv', 'csv', 'header');
1299+
* // Create a 200x200 canvas
1300+
* createCanvas(200, 200);
1301+
*
1302+
* // Load the CSV file with a header row
1303+
* table = await loadTable('assets/mammals.csv', ',', 'header');
1304+
*
1305+
* // Set text properties for drawing on the canvas
1306+
* fill(0); // Set text color to black
1307+
* textSize(12); // Adjust text size as needed
12841308
*
1285-
* //print the column names
1309+
* // Display the column names on the canvas
12861310
* for (let c = 0; c < table.getColumnCount(); c++) {
1287-
* print('column ' + c + ' is named ' + table.columns[c]);
1311+
* text('column ' + c + ' is named ' + table.columns[c], 10, 30 + c * 20);
12881312
* }
12891313
* }
12901314
* </code>

0 commit comments

Comments
 (0)