Using a computed axis title in a graph

Requires Analytica 6.0 or later

On this page, I show an example how you can display a computed axis title. By doing so, you can dynamically change your axis title as a user changes a slicer, so that the graph for each slice uses a different axis title.

Because the axis title is computed by any Analytica expression you want to come up with, your computed title could depend on just about anything, changing with the specific values in the data, the uncertainty view, the specific pivot, and so on. Also, you can use the same approach to set a graph key title to computed text.

In the current example, we will include the currently selected value of an index named Metric in the Axis title, when the graph is pivoted so that Metric is slicer. You can see this in these screenshots as I use the slicer to select different Metric:

Computed axis title Rider.pngComputed axis title Driver.png

You may want to download this example as you follow along: Computed Axis Title includes Slicer value.ana

Computing title in the OnGraphDraw attribute

I compute the axis title in the OnGraphDraw attribute during the "before drawing" phase.

To use the OnGraphDraw attribute in your own model, you need to make it visible from the Attributes dialog on the Object menu.

The OnGraphDraw attribute accepts an expression, just like a Definition of a variable accepts an expression, but this expression gets computed while the graph is being rendered. Many aspects of the graph can change between when your variable's main result finishes computing and the graph itself is displayed. For example, a user might change the uncertainty view, pivot axes, change graphing roles such as color or symbol, or change slicer values. You can get access to the current values of these by putting your expressions inside OnGraphDraw, and in addition, your code can alter certain aspects of the graph from OnGraphDraw.

The OnGraphDraw expression can be evaluated up to 4 times, at 4 distinct phases of drawing. To change the axis title, your expression needs to change the title on of the first two phases, "before layout" or "before drawing". If you were to change it in the "after axes, before data" phase, it would be too late since the axes are already drawn. In the Object Window, you can select the phase:

OnGraphDraw PhaseBeforeAxes and after.png

When you select only one phase, your code doesn't need to include conditional logic based on the phase, since it will be run only once. If your OnGraphDraw code is doing multiple things, you may need to enable multiple phases, in which case you could surround the code developed here inside If phase=2 Then ( «code in this article» ). By so doing, you won't incur the inefficiency of running the same code multiple times. The phases are numbered 1, 2, 4 and 8. Since I am enabling only one phase, I don't include this test here.

To actually change the title, your code will assign to the local variable «roleChanges», which is one of the locals provided to the OnGraphDraw attribute.

Accessing the Slicer position

We want to use the currently selected value of Metric in our title, so we need to access the slicer for the Metric index and determine which value is currently selected. This is accomplished with the following two lines in OnGraphDraw:

Local slicers := #info[ OnGraphDrawItem='SlicerPositions'];
Local pos := slicers[.Slicer=Handle(Metric)];

Note that Metric is the name of our index in our model. OnGraphDrawItem is a system index that indexes the local variable info. The local info is pre-defined by the graphing engine before evaluating OnGraphDraw, and contains a large amount of information about the graph being drawn (see OnGraphDrawItem for the list of what is there).

The first line extracts an array containing all the slicers and their positions, the second line extracts the currently selected position for the slicer for Metric. If the pivot is changed so that Metric is no longer a slicer, pos will be Null.

Computing the axis title text

Once we know the selected position, the expression Slice(Metric, pos) returns the selected index value. (Aside: In the case of a self-indexed array, you would need to use Slice(IndexValue(Metric),pos).) Hence, we can now compute our axis title text using a Formatted Text Literal as

f"Cost for {Slice(Metric,pos)}"

Changing the axis title

To change the axis title in the graph to our computed value, we assign to the appropriate cell of roleChanges:

roleChanges[ GraphingRole='Y axis', GraphFillerInfo='Title'] :=
f"Cost for {Slice(Metric,pos)}"
Comments


You are not allowed to post comments.