-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathp5.Table.js
More file actions
1317 lines (1275 loc) · 42 KB
/
p5.Table.js
File metadata and controls
1317 lines (1275 loc) · 42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module IO
* @submodule Table
*/
import { stringify } from './csv';
class Table {
constructor(rows) {
this.columns = [];
this.rows = [];
}
toString(separator=',') {
let rows = this.rows.map(row => row.arr);
if(!this.columns.some(column => column === null)){
rows = [this.columns, ...rows];
}
return stringify(rows, {
separator
});
}
/**
* Use <a href="/reference/p5.Table/addRow/">addRow()</a> to add a new row of data to a <a href="#/p5.Table">p5.Table</a> object. By default,
* an empty row is created. Typically, you would store a reference to
* the new row in a TableRow object (see newRow in the example above),
* and then set individual values using <a href="#/p5/set">set()</a>.
*
* If a <a href="#/p5.TableRow">p5.TableRow</a> object is included as a parameter, then that row is
* duplicated and added to the table.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {p5.TableRow} [row] row to be added to the table
* @return {p5.TableRow} the row that was added
*
* @example
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* async function setup() {
* // Create a 300x300 canvas
* createCanvas(300, 300);
*
* // Load the CSV file from the assets folder with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Add a new row for "Wolf"
* let newRow = table.addRow();
* newRow.setString('id', table.getRowCount() - 1);
* newRow.setString('species', 'Canis Lupus');
* newRow.setString('name', 'Wolf');
*
* // Set text properties
* fill(0); // Text color: black
* textSize(12); // Adjust text size as needed
*
* // Display the table data on the canvas
* // Each cell is positioned based on its row and column
* for (let r = 0; r < table.getRowCount(); r++) {
* for (let c = 0; c < table.getColumnCount(); c++) {
* let x = c * 50 + 10; // Horizontal spacing for each column
* let y = r * 30 + 20; // Vertical spacing for each row
* text(table.getString(r, c), x * c, y);
* }
* }
*
* describe('no image displayed');
* }
*/
addRow (row) {
// make sure it is a valid TableRow
const r = row || new p5.TableRow();
if (typeof r.arr === 'undefined' || typeof r.obj === 'undefined') {
//r = new p5.prototype.TableRow(r);
throw new Error(`invalid TableRow: ${r}`);
}
r.table = this;
this.rows.push(r);
return r;
}
/**
* Removes a row from the table object.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {Integer} id ID number of the row to remove
*
* @example
* let table;
*
* async function setup() {
* // Create a 200x200 canvas and set a white background
* createCanvas(200, 200);
* background(255);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Remove the first row from the table
* table.removeRow(0);
*
* // Set text properties for drawing on the canvas
* fill(0); // Set text color to black
* textSize(12); // Adjust text size as needed
*
* // Display the table values on the canvas:
* // Each row's cell values are joined into a single string and drawn on a new line.
* let y = 20; // Starting vertical position
* for (let r = 0; r < table.getRowCount(); r++) {
* let rowText = "";
* for (let c = 0; c < table.getColumnCount(); c++) {
* rowText += table.getString(r, c) + " ";
* }
* text(rowText, 18, y * 3);
* y += 20;
* }
*
* describe('no image displayed');
* }
*/
removeRow (id) {
this.rows[id].table = null; // remove reference to table
const chunk = this.rows.splice(id + 1, this.rows.length);
this.rows.pop();
this.rows = this.rows.concat(chunk);
}
/**
* Returns a reference to the specified <a href="#/p5.TableRow">p5.TableRow</a>. The reference
* can then be used to get and set values of the selected row.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {Integer} rowID ID number of the row to get
* @return {p5.TableRow} <a href="#/p5.TableRow">p5.TableRow</a> object
*
* @example
* let table;
*
* async function setup() {
* // Create a 200x200 canvas
* createCanvas(200, 200);
* background(255); // Set background to white
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Get the row at index 1 (second row)
* let row = table.getRow(1);
*
* // Set text properties for drawing on the canvas
* fill(0); // Set text color to black
* textSize(12); // Set the text size
*
* // Loop over each column in the row and display its value on the canvas
* for (let c = 0; c < table.getColumnCount(); c++) {
* text(row.getString(c), 10, 20 + c * 50 + 20);
* }
*
* describe('no image displayed');
* }
*/
getRow (r) {
return this.rows[r];
}
/**
* Gets all rows from the table. Returns an array of <a href="#/p5.TableRow">p5.TableRow</a>s.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @return {p5.TableRow[]} Array of <a href="#/p5.TableRow">p5.TableRow</a>s
*
* @example
* let table;
*
* async function setup() {
* // Create a 200x200 canvas and set a white background
* createCanvas(200, 200);
* background(255);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* let rows = table.getRows();
*
* // Warning: rows is an array of objects.
* // Set the 'name' of each row to 'Unicorn'
* for (let r = 0; r < rows.length; r++) {
* rows[r].set('name', 'Unicorn');
* }
*
* // Set text properties
* fill(0); // Set text color to black
* textSize(12); // Adjust text size as needed
*
* // Display the modified table values on the canvas
* // We'll join each row's values with a space and display each row on a new line.
* let y = 20; // Starting y position
* for (let r = 0; r < table.getRowCount(); r++) {
* let rowText = "";
* for (let c = 0; c < table.getColumnCount(); c++) {
* rowText += table.getString(r, c) + " ";
* }
* text(rowText, 10, y * 2);
* y += 20; // Move to next line
* }
*
* describe('no image displayed');
* }
*/
getRows () {
return this.rows;
}
/**
* Finds the first row in the Table that contains the value
* provided, and returns a reference to that row. Even if
* multiple rows are possible matches, only the first matching
* row is returned. The column to search may be specified by
* either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String} value The value to match
* @param {Integer|String} column ID number or title of the
* column to search
* @return {p5.TableRow}
*
* @example
* let table;
*
* async function setup() {
* // Create a 100x100 canvas
* createCanvas(100, 100);
* background(255); // Set background to white
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Find the row with the animal named "Zebra"
* let row = table.findRow('Zebra', 'name');
*
* // Get the species from the found row
* let species = row.getString('species');
*
* // Set text properties and display the species on the canvas
* fill(0); // Set text color to black
* textSize(12); // Adjust text size as needed
* text(species, 10, 30);
*
* describe('no image displayed');
* }
*/
findRow (value, column) {
// try the Object
if (typeof column === 'string') {
for (let i = 0; i < this.rows.length; i++) {
if (this.rows[i].obj[this.columns.indexOf(column)] === value) {
return this.rows[i];
}
}
} else {
// try the Array
for (let j = 0; j < this.rows.length; j++) {
if (this.rows[j].arr[column] === value) {
return this.rows[j];
}
}
}
// otherwise...
return null;
}
/**
* Finds the rows in the Table that contain the value
* provided, and returns references to those rows. Returns an
* Array, so for must be used to iterate through all the rows,
* as shown in the example above. The column to search may be
* specified by either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String} value The value to match
* @param {Integer|String} column ID number or title of the
* column to search
* @return {p5.TableRow[]} An Array of TableRow objects
*
* @example
* let table;
*
* async function setup() {
* // Create a 200x200 canvas
* createCanvas(200, 200);
* background(255); // Set background to white
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Add another goat entry
* let newRow = table.addRow();
* newRow.setString('id', table.getRowCount() - 1);
* newRow.setString('species', 'Scape Goat');
* newRow.setString('name', 'Goat');
*
* // Find rows where the name is "Goat"
* let rows = table.findRows('Goat', 'name');
*
* // Set text properties
* fill(0); // Set text color to black
* textSize(12); // Adjust text size as needed
*
* // Display the result on the canvas
* text(rows.length + ' Goats found', 10, 30);
*
* describe('no image displayed');
* }
*/
findRows (value, column) {
const ret = [];
if (typeof column === 'string') {
for (let i = 0; i < this.rows.length; i++) {
if (this.rows[i].obj[this.columns.indexOf(column)] === value) {
ret.push(this.rows[i]);
}
}
} else {
// try the Array
for (let j = 0; j < this.rows.length; j++) {
if (this.rows[j].arr[column] === value) {
ret.push(this.rows[j]);
}
}
}
return ret;
}
/**
* Finds the first row in the Table that matches the regular
* expression provided, and returns a reference to that row.
* Even if multiple rows are possible matches, only the first
* matching row is returned. The column to search may be
* specified by either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String|RegExp} regexp The regular expression to match
* @param {String|Integer} column The column ID (number) or
* title (string)
* @return {p5.TableRow} TableRow object
*
* @example
* let table;
*
* async function setup() {
* // Create a 200x200 canvas
* createCanvas(200, 200);
* background(255); // Set background to white
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Search using the specified regex on column index 1 (species)
* let mammal = table.matchRow(new RegExp('ant'), 1);
* let species = mammal.getString(1); // "Panthera pardus"
*
* // Set text properties for drawing on the canvas
* fill(0); // Text color: black
* textSize(12); // Adjust text size as needed
*
* // Display the species on the canvas
* text(species, 10, 30);
*
* describe('no image displayed');
* }
*/
matchRow (regexp, column) {
if (typeof column === 'number') {
for (let j = 0; j < this.rows.length; j++) {
if (this.rows[j].arr[column].match(regexp)) {
return this.rows[j];
}
}
} else {
for (let i = 0; i < this.rows.length; i++) {
if (this.rows[i].obj[this.columns.indexOf(column)].match(regexp)) {
return this.rows[i];
}
}
}
return null;
}
/**
* Finds the rows in the Table that match the regular expression provided,
* and returns references to those rows. Returns an array, so for must be
* used to iterate through all the rows, as shown in the example. The
* column to search may be specified by either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String} regexp The regular expression to match
* @param {String|Integer} [column] The column ID (number) or
* title (string)
* @return {p5.TableRow[]} An Array of TableRow objects
* @example
* let table;
*
* function setup() {
* // Create a 200x200 canvas and set a white background
* createCanvas(200, 200);
* background(255);
*
* // Create a new p5.Table and add columns
* table = new p5.Table();
* table.addColumn('name');
* table.addColumn('type');
*
* // Add rows to the table
* let newRow = table.addRow();
* newRow.setString('name', 'Lion');
* newRow.setString('type', 'Mammal');
*
* newRow = table.addRow();
* newRow.setString('name', 'Snake');
* newRow.setString('type', 'Reptile');
*
* newRow = table.addRow();
* newRow.setString('name', 'Mosquito');
* newRow.setString('type', 'Insect');
*
* newRow = table.addRow();
* newRow.setString('name', 'Lizard');
* newRow.setString('type', 'Reptile');
*
* // Search for rows where the "type" starts with "R"
* let rows = table.matchRows('R.*', 'type');
*
* // Set text properties for drawing on the canvas
* fill(0); // Text color: black
* textSize(12); // Text size
*
* // Display each matching row on the canvas
* let y = 20;
* for (let i = 0; i < rows.length; i++) {
* let output = rows[i].getString('name') + ': ' + rows[i].getString('type');
* text(output, 10, y);
* y += 20;
* }
* }
*/
matchRows (regexp, column) {
const ret = [];
if (typeof column === 'number') {
for (let j = 0; j < this.rows.length; j++) {
if (this.rows[j].arr[column].match(regexp)) {
ret.push(this.rows[j]);
}
}
} else {
for (let i = 0; i < this.rows.length; i++) {
if (this.rows[i].obj[this.columns.indexOf(column)].match(regexp)) {
ret.push(this.rows[i]);
}
}
}
return ret;
}
/**
* Retrieves all values in the specified column, and returns them
* as an array. The column may be specified by either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String|Number} column String or Number of the column to return
* @return {Array} Array of column values
*
* @example
* // META:norender
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* async function setup() {
* // The table is comma separated value "csv"
* // and has a header specifying the columns labels.
* table = await loadTable('assets/mammals.csv', 'csv', 'header');
*
* //getColumn returns an array that can be printed directly
* print(table.getColumn('species'));
* //outputs ["Capra hircus", "Panthera pardus", "Equus zebra"]
* describe('no image displayed');
* }
*/
getColumn (value) {
const ret = [];
if (typeof value === 'string') {
for (let i = 0; i < this.rows.length; i++) {
ret.push(this.rows[i].obj[this.columns.indexOf(value)]);
}
} else {
for (let j = 0; j < this.rows.length; j++) {
ret.push(this.rows[j].arr[value]);
}
}
return ret;
}
/**
* Removes all rows from a Table. While all rows are removed,
* columns and column titles are maintained.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
*
* @example
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* let table;
*
* async function setup() {
* // Create a 200x200 canvas
* createCanvas(200, 200);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Clear all rows from the table
* table.clearRows();
*
* // Set text properties
* fill(0); // Text color: black
* textSize(12); // Adjust text size as needed
*
* // Display the number of rows and columns on the canvas
* text(table.getRowCount() + ' total rows in table', 10, 30);
* text(table.getColumnCount() + ' total columns in table', 10, 60);
*
* describe('no image displayed');
* }
*/
clearRows () {
delete this.rows;
this.rows = [];
}
/**
* Use <a href="/reference/p5.Table/addColumn/">addColumn()</a> to add a new column to a <a href="#/p5.Table">Table</a> object.
* Typically, you will want to specify a title, so the column
* may be easily referenced later by name. (If no title is
* specified, the new column's title will be null.)
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String} [title] title of the given column
*
* @example
* let table;
*
* async function setup() {
* createCanvas(300, 300);
* table = await loadTable('/assets/mammals.csv', ',', 'header');
*
* table.addColumn('carnivore');
* table.set(0, 'carnivore', 'no');
* table.set(1, 'carnivore', 'yes');
* table.set(2, 'carnivore', 'no');
*
* fill(0); // Set text color to black
* textSize(11); // Adjust text size as needed
*
* for (let r = 0; r < table.getRowCount(); r++) {
* for (let c = 0; c < table.getColumnCount(); c++) {
* // Keep column spacing consistent (e.g. 80 pixels apart).
* let x = c * 80 + 10;
* let y = r * 30 + 20;
* // Use x directly, rather than multiplying by c again
* text(table.getString(r, c), x, y);
* }
* }
*
* describe('no image displayed');
* }
*/
addColumn (title) {
const t = title || null;
this.columns.push(t);
}
/**
* Returns the total number of columns in a Table.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @return {Integer} Number of columns in this table
* @example
* // given the cvs file "blobs.csv" in /assets directory
* // ID, Name, Flavor, Shape, Color
* // Blob1, Blobby, Sweet, Blob, Pink
* // Blob2, Saddy, Savory, Blob, Blue
*
* let table;
*
* async function setup() {
* table = await loadTable('assets/blobs.csv');
*
* createCanvas(200, 100);
* textAlign(CENTER);
* background(255);
* }
*
* function draw() {
* let numOfColumn = table.getColumnCount();
* text('There are ' + numOfColumn + ' columns in the table.', 100, 50);
* }
*/
getColumnCount () {
return this.columns.length;
}
/**
* Returns the total number of rows in a Table.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @return {Integer} Number of rows in this table
* @example
* // given the cvs file "blobs.csv" in /assets directory
* //
* // ID, Name, Flavor, Shape, Color
* // Blob1, Blobby, Sweet, Blob, Pink
* // Blob2, Saddy, Savory, Blob, Blue
*
* let table;
*
* async function setup() {
* table = await loadTable('assets/blobs.csv');
*
* createCanvas(200, 100);
* textAlign(CENTER);
* background(255);
* }
*
* function draw() {
* text('There are ' + table.getRowCount() + ' rows in the table.', 100, 50);
* }
*/
getRowCount () {
return this.rows.length;
}
/**
* Removes any of the specified characters (or "tokens").
*
* If no column is specified, then the values in all columns and
* rows are processed. A specific column may be referenced by
* either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String} chars String listing characters to be removed
* @param {String|Integer} [column] Column ID (number)
* or name (string)
*
* @example
* // META:norender
* function setup() {
* let table = new p5.Table();
*
* table.addColumn('name');
* table.addColumn('type');
*
* let newRow = table.addRow();
* newRow.setString('name', ' $Lion ,');
* newRow.setString('type', ',,,Mammal');
*
* newRow = table.addRow();
* newRow.setString('name', '$Snake ');
* newRow.setString('type', ',,,Reptile');
*
* table.removeTokens(',$ ');
* print(table.getArray());
* }
*
* // prints:
* // 0 "Lion" "Mamal"
* // 1 "Snake" "Reptile"
*/
removeTokens (chars, column) {
const escape = s => s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
const charArray = [];
for (let i = 0; i < chars.length; i++) {
charArray.push(escape(chars.charAt(i)));
}
const regex = new RegExp(charArray.join('|'), 'g');
if (typeof column === 'undefined') {
for (let c = 0; c < this.columns.length; c++) {
for (let d = 0; d < this.rows.length; d++) {
let s = this.rows[d].arr[c];
s = s.replace(regex, '');
this.rows[d].arr[c] = s;
this.rows[d].obj[this.columns[c]] = s;
}
}
} else if (typeof column === 'string') {
for (let j = 0; j < this.rows.length; j++) {
let val = this.rows[j].obj[column];
val = val.replace(regex, '');
this.rows[j].obj[column] = val;
const pos = this.columns.indexOf(column);
this.rows[j].arr[pos] = val;
}
} else {
for (let k = 0; k < this.rows.length; k++) {
let str = this.rows[k].arr[column];
str = str.replace(regex, '');
this.rows[k].arr[column] = str;
this.rows[k].obj[this.columns[column]] = str;
}
}
}
/**
* Trims leading and trailing whitespace, such as spaces and tabs,
* from String table values. If no column is specified, then the
* values in all columns and rows are trimmed. A specific column
* may be referenced by either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String|Integer} [column] Column ID (number)
* or name (string)
* @example
* // META:norender
* function setup() {
* let table = new p5.Table();
*
* table.addColumn('name');
* table.addColumn('type');
*
* let newRow = table.addRow();
* newRow.setString('name', ' Lion ,');
* newRow.setString('type', ' Mammal ');
*
* newRow = table.addRow();
* newRow.setString('name', ' Snake ');
* newRow.setString('type', ' Reptile ');
*
* table.trim();
* print(table.getArray());
* }
*
* // prints:
* // 0 "Lion" "Mamal"
* // 1 "Snake" "Reptile"
*/
trim (column) {
const regex = new RegExp(' ', 'g');
if (typeof column === 'undefined') {
for (let c = 0; c < this.columns.length; c++) {
for (let d = 0; d < this.rows.length; d++) {
let s = this.rows[d].arr[c];
s = s.replace(regex, '');
this.rows[d].arr[c] = s;
this.rows[d].obj[this.columns[c]] = s;
}
}
} else if (typeof column === 'string') {
for (let j = 0; j < this.rows.length; j++) {
let val = this.rows[j].obj[column];
val = val.replace(regex, '');
this.rows[j].obj[column] = val;
const pos = this.columns.indexOf(column);
this.rows[j].arr[pos] = val;
}
} else {
for (let k = 0; k < this.rows.length; k++) {
let str = this.rows[k].arr[column];
str = str.replace(regex, '');
this.rows[k].arr[column] = str;
this.rows[k].obj[this.columns[column]] = str;
}
}
}
/**
* Use <a href="/reference/p5.Table/removeColumn/">removeColumn()</a> to remove an existing column from a Table
* object. The column to be removed may be identified by either
* its title (a String) or its index value (an int).
* removeColumn(0) would remove the first column, removeColumn(1)
* would remove the second column, and so on.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {String|Integer} column columnName (string) or ID (number)
*
* @example
* let table;
*
* async function setup() {
* // Create a 100x100 canvas
* createCanvas(100, 100);
* background(255); // Set background to white
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Remove the "id" column
* table.removeColumn('id');
*
* // Get the remaining column count
* let colCount = table.getColumnCount();
*
* // Set text properties
* fill(0); // Text color: black
* textSize(12); // Adjust text size as needed
*
* // Display the column count on the canvas
* text(colCount, 40, 50);
*
* describe('no image displayed');
* }
*/
removeColumn (c) {
let cString;
let cNumber;
if (typeof c === 'string') {
// find the position of c in the columns
cString = c;
cNumber = this.columns.indexOf(c);
} else {
cNumber = c;
cString = this.columns[c];
}
const chunk = this.columns.splice(cNumber + 1, this.columns.length);
this.columns.pop();
this.columns = this.columns.concat(chunk);
for (let i = 0; i < this.rows.length; i++) {
const tempR = this.rows[i].arr;
const chip = tempR.splice(cNumber + 1, tempR.length);
tempR.pop();
this.rows[i].arr = tempR.concat(chip);
delete this.rows[i].obj[cString];
}
}
/**
* Stores a value in the Table's specified row and column.
* The row is specified by its ID, while the column may be specified
* by either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {Integer} row row ID
* @param {String|Integer} column column ID (Number)
* or title (String)
* @param {String|Number} value value to assign
*
* @example
* let table;
*
* async function setup() {
* // Create a 200x200 canvas and set a white background
* createCanvas(200, 200);
* background(255);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Update the first row: change species to "Canis Lupus" and name to "Wolf"
* table.set(0, 'species', 'Canis Lupus');
* table.set(0, 'name', 'Wolf');
*
* // Set text properties for drawing on the canvas
* fill(0); // Text color: black
* textSize(12); // Adjust text size as needed
*
* // Display the table values on the canvas:
* // Each row's values are concatenated into a single string and displayed on a new line.
* let y = 20; // Starting vertical position
* for (let r = 0; r < table.getRowCount(); r++) {
* let rowText = "";
* for (let c = 0; c < table.getColumnCount(); c++) {
* rowText += table.getString(r, c) + " ";
* }
* text(rowText, 10, y * 2.5);
* y += 20;
* }
*
* describe('no image displayed');
* }
*/
set (row, column, value) {
this.rows[row].set(column, value);
}
/**
* Stores a Float value in the Table's specified row and column.
* The row is specified by its ID, while the column may be specified
* by either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {Integer} row row ID
* @param {String|Integer} column column ID (Number)
* or title (String)
* @param {Number} value value to assign
*
* @example
* let table;
*
* async function setup() {
* // Create a 100x100 canvas and set a white background
* createCanvas(100, 100);
* background(255);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Set the value in row 1, column "id" to the number 1
* table.setNum(1, 'id', 1);
*
* // Get the first column as an array and join its values into a string for display.
* let col0 = table.getColumn(0); // Expected output: ["0", 1, "2"]
* let output = col0.join(", ");
*
* // Set text properties and display the output on the canvas
* fill(0); // Text color: black
* textSize(12); // Adjust text size as needed
* text(output, 30, 50);
*
* describe('no image displayed');
* }
*/
setNum (row, column, value) {
this.rows[row].setNum(column, value);
}
/**
* Stores a String value in the Table's specified row and column.
* The row is specified by its ID, while the column may be specified
* by either its ID or title.
*
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @param {Integer} row row ID
* @param {String|Integer} column column ID (Number)
* or title (String)
* @param {String} value value to assign
* @example
* let table;
*
* async function setup() {
* // Create a 200x200 canvas and set a white background
* createCanvas(200, 200);
* background(255);
*
* // Load the CSV file from the assets folder with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Add a new row with the new animal data
* let newRow = table.addRow();
* newRow.setString('id', table.getRowCount() - 1);
* newRow.setString('species', 'Canis Lupus');
* newRow.setString('name', 'Wolf');
*
* // Convert the table to a 2D array
* let tableArray = table.getArray();
*
* // Set text properties
* fill(0); // Set text color to black
* textSize(12); // Adjust text size as needed
*
* // Display each row of the table on the canvas
* let y = 20; // Starting y position
* for (let i = 0; i < tableArray.length; i++) {
* // Join the values of each row with a comma separator
* let rowText = tableArray[i].join(', ');
* text(rowText, 15, y * 2);
* y += 20; // Increment y position for the next row
* }
*
* describe('no image displayed');
* }
*/
setString (row, column, value) {
this.rows[row].setString(column, value);
}