Microsoft KB Archive/286211

= How To Create a Combination Chart Using the Office XP Chart Component or the Office 2003 Chart Component =

Article ID: 286211

Article Last Modified on 2/20/2007

-

APPLIES TO


 * Microsoft Office Web Components
 * Microsoft Office Web Components

-



This article was previously published under Q286211



SUMMARY
This article provides sample code that illustrates how you can create a combination chart using the Microsoft Office XP Chart component or the Microsoft Office 2003 Chart component.



MORE INFORMATION
Using the Office XP Chart component or the Office 2003 Chart component, you can create a combination chart that contains a mixture of columns, lines, and areas. Both charts and series have a Type property. Setting the Type property for a chart changes the chart type for every series, whereas setting the Type property for a series changes only that series if the new series type is compatible with the rest of the chart. If the new series type is not compatible with a combination chart, all series in the chart reflect the new type.

The following sample demonstrates run-time creation of a combination chart with two series: one series has a column type and the other series has a line type. The first series has values in the range 0 to 8000, and the second series has values in the range 0.10 to 0.25. If the two series were plotted against a single value axis, the line would appear dwarfed in comparison to the column. Hence, the line series is plotted against its own value axis with scaling that is different from the axis for the column series.

Note The ability to have two value axes with separate scaling is a feature that is introduced with Office XP.

Sample Code


Sub Window_OnLoad

Dim oChart Dim oSeries1, oSeries2 Dim oAxis1, oAxis2 Dim oConst ChartSpace1.Clear Set oConst = ChartSpace1.Constants

'Create a new chart in the ChartSpace. Set oChart = ChartSpace1.Charts.Add 'Add a series of type Column. Set oSeries1 = oChart.SeriesCollection.Add oSeries1.Caption = &quot;Sales&quot; oSeries1.SetData oConst.chDimCategories, oConst.chDataLiteral, _ Array(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;) oSeries1.SetData oConst.chDimValues, oConst.chDataLiteral, _ Array(5000, 6025, 5575, 5900) oSeries1.Type = oConst.chChartTypeColumnClustered 'Add a second series of type Line. Set oSeries2 = oChart.SeriesCollection.Add oSeries2.Caption = &quot;Discount&quot; oSeries2.SetData oConst.chDimCategories, oConst.chDataLiteral, _ Array(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;) oSeries2.SetData oConst.chDimValues, oConst.chDataLiteral, _ Array(0.1, 0.2, 0.1, 0.15) oSeries2.Type = oConst.chChartTypeLine

' Change the Min/Max, Numberformat and Gridlines for the value-axis of the first series. Set oAxis1 = oChart.Axes(oConst.chAxisPositionLeft) oAxis1.Scaling.Maximum = 8000 oAxis1.Scaling.Minimum = 0 oAxis1.NumberFormat = &quot;$0&quot; oAxis1.HasMajorGridlines = False ' Ungroup the series so that they can have separate value-axis scaling. oSeries2.Ungroup True

' Add a new value-axis for the second series. Position the new axis on the right side 'of the chart and modify the gridlines and numberformat. Set oAxis2 = oChart.Axes.Add(oSeries2.Scalings(oConst.chDimValues)) oAxis2.Position = oConst.chAxisPositionRight oAxis2.HasMajorGridlines = False oAxis2.NumberFormat = &quot;0%&quot; 'Display the legend. oChart.HasLegend = True oChart.Legend.Position = oConst.chLegendPositionBottom 'Display the title for the chart. oChart.HasTitle = True oChart.Title.Caption = &quot;Sales & Discounts&quot;

End Sub

