Create Microsoft Excel Column Chart in C#.NET

Chart is a graphical representation of data that enables user to easily understand the large quantity of data and relationships between parts of data. There are many types of charts available in Microsoft Excel and almost all of them are supported by Aspose.Cells for .NET. As a matter of fact, Aspose.Cells can be used to create, edit and manipulate Excel spreadsheets almost in any platform without any need to install Microsoft Excel or without using any sort of Microsoft Office automation.

Article Description

The purpose of this article is to explain how developers can create Microsoft Excel Column Chart in C# or in any other .NET Framework supported language e.g. VB.NET etc.

Supported Platforms

Aspose.Cells API supports all .NET frameworks e.g. .NET 2.0, .NET 3.5, .NET 4.0, .NET 7.0, .NET Core, .NET Standard 2.0, Xamarin etc. It is also available in other platforms e.g. Java, C++, Android, JavaScript, PHP etc. Besides, Aspose.Cells is available in Cloud as REST or RESTful APIs.

Types of Column Charts

There are various types of Column charts, some of them are listed below.

  • ColumnColumn Stacked
  • Column 100% Stacked
  • Column 3D
  • Column 3D Clustered
  • Column 3D Stacked
  • Column 3D 100% Stacked

Sample Input Microsoft Excel Document

For demonstration, we will use the following sample input Microsoft Excel document that contains the chart data. Here, column A contains the category axis data and other columns B, C and D contain chart series data.

Sample Input Microsoft Excel Document containing Chart Data.

Sample Code

The following sample code creates Microsoft Excel Column Chart by performing these steps.

  • Load sample input Microsoft Excel document containing the chart data.
  • Create Column chart with specified dimensions.
  • Set the chart title and format it.
  • Add three vertical series, set their names and fill colors.
  • Format various chart items e.g. plot area, value axis, category axis, major tick marks etc.
  • Save the workbook in XLSX format. You can also save it in other formats e.g. XLS, XLSB, XLSM etc.


// Directory path of input and output files.
string dirPath = "D:/Download/";
// Load source Excel file containing the chart data.
Workbook wb = new Workbook(dirPath + "sampleCreateMicrosoftExcelColumnChart.xlsx");
// Access first worksheet.
Worksheet ws = wb.Worksheets[0];
// Specify dimensions of the chart.
int upperLeftRow = 7;
int upperLeftColumn = 4;
int lowerRightRow = 24;
int lowerRightColumn = 13;
// Create Column chart with specified dimensions.
int idx = ws.Charts.Add(ChartType.Column, upperLeftRow, upperLeftColumn, lowerRightRow, lowerRightColumn);
// Access the Column chart.
Chart ch = ws.Charts[idx];
// Set the outline of chart area.
ch.ChartArea.Border.Color = Color.Black;
ch.ChartArea.Border.Weight = WeightType.SingleLine;
// Set the chart title, make it non-bold and set its font size.
ch.Title.Text = "Classification of Languages";
ch.Title.Font.IsBold = false;
ch.Title.Font.Size = 15;
// Add three vertical series in chart covering the range B2:D5.
ch.NSeries.Add("B2:D5", true);
// Set the category data covering the range A2:A5.
ch.NSeries.CategoryData = "A2:A5";
// Set the names of the chart series taken from cells.
ch.NSeries[0].Name = "=B1";
ch.NSeries[1].Name = "=C1";
ch.NSeries[2].Name = "=D1";
// Set the 1st series fill color.
ch.NSeries[0].Area.ForegroundColor = Color.FromArgb(74, 127, 176);
ch.NSeries[0].Area.Formatting = FormattingType.Custom;
// Set the 2nd series fill color.
ch.NSeries[1].Area.ForegroundColor = Color.FromArgb(91, 155, 213);
ch.NSeries[1].Area.Formatting = FormattingType.Custom;
// Set the 3rd series fill color.
ch.NSeries[2].Area.ForegroundColor = Color.FromArgb(173, 198, 229);
ch.NSeries[2].Area.Formatting = FormattingType.Custom;
// Set plot area formatting as none and hide its border.
ch.PlotArea.Area.FillFormat.FillType = FillType.None;
ch.PlotArea.Border.IsVisible = false;
// Set value axis major tick mark as none and hide axis line.
// Also set the color of value axis major grid lines.
ch.ValueAxis.MajorTickMark = TickMarkType.None;
ch.ValueAxis.AxisLine.IsVisible = false;
ch.ValueAxis.MajorGridLines.Color = Color.FromArgb(217, 217, 217);
// Save the output Excel file in XLSX format.
wb.Save(dirPath + "outputCreateMicrosoftExcelColumnChart.xlsx", SaveFormat.Xlsx);

Output Microsoft Excel Column Chart by Aspose.Cells

The following snapshot shows the Output Microsoft Excel Column Chart generated by Aspose.Cells with the code given above. Similarly, you can create all sorts of Column charts with Aspose.Cells API easily.

Microsoft Excel Column Chart created by Aspose.Cells API.

 

Leave a comment

Design a site like this with WordPress.com
Get started
search previous next tag category expand menu location phone mail time cart zoom edit close