How to create a mixed chart instance _c# tutorial in C # in Excel worksheets

Source: Internet
Author: User
Tags mixed

When charting, we might want to present two or more styles of charts in one chart to see different data sizes and trends more clearly and intuitively. In this article, I'll share in C # How to create different chart types in a chart, including how to add a second axis to the same chart.

Here's a simple Excel worksheet where you can see that series 3 data differs from Series 1 and 2 so that we can draw different chart types and different axes to represent changing data:

Code fragment:

Step 1: Create a new object for the workbook class and load the Excel file that you want to create the chart in.

Workbook workbook = new Workbook ();
Workbook. LoadFromFile (@ "E:\Visual studio\sample\book1.xlsx");

Step 2: Get the first worksheet of the workbook.

Worksheet Sheet=workbook. Worksheets[0];

Step 3: Add the chart to the worksheet and set the data A1 to D5 as the data source for the chart.

Chart Chart = sheet. Charts.add ();
Chart. DataRange = sheet. range["A1:d5"];
Chart. Seriesdatafromrange = false;

Step 4: Set the chart position.

Chart. Leftcolumn = 6;
Chart. Toprow = 1;
Chart. Rightcolumn =;
Chart. Bottomrow = 13;

Step 5: Series 1 and 2 use a histogram, Series 3 using a line chart.

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;

Step 6: Add an axis to the chart and draw the data for Series 3.

Chart. Secondarycategoryaxis.ismaxcross = true;
CS3. Useprimaryaxis = false;

Step 7: Save and run the file.

Workbook. SaveToFile ("result.xlsx");
System.Diagnostics.Process.Start ("result.xlsx");

This is the effect diagram of a mixed chart:

All code:

 workbook workbook = new Workbook (), workbook.
LoadFromFile (@ "E:\Visual studio\sample\book1.xlsx"); Worksheet sheet = workbook.
 
Worksheets[0]; Add a chart to the worksheet and set its data to the data source for the chart Chart Chart = sheet.
Charts.add (); Chart. DataRange = sheet.
range["A1:d5"]; Chart.
 
Seriesdatafromrange = false; Sets the chart position chart.
Leftcolumn = 6; Chart.
Toprow = 1; Chart.
Rightcolumn = 12; Chart.
 
Bottomrow = 13; According to the series use different chart types 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 one more axis chart.
Secondarycategoryaxis.ismaxcross = true; CS3.
 
Useprimaryaxis = false; Save and run the file workbook.
SaveToFile ("result.xlsx");
System.Diagnostics.Process.Start ("result.xlsx"); 

  In the above example I use free Spire.xls, the data on the worksheet above you can change according to your requirements, the steps are simpler, you can try to run. I've also written about how to add trend lines, error bars, and other articles in Excel tables, and friends who need them can also refer to them. Thanks for browsing!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.