Documents for Excel .NET Edition | GCDocuments
Features / Chart / Customize Chart Objects / Data Label
In This Topic
    Data Label
    In This Topic

    GcExcel .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 configure the text angle for data labels by using the Orientation property of IDataLabel interface. The text angle can also be exported or imported to JSON.

    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");