Datalabels position

Hi All,

I need your help.
I am trying to add annotation to my chart.
I have no problem with that but I would like to place my annotation where I want.
I have seen that we can set a position for datalabels
I tested DataLabels.Position | Aspose.Cells for .NET API Reference
but it does not do what I want. It is too close of the data point.
I have the annotation above the chart but not like I want. I would like a line from the annotation to the datapoint.
What I have:
image.png (6.3 KB)

What I want:
image.png (6.3 KB)

So the best would be, to add offset to the X position and Y position.
But there are equal to 0… (from the top left) : dataLabels.Y

So I don’t have the current position of the datalabel or even datapoints.

I had a look to this topic:

But I don’t have ActualX for datalabels. I have it for datapoints but there are null.

So how to set the datalabels at the position wished.

I can also use shapes/Textbox if it may help but I am not sure.

Thanks,

Cf this example of code below:

var pathWorkbook = @"pathinput";
            var pathWorkbookbis = @"pathoutput";
            var wbtest = new Workbook(pathWorkbook);
            Worksheet sheettest = wbtest.Worksheets[0];
            Chart charttest = sheettest.Charts[0];

            var point = charttest.NSeries[0].Points[10];

            var dataLabels = point.DataLabels;

            dataLabels.Text = "test annotation";
            dataLabels.Border.IsVisible = true; // Enable border
            dataLabels.Border.Color = Color.Black; // Set border color
            dataLabels.Border.Weight = 0; // Set border weight (thickness)

            // Optionally, configure what to show
            dataLabels.ShowValue = false;
            dataLabels.ShowCategoryName = false;

            // Customize the appearance
            dataLabels.Font.Size = 10;
            dataLabels.Font.Color = Color.Red;

            // Set data label to be positioned above the data point
            dataLabels.Position = LabelPositionType.Above;

            // Add offset
            // dataLabels.Y = dataLabels.Y + 500;

            // Aspose.Cells.Drawing.TextBox textbox0  = charttest.Shapes.AddTextBoxInChart(0, 0, 350, 900);
            // textbox0 .Text = "A Label In Chart";

            wbtest.Save(pathWorkbookbis);

TestAsposeAnnotation.7z (11.6 KB)

@hormancey

To position data labels on your chart exactly where you want them, you can use a combination of data label properties and shapes. Unfortunately, the Aspose.Cells library does not provide direct methods to set an offset for data labels from their data points. However, you can achieve your desired effect by using shapes or text boxes to create annotations that connect to the data points.

Here’s a suggested approach:

  1. Add Data Labels: First, add your data labels to the chart as you have done in your code.
  2. Create Shapes/Text Boxes: Use shapes or text boxes to create annotations. You can position these shapes at your desired coordinates.
  3. Draw Lines: If you want to visually connect the annotation to the data point, you can draw lines from the shape/text box to the data point.

Here’s an example of how you can implement this:

var pathWorkbook = @"pathinput";
var pathWorkbookbis = @"pathoutput";
var wbtest = new Workbook(pathWorkbook);
Worksheet sheettest = wbtest.Worksheets[0];
Chart charttest = sheettest.Charts[0];

var point = charttest.NSeries[0].Points[10];
var dataLabels = point.DataLabels;

dataLabels.Text = "test annotation";
dataLabels.Border.IsVisible = true; 
dataLabels.Border.Color = Color.Black; 
dataLabels.Border.Weight = 0; 

dataLabels.ShowValue = false;
dataLabels.ShowCategoryName = false;

dataLabels.Font.Size = 10;
dataLabels.Font.Color = Color.Red;
dataLabels.Position = LabelPositionType.Above;

// Create a text box for annotation
Aspose.Cells.Drawing.TextBox textBox = charttest.Shapes.AddTextBoxInChart(0, 0, 350, 900);
textBox.Text = "Your Annotation Here";

// Optionally, draw a line from the text box to the data point
// Note: You will need to calculate the coordinates based on your chart layout
Aspose.Cells.Drawing.LineShape line = charttest.Shapes.AddLine(0, 0, 0, 0);
line.StartX = textBox.X + textBox.Width / 2; // Adjust as needed
line.StartY = textBox.Y + textBox.Height; // Adjust as needed
line.EndX = point.X; // X coordinate of the data point
line.EndY = point.Y; // Y coordinate of the data point

wbtest.Save(pathWorkbookbis);

In this code, you create a text box for your annotation and a line that connects the text box to the data point. You will need to adjust the coordinates based on your specific chart layout to ensure that the line connects properly.

This method allows you to have more control over the positioning of your annotations and can help you achieve the desired visual effect. If you need further customization, consider adjusting the properties of the shapes and lines accordingly.

For more examples on adding custom labels to data points, you can refer to the following resources:

Feel free to ask if you have any more questions!

Point.X does not exist and point.XValue is null.
So how to get the position of my data point.

Sorry I found.
Need to use the chart.calculate…

 // Set data label to be positioned above the data point
            dataLabels.Position = LabelPositionType.Above;

            charttest.Calculate();

            // Add offset
            dataLabels.Y -= 500;

It is doing the job :wink:

@hormancey,

Good to know that Chart.Calculate works for your needs. Please feel free to contact us any time if you have further queries or comments.

Hi @amjad.sahi ,

I still need little help.
The result is good but I would like an arrow at the end of the line of the datalabel.
No problem to do a circle. For that, I used marker but I can’t do an “arrow” with marker:
point.Marker.MarkerStyle = ChartMarkerType.Circle;
point.Marker.ForegroundColor = Color.Red;
point.Marker.MarkerSize = 7;

So I tried different solutions:

But it does not seem possible or only for pie chart…
I am able to do it via Excel but not via aspose: No leaderline in point.DataLabels object

image.png (43.0 KB)
Could you help with this ?

So I tried another solution: using shape like a textbox and a line.
The problem is that if you move textbox, the line does not follow it automatically. (even if I group).
Moreover I did a lot of calculation to place the textbox and line at the good place.
Another problem: text does not fit automatically the textbox. It is working fine for datalabels.

So I think definitely datalabels are what we want.
But do you know any way to add an arrow at the end of the line ?

Thanks,

If you want to have a look if I have my code below:

var pathWorkbook = @"pathinput";
            var pathWorkbookbis = @"pathoutput";

            var wbtest = new Workbook(pathWorkbook);
            Worksheet sheettest = wbtest.Worksheets[0];
            Chart charttest = sheettest.Charts[0];
            var point = charttest.NSeries[0].Points[10];

            //----------------------------------------------------------//
            // Set data label to be positioned above the data point
            //----------------------------------------------------------//
            var dataLabels = point.DataLabels;
            dataLabels.Text = "test annotation";

            dataLabels.Border.IsVisible = true;
            dataLabels.Border.Color = Color.Black;
            dataLabels.Border.Weight = 0;

            dataLabels.Position = LabelPositionType.Above;
            charttest.Calculate();
            dataLabels.Y -= labelYOffset;

            // Draw a circle with markers:
            // Draw the datapoint in red.
            point.Marker.MarkerStyle = ChartMarkerType.Circle;
            point.Marker.Border.Color = Color.Red;
            point.Marker.BackgroundColor = Color.Red;
            point.Marker.MarkerSize = 7;

            wbtest.Save(pathWorkbookbis);

Thanks a lot,
Hadrien

@hormancey,

Using Aspose.Cells, you can try setting the HasLeaderLines Boolean property on specific series to True. Then position of the label should be far enough from the data point that MS Excel will show/render the leader line accordingly. See the sample lines of code.

charttest.NSeries[0].HasLeaderLines = true;
charttest.Calculate();