VBA TechniquesSo far, we've concentrated on the techniques we can use to get the most out of Excel's charting engine through the user interface. In this section, we examine how we can use VBA to manipulate charts. Converting Between Chart Coordinate SystemsWhen using VBA to work with charts, there are (at least) four different coordinate systems that we often need to convert between:The chart series data displayed inside the plot area is in the axis coordinates if it's an XY Scatter chart.The mouse pointer coordinates given in the MouseMove etc. events are measured in pixels, with the origin in the top-left corner of the ChartObject window.The coordinates of any drawing objects added to the chart are in points, with the origin being the top left of the chart area, slightly inside the ChartObject window.The coordinates used by the GET.CHART.ITEM XLM function to locate the vertices of chart objects are in points, but with the origin in the bottom-left corner of the chart area. See the Locating Chart Items section later for an example of its use. Furthermore, if the chart is embedded on a worksheet, the worksheet zoom factor affects the mouse pointer coordinates, but not the data nor location of any drawing objects on the chart.Chapter 9 Understanding and Using Windows API Calls: Listing 15-2. Converting from Mouse Coordinates to Data and Drawing Object Coordinates
Locating Chart ItemsSometimes, however hard we try, the only way to get a chart looking exactly how we want it is to add drawing objects to it, such as rectangles, lines, arrows and so on. As soon as we do that, we hit the problem of trying to identify where in the drawing object coordinate space an item on the chart is located, such as the top middle of a specific column in a column chart.That level of positional information cannot be obtained through the Excel object model, but can be obtained by calling on the long-disused XLM function GET.CHART.ITEM. This function has the following parameters: Where:x_y_index is 1 to return the x position and 2 to return the y position.point_index depends on the item we're looking at, but is a number from 1 to 8 to identify a specific vertex within the item. For example, 2 is the upper middle of any rectangular item, such as a column in a column chart.item_text identifies the item we're interested in, such as "Plot" for the plot area, or "S2P4" for the fourth data point in the second series in the chart. The full list of available parameters can be found in the XLM Macros help file available for download from the Microsoft Web site at [ http://support.microsoft.com/?kbid=128175 ]. The only caveat with using GET.CHART.ITEM is that the chart must be active for it to work. The code in Listing 15-3 moves an arrow on a chart to be from the top-left corner of the inside of the plot area (using normal VBA positioning) to the top middle of the third column of a column chart, resulting in the chart shown in Figure 15-16. Listing 15-3. Using GET.CHART.ITEM to Locate a Chart Item's Vertices
Figure 15-16. Moving an Arrow to Point to the Top Middle of a Column![]() Calculating Reasonable Axis ScalesOften when we're controlling charts through VBA, we need to set our own values for the axis scales. The code in Listing 15-4 calculates tidy Minimum, Maximum and MajorUnit values. It is a different algorithm than the one Excel uses to determine chart axis scales, but is one that we have found to give pleasant-looking results. Listing 15-4. Function to Calculate Reasonable Chart Axes Scales
|