Okay, my assignment is to create a program that generates a PieChart with JavaFX's
javafx.scene.shape.Arc class, and overlay text on top of the slices that describes them. Problem is, when I try to use a StackPane to overlay the text, it breaks the PieChart. How can I overlay the text on top of the slices without destroying the layout of the slices? I'm trying to avoid hard coding the text positions if at all possible.
#JavaFX centering help
4 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @muted garnet! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package mp1_fx_calebfontenot;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
*
* @author caleb
*/
public class PieChart extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Create pane
Pane pane = new Pane();
ArrayList<Arc> pie = new ArrayList<Arc>();
ArrayList<Text> pieDesc = new ArrayList<Text>();
ArrayList<Group> layoutSlice = new ArrayList<Group>();
float[] pieValues = {20.0f, 10.0f, 20.0f, 40.0f, 30.0f}; // First value controls the initial angle
String[] textValues = { "Quiz", "Project", "Final", "Midterm"};
int j = 1;
float startAngle = 0.0f;
final float PIE_SIZE = 200.0f;
for (int i = 0; i < 4; i++) {
// Arc
startAngle = startAngle + toDegrees(pieValues[i]);
System.out.println("i:" + toDegrees(pieValues[i]) + " j: " + toDegrees(pieValues[j]));
Arc pieSlice = new Arc();
pieSlice.setCenterX(PIE_SIZE);
pieSlice.setCenterY(PIE_SIZE);
pieSlice.setRadiusX(PIE_SIZE);
pieSlice.setRadiusY(PIE_SIZE);
pieSlice.setStartAngle(startAngle); //toDegrees(pieValues[i])
pieSlice.setLength(toDegrees(pieValues[j]));
pieSlice.setType(ArcType.ROUND);
// Text
//System.out.println(pieSlice.getTranslateX() + ", " + pieSlice.getCenterY());
String descriptionString = textValues[i] + "--" + pieValues[j] + "%";
Text descText = new Text( descriptionString);
// Add Arcs and text together into a StackPane
Group pieSliceStack = new Group();
//pieSliceStack.setAlignment(Pos.CENTER);
pieSliceStack.getChildren().addAll(pieSlice, descText);
// Add Arc and text to respective ArrayLists
pie.add(pieSlice);
pieDesc.add(descText);
layoutSlice.add(pieSliceStack);
j++;
}
pie.get(0).setFill(Color.BLUE);
pie.get(1).setFill(Color.RED);
pie.get(2).setFill(Color.YELLOW);
pie.get(3).setFill(Color.GREEN);
//Object[] pieArr = pie.toArray();
for (int i = 3; i >= 0; i--) {
pane.getChildren().add(layoutSlice.get(i));
}
//pane.getChildren().add(masterStack);
// Text
Scene scene = new Scene(pane);
primaryStage.setTitle("Pie Chart");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* This function converts a value between 0 and 100 to a value between 0 and 360, while retaining the ratio between those two number values. So for example, 35 turns into 90.
*
* @param oldValue
* @return
*/
public static float toDegrees(float oldValue) {
final float oldMin = 0;
final float oldMax = 100;
final float newMin = 0;
final float newMax = 360;
float newValue = ((oldValue - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin;
return Math.round(newValue);
}
public static void main(String[] args) {
launch(args);
}
}