Sorting Legends in a Chart

How to:

With many chart types, you can use legends to quickly visualize segments of your data by color. In the following image, we have an example of the Year by Revenue stacked bar chart, with the legend showing you subregions.

As you can see, the legend is sorted alphabetically, by default. You can change this default sorting to reflect the order that better fits your data narrative. There are two ways to change the legend sorting: by borrowing your sorting order from another field or by configuring a DEFINE field to specify your exact sorting order.

Procedure: How to Sort a Legend by Another Field

In this procedure, we will sort the subregion legend by region instead of the alphabetical order.

  1. On the WebFOCUS Hub, click the Plus button, and then click Create Visualizations.
  2. In the Select Data Source dialog box, choose a data source and click Create Visualizations.

    In our example, we are working with the wf_retail_lite master file.

  3. Select the fields for your chart.

    In our example, we are adding the Sale Year field to the Horizontal bucket, and the Revenue field to the Vertical bucket to recreate the Year by Revenue stacked bar chart mentioned earlier.

  4. To sort our subregion legend by region, start by dragging the Store Business Region field to the Color bucket.
  5. Right-click the Store Business Region field in the Color bucket, and then click Hide.
  6. Double-click the Store Business Sub Region field in the Resources panel.

    The field gets added to the Color bucket. Notice that the subregion legend is now sorted according to the hidden Store Business Region field, as shown in the following image.

Procedure: How to Sort a Legend With a DEFINE Field

In this procedure, we will create a DEFINE field with specific sorting parameters and use it to sort the legend.

  1. Create a basic stacked chart, as described in the previous procedure.
  2. Right-click a field that you want to sort the legend for in the Resources panel, and then click Add calculation.

    The New Calculation dialog box opens.

  3. Add the following calculation syntax:
    DECODE "Field Title" ('Value 1' 1 'Value 2' 2)

    where:

    Field Title

    Is the title of the field for which you want to sort the legend. Typically, the field title is already displayed when the New Calculation dialog box opens.

    Value 1

    Is the name of the value that you are sorting. Each value, in single quotation marks, followed by a designated number, creates the sorting order for the legend.

    An example of the configured calculation is displayed in the following image.

  4. Click Validate to make sure your calculation is correct, then click OK.
  5. Drag your newly configured DEFINE field to the Color bucket.

    Notice that the sorting order is consistent with the one we configured in our calculation, but the values are represented by sorting numbers

  6. Right-click the DEFINE field in the Color bucket, and then click Hide.
  7. Double-click the regular field in the Resources panel.

    The field gets added to the Color bucket and its legend assumes the same sorting order as the one calculated in the hidden DEFINE field, as shown in the following images.