C# creates a hybrid chart in Excel

When we use data for analysis, we may not only need a single chart to assist our work, but in some cases, we need to present a mixed chart of two or more different types of charts. In this article, I will introduce how to create a mixed chart in Excel.

Tools used: Free Spire.XLS for .NET (Community Edition)

This article is reproduced from the blog http://www.cnblogs.com/Yesi/p/6088822.html

C#

//Instantiate the Workbook class object and load the Excel test document
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"E:\Visual Studio\Sample\Book1.xlsx");
// get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
 
//Add a chart to the worksheet and set the data in the range of cells [A1:D5] as the data source of the chart
Chart chart = sheet.Charts.Add();
chart.DataRange = sheet.Range["A1:D5"];
chart.SeriesDataFromRange = false;
 
//set the chart position
chart.LeftColumn = 6;
chart.TopRow = 1;
chart.RightColumn = 12;
chart.BottomRow = 13;
 
//Use a column chart for series 1 and 2, and a line chart for series 3
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;
 
//Add another axis to draw the data parameters of series 3
chart.SecondaryCategoryAxis.IsMaxCross = true;
cs3.UsePrimaryAxis = false;
 
// save and run the file
workbook.SaveToFile("result.xlsx");
System.Diagnostics.Process.Start("result.xlsx");

 

Source data chart:

 

Graph to generate graph:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326171239&siteId=291194637