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)
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:
Add Data Labels: First, add your data labels to the chart as you have done in your code.
Create Shapes/Text Boxes: Use shapes or text boxes to create annotations. You can position these shapes at your desired coordinates.
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:
// Set data label to be positioned above the data point
dataLabels.Position = LabelPositionType.Above;
charttest.Calculate();
// Add offset
dataLabels.Y -= 500;
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 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);
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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.