Plot/Chart - Automatically Color Rows or Bars of Plots (and Tables)

From Q
Jump to: navigation, search

This rule colors bars or columns in a bar or column chart that contain a specified text in their labels. It is for situations where there is a need to automatically format many different tables or charts (e.g., if wanting a brand to always be colored with its own color).

Example

Technical details

  • The input text is searched for in the labels in a case-insensitive way (capitalization does not matter).
  • This script is not compatible with tables/charts that have row spans.
  • The For Loop goes through and makes the change to all the rows columns in the underlying table.
  • A list of different colors is at JavaScript Color Table.
  • The object containing the colors, colors, is an Array, where each element in the array relates to a row and, this element is, initself, another Array, where each element refers to the column within the array.
  • Having modified the array we then need to tell Q to use the modified array, which is what occurs with the code table.cellColors = colors;

How to apply this rule

For the first time in a project

  • Select the table(s)/chart(s) that you wish to apply the rule to.
  • Start typing the name of the Rule into the Search features and data box in the top right of the Q window.
  • Click on the Rule when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Choose this rule from the list.

Additional applications of the rule

  • Select a table or chart that has the rule and any table(s)/chart(s) that you wish to apply the rule to.
  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to apply.
  • Click on the Apply drop-down and choose your desired option.
  • Check New items to have it automatically applied to new items that you create. Use Edit > Project Options > Save as Template to create a new project template that automatically uses this rule.

Removing the rule

  • Select the table(s)/chart(s) that you wish to remove the rule from.
  • Press the Rules tab (bottom-right corner).
  • Press Apply next to the rule you wish to remove and choose the appropriate option.

How to modify the rule

  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to modify.
  • Click Edit Rule and make the desired changes. Alternatively, you can use the JavaScript below to make your own rule (see Customizing Rules).

JavaScript

You can find a simpler version of this code, which does not contain the controls, here.

if (table.rowSpans.length > 0)
    form.ruleNotApplicable('table/plot has row spans');
 
// Set up controls for user input.
form.setHeading('Color Rows or Bars');
var label_row = form.newLabel('Text:');
var text_box = form.newTextBox('textBox');
form.setInputControls([label_row, text_box]);
var color_picker = form.newColorPicker('color');
color_picker.setDefault('LightSalmon');
form.setOutputControls([color_picker]);
var row_text = text_box.getValue().toLowerCase(); // case insensitive
var color = color_picker.getValue();
form.setOutputColor(color);
form.setSummary('Color rows or bars containing "' + row_text + '"');
 
var colors = table.cellColors;
var row_labels = table.rowLabels;
row_labels.forEach(function (label, index) {
    if (label.toLowerCase().indexOf(row_text) > -1) { // check if row label contains text
        for (var i = 0; i < table.numberColumns; i++) // set color for each element in row
            colors[index][i] = color;
    }
});
table.cellColors = colors;

Prior to the 15th of December, 2015, this page was known as Plots – Automatically Color Rows or Bars of Plots (and Tables)

See also

Personal tools
Namespaces

Variants
Actions
Navigation
Categories
Toolbox