Displaying additional label in JavaFX Pie Chart

Cherple :

I have a pie chart displaying legends(text) and labels(values). I wish to display a pie chart that has values on the outside of the pie and the % on the inside.

Here is a pie chart example.

I have followed this post to change the outer label to display the value instead of the text, but I would like to have another internal label displaying the percentage.

Is there any way I can add those labels? If so, how do I make it? What equation should I use to align those labels nicely?

Cherple :

I found the solution, i pretty much just copied nearly the entire "layoutChartChildren()" method. The link to the source code here.

What you need to take note of is mainly just the following 2 lines, which tells you the coordinates to place the label.

double sliceCenterEdgeX = calcX(labelAngles[index], pieRadius, centerX);
double sliceCenterEdgeY = calcY(labelAngles[index], pieRadius, centerY);

pieRadius will be the distance from the center of the circle where the label will be placed. I'll include a short snippet of the extracted code which i copied, the other methods can be copied directly from the source code provided.

double[] labelAngles = new double[getData().size()];
double scale = (total != 0) ? 360 / total : 0;
double start = getStartAngle();

int index = 0;
for (Data d : getData()){
  double size = (isClockwise()) ? (-scale * Math.abs(item.getCurrentPieValue())) : (scale * Math.abs(item.getCurrentPieValue()));
  labelAngles[index] = normalizeAngle(start + (size / 2));
  double sliceX = calcX(labelAngles[index], pieRadius * 0.7, centerX);
  double sliceY = calcY(labelAngles[index], pieRadius * 0.7, centerY);
  //Note: to place it in perfect center:
  //sliceX = sliceX - (text.getBoundsInLocal().getWidth() / 2;
  //sliceY = sliceY - (text.getBoundsInLocal().getHeight() / 2;
  text.relocate(sliceX, sliceY);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154086&siteId=1