The first parameter must be the Environment that this
TableRenderer will render.
The second parameter specifies options, which can include:
"type" ("csv" | "table" = "table") — Whether to render output in CSV or HTML <table> format"filter" — Include a function (Agent => boolean) to specify which rows to include in the output. For example, if you only want to include Agents with an x value greater than 100:const renderer = new TableRenderer(environment, {
filter: agent => {
return agent.get('x') > 100;
}
});
"limit" (number = Infinity) — The maximum number of rows (Agents) to render. If using a filter function, applies the limit after filtering."sortKey" (string = null) — Sort the Agent data by this key of data"order" ("asc" | "desc" = "desc") — When using a "sortKey", specify whether Agents should be listed in ascending or descending order"precision" (number = 3) — For floating point values, the number of decimal places to display"refresh" (number = 500) — The number of milliseconds that should elapse between re-rendering (if this happens too quickly the effect can be visually jarring)The TableRenderers columns should be an array of the keys of Agent
data that you want to render.
// Suppose Agents in the Environment have data that looks like:
// {
// "favor": number,
// "oppose": number,
// "neutral" number
// }
// order of columns is determined by order in the array
renderer.columns = ['neutral', 'favor', 'oppose'];
Points to the Environment that this
renderer is tied to. This is automatically set when the
renderer is created.
Mount this renderer to a DOM element. Pass either a string representing a CSS selector matching the element or the element itself.
// mounts the renderer to the element with the ID `container`
renderer.mount('#container');
// mounts the renderer to the element itself
const container = document.getElementById('container');
renderer.mount(container);
Returns the outer HTML of the table or the CSV data as a string. This can be useful for exporting data, particularly in a Node.js environment as opposed to in a browser. For instance, in a Node.js script, you could write the CSV data to a file as follows:
const fs = require('fs'); // import the file system module
const environment = new Environment();
for (let i = 0; i < 3; i++) environment.addAgent(new Agent({ i }));
const renderer = new TableRenderer(environment, { type: 'csv' });
renderer.columns = ['i'];
// write the TableRenderer's output to a CSV file named data.csv
fs.writeFileSync('./data.csv', renderer.output());
Generated using TypeDoc
A
TableRendererrenders an HTML table (for browsers only) or CSV (comma-separated value) representation ofAgentdata.The
TableRendererrenders:0.5.0