/home/caleb/ASDV-Java/Semester 3/Assignments/MP1_FX_CalebFontenot/src/mp1_fx_calebfontenot/PieChart.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package mp1_fx_calebfontenot;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class PieChart extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
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};
String[] textValues = { "Quiz", "Project", "Final", "Midterm"};
float[][] textPos = { {165, 50}, {70, 120}, {150, 300}, {250, 204} };
int j = 1;
float startAngle = 0.0f;
final float PIE_SIZE = 200.0f;
for (int i = 0; i < 4; i++) {
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);
pieSlice.setLength(toDegrees(pieValues[j]));
pieSlice.setType(ArcType.ROUND);
String descriptionString = textValues[i] + "--" + pieValues[j] + "%";
Text descText = new Text(textPos[i][0], textPos[i][1], descriptionString);
Group pieSliceStack = new Group();
pieSliceStack.getChildren().addAll(pieSlice, descText);
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);
for (int i = 3; i >= 0; i--) {
pane.getChildren().add(layoutSlice.get(i));
}
Scene scene = new Scene(pane);
primaryStage.setTitle("Pie Chart");
primaryStage.setScene(scene);
primaryStage.show();
}
oldValue
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);
}
}