Almost all of STATISTICA’s functionality can be accessed in automation with STATISTICA Visual Basic (SVB). Tasks from opening the data and data management to analysis and graphing can be recorded into SVB macros. With the release of Version 10, graph customizations can also be recorded in a macro. These recorded macros can be further customized to give you the specific analyses and output you need.
Many STATISTICA users take advantage of SVB macros to perform customized tasks or automate tasks performed on a regular basis. While users have always been able to customize graphs with a macro, the process is much simpler when starting with a recorded macro. This existing macro can be further enhanced by adding graph customization commands. This process can be quite manageable even for those with limited familiarity with Visual Basic and SVB. This article demonstrates how an existing macro is modified to add customizations to the graph created by the macro. Segments of SVB code from newly recorded graph customization macros are used in the existing SVB macro.
STATISTICA graphs are highly customizable, meaning you can change visual attributes such as colors, patterns, point markers, and font styles, as well as adding statistics or fit lines, etc. The possibilities are vast. New customization options are added to STATISTICA regularly, so it can be beneficial to update macros with new features.
Recording a Macro of Graph Customization
Creating a New Macro
At times, it may be easier to create a macro from scratch to perform the tasks needed rather than modifying an existing one. To record a new macro, select the Home tab. In the Tools group, click the Macro arrow. From the menu, select Start Recording Log of Analyses (Master Macro). The macro Recording dialog box will be displayed.
Create the analysis and graphs needed, including customizations to the graphs. Click the Stop Recording button in the macro Recording dialog box, and in the New Macro dialog, assign a name to the macro and click OK to generate the code.
The result is an SVB macro that automates the tasks performed in the manual analysis.
Creating Graph Customization Code for Existing Macro
We have an existing macro that creates a graph, and we want to add code to this macro that will customize the appearance of the graph. For this process, only the graph customizations need to be recorded. We’ll start by running the existing macro to create the graphs that need customized.
Open the example macro Histogram with break.svb: Select the Home tab. In the File group, click the Open arrow. From the menu, select Open Examples to display the Open a STATISTICA Data File dialog box. Double-click the Macros folder. The SVB file is located in the Graph Examples folder.
Also, open the Characteristics.sta data set from the Examples\Datasets folder.
The macro creates a histogram split on the y-axis (frequency). The two sections use different y scaling to better show frequencies when the frequencies greatly vary. Run the macro (select the Debug tab; in the Run group, click Run) selecting Weight as the variable and entering 5 as the split value. The following illustration shows the result.
We want to further customize this macro; specifically, we want to change the color and pattern of the histogram bars.
Double-click on the histogram bars to display the Graph Options dialog box. Plot - Bars will automatically be selected, which displays options to customize histogram bars. Click the Area button to display the Area Properties dialog box. Make any changes to the color and pattern.
Click the Close button in the Area Properties dialog box. The Area button will update with the selections. Select the Record macro check box located at the bottom of the Graph Options dialog box, and click OK. The New Macro dialog box will be displayed, prompting you to name and enter a description of your macro.
Click OK to generate the code.
This newly created code will be incorporated into the existing macro that produces the histogram in the next section.
Incorporating Graph Customization Code into Existing Macros
When incorporating the new graph customization code into an existing macro, a few things are important to keep in mind. First, we need to find the right place to insert the recorded code. Parts of the code belong in different parts of the existing macro. Also, the existing code has object names in place. The new graph customization code does not necessarily use the same object names. So, depending on how you choose to incorporate the code, modification may be needed.
This particular existing macro creates 2 histograms and combines them. So we need to find those two sections of code to insert the proper commands. At line 56, we see a For loop is used to create the 2 histograms. (Press CTRL+G on your keyboard to display the Go To Line dialog box, enter 56, and click the Go button.)
Scroll further down to where the graphs are actually created. At line 112, the first graph is made visible and modified. (The number of the line your cursor is on is displayed at the bottom of the macro on the right side of the status bar.) Below this is an appropriate place to add the line from the newly recorded macro, “GraphOptions(ActiveGraphEx).” This line calls the graph customization sub routine that we will add to the end of the existing macro. Also, it is good practice to use comment lines to indicate what a line of code does. The comment line is seen in green and begins with a single quote.
The line of code just added will require the sub routine from the newly recorded macro. The sub routine begins with “Sub GraphOptions(oGraph as Graph)” and goes to “End Sub.” Copy and paste this section to the end of the existing macro. It should not be placed within the Sub Main or a function. (When the Add Macro References? dialog box is displayed, click the No button.)
This section needs an additional step due to the nature of the graph created. The existing macro was recorded for a graph with 2 plots. The line “Set oGraphObj = oGraphObj.Content.Panes(1).Graph” specifies to customize one of the plots (as we did in our newly recorded macro). So, this line, designating one plot within the graph, is not needed and should be deleted.
The macro needs one more modification to the section of code that creates the second plot. The second plot is made and edited before being combined with the first to make the final graph. Because of the different way this particular graph is made, specifically that it is not made visible before making the edits, editing it must be handled differently. The graphs are created in an If – Else statement. Around line 142, we see the Else where the second graph is created.
The code from the sub routine will be used directly to modify the histogram bars. The existing macro has already specified a graph layout parameter, “GL”. The newly recorded macro does this as well, but calls its graph layout parameter “oGL”. Copy the lines below “Set oGL = oGraphObj.Content” and above “End Sub” from the newly recorded macro and paste them below the line “Set GL = g.Content”. Change “oGL” in the pasted text to “GL” (i.e., delete the “o”).
Now, running the macro creates the desired graph with custom histogram bars. The graph customization commands from the newly recorded macro were properly merged with the existing graph macro to successfully create the desired final graph.
STATISTICA is always expanding to offer more analyses and options, and these new features can be used to greatly enhance your analytic processes. This example of incorporating newly recorded macro commands into existing macros can help you to enhance your own macros. The end result will be specialized output specific to your needs.