@@ -51,20 +51,31 @@ class Table {
51
51
* let table;
52
52
*
53
53
* 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);
57
56
*
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"
59
61
* let newRow = table.addRow();
60
62
* newRow.setString('id', table.getRowCount() - 1);
61
63
* 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
+ * }
68
79
*
69
80
* describe('no image displayed');
70
81
* }
@@ -500,7 +511,7 @@ class Table {
500
511
const ret = [ ] ;
501
512
if ( typeof value === 'string' ) {
502
513
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 ) ] ) ;
504
515
}
505
516
} else {
506
517
for ( let j = 0 ; j < this . rows . length ; j ++ ) {
@@ -530,13 +541,23 @@ class Table {
530
541
* let table;
531
542
*
532
543
* 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);
536
546
*
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
537
551
* 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
+ *
540
561
* describe('no image displayed');
541
562
* }
542
563
* </code>
@@ -559,30 +580,35 @@ class Table {
559
580
* @example
560
581
* <div class="norender">
561
582
* <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
- *
570
583
* let table;
571
584
*
572
585
* 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');
576
591
*
592
+ * // Add a new column 'carnivore' and set its values
577
593
* table.addColumn('carnivore');
578
594
* table.set(0, 'carnivore', 'no');
579
595
* table.set(1, 'carnivore', 'yes');
580
596
* table.set(2, 'carnivore', 'no');
581
597
*
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
+ * }
586
612
*
587
613
* describe('no image displayed');
588
614
* }
@@ -1267,24 +1293,22 @@ function table(p5, fn){
1267
1293
* @example
1268
1294
* <div class="norender">
1269
1295
* <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
- *
1278
1296
* let table;
1279
1297
*
1280
1298
* 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
1284
1308
*
1285
- * //print the column names
1309
+ * // Display the column names on the canvas
1286
1310
* 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 );
1288
1312
* }
1289
1313
* }
1290
1314
* </code>
0 commit comments