Chart Series in Excel document

26 Aug 202424 minutes to read

In a chart, a series represents a set of related data points, often depicted using lines, bars, or markers to show data trends or comparisons. Using XlsIO, you can customize the series in the chart.

Add

The following code snippet illustrates how to add series in chart.

//Add series
IChartSerie Amount = chart.Series.Add("Amount");
Amount.Values = worksheet.Range["B2:B6"];
Amount.CategoryLabels = worksheet.Range["A2:A6"];
//Add series
IChartSerie Amount = chart.Series.Add("Amount");
Amount.Values = worksheet.Range["B2:B6"];
Amount.CategoryLabels = worksheet.Range["A2:A6"];
'Add series
IChartSerie Amount = chart.Series.Add("Amount")
Amount.Values = worksheet.Range["B2:B6"]
Amount.CategoryLabels = worksheet.Range["A2:A6"]

Format

Border

The following code snippet illustrates how to format the border of the series.

//Set the border
chart.Series[1].SerieFormat.LineProperties.LineColor = Color.Red;
chart.Series[1].SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot;
chart.Series[1].SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow;
//Set the border
chart.Series[1].SerieFormat.LineProperties.LineColor = Color.Red;
chart.Series[1].SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot;
chart.Series[1].SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow;
' Set the border
chart.Series(1).SerieFormat.LineProperties.LineColor = Color.Red
chart.Series(1).SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot
chart.Series(1).SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow

Color

The following code snippet illustrates how to format the color of the series.

//Set the color
chart.Series[1].SerieFormat.Fill.FillType = ExcelFillType.Gradient;
chart.Series[1].SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chart.Series[1].SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234);
chart.Series[1].SerieFormat.Fill.ForeColor = Color.Red;
//Set the color
chart.Series[1].SerieFormat.Fill.FillType = ExcelFillType.Gradient;
chart.Series[1].SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor;
chart.Series[1].SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234);
chart.Series[1].SerieFormat.Fill.ForeColor = Color.Red;
'Set the color
chart.Series(1).SerieFormat.Fill.FillType = ExcelFillType.Gradient
chart.Series(1).SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor
chart.Series(1).SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234)
chart.Series(1).SerieFormat.Fill.ForeColor = Color.Red

Transparency

The following code snippet illustrates how to apply transparency to the series.

//Set the transparency
chart.Series[1].SerieFormat.Fill.Transparency = 1.0;
//Set the transparency 
chart.Series[1].SerieFormat.Fill.Transparency = 1.0;
'Set the transparency 
chart.Series(1).SerieFormat.Fill.Transparency = 1.0

NOTE

Transparency is only applicable when FillType is set as SolidColor. Color-shaded fill is represented as a floating-point value ranging from 0.0 (Clear) to 1.0 (Opaque).

Series Type

The following code snippet illustrates how to set the series type.

//Set the series type
chart.Series[0].SerieType = ExcelChartType.Line_Markers;
chart.Series[1].SerieType = ExcelChartType.Bar_Clustered;
//Set the series type
chart.Series[0].SerieType = ExcelChartType.Line_Markers;
chart.Series[1].SerieType = ExcelChartType.Bar_Clustered;
'Set the series type
chart.Series(0).SerieType = ExcelChartType.Line_Markers
chart.Series(1).SerieType = ExcelChartType.Bar_Clustered

The complete code snippet illustrating the above options is shown below.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Create(1);
    IWorksheet worksheet = workbook.Worksheets[0];

    worksheet.Range["A1"].Text = "Items";
    worksheet.Range["A2"].Text = "Beverages";
    worksheet.Range["A3"].Text = "Condiments";
    worksheet.Range["A4"].Text = "Confections";
    worksheet.Range["A5"].Text = "Dairy Products";
    worksheet.Range["A6"].Text = "Grains/Cereals";

    worksheet.Range["B1"].Text = "Amount(in $)";
    worksheet.Range["B2"].Number = 2776;
    worksheet.Range["B3"].Number = 1077;
    worksheet.Range["B4"].Number = 2287;
    worksheet.Range["B5"].Number = 1368;
    worksheet.Range["B6"].Number = 3325;

    worksheet.Range["C1"].Text = "Count";
    worksheet.Range["C2"].Number = 925;
    worksheet.Range["C3"].Number = 378;
    worksheet.Range["C4"].Number = 880;
    worksheet.Range["C5"].Number = 581;
    worksheet.Range["C6"].Number = 189;

    IChartShape chart = worksheet.Charts.Add();

    //Set chart type
    chart.ChartType = ExcelChartType.Column_Clustered;

    //Set chart title
    chart.ChartTitle = "Product Sales";

    //Add first serie
    IChartSerie serie1 = chart.Series.Add("Amount");
    serie1.Values = worksheet.Range["B2:B6"];
    serie1.CategoryLabels = worksheet.Range["A2:A6"];

    //Add second serie
    IChartSerie serie2 = chart.Series.Add("Count");
    serie2.Values = worksheet.Range["C2:C6"];
    serie2.CategoryLabels = worksheet.Range["A2:A6"];

    //Set the series type
    chart.Series[0].SerieType = ExcelChartType.Line_Markers;
    chart.Series[1].SerieType = ExcelChartType.Bar_Clustered;

    //Set the color
    chart.Series[1].SerieFormat.Fill.FillType = ExcelFillType.Gradient;
    chart.Series[1].SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor;
    chart.Series[1].SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234);
    chart.Series[1].SerieFormat.Fill.ForeColor = Color.Red;

    //Set the border
    chart.Series[1].SerieFormat.LineProperties.LineColor = Color.Red;
    chart.Series[1].SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot;
    chart.Series[1].SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow;

    //Positioning chart in a worksheet
    chart.TopRow = 9;
    chart.LeftColumn = 1;
    chart.RightColumn = 10;
    chart.BottomRow = 25;

    //Saving the workbook as stream
    FileStream stream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
    workbook.SaveAs(stream);

    //Dispose streams
    stream.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
   IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Create(1);
    IWorksheet worksheet = workbook.Worksheets[0];

    worksheet.Range["A1"].Text = "Items";
    worksheet.Range["A2"].Text = "Beverages";
    worksheet.Range["A3"].Text = "Condiments";
    worksheet.Range["A4"].Text = "Confections";
    worksheet.Range["A5"].Text = "Dairy Products";
    worksheet.Range["A6"].Text = "Grains/Cereals";

    worksheet.Range["B1"].Text = "Amount(in $)";
    worksheet.Range["B2"].Number = 2776;
    worksheet.Range["B3"].Number = 1077;
    worksheet.Range["B4"].Number = 2287;
    worksheet.Range["B5"].Number = 1368;
    worksheet.Range["B6"].Number = 3325;

    worksheet.Range["C1"].Text = "Count";
    worksheet.Range["C2"].Number = 925;
    worksheet.Range["C3"].Number = 378;
    worksheet.Range["C4"].Number = 880;
    worksheet.Range["C5"].Number = 581;
    worksheet.Range["C6"].Number = 189;

    IChartShape chart = worksheet.Charts.Add();

    //Set chart type
    chart.ChartType = ExcelChartType.Column_Clustered;

    //Set chart title
    chart.ChartTitle = "Product Sales";

    //Add first serie
    IChartSerie serie1 = chart.Series.Add("Amount");
    serie1.Values = worksheet.Range["B2:B6"];
    serie1.CategoryLabels = worksheet.Range["A2:A6"];

    //Add second serie
    IChartSerie serie2 = chart.Series.Add("Count");
    serie2.Values = worksheet.Range["C2:C6"];
    serie2.CategoryLabels = worksheet.Range["A2:A6"];

    //Set the series type
    chart.Series[0].SerieType = ExcelChartType.Line_Markers;
    chart.Series[1].SerieType = ExcelChartType.Bar_Clustered;

    //Set the color
    chart.Series[1].SerieFormat.Fill.FillType = ExcelFillType.Gradient;
    chart.Series[1].SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor;
    chart.Series[1].SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234);
    chart.Series[1].SerieFormat.Fill.ForeColor = Color.Red;

    //Set the border
    chart.Series[1].SerieFormat.LineProperties.LineColor = Color.Red;
    chart.Series[1].SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot;
    chart.Series[1].SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow;

    //Positioning chart in a worksheet
    chart.TopRow = 9;
    chart.LeftColumn = 1;
    chart.RightColumn = 10;
    chart.BottomRow = 25;

    //Saving the workbook
    workbook.SaveAs("Output.xlsx");
}
Using excelEngine As New ExcelEngine()
    Dim application As IApplication = excelEngine.Excel
    application.DefaultVersion = ExcelVersion.Xlsx
    Dim workbook As IWorkbook = application.Workbooks.Create(1)
    Dim worksheet As IWorksheet = workbook.Worksheets(0)

    worksheet.Range("A1").Text = "Items"
    worksheet.Range("A2").Text = "Beverages"
    worksheet.Range("A3").Text = "Condiments"
    worksheet.Range("A4").Text = "Confections"
    worksheet.Range("A5").Text = "Dairy Products"
    worksheet.Range("A6").Text = "Grains/Cereals"

    worksheet.Range("B1").Text = "Amount(in $)"
    worksheet.Range("B2").Number = 2776
    worksheet.Range("B3").Number = 1077
    worksheet.Range("B4").Number = 2287
    worksheet.Range("B5").Number = 1368
    worksheet.Range("B6").Number = 3325

    worksheet.Range("C1").Text = "Count"
    worksheet.Range("C2").Number = 925
    worksheet.Range("C3").Number = 378
    worksheet.Range("C4").Number = 880
    worksheet.Range("C5").Number = 581
    worksheet.Range("C6").Number = 189

    Dim chart As IChartShape = worksheet.Charts.Add()

    'Set chart type
    chart.ChartType = ExcelChartType.Column_Clustered

    'Set chart title
    chart.ChartTitle = "Product Sales"

    'Add first series
    Dim serie1 As IChartSerie = chart.Series.Add("Amount")
    serie1.Values = worksheet.Range("B2:B6")
    serie1.CategoryLabels = worksheet.Range("A2:A6")

    'Add second series
    Dim serie2 As IChartSerie = chart.Series.Add("Count")
    serie2.Values = worksheet.Range("C2:C6")
    serie2.CategoryLabels = worksheet.Range("A2:A6")

    'Set the series type
    chart.Series(0).SerieType = ExcelChartType.Line_Markers
    chart.Series(1).SerieType = ExcelChartType.Bar_Clustered

    'Set the color
    chart.Series(1).SerieFormat.Fill.FillType = ExcelFillType.Gradient
    chart.Series(1).SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor
    chart.Series(1).SerieFormat.Fill.BackColor = Color.FromArgb(205, 217, 234)
    chart.Series(1).SerieFormat.Fill.ForeColor = Color.Red

    'Set the border
    chart.Series(1).SerieFormat.LineProperties.LineColor = Color.Red
    chart.Series(1).SerieFormat.LineProperties.LinePattern = ExcelChartLinePattern.Dot
    chart.Series(1).SerieFormat.LineProperties.LineWeight = ExcelChartLineWeight.Narrow

    'Positioning chart in a worksheet
    chart.TopRow = 9
    chart.LeftColumn = 1
    chart.RightColumn = 10
    chart.BottomRow = 25

    'Saving the workbook
    workbook.SaveAs("Output.xlsx")
End Using

A complete working example for the chart data series in C# is present on this GitHub page.

Series Settings

Add DataPoint as total

The following code snippet illustrates how to add the Data Point as total in chart.

//Data point settings as total in chart
chart.Series[0].DataPoints[3].SetAsTotal = true;
//Data point settings as total in chart
chart.Series[0].DataPoints[3].SetAsTotal = true;
'Data point settings as total in chart
chart.Series(0).DataPoints(3).SetAsTotal = True

Add Connector lines between data points

The following code snippet illustrates how to add the connector lines between data points.

//Showing the connector lines between data points
chart.Series[0].SerieFormat.ShowConnectorLines = true;
//Showing the connector lines between data points
chart.Series[0].SerieFormat.ShowConnectorLines = true;
'Showing the connector lines between data points
chart.Series(0).SerieFormat.ShowConnectorLines = True

Add space between bars

Spaces between chart bars are of two types.

  1. Series Overlap : Space between bars of different data series of single category.
  2. Gap Width : Space between different categories.

XlsIO allows you to adjust the space between chart bars using Overlap and GapWidth properties of IChartFormat interface.

The following code snippet illustrates how to add space between bars.

//Adding space between bars of different series of single category.
chart.Series[0].SerieFormat.CommonSerieOptions.Overlap = 60;

//Adding space between bars of different categories.
chart.Series[0].SerieFormat.CommonSerieOptions.GapWidth = 80;
//Adding space between bars of different series of single category.
chart.Series[0].SerieFormat.CommonSerieOptions.Overlap = 60;

//Adding space between bars of different categories.
chart.Series[0].SerieFormat.CommonSerieOptions.GapWidth = 80;
'Adding space between bars of different series of a single category.
chart.Series(0).SerieFormat.CommonSerieOptions.Overlap = 60

'Adding space between bars of different categories.
chart.Series(0).SerieFormat.CommonSerieOptions.GapWidth = 80

A complete working example for adding space between chart bars in C# is present on this GitHub page.

Add High-Low Lines

The following code snippet illustrates how to add high-low lines.

//Set HasHighLowLines property to true
chart.Series[0].SerieFormat.CommonSerieOptions.HasHighLowLines = true;

//Apply formats to HighLowLines
chart.Series[0].SerieFormat.CommonSerieOptions.HighLowLines.LineColor = Syncfusion.Drawing.Color.Red;
chart.Series[0].SerieFormat.CommonSerieOptions.HighLowLines.LinePattern = ExcelChartLinePattern.Dot;
chart.Series[0].SerieFormat.CommonSerieOptions.HighLowLines.LineWeight = ExcelChartLineWeight.Narrow;
//Set HasHighLowLines property to true
chart.Series[0].SerieFormat.CommonSerieOptions.HasHighLowLines = true;

//Apply formats to HighLowLine
chart.Series[0].SerieFormat.CommonSerieOptions.HighLowLines.LineColor = Color.Red;
chart.Series[0].SerieFormat.CommonSerieOptions.HighLowLines.LinePattern = ExcelChartLinePattern.Dot;
chart.Series[0].SerieFormat.CommonSerieOptions.HighLowLines.LineWeight = ExcelChartLineWeight.Narrow;
'Set HasHighLowLines property to true
chart.Series(0).SerieFormat.CommonSerieOptions.HasHighLowLines = True

'Apply formats to HighLowLines
chart.Series(0).SerieFormat.CommonSerieOptions.HighLowLines.LineColor = Color.Red
chart.Series(0).SerieFormat.CommonSerieOptions.HighLowLines.LinePattern = ExcelChartLinePattern.Dot
chart.Series(0).SerieFormat.CommonSerieOptions.HighLowLines.LineWeight = ExcelChartLineWeight.Narrow

A complete working example to show high low lines of chart in C# is present on this GitHub page.

Add Drop Lines

The following code snippet illustrates how to add drop lines.

//Set the HasDropLines property to true.
chart.Series[0].SerieFormat.CommonSerieOptions.HasDropLines = true;

//Apply formats to DropLines.
chart.Series[0].SerieFormat.CommonSerieOptions.DropLines.LineColor = Syncfusion.Drawing.Color.Red;
chart.Series[0].SerieFormat.CommonSerieOptions.DropLines.LinePattern = ExcelChartLinePattern.Dot;
chart.Series[0].SerieFormat.CommonSerieOptions.DropLines.LineWeight = ExcelChartLineWeight.Narrow;
//Set the HasDropLines property to true.
chart.Series[0].SerieFormat.CommonSerieOptions.HasDropLines = true;

//Apply formats to DropLines.
chart.Series[0].SerieFormat.CommonSerieOptions.DropLines.LineColor = Color.Red;
chart.Series[0].SerieFormat.CommonSerieOptions.DropLines.LinePattern = ExcelChartLinePattern.Dot;
chart.Series[0].SerieFormat.CommonSerieOptions.DropLines.LineWeight = ExcelChartLineWeight.Narrow;
'Set HasDropLines property to true.
chart.Series(0).SerieFormat.CommonSerieOptions.HasDropLines = True

'Apply formats to DropLines.
chart.Series(0).SerieFormat.CommonSerieOptions.DropLines.LineColor = Color.Red
chart.Series(0).SerieFormat.CommonSerieOptions.DropLines.LinePattern = ExcelChartLinePattern.Dot;
chart.Series(0).SerieFormat.CommonSerieOptions.DropLines.LineWeight = ExcelChartLineWeight.Narrow

A complete working example to add drop lines of chart in C# is present on this GitHub page.

Add Series Lines

The following code snippet illustrates how to add series lines in chart.

//Set HasSeriesLines property to true
chart.Series[0].SerieFormat.CommonSerieOptions.HasSeriesLines = true;

//Apply formats to SeriesLines
chart.Series[0].SerieFormat.CommonSerieOptions.PieSeriesLine.LineColor = Syncfusion.Drawing.Color.Red;
chart.Series[0].SerieFormat.CommonSerieOptions.PieSeriesLine.LinePattern = ExcelChartLinePattern.Dot;
chart.Series[0].SerieFormat.CommonSerieOptions.PieSeriesLine.LineWeight = ExcelChartLineWeight.Narrow;
//Set HasSeriesLines property to true
chart.Series[0].SerieFormat.CommonSerieOptions.HasSeriesLines = true;

//Apply formats to SeriesLines
chart.Series[0].SerieFormat.CommonSerieOptions.PieSeriesLine.LineColor = Color.Red;
chart.Series[0].SerieFormat.CommonSerieOptions.PieSeriesLine.LinePattern = ExcelChartLinePattern.Dot;
chart.Series[0].SerieFormat.CommonSerieOptions.PieSeriesLine.LineWeight = ExcelChartLineWeight.Narrow;
'Set HasSeriesLines property to true
chart.Series(0).SerieFormat.CommonSerieOptions.HasSeriesLines = true

'Apply formats to SeriesLines
chart.Series(0).SerieFormat.CommonSerieOptions.PieSeriesLine.LineColor = Color.Red
chart.Series(0).SerieFormat.CommonSerieOptions.PieSeriesLine.LinePattern = ExcelChartLinePattern.Dot
chart.Series(0).SerieFormat.CommonSerieOptions.PieSeriesLine.LineWeight = ExcelChartLineWeight.Narrow

A complete working example to add series lines of chart in C# is present on this GitHub page.