{ Analytica Model Dynamically_changing, encoding="UTF-8" } SoftwareVersion 6.0.5 { System Variables with non-default values: } SampleSize := 1000 TypeChecking := 1 Checking := 1 SaveOptions := 2 SaveValues := 0 {!-50299|DiagramColor Model: 65535,65535,65535} {!-50299|DiagramColor Module: 65535,65535,65535} {!-50299|DiagramColor LinkModule: 65535,65535,65535} {!-50299|DiagramColor Library: 65535,65535,65535} {!-50299|DiagramColor LinkLibrary: 65535,65535,65535} {!-50299|DiagramColor Form: 65535,65535,65535} NodeInfo FormNode: 1,0,0,,0,0,,,,0,,,0 {!-50299|NodeColor Text: 62258,62258,62258} {!-60000|Attribute AcpStyles} {!50103|AskAttribute OnGraphDraw,Variable,Yes} Model Dynamically_changing Title: Dynamically changing graph title Description: Demonstrates how you can compute the axis title in a graph. ~ (Requires Analytica 6.0.5 or better).~ ~ The code of interest is in the OnGraphDraw expression for Cost.~ ~ To see it is action, view the result for Cost and then select different metrics. Author: Dale Rice Date: Thu, Jun 10, 2021 11:14 AM DiagState: 2,0,0,724,618,21,, WindState: 2,400,293,720,350 FontStyle: Arial,15 FileInfo: 0,Model Dynamically_changing,2,2,0,0,W:\TestModels\Computed AxisTitle includes Slicer value.ana Index Metric Title: Metric Definition: ['Rider','Driver'] NodeLocation: 96,48,1 NodeSize: 64,24 {!40300|Att_SlicerPopupSize: 257,64} Index Case Title: Case Definition: 'A'..'D' NodeLocation: 96,112,1 NodeSize: 64,24 Variable Cost Title: Cost Units: $ Description: This graph has a computed Y-axis title. To see it in action, view the graph, then change the Metric slicer to different values. Notice how the text of the Y-axis changes.~ ~ To see a description of how this works, see Using a computed axis title in a graph. Definition: Random(Uniform(10,100,over:Case,Metric)) NodeLocation: 264,48,1 NodeSize: 64,24 WindState: 2,636,41,720,552 ValueState: 2,615,23,545,346,1,MIDM Aliases: FormNode Fo1282220419 GraphSetup: {!50500|Graph_ShowTitle:0} ReformVal: [Case,Metric] Att__TotalsIndex: [] Att_ResultSliceState: [Metric,1,Metric,2,Case,1] Att_ColorRole: Null {!40600|Att_ClusterIndex: Null} {!50103|OnGraphDraw: Local slicers := #info[OnGraphDrawItem='SlicerPositions'];~ Local pos := slicers[.Slicer=Handle(Metric)];~ ~ if pos<>Null then~ roleChanges[GraphingRole='Y axis', GraphFillerInfo='Title'] := ~ f"Cost for {Slice(Metric,pos)}"} {!50103|onGraphDrawFlagsAtt: 2} FormNode Fo1282220419 Title: Cost Definition: 1 NodeLocation: 296,68,1 NodeSize: 48,12 NodeInfo: 1,,,0 Original: Cost Text Te933880195 Description: View result graph for Cost. ~ ~ Change Metric and notice how the Y-axis adapts. NodeLocation: 464,72,-1 NodeSize: 104,40 Function AxisTitleFromSlicer_( info, ~ roleChanges ; ~ I : Index ; ~ textFn : Function(slicerLabel) ; ~ role : text = 'Y axis') Title: AxisTitleFromSlicer b Description: Use in OnGraphDraw to create an axis (or key) label that changes as you change a slicer to different values. This accepts a function for the textFn which should compute the final label from the slicer value. The function you pass can be a UDF or a local function. This is more esoteric than the first method, but is also more general.~ ~ Function generateTitle( slicerLabel) : f"Cost of {slicerLabel}";~ roleChanges := AxisTitleFromSlicer( info, roleChanges, Metric, generateTitle ) Definition: Local slicers := #info[OnGraphDrawItem='SlicerPositions'];~ Local pos := slicers[.Slicer=Handle(I)];~ if pos<>Null then~ roleChanges[GraphingRole='Y axis', GraphFillerInfo='Title'] := ~ f"Cost for {Slice(I,pos)}";~ roleChanges NodeLocation: 136,496,1 NodeSize: 80,24 WindState: 2,557,34,721,504 Variable Cost_b Title: Cost b Units: $ Description: This graph has a computed Y-axis title. To see it in action, view the graph, then change the Metric slicer to different values. Notice how the text of the Y-axis changes.~ ~ To see a description of how this works, see Using a computed axis title in a graph. Definition: Random(Uniform(10,100,over:Case,Metric)) NodeLocation: 296,496,1 NodeSize: 64,24 WindState: 2,282,100,1072,552 ValueState: 2,687,21,545,346,1,MIDM Aliases: FormNode Fo237887875 GraphSetup: {!50500|Graph_ShowTitle:0} ReformVal: [Case,Metric] Att__TotalsIndex: [] Att_ResultSliceState: [Metric,1,Metric,2,Case,1] Att_ColorRole: Null {!40600|Att_ClusterIndex: Null} {!50103|OnGraphDraw: Function generateTitle(slicerLabel) : f"Cost of {slicerLabel}";~ roleChanges := AxisTitleFromSlicer_( info, roleChanges, Metric, generateTitle )} {!50103|onGraphDrawFlagsAtt: 2} Function AxisTitleFromSlicer( info, ~ roleChanges ; ~ I : Index ; ~ titleTemplate ; ~ role : text = 'Y axis') Title: AxisTitleFromSlicer Description: Use in OnGraphDraw to create an axis (or key) label that changes as you change a slicer to different values. Use the pattern "%L%" in «titleTemplate» to indicate where you want the index value to be substituted.~ ~ Set roleChanges to the result, e.g.,~ ~ roleChanges := AxisTitleFromSlicer( info, roleChanges, Metric, "Cost of %L%" ) Definition: Local slicers := #info[OnGraphDrawItem='SlicerPositions'];~ Local pos := slicers[.Slicer=Handle(I)];~ if pos<>Null then~ roleChanges[GraphingRole='Y axis', GraphFillerInfo='Title'] := ~ TextReplace( titleTemplate, "%L%", Slice(I,pos), all:true );~ roleChanges NodeLocation: 136,416,1 NodeSize: 80,24 WindState: 2,651,32,722,507 Variable Cost2 Title: Cost Units: $ Description: This graph has a computed Y-axis title. To see it in action, view the graph, then change the Metric slicer to different values. Notice how the text of the Y-axis changes.~ ~ To see a description of how this works, see Using a computed axis title in a graph. Definition: Random(Uniform(10,100,over:Case,Metric)) NodeLocation: 296,416,1 NodeSize: 64,24 WindState: 2,282,100,1072,552 ValueState: 2,687,21,545,346,1,MIDM Aliases: FormNode Fo1311629699 GraphSetup: {!50500|Graph_ShowTitle:0} ReformVal: [Case,Metric] Att__TotalsIndex: [] Att_ResultSliceState: [Metric,1,Metric,2,Case,1] Att_ColorRole: Null {!40600|Att_ClusterIndex: Null} {!50103|OnGraphDraw: roleChanges := AxisTitleFromSlicer( info, roleChanges, Metric, "cost of %L%" )} {!50103|onGraphDrawFlagsAtt: 2} Text Te1984815491 Title: Wrapping logic in UDF for reuse Description: Once you've perfected an OnGraphDraw feature, you can wrap the logic into a UDF for easy reuse, which also saves you from having to remember how to do it each time.~ ~ Two examples of possible ways to encapsulate the insertion of a slicer label into a axis (or key) title are shown below. NodeLocation: 240,396,-1 NodeSize: 216,172 NodeInfo: 1,,,,1 FormNode Fo237887875 Title: Cost b Definition: 1 NodeLocation: 352,516,1 NodeSize: 48,12 NodeInfo: 1,,,0 Original: Cost_b FormNode Fo1311629699 Title: Cost Definition: 1 NodeLocation: 352,436,1 NodeSize: 48,12 NodeInfo: 1,,,0 Original: Cost2 Close Dynamically_changing