DsExcel .NET allows you to insert data labels in a chart to ensure the information depicted in it can be easily interpreted and visualized. You can add data labels in a chart using the properties and methods of the IPoint interface and the ISeries interface.
Refer to the following example code to set data labels in a chart and customize the data label text:
C# |
Copy Code |
---|---|
worksheet.Range["A1:D6"].Value = new object[,] { {null, "S1", "S2", "S3"}, {"Item1", 10, 25, 25}, {"Item2", -51, -36, 27}, {"Item3", 52, -85, -30}, {"Item4", 22, 65, 65}, {"Item5", 23, 69, 69} }; //Set Series' all data labels and specific data label's format. IShape shape1 = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 200, 50, 300, 300); shape1.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true); ISeries series1 = shape1.Chart.SeriesCollection[0]; series1.HasDataLabels = true; //set series1's all data label's format. series1.DataLabels.Format.Fill.Color.RGB = Color.Green; series1.DataLabels.Format.Line.Color.RGB = Color.Red; series1.DataLabels.Format.Line.Weight = 3; //set series1's specific data label's format. series1.DataLabels[2].Format.Fill.Color.RGB = Color.Yellow; series1.Points[2].DataLabel.Format.Line.Color.RGB = Color.Blue; series1.Points[2].DataLabel.Format.Line.Weight = 5; //Customize data label's text. IShape shape2 = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 550, 50, 300, 300); shape2.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true); ISeries series2 = shape2.Chart.SeriesCollection[0]; series2.HasDataLabels = true; //customize data lables' text. series2.DataLabels.ShowCategoryName = true; series2.DataLabels.ShowSeriesName = true; series2.DataLabels.ShowLegendKey = true; |
You can also set the direction of the data labels to horizontal, vertical, rotated (to 90 or 270 degree), and stacked (with text reading left-to-right or right to left). The Direction property in IDataLabels, IDataLabel, IDataLabels.ITextFrame, and IDataLabel.ITextFrame interfaces allows you to set the direction of the data labels using TextDirection enumeration.
Refer to the following example code to set the direction of the data labels to stacked and vertical:
C# |
Copy Code |
---|---|
// Create chart. shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true); // Add data labels to series 1. ISeries series1 = shape.Chart.SeriesCollection[0]; series1.HasDataLabels = true; // Set direction of all data lables to stacked. series1.DataLabels.Direction = TextDirection.Stacked; // Set direction of first data label to vertical. series1.Points[0].DataLabel.TextFrame.Direction = TextDirection.Vertical; // Set direction of second data label to vertical. series1.Points[1].DataLabel.Direction = TextDirection.Vertical; |
DsExcel also allows you to configure the text angle for data labels by using the Orientation property of IDataLabel interface.
Refer to the following example code to set text angle for data label:
C# |
Copy Code |
---|---|
//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); IWorksheet worksheet = workbook.Worksheets[0]; //add chart IShape shape = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 250, 20, 360, 230); worksheet.Range["A1:B5"].Value = new object[,] { {null, "S1"}, {"Item1", -20}, {"Item2", 30}, {"Item3", 50 }, {"Item3", 40 } }; shape.Chart.SeriesCollection.Add(worksheet.Range["A1:B5"], RowCol.Columns, true, true); ISeries series1 = shape.Chart.SeriesCollection[0]; series1.HasDataLabels = true; //set series1's all data labels' angle series1.DataLabels.Orientation = 45; //set series1's specific data label's angle series1.DataLabels[2].Orientation = -45; //save to an excel file workbook.Save("configdatalabelangle.xlsx"); |
The direction and orientation of the data labels can also be exported or imported into a JSON file.