/home/caleb/ASDV-Java/Semester 3/Assignments/MP1_FX_CalebFontenot/src/mp1_fx_calebfontenot/PieChart.java
/*
 * 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"};
        float[][] textPos = { {165, 50}, {70, 120}, {150, 300}, {250, 204} }; // I really didn't want to hard code the coords for the text, but JavaFX forced my hand
        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
            String descriptionString = textValues[i] + "--" + pieValues[j] + "%";
            Text descText = new Text(textPos[i][0], textPos[i][1], descriptionString);
            
            // Add Arcs and text together into a Group
            Group pieSliceStack = new Group();
            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);

        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();

    }

    /**
     * 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);
    }
}