-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathp5.TableRow.js
More file actions
351 lines (339 loc) · 10.7 KB
/
p5.TableRow.js
File metadata and controls
351 lines (339 loc) · 10.7 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
/**
* @module IO
* @submodule Table
*/
class TableRow {
constructor(row=[]){
let arr = row;
this.arr = arr;
this.obj = Object.fromEntries(arr.entries());
this.table = null;
}
/**
* Stores a value in the TableRow's specified column.
* 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|Integer} column Column ID (Number)
* or Title (String)
* @param {String|Number} value The value to be stored
*
* @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');
*
* // Set every row's "name" to "Unicorn"
* let rows = table.getRows();
* for (let r = 0; r < rows.length; r++) {
* rows[r].set('name', 'Unicorn');
* }
*
* // Convert the table to an array
* let tableArray = table.getArray();
*
* // Set text properties
* fill(0); // Set text color to black
* textSize(12); // Set text size
*
* // Display each row of the table on the canvas
* let y = 20; // Starting y position
* for (let i = 0; i < tableArray.length; i++) {
* let rowText = tableArray[i].join(', ');
* text(rowText, 10, y * 2.5);
* y += 20; // Increment y position for the next row
* }
*
* describe('no image displayed');
* }
*/
set(column, value) {
// if typeof column is string, use .obj
if (typeof column === 'string') {
const cPos = this.table.columns.indexOf(column); // index of columnID
if (cPos >= 0) {
this.obj[column] = value;
this.arr[cPos] = value;
} else {
throw new Error(`This table has no column named "${column}"`);
}
} else {
// if typeof column is number, use .arr
if (column < this.table.columns.length) {
this.arr[column] = value;
const cTitle = this.table.columns[column];
this.obj[cTitle] = value;
} else {
throw new Error(`Column #${column} is out of the range of this table`);
}
}
}
/**
* Stores a Float value in the TableRow's specified column.
* 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|Integer} column Column ID (Number)
* or Title (String)
* @param {Number|String} value The value to be stored
* as a Float
* @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 each row's "id" to (row index + 10)
* let rows = table.getRows();
* for (let r = 0; r < rows.length; r++) {
* rows[r].setNum('id', r + 10);
* }
*
* // Convert the table to a 2D array for display
* let tableArray = table.getArray();
*
* // Set text properties
* fill(0); // Text color: 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 each row's values with a comma separator
* let rowText = tableArray[i].join(', ');
* text(rowText, 10, y * 2.5);
* y += 20; // Increment y for the next row
* }
*
* describe('no image displayed');
* }
*/
setNum(column, value) {
const floatVal = parseFloat(value);
this.set(column, floatVal);
}
/**
* Stores a String value in the TableRow's specified column.
* 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|Integer} column Column ID (Number)
* or Title (String)
* @param {String|Number|Boolean|Object} value The value to be stored
* as a String
* @example
* let table;
*
* async function setup() {
* // Create a 300x200 canvas and set a white background
* createCanvas(300, 200);
* background(255);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Update each row's "name" field
* let rows = table.getRows();
* for (let r = 0; r < rows.length; r++) {
* let name = rows[r].getString('name');
* rows[r].setString('name', 'A ' + name + ' named George');
* }
*
* // Convert the table to a 2D array for display
* let tableArray = table.getArray();
*
* // Set text properties
* fill(0); // Text color: 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++) {
* let rowText = tableArray[i].join(', ');
* text(rowText, 10, y * 2.5);
* y += 20; // Increment y for the next row
* }
*
* // describe('no image displayed');
* }
*/
setString(column, value) {
const stringVal = value.toString();
this.set(column, stringVal);
}
/**
* Retrieves a value from the TableRow's specified column.
* 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|Integer} column columnName (string) or
* ID (number)
* @return {String|Number}
*
* @example
* let table;
*
* async function setup() {
* // Create a 200x100 canvas and set a white background
* createCanvas(200, 100);
* background(255);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* // Extract the names from each row and store them in an array
* let names = [];
* let rows = table.getRows();
* for (let r = 0; r < rows.length; r++) {
* names.push(rows[r].get('name'));
* }
*
* // Set text properties and display the names on the canvas
* fill(0); // Set text color to black
* textSize(12); // Set text size
*
* // Join names into a single string separated by commas
* let namesText = names.join(', ');
* text(namesText, 35, 50);
*
* describe('no image displayed');
* }
*/
get(column) {
if (typeof column === 'string') {
return this.obj[this.table.columns.indexOf(column)];
} else {
return this.arr[column];
}
}
/**
* Retrieves a Float value from the TableRow's specified
* column. 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|Integer} column columnName (string) or
* ID (number)
* @return {Number} Float Floating point number
* @example
* let table;
*
* async function setup() {
* // Create a 300x100 canvas and set a white background
* createCanvas(300, 100);
* background(255);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* let rows = table.getRows();
* let minId = Infinity;
* let maxId = -Infinity;
*
* for (let r = 0; r < rows.length; r++) {
* let id = rows[r].getNum('id');
* minId = min(minId, id);
* maxId = max(maxId, id);
* }
*
* let result = 'minimum id = ' + minId + ', maximum id = ' + maxId;
*
* // Set text properties and display the result on the canvas
* fill(0); // Set text color to black
* textSize(12); // Set text size
* text(result, 10, 50);
*
* describe('no image displayed');
* }
*/
getNum(column) {
let ret;
if (typeof column === 'string') {
ret = parseFloat(this.obj[this.table.columns.indexOf(column)]);
} else {
ret = parseFloat(this.arr[column]);
}
if (ret.toString() === 'NaN') {
throw `Error: ${this.obj[column]} is NaN (Not a Number)`;
}
return ret;
}
/**
* Retrieves an String value from the TableRow's specified
* column. 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|Integer} column columnName (string) or
* ID (number)
* @return {String} String
* @example
* let table;
*
* async function setup() {
* // Create a 200x100 canvas and set a white background
* createCanvas(200, 100);
* background(255);
*
* // Load the CSV file with a header row
* table = await loadTable('assets/mammals.csv', ',', 'header');
*
* let rows = table.getRows();
* let longest = '';
* for (let r = 0; r < rows.length; r++) {
* let species = rows[r].getString('species');
* if (longest.length < species.length) {
* longest = species;
* }
* }
*
* let result = 'longest: ' + longest;
*
* // Set text properties and display the result on the canvas
* fill(0); // Set text color to black
* textSize(12); // Set text size
* text(result, 30, 50);
*
* describe('no image displayed');
* }
*/
getString(column) {
if (typeof column === 'string') {
return this.obj[this.table.columns.indexOf(column)].toString();
} else {
return this.arr[column].toString();
}
}
};
function tableRow(p5, fn){
/**
* A TableRow object represents a single row of data values,
* stored in columns, from a table.
*
* A Table Row contains both an ordered array, and an unordered
* JSON object.
*
* @class p5.TableRow
* @deprecated p5.Table will be removed in a future version of p5.js to make way for a new, friendlier version :)
* @constructor
* @param {any[]} row optional: populate the row with an
* array of values
*/
p5.TableRow = TableRow;
}
export default tableRow;
if(typeof p5 !== 'undefined'){
tableRow(p5, p5.prototype);
}