Hi,
I want to show the leader lines of the pie chart. The problem arises when I have small slices and the labels overlap. How can I handle this situation?
resources.zip (30.2 KB)
Hi,
I want to show the leader lines of the pie chart. The problem arises when I have small slices and the labels overlap. How can I handle this situation?
resources.zip (30.2 KB)
Your query is related to Aspose.Cells. We are moving this thread to Aspose.Cells forum where you will be guided appropriately.
Please try the following code for your reference:
//Calculate chart
chart.calculate();
//You need to move DataLabels a little leftward or rightward depending on their position
//to show leader lines
int DELTA = 0;
float minAngle = 10;
double totalValue=0;
for (int i = 0; i < chart.getNSeries().get(0).getPoints().getCount(); i++)
{
totalValue += Double.parseDouble(String.valueOf(chart.getNSeries().get(0).getPoints().get(i).getYValue()));
}
for (int i = chart.getNSeries().get(0).getPoints().getCount()-1; i >=0 ; i--)
{
ChartPoint point = chart.getNSeries().get(0).getPoints().get(i);
int X = point.getDataLabels().getX();
int Y =point.getDataLabels().getY();
double val = Double.parseDouble(String.valueOf(point.getYValue()));
double angle = val / totalValue * 360;
int delta2 = DELTA;
if (angle < minAngle)
{
int k= i + 1 ;
while (k < chart.getNSeries().get(0).getPoints().getCount())
{
val += Double.parseDouble(String.valueOf(chart.getNSeries().get(0).getPoints().get(k ).getYValue()));
if(val / totalValue * 360 >= minAngle )
{
break;
}
delta2 += 50;
k++;
}
// More offset is required for successive angles that are too small
DELTA += 50 + delta2;
}
else
{
DELTA = 0;
if(angle > 2*minAngle)
delta2 = 0;
}
if(delta2 > 0)
{
X -= delta2;
point.getDataLabels().setX(X);
}
else if(Y < 150)
{
// set label further away from chart to make the leader line show
point.getDataLabels().setY(Y - 10);
}
}
Thanks for the shared code. I have another problem. When I save the graph image, the text of the labels wraps. Because? Everything is fine in the Excel
resources.zip (88.6 KB)
After an initial test, I am able to reproduce the issue as you mentioned using the updated code segment. I found, in the output graph image, the text of the labels wrap. It works fine in the output Excel file format though. I have logged a ticket with an id “CELLSJAVA-44130” for your issue. We will look into it soon.
Once we have an update on it, we will let you know.
The issues you have found earlier (filed as CELLSJAVA-44130) have been fixed in this update. This message was posted using Bugs notification tool by johnson.shi