Home     Products      Support      Corporate     Sign In 

Support Knowledge Base, Article 1040

Product
ExcelWriter
Version
5.x, 6.x
Title
Can I create or modify combination charts using ExcelWriter, for example Column/Line charts?
Problem
In Excel, it is possible to create combination charts by assigning a different chart type to the primary and secondary axes. Is this possible to do using ExcelWriter?
Solution

A combination chart uses two chart types to emphasize different kinds of data or to make comparing two sets of data easier. As in Excel, it is possible to create charts containing a combination of chart types with ExcelWriter. The ExcelApplication object/class can be used programmatically to do so.

The ExcelApplication COM object in v5 and later supports programmatic generation and manipulation of Column/Line combination charts in ASP (and ASP.NET using the SAExcelApplicationDotNet wrapper class). The native .NET and Java ExcelApplication classes introduced in v6 have full support for all types of combination charts.

By assigning one or more series of data to the secondary axis, the chart type for those series can be changed to a type different that that used by the series on the primary axis. This allows two different chart types to be simultaneously displayed within the same chart.

The following code samples demonstrate creating a Column/Line combination chart using the COM ExcelApplication object available in v5 and later and the native .NET and Java ExcelApplication classes introduced in v6.

Creating a combination chart

Input file

Both examples below start with the following Excel file, which contains two columns of data:

Example 1: Create a Column/Line chart using the COM ExcelApplication object (or SAExcelApplicationDotNet wrapper class in ASP.NET)

Key steps:

  1. Add a Chart of type saxlsColumnLineChart using Charts.Add.
  2. Add a Series for the bar chart and a Series for the line chart using SeriesCollection.Add.
  3. Change the AxisGroup property of the line series to saxlsSecondaryAxis.
  4. Change the ChartType property of the line series to saxlsLineChart.

[COM/ASP] [C#] [VB.NET]  
'--- Instantiate ExcelApplication object
Set xla = Server.CreateObject("SoftArtisans.ExcelWriter")

'--- Open workbook which contains data
xla.Open Server.MapPath("in.xls"), False

'--- Get first worksheet
Set sheet1 = xla.Worksheets(1)

'--- Add combination chart
Set comboChart = sheet1.Charts.Add(saxlsColumnLineChart, 0, 7, 0, 19, 7)

'--- Set category axis data
comboChart.SeriesCollection.CategoryData = "A4:A6"

'--- Add series for Projected values as a column chart
Set barSeries = comboChart.SeriesCollection.Add("B4:B6", , 1)
barSeries.Name = "Projected"

'--- Add series for Actual values as a line chart
Set lineSeries = comboChart.SeriesCollection.Add("C4:C6", , 1)
lineSeries.Name = "Actual"
lineSeries.AxisGroup = saxlsSecondaryAxis
lineSeries.ChartType = saxlsLineChart

'--- Save workbook
xla.Save "out.xls", saOpenInExcel, saFileFormatDefault, Response

Example 2: Create a Column/Line chart using the native .NET or Java ExcelApplication class

Key steps:

  1. Create a Chart of type ChartType.Column.Clustered using Charts.CreateChart().
    (We are using ChartType.Column.Clustered in this example, but you can use any available chart type.)
  2. Create a Series for the bar chart and a Series for the line chart using SeriesCollection.CreateSeries().
  3. Change the AxisType property of the line series to AxisType.Secondary.
  4. Change the ChartType property of the line series to ChartType.Line.StandardLine.
    (Unlike when using the COM ExcelApplication object, this ChartType property can be set to any chart type available, allowing any combination of chart types on the primary and secondary axes.)

[C#] [VB.NET] [Java]  
//--- Instantiate ExcelApplication class
ExcelApplication xla = new ExcelApplication();

//--- Open workbook
Workbook wb = xla.Open(Server.MapPath("in.xls"));

//--- Set first worksheet
Worksheet sheet1 = wb.Worksheets[0];

//--- Create combination chart
Chart comboChart = sheet1.Charts.CreateChart(ChartType.Column.Clustered, 
     sheet1.CreateAnchor(7, 0, 0, 0));
            
//--- Set category axis data
comboChart.SeriesCollection.CategoryData = "A4:A6";

//--- Create series for Projected values as a column chart
Series barSeries = comboChart.SeriesCollection.CreateSeries("B4:B6");
barSeries.NameFormula = "B3";

//--- Create series for Actual values as a line chart
Series lineSeries = comboChart.SeriesCollection.CreateSeries("C4:C6");
lineSeries.NameFormula = "C3";
lineSeries.AxisType = AxisType.Secondary;
lineSeries.ChartType = ChartType.Line.StandardLine;

//--- Save workbook
xla.Save(wb, Response, "out2.xls", false);

Output file

Both examples result in output similar to the following:

Modifying an existing combination chart

Using the COM ExcelApplication object, you cannot access any existing charts on a worksheet. However, with the native .NET or Java ExcelApplication class, existing charts (including combination charts) can be accessed and modified. Simply use the Worksheet.Charts collection (.NET) or Worksheet.getCharts().getChart() method (Java) to access a chart, as you would any other.

For example:

[C#] [VB.NET] [Java]  
//--- Instantiate ExcelApplication class
ExcelApplication xla = new ExcelApplication();

//--- Open workbook
Workbook wb = xla.Open(Server.MapPath("in.xls"));

//--- Set first worksheet
Worksheet sheet1 = wb.Worksheets[0];

//--- Get existing combination chart
Chart comboChart = sheet1.Charts[0];
            
...

Related Links
OfficeWriter Home Page
Purchase OfficeWriter
OfficeWriter Enterprise Edition
Latest OfficeWriter News
OfficeWriter: Programmatic Runtime Control

Created : 8/29/2006 11:36:13 AM (last modified : 8/29/2006 11:36:13 AM)

Rate this article!

 
Comments



Copyright 2006 © SoftArtisans, Inc. All Rights Reserved.

Site Map     |     Privacy Policy     |     Contact Us