How to add chart labels to scatter points?

8 Dec 20233 minutes to read

The following code illustrates adding chart labels to the scatter points of the chart.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Get the chart from the charts collection
  IChart chart = worksheet.Charts[0];

  //Get the first series from the Series collection
  IChartSerie serieOne = chart.Series[0];

  //Set the Series name to the Data Labels through Data Points
  serieOne.DataPoints[0].DataLabels.IsSeriesName = true;

  //Set the Value to the Data Labels through Data Points
  serieOne.DataPoints[0].DataLabels.IsValue = true;

  FileStream stream = new FileStream("ChartLabels.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  workbook.SaveAs(stream);
  workbook.Close();
  excelEngine.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
  IWorksheet worksheet = workbook.Worksheets[0];

  //Get the chart from the charts collection
  IChart chart = worksheet.Charts[0];

  //Get the first series from the Series collection
  IChartSerie serieOne = chart.Series[0];

  //Set the Series name to the Data Labels through Data Points
  serieOne.DataPoints[0].DataLabels.IsSeriesName = true;

  //Set the Value to the Data Labels through Data Points
  serieOne.DataPoints[0].DataLabels.IsValue = true;

  workbook.SaveAs("ChartLabels.xlsx");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013
  Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
  Dim worksheet As IWorksheet = workbook.Worksheets(0)

  'Get the chart from the charts collection
  Dim chart As IChart = worksheet.Charts(0)

  'Get the first series from the Series collection
  Dim serieOne As IChartSerie = chart.Series(0)

  'Set the Series name to the Data Labels through Data Points
  serieOne.DataPoints(0).DataLabels.IsSeriesName = True

  'Set the Value to the Data Labels through Data Points
  serieOne.DataPoints(0).DataLabels.IsValue = True

  workbook.SaveAs("ChartLabels.xlsx")
End Using

See Also