Add a line in chart via C#
Native and high performance MS Excel creation programmatically using server side .NET APIs.
How to Add a Line in Charts via C#
It is easy for the developers to add a line in chart within running different reporting applications for data processing in just a few lines of code.
- Create Workbook class instance.
- Add some data to worksheet cells with the Cell object’s PutValue method. This will be used as the data source for the chart.
- Add a Chart to the worksheet by calling the Charts collection’s Add method, encapsulated in the Worksheet object.
- Access the new Chart object from the Charts collection by passing its index, specify the chart’s data source by calling Chart.SetChartDataRange .
- Calculate the chart position by calling Calculate method.
- Add a Line Shape by calling Chart.Shapes.AddShapeInChartByScale method.
- Set the line format
System Requirements
Just make sure that system have Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Windows Azure, Mono or Xamarin Platforms as well as development environment like Microsoft Visual Studio.
- Install from command line as
nuget install Aspose.Cells
or via Package Manager Console of Visual Studio withInstall-Package Aspose.Cells
. - Alternatively, get the offline MSI installer or all DLLs in a ZIP file from downloads
Following source code shows how to add a line in chart to MS Excel XLSX file using C#.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
Workbook wkb = new Workbook( ); | |
Worksheet wks = wkb.Worksheets[0]; | |
// Adding sample values to cells | |
wks.Cells["A2"].PutValue("Category1"); | |
wks.Cells["A3"].PutValue("Category2"); | |
wks.Cells["A4"].PutValue("Category3"); | |
wks.Cells["B1"].PutValue("column1"); | |
wks.Cells["B2"].PutValue(524); | |
wks.Cells["B3"].PutValue(200); | |
wks.Cells["B4"].PutValue(350); | |
wks.Cells["C1"].PutValue("column2"); | |
wks.Cells["C2"].PutValue(400); | |
wks.Cells["C3"].PutValue(500); | |
wks.Cells["C4"].PutValue(600); | |
// Adding a chart to the worksheet | |
int chartIndex = wks.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 0, 20, 10); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = wks.Charts[chartIndex]; | |
// Setting chart data source as the range "A1:C4" | |
chart.SetChartDataRange("A1:C4", true); | |
chart.Calculate(); | |
double lineLeftScale = (double)chart.PlotArea.InnerX / 4000; | |
double lineRightScale = (double)(chart.PlotArea.InnerX + chart.PlotArea.InnerWidth) / 4000; | |
int unitCount = chart.ValueAxis.GetAxisTexts().Length; | |
double targetScale = 2; | |
double lineTopScale = (double)chart.PlotArea.InnerY / 4000 + targetScale / (unitCount - 1) * chart.PlotArea.InnerHeight / 4000; | |
LineShape shape = (LineShape)chart.Shapes.AddShapeInChartByScale(MsoDrawingType.Line, PlacementType.MoveAndSize, lineLeftScale, lineTopScale, lineRightScale, lineTopScale); | |
// Customize shape | |
shape.Line.DashStyle = MsoLineDashStyle.RoundDot; | |
shape.Line.SolidFill.Color = Color.Red; | |
shape.Line.Weight = 2.5; | |
wkb.Save("output.xlsx"); |