Add Table Statistic To Row Labels
Jump to navigation
Jump to search
This code may be used to add a statistic from Statistics - Right to the row labels in your table. In this example, the Average value from Statistics - Right is being added to the row labels with specified text and fixed to 2 decimal places.
To use this snippet:
- Select your table.
- Select Automate > Custom Rule.
- Paste in the code from below.
- Click the 'Play' icon and close.
includeWeb('Table JavaScript Utility Functions');
// Set parameters and text
var statistic_to_add = "Average";
var decimals = 2;
var text_before = "Average =";
// Get labels and values
var vals = right_table.get(statistic_to_add);
var row_labs = table.rowLabels;
var new_labs = [];
// Loop through each row
for (var row = 0; row < table.numberRows; row++) {
// Create string with old row label and value of specified statistic
var val = parseFloat(vals[row]);
var old_lab = row_labs[row];
var new_lab = old_lab + " (" + text_before + " " + val.toFixed(decimals) + ")"
new_labs[row] = new_lab;
}
// Set new row labels
table.rowLabels = new_labs;
See also
- Table JavaScript and Plot JavaScript for an explanation of how to run this code.
- Table JavaScript and Plot JavaScript Reference for technical information.
- Table JavaScript and Plot JavaScript Examples Library for other examples.
- JavaScript for information about the JavaScript programming language.
- QScript for tools for automating projects using JavaScript.