Create Combination Chart in C#

A combination chart in Excel is a chart that consists of bar and line chart in a single chart. As the value may vary from one category to another category, you can choose which category to be displayed as bar, which category to be displayed as line so that you can compare the values in different categories easily.

In the following section, I made a simply example to present how to create a combination chart via .NET Excel component in C#.

  1. Download the free Excel component, install it on your computer.

  2. Create a project in your VS studio, add the Spire.Xls.dll to the .NET assemblies.

  3. Using following code snippet to insert sample into Excel, and create bar and line chart based on the input data.

using Spire.Xls;

using Spire.Xls.Charts;

using System.Drawing;

namespace CreateCombinationChart

{

    class Program

    {

        static void Main(string[] args)

        {

            Workbook workbook = new Workbook();          

            Worksheet sheet=workbook.Worksheets[0];

            //insert sample data with formatting into the sheet

            sheet.Range[“A1”].Value = “Month”;

            sheet.Range[“A2”].Value = “Jan”;

            sheet.Range[“A3”].Value = “Feb”;

            sheet.Range[“A4”].Value = “Mar”;

            sheet.Range[“B1”].Value = “Visits”;

            sheet.Range[“B2”].Value = “548”;

            sheet.Range[“B3”].Value = “256”;

            sheet.Range[“B4”].Value = “471”;

            sheet.Range[“C1”].Value = “Revenue”;

            sheet.Range[“C2”].Value = “530”;

            sheet.Range[“C3”].Value = “340”;

            sheet.Range[“C4”].Value = “673”;

            sheet.Range[“C2:C4”].NumberFormat = “$#,##0.00”;

            sheet.Range[“D1”].Value = “Conversion
Rate”
;

            sheet.Range[“D2”].NumberValue = 0.027;

            sheet.Range[“D3”].NumberValue = 0.034;

            sheet.Range[“D4”].NumberValue = 0.092;

            sheet.Range[“D2:D4”].NumberFormat = “0.00%”;

            //add a chart based the data from A1 to D4

            Chart chart = sheet.Charts.Add();

            chart.DataRange = sheet.Range[“A1:D4”];

            chart.ChartTitle = “Combination Chart”;

            chart.SeriesDataFromRange = false;

            //set position of chart

            chart.LeftColumn = 1;

            chart.TopRow = 7;

            chart.RightColumn = 9;

            chart.BottomRow = 25;

            //apply different chart type to different series

            var cs1 = (ChartSerie)chart.Series[0];

            cs1.SerieType = ExcelChartType.ColumnClustered;

            var cs2 = (ChartSerie)chart.Series[1];

            cs2.SerieType = ExcelChartType.ColumnClustered;

            var cs3 = (ChartSerie)chart.Series[2];

            cs3.SerieType = ExcelChartType.LineMarkers;

 

            //plot series 3 to Y-axis
chart.SecondaryCategoryAxis.IsMaxCross = true;

            cs3.UsePrimaryAxis = false;

            //save and launch the file

            workbook.SaveToFile(“result.xlsx”,ExcelVersion.Version2010);

            System.Diagnostics.Process.Start(“result.xlsx”);

        }

    }

}

Output

2015-10-08

Advertisements