Difference between revisions of "Computed cell formats"

m (Changed Var declarations to Local)
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[category:Analytica 5.0]]
 
[[category:Analytica 5.0]]
 
[[category:Concepts]]
 
[[category:Concepts]]
 +
{{ReleaseBar}}
  
 
''New to [[Analytica 5.0]]
 
''New to [[Analytica 5.0]]
  
== Overview ==
+
You can use an expression to depict interesting features of the data in a table as a format for each cell, using font style, size, and color, fill color, text alignment, border style, number format, and bars within each cell. Computed formats offer a wide variety of creative ways to visualize what's important and interesting about your data.
You can write and use an Analytica expression to compute cell-level formats for a table (usually a result table), thus enabling the appearance of the table to adapt to data and results in the model. Fill color, font style and color, text alignment, border style, number format, and cell bars can all be computed, with virtually unlimited possibilities for the sophistication of logic.
+
 
 +
__TOC__
  
 
=== Some examples ===
 
=== Some examples ===
Several examples of computed formats are show here is illustrate the potential application of computed formats.
+
Here are a few examples to show some of what's possibe with computed cell formats:
  
 +
 +
A heat map emphasizes the numeric quantity, using green vs. red to show positive versus negative, and transparency/opaqueness to reflect the magnitude of the numbers in each cell.
 
:[[image:ComputedCellFormat_heatmap.png]]
 
:[[image:ComputedCellFormat_heatmap.png]]
A heat map can be used to emphasizes the numeric quantity, where green vs. red is used to reflect positive versus negative, and transparency/opaqueness used to emphasize magnitude of the numbers in each cell.
 
  
 +
Show numeric magnitude by varying font size:
 
:[[image:ComputedCellFormat_fontSize.png]]
 
:[[image:ComputedCellFormat_fontSize.png]]
Emphasizing numeric magnitude using varying font size.
 
  
 +
Depicting quantity using cell bars:
 
:[[image:ComputedCellFormat_bars.png]]
 
:[[image:ComputedCellFormat_bars.png]]
And yet another method, depicting quantity using cell bars.
 
  
 +
Alternate row colors:
 
:[[image:ComputedCellFormat_alternatingRowColor.png]]
 
:[[image:ComputedCellFormat_alternatingRowColor.png]]
Alternating row colors.
 
  
 +
Use fill color and outer border to emphasize cells values with a certain property, in this case numbers that are divisible by 3:
 
:[[image:ComputedCellFormat_divisibleBy3.png]]
 
:[[image:ComputedCellFormat_divisibleBy3.png]]
Fill color and outer border emphasize cells containing values with a certain property, in this case numbers that are divisible by 3.
 
  
 +
Compute the indentation of index elements based on information in the model -- in this case, whether the element is a state, country or city. This can be easier that manually indenting the items from the dialog, and automatically adapts as you add locations to the hierarchy:
 
:[[image:ComputedCellFormat_indentation.png]]
 
:[[image:ComputedCellFormat_indentation.png]]
The indentation for the elements of an index are computed based on information in the model (in this case, whether the element is a state, country or city). This can be easier that manually indenting the items from the dialog, and can automatically adapt to new locales that appear later.
+
 
 +
=== Tutorial video ===
 +
 
 +
<center>
 +
<iframe width="640" height="360" src="https://www.youtube.com/embed/LcaBofuv68U" frameborder="0" allowfullscreen></iframe>
 +
 
 +
'''Video:''' [https://youtu.be/LcaBofuv68U How to show negative numbers in red] (time 2:30)
 +
</center>
  
 
== Cell Format Expressions ==
 
== Cell Format Expressions ==
To use an expression to computed the cell formats, enter the expression into the '''Cell Format Expression''' attribute. To make this attribute visible in the [[Object window]] and [[Attribute pane]], go to the [[Attributes dialog]] (on the ''Object'' menu) and turn on  '''Cell Format Expression'''. Then edit the expression for your table from its [[Object window]] or from the [[Attribute pane]].
 
  
By default, your computed format will apply to the result table of the variable with the format expression, but not to the edit table. Your computed formats take precedence over any manually selected formats (set from the [[Cell format dialog]]).
+
To specify a computed cell format, you enter the expression into the '''Cell Format Expression''' attribute. You may first need to make make this attribute visible in the [[Object window]] and [[Attribute pane]]. Open the [[Attributes dialog]] (on the ''Object'' menu), select Variable if needed, and and check '''Cell Format Expression''' in the list of attributes. Then you can enter and edit the expression in the Object's [[Object window]] or the [[Attribute pane]].
  
Cell format expressions are just normal Analytica expressions, but with calls to special functions called ''cell format predicates'': [[CellAlignment]], [[CellBars]], [[CellBorder]], [[CellFill]], [[CellFont]], [[CellNumberFormat]], and [[CellOnClick]]. The [[CellFormats]] function is used to combine multiple predicates. The [[CellSpan]] function is used to apply formats to header cells and totals. The [[CellComparisonFormat]] is used to apply formats to external comparison variable columns.
+
By default, a computed cell format for a variable defined as an Edit table (or Subtable, Multitable, or DetermTable) applies to its result table, but not to its edit table. A computed format takes precedence over any manually selected formats set from the [[Cell format dialog]].
  
== Simple examples ==
+
Cell format expressions are just normal Analytica expressions, but with special functions called [[:category:Cell format functions|''cell format predicates'']]: [[CellAlignment]], [[CellBar]], [[CellBorder]], [[CellFill]], [[CellFont]], [[CellNumberFormat]], and [[CellOnClick]]. The [[CellFormats]] function lets you combine multiple predicates. The [[CellSpan]] function applies formats to header cells and totals. The [[CellComparisonFormat]] applies formats to external comparison variable columns.
 +
 
 +
== Computing formats ==
  
 
=== Simplest example ===
 
=== Simplest example ===
The simplest example of a computed format would be an expression that contains a single predicate:
+
The simplest example of a computed format would be an expression that contains a single predicate, and so applies the same format to every body cell:
 
:<code>[[CellFill]]('Green', 0.1)</code>
 
:<code>[[CellFill]]('Green', 0.1)</code>
The 0.1 is the alpha channel (on a scale from 0.0 to 1.0), and serves here to lighten the green  fill color. With this [[Cell format expression]], every body cell contains a dim green fill.
+
The 0.1 is the alpha channel (on a scale from 0.0 to 1.0), and serves here to lighten the green  fill color.  
  
 
:[[image:CellFormatExpression_SimplestExample.png]]
 
:[[image:CellFormatExpression_SimplestExample.png]]
  
 
=== Highlighting largest row and column ===
 
=== Highlighting largest row and column ===
Next, we use the <code>[[CellFill]]('Green', 0.1)</code> inside some logic. In this case, we'll highlight the cells in the row and column with the largest sum. The [[CellFormatExpression]] is now
+
Next, we use the <code>[[CellFill]]('Green', 0.1)</code> inside some logic to highlight the cells in the row and column with the largest sum:
  [[Var]] si := [[Sum]]([[Self]],I);
+
  [[Local]] si := [[Sum]]([[Self]], I);
  [[Var]] sj := [[Sum]]([[Self]],J);
+
  [[Local]] sj := [[Sum]]([[Self]], J);
  [[If]] si=[[Max]](si,J)  or  sj=[[Max]](sj,I) [[Then]] [[CellFill]]('Green', 0.1)
+
  [[If]] si=[[Max]](si, J)  or  sj=[[Max]](sj, I) [[Then]] [[CellFill]]('Green', 0.1)
 
   
 
   
 
  :[[image:ComputedCellFormat_Largest_I_and_J.png]]
 
  :[[image:ComputedCellFormat_Largest_I_and_J.png]]
  
Notice that the expression references [[Self]].
+
Notice that the expression references [[Self]] -- that is the result of the variable for which you are computing a format.
  
 
=== Highlight tails  ===
 
=== Highlight tails  ===
In this example, we put the top 5 values along <code>J</code> in bold, and the bottom 5 values along <code>J</code> in italics.
+
In this example, we highlight the largerst 2 values along <code>J</code> in bold, and the lowest 2 values along <code>J</code> in italics.
  [[Var]] r := [[Rank]]([[Self]],J);
+
  [[Local]] r := [[Rank]]([[Self]],J);
 
  [[If]] r<=2 [[Then]] [[CellFont]](italics:true)
 
  [[If]] r<=2 [[Then]] [[CellFont]](italics:true)
 
  [[Else]] [[If]] r+2>[[IndexLength]](J) [[Then]] [[CellFont]](bold:true)
 
  [[Else]] [[If]] r+2>[[IndexLength]](J) [[Then]] [[CellFont]](bold:true)
Line 63: Line 75:
  
 
=== Combining multiple predicates: hiding zeros ===
 
=== Combining multiple predicates: hiding zeros ===
This example uses [[CellFormats]]( ) to combine two predicates, so that two properties are changed together (the font and the border). The example hides cells with zeros in a sparse matrix by setting the font color to white and the border type for all inner sides along <code>I</code> and along <code>J</code> to <code>'None'</code>.
+
 
 +
This example uses [[CellFormats]]( ) to combine two predicates to change two properties -- font and the border. It hides cells with zeros by setting the font color to white and not showing their cell border on inner sides along <code>I</code> and<code>J</code>:
  
 
  [[If]] [[Self]]=0 [[Then]] [[CellFormats]](  
 
  [[If]] [[Self]]=0 [[Then]] [[CellFormats]](  
         [[CellFont]](color:'White'),
+
         [[CellFont]](color: 'White'),
         [[CellBorder]]('None',1,'Inner',I,J)
+
         [[CellBorder]]('None', 1, 'Inner', I, J)
 
  )
 
  )
 
   
 
   
Line 73: Line 86:
  
 
=== Combining multiple logic fragments ===
 
=== Combining multiple logic fragments ===
In addition to combining cell predicates, the [[CellFormats]] function is also used to combine multiple logic fragments. In this example, the first logic fragment sets the cell fill for every other row along index <code>I</code>, and the second logic fragment sets the font color of negative numbers to red.
+
 
 +
You can also use [[CellFormats]] to combine multiple logic fragments. In this example, the first fragment sets the cell fill for every other row along index <code>I</code>, and the second fragment sets the font color of negative numbers to red:
  
 
  [[CellFormats]](
 
  [[CellFormats]](
Line 83: Line 97:
  
 
== Applying formats to Totals rows and columns ==
 
== Applying formats to Totals rows and columns ==
 +
 
The cells that appear in a Totals row or Totals column are outside the range of coordinates that can be described using the array indexes and [[Self]]. To specify a format that applies only to the Totals row or column, use the [[CellSpan]] function with a position of zero. The syntax is:
 
The cells that appear in a Totals row or Totals column are outside the range of coordinates that can be described using the array indexes and [[Self]]. To specify a format that applies only to the Totals row or column, use the [[CellSpan]] function with a position of zero. The syntax is:
 
:<code>[[CellSpan]]( I, fmt, 0 )</code>
 
:<code>[[CellSpan]]( I, fmt, 0 )</code>
Line 125: Line 140:
 
  :[[image:ComputedCellFormat_HeaderAndBody.png]]
 
  :[[image:ComputedCellFormat_HeaderAndBody.png]]
  
You can nest [[CellFormats]] inside (or outside) [[CellSpan]], and [[CellSpan]] calls are often nested, often when they involve different indexes. For example, apply a fill and border change to header and body cells along <code>J</code>, but only in the totals row along <code>I</code>.
+
You can nest [[CellFormats]] inside (or outside) [[CellSpan]]. Here the [[CellSpan]] selects the totals row along «I», and the header cells of «I» as well as body cells (because the «header» parameter is omitted, and then within the [[CellSpan]] both fill and border are changed, so a [[CellFormats]] is used to combine them.
 +
 
 +
[[CellSpan]]( I, [[CellFormats]](
 +
      [[CellFill]]('Yellow'),
 +
      [[CellBorder]]( 'Double', ,'Near', I, color:'Red' )
 +
), 0)
 +
 +
:[[image:ComputedCellFormat_CFinsideCP.png]]
 +
 
 +
== Comparison variable columns ==
 +
When you have [[XY_Comparison|comparison variables]] (or expressions) in your result table, [[CellComparisonFormat]] is used to select the column(s) that the format should apply to. The result for a variable <code>y</code> is shown here, and it the same table the results for variables <code>x</code> and <code>u</code> are also shown and have been added to this table as [[XY_Comparison|comparison variables]]. The Cell Format Expression is set to <code>[[CellBar]](Self)</code>, and since [[Self]] here is the result of <code>y</code>, the computed cell format has only information about <code>y</code>, but displays in the columns for <code>x</code> and <code>u</code>.
 +
 
 +
[[CellBar]]([[Self]])
 +
 +
:[[image:ComputedCellFormat_ComparisonVars1.png]]
 +
 
 +
The above is not the desired display, since the bars that appear in the <code>x</code> column reflect the value of <code>y</code>. To restrict these bars to the y column, use
 +
 
 +
[[CellComparisonFormat]]( [[CellBar]]([[Self]]), [[Self]] )
 +
 +
:[[image:ComputedCellFormat_ComparisonVars2.png]]
 +
 
 +
To show the correct bars in each respective column
 +
 
 +
CellFormats(
 +
    CellComparisonFormat( CellBar(Self), Self ),
 +
    CellComparisonFormat( CellBar(x), x ),
 +
    CellComparisonFormat( CellBar(u), u )
 +
)
 
   
 
   
 +
:[[image:ComputedCellFormat_ComparisonVars3.png]]
 +
 +
== Interaction with manually set formats ==
 +
When you have set some cell formats from the [[Cell format dialog]], and also have computed formats, the computed formats take precedence. Hence, if you set the fill to red using the dialog, but your '''Cell Format Expression''' sets cell fill to green for the same cell, the cell will be green.
 +
 +
== Auxilliary variables ==
 +
Your '''Cell Format Expression''' can reference several values that provide information about the view being shown, as follows.
 +
* <code>Self</code>: The value in each cell of the result or edit table. (For edit tables, these might be expressions).
 +
* <code>HorizIndex</code>: Synonymous with the horizontal index in the current pivot.
 +
* <code>VertIndex</code>: Synonymous with the Vertical index in the current pivot.
 +
* <code>ComparisonIndex</code>: Synonymous with the comparison index, if any.
 +
* <code>ViewMode</code>: The view type, one of: <code>'Mid'</code>, <code>'Mean'</code>, <code>'Sample'</code>, <code>'PDF'</code>, <code>'CDF'</code>, <code>'Statistics'</code>, <code>'Bands'</code>, <code>'Edit'</code>.
 +
* <code>VertIndexes</code>: A list of row indexes (currently either 0 or 1 [[handle]]s in length).
 +
* <code>HorizIndexes</code>: A list of column indexes (currently 0, 1, or 2 [[handle]]s in length).
 +
* <code>SlicersSetToTotal</code>: A list of handles to slicer indexes that are currently set to Total.
 +
 +
Finally, the list of slicer index settings can be obtained from the top level of your logic, but not necessarily from within nested expressions, using the [[GetEvaluationContext]] function. For example, <code>[[GetEvaluationContext]]( )</code> returns the list of slicer indexes, except those set to '''Totals'''. Then, for any of these, <code>[[GetEvaluationContext]](h)</code> returns the selected position along that index.
 +
 +
=== Alternating row fill ===
 +
An example earlier on this page showed an alternating row fill along the <code>I</code> index. If you pivot the table and put <code>I</code> on the horizontal, the alternation becomes an alternation of column colors.  And it the pivot is such that <code>I</code> is on neither axis, no fill alternation appears at all, because the alternation is associated with the index <code>I</code>.
 +
 +
Using the auxiliary variable <code>VertIndex</code>, you can alternative rows in such a way that the row color alternates no matter what index is on the vertical.
 +
 +
[[If]] [[Mod]](@VertIndex,2) [[Then]] [[CellFill]]( 0xeeeeee ) [[Else]] [[CellFill]]( 0xffffcc )
 +
 +
:[[image:ComputedCellFormats_VertIndex.png]]
 +
 +
and if the table is pivoted, it is still the row fill that alternates.
 +
:[[image:ComputedCellFormats_VertIndex2.png]]
 +
 +
Although auxiliary variables are seldom used, outside the alternating row example, they do enable you to encode conditional logic that changes based on the view being displayed.
 +
 +
== Edit Tables ==
 +
By default, computed formats apply only to result tables, but static formats set from the [[Cell format dialog]] apply to both edit tables and result tables.
 +
 +
To apply your computed format to edit tables, {{Release|6.0||right-click on your '''Cell Format Expression''' in the object window select ''Apply computed cell format to edit table''. Internally this sets the [[CellFormatFlags]] attribute for the variable to 1. This is demonstrated in the following video
 +
:[[image:Apply compute cell formats to edit table.gif]]
 +
}}{{Release||5.9|set the [[CellFormatFlags]] attribute for your variable to 1. This must be done from [[Typescript]]:
 +
* Press F12 to open the [[Typescript Window]]
 +
* Type: <code>CellFormatFlags x : 1</code>
 +
where <code>x</code> is the identifier of your table.}}
 +
 +
Computed formats should be applied to edit tables only in certain safe cases. Many problems can result, so you should take great care before doing so. First, when your logic references [[Self]], in many cases the values might be expressions in the edit table view, not numbers as they would be in the result table. This is likely to cause errors if your logic does even very common operations on the value, such as computing a [[Sum]] or [[Max]], or using the value where a number is expected. Hence, you usually only want to consider computed formats that are restricted to numeric entries only.
 +
 +
Another consideration is the computation time required to re-evaluate your cell format logic. The expression will need to be re-evaluated often, including after individual cell changes. In a huge table, an operation over the entire array can take a substantial amount of time, and cause your edit table to be unwieldy.
 +
 +
Your computed expression can use the value of <code>ViewMode</code>, which is set to <code>'Edit'</code> when an edit table is showing, as a way to alter your logic for the edit table view.
 +
:<code>[[If]] ViewMode='Edit' [[Then]] «expression used for edit tables» [[Else]] «expression used for result tables»</code>
  
 
== See Also ==
 
== See Also ==
 
* [[Cell format dialog]]
 
* [[Cell format dialog]]
 +
* Predicates for specifying formats: [[CellAlignment]], [[CellBar]], [[CellBorder]], [[CellFill]], [[CellFont]], [[CellNumberFormat]], and [[CellOnClick]]
 +
* Combining predicates or logic: [[CellFormats]]
 +
* Selecting special cells (totals, headers, comparison columns): [[CellSpan]], [[CellComparisonFormat]]
 +
* [[Attributes dialog]]

Latest revision as of 23:12, 8 October 2021



Release:

4.6  •  5.0  •  5.1  •  5.2  •  5.3  •  5.4  •  6.0  •  6.1  •  6.2  •  6.3  •  6.4  •  6.5


New to Analytica 5.0

You can use an expression to depict interesting features of the data in a table as a format for each cell, using font style, size, and color, fill color, text alignment, border style, number format, and bars within each cell. Computed formats offer a wide variety of creative ways to visualize what's important and interesting about your data.

Some examples

Here are a few examples to show some of what's possibe with computed cell formats:


A heat map emphasizes the numeric quantity, using green vs. red to show positive versus negative, and transparency/opaqueness to reflect the magnitude of the numbers in each cell.

ComputedCellFormat heatmap.png

Show numeric magnitude by varying font size:

ComputedCellFormat fontSize.png

Depicting quantity using cell bars:

ComputedCellFormat bars.png

Alternate row colors:

ComputedCellFormat alternatingRowColor.png

Use fill color and outer border to emphasize cells values with a certain property, in this case numbers that are divisible by 3:

ComputedCellFormat divisibleBy3.png

Compute the indentation of index elements based on information in the model -- in this case, whether the element is a state, country or city. This can be easier that manually indenting the items from the dialog, and automatically adapts as you add locations to the hierarchy:

ComputedCellFormat indentation.png

Tutorial video

Video: How to show negative numbers in red (time 2:30)

Cell Format Expressions

To specify a computed cell format, you enter the expression into the Cell Format Expression attribute. You may first need to make make this attribute visible in the Object window and Attribute pane. Open the Attributes dialog (on the Object menu), select Variable if needed, and and check Cell Format Expression in the list of attributes. Then you can enter and edit the expression in the Object's Object window or the Attribute pane.

By default, a computed cell format for a variable defined as an Edit table (or Subtable, Multitable, or DetermTable) applies to its result table, but not to its edit table. A computed format takes precedence over any manually selected formats set from the Cell format dialog.

Cell format expressions are just normal Analytica expressions, but with special functions called cell format predicates: CellAlignment, CellBar, CellBorder, CellFill, CellFont, CellNumberFormat, and CellOnClick. The CellFormats function lets you combine multiple predicates. The CellSpan function applies formats to header cells and totals. The CellComparisonFormat applies formats to external comparison variable columns.

Computing formats

Simplest example

The simplest example of a computed format would be an expression that contains a single predicate, and so applies the same format to every body cell:

CellFill('Green', 0.1)

The 0.1 is the alpha channel (on a scale from 0.0 to 1.0), and serves here to lighten the green fill color.

CellFormatExpression SimplestExample.png

Highlighting largest row and column

Next, we use the CellFill('Green', 0.1) inside some logic to highlight the cells in the row and column with the largest sum:

Local si := Sum(Self, I);
Local sj := Sum(Self, J);
If si=Max(si, J)  or  sj=Max(sj, I) Then CellFill('Green', 0.1)

:ComputedCellFormat Largest I and J.png

Notice that the expression references Self -- that is the result of the variable for which you are computing a format.

Highlight tails

In this example, we highlight the largerst 2 values along J in bold, and the lowest 2 values along J in italics.

Local r := Rank(Self,J);
If r<=2 Then CellFont(italics:true)
Else If r+2>IndexLength(J) Then CellFont(bold:true)

:ComputedCellFormat HighlightTails.png

Combining multiple predicates: hiding zeros

This example uses CellFormats( ) to combine two predicates to change two properties -- font and the border. It hides cells with zeros by setting the font color to white and not showing their cell border on inner sides along I andJ:

If Self=0 Then CellFormats( 
       CellFont(color: 'White'),
       CellBorder('None', 1, 'Inner', I, J)
)

:ComputedCellFormat HideZeros.png

Combining multiple logic fragments

You can also use CellFormats to combine multiple logic fragments. In this example, the first fragment sets the cell fill for every other row along index I, and the second fragment sets the font color of negative numbers to red:

CellFormats(
     If Mod(@I,2) Then CellFill(0xeeeeee),
     If Self<0 Then CellFont(color:'Red')
)
 
:ComputedCellFormat MultipleLogic.png

Applying formats to Totals rows and columns

The cells that appear in a Totals row or Totals column are outside the range of coordinates that can be described using the array indexes and Self. To specify a format that applies only to the Totals row or column, use the CellSpan function with a position of zero. The syntax is:

CellSpan( I, fmt, 0 )

where you are specifying format for the total over the I index, and «fmt» is a cell format expression. The 0 is the third parameter identifies the totals.

To create a thick border (width 3 pixels) between the totals and the body along index I, you can use

CellSpan( I, CellBorder( 'Solid', 3, 'Near', I ), 0 )

:ComputedCellFormat TotalsBorder.png


To do this for both indexes

CellFormats(
     CellSpan( I, CellBorder( 'Solid', 3, 'Near', I ), 0 ),
     CellSpan( J, CellBorder( 'Solid', 3, 'Near', J ), 0 )
)

:ComputedCellFormat TotalsBorder2.png
To set the borders of only the bottom right corner total (all sides)
CellSpan( I, CellSpan( J, CellBorder( 'Solid', 3, 'Both', I, J ), 0), 0 )

:ComputedCellFormat TotalsBorder3.png

Notice that in these examples, both the CellSpan predicate and the CellBorder predicate expect an index, but these indexes are playing different roles. In CellSpan it is identifying which cells (i.e., totals row or totals column), whereas in CellBorder it is identifying which side of the cells that were identified by the outer CellSpan (i.e., top/bottom or left/right). This is demonstrated by this example where one index is passed to CellSpan and the other index to CellBorder:

CellSpan( J, CellBorder( 'Solid', 3, 'Both', I ), 0) 

:ComputedCellFormat TotalsBorder4.png

Applying formats to header cells

To apply a format to the header cells for index I, use CellSpan( I, fmt, header:true ).

CellSpan( I, CellFill('Yellow'), header:true )

:ComputedCellFormat Header.png

With the «header» parameter set to false, CellSpan( I, fmt, header:false ) applies the format only to body cells, not to header cells, which is what usually happens without the CellSpan unless the expression is nested inside another CellSpan that applies it to headers. To apply the format to both headers and body cells, omit the header parameter.

CellSpan( I, CellFill('Yellow') )

:ComputedCellFormat HeaderAndBody.png

You can nest CellFormats inside (or outside) CellSpan. Here the CellSpan selects the totals row along «I», and the header cells of «I» as well as body cells (because the «header» parameter is omitted, and then within the CellSpan both fill and border are changed, so a CellFormats is used to combine them.

CellSpan( I, CellFormats( 
      CellFill('Yellow'), 
      CellBorder( 'Double', ,'Near', I, color:'Red' )
), 0)

:ComputedCellFormat CFinsideCP.png

Comparison variable columns

When you have comparison variables (or expressions) in your result table, CellComparisonFormat is used to select the column(s) that the format should apply to. The result for a variable y is shown here, and it the same table the results for variables x and u are also shown and have been added to this table as comparison variables. The Cell Format Expression is set to CellBar(Self), and since Self here is the result of y, the computed cell format has only information about y, but displays in the columns for x and u.

CellBar(Self)

:ComputedCellFormat ComparisonVars1.png

The above is not the desired display, since the bars that appear in the x column reflect the value of y. To restrict these bars to the y column, use

CellComparisonFormat( CellBar(Self), Self )

:ComputedCellFormat ComparisonVars2.png

To show the correct bars in each respective column

CellFormats(
    CellComparisonFormat( CellBar(Self), Self ),
    CellComparisonFormat( CellBar(x), x ),
    CellComparisonFormat( CellBar(u), u )
)

:ComputedCellFormat ComparisonVars3.png

Interaction with manually set formats

When you have set some cell formats from the Cell format dialog, and also have computed formats, the computed formats take precedence. Hence, if you set the fill to red using the dialog, but your Cell Format Expression sets cell fill to green for the same cell, the cell will be green.

Auxilliary variables

Your Cell Format Expression can reference several values that provide information about the view being shown, as follows.

  • Self: The value in each cell of the result or edit table. (For edit tables, these might be expressions).
  • HorizIndex: Synonymous with the horizontal index in the current pivot.
  • VertIndex: Synonymous with the Vertical index in the current pivot.
  • ComparisonIndex: Synonymous with the comparison index, if any.
  • ViewMode: The view type, one of: 'Mid', 'Mean', 'Sample', 'PDF', 'CDF', 'Statistics', 'Bands', 'Edit'.
  • VertIndexes: A list of row indexes (currently either 0 or 1 handles in length).
  • HorizIndexes: A list of column indexes (currently 0, 1, or 2 handles in length).
  • SlicersSetToTotal: A list of handles to slicer indexes that are currently set to Total.

Finally, the list of slicer index settings can be obtained from the top level of your logic, but not necessarily from within nested expressions, using the GetEvaluationContext function. For example, GetEvaluationContext( ) returns the list of slicer indexes, except those set to Totals. Then, for any of these, GetEvaluationContext(h) returns the selected position along that index.

Alternating row fill

An example earlier on this page showed an alternating row fill along the I index. If you pivot the table and put I on the horizontal, the alternation becomes an alternation of column colors. And it the pivot is such that I is on neither axis, no fill alternation appears at all, because the alternation is associated with the index I.

Using the auxiliary variable VertIndex, you can alternative rows in such a way that the row color alternates no matter what index is on the vertical.

If Mod(@VertIndex,2) Then CellFill( 0xeeeeee ) Else CellFill( 0xffffcc )

:ComputedCellFormats VertIndex.png

and if the table is pivoted, it is still the row fill that alternates.

:ComputedCellFormats VertIndex2.png

Although auxiliary variables are seldom used, outside the alternating row example, they do enable you to encode conditional logic that changes based on the view being displayed.

Edit Tables

By default, computed formats apply only to result tables, but static formats set from the Cell format dialog apply to both edit tables and result tables.

To apply your computed format to edit tables, right-click on your Cell Format Expression in the object window select Apply computed cell format to edit table. Internally this sets the CellFormatFlags attribute for the variable to 1. This is demonstrated in the following video

Apply compute cell formats to edit table.gif

Computed formats should be applied to edit tables only in certain safe cases. Many problems can result, so you should take great care before doing so. First, when your logic references Self, in many cases the values might be expressions in the edit table view, not numbers as they would be in the result table. This is likely to cause errors if your logic does even very common operations on the value, such as computing a Sum or Max, or using the value where a number is expected. Hence, you usually only want to consider computed formats that are restricted to numeric entries only.

Another consideration is the computation time required to re-evaluate your cell format logic. The expression will need to be re-evaluated often, including after individual cell changes. In a huge table, an operation over the entire array can take a substantial amount of time, and cause your edit table to be unwieldy.

Your computed expression can use the value of ViewMode, which is set to 'Edit' when an edit table is showing, as a way to alter your logic for the edit table view.

If ViewMode='Edit' Then «expression used for edit tables» Else «expression used for result tables»

See Also

Comments


You are not allowed to post comments.