Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this assignment is to use JavaFX to create a pie chart. The pie c

ID: 3708540 • Letter: T

Question

The purpose of this assignment is to use JavaFX to create a pie chart. The pie chart will track the percentage of students at a fictional college who are freshman, sophomore, junior, senior, or graduate students. Don't use the PieChart class from the javafx package. You must create your own original pie chart. A sample pie chart is displayed below.

Create a project named StudentAnalysis. Use a package name of edu.arizonastate.studentanalysis. (You can substitute your domain name for edu.arizonastate if you wish.) Make sure you create a regular Java project in Eclipse not a JavaFX project.

Create an application class that extends the javafx.application.Application class. This will be the class that creates the JavaFX application. Name this class StudentAnalysis.

Create the application stage as shown in the example above.

Create a "title" for the pie chart as shown in the example above. This should read "Percent of Students by Class".

The pie chart itself needs 5 "slices" for the percentage of students that are freshman, sophomore, junior, senior, and graduate students. The actual number of students in each category is hard coded as a constant. Create a separate constant for the number of freshman, sophomore, junior, senior, and graduate students. Make sure to use a constant for the values and also use the constant in all calculations. You can choose any numbers you wish for the values. Your application should work fine and present a different pie chart when I change the numbers in the constants. The chart above was created using the following numbers: freshman - 100, sophomore - 200, junior - 50, senior - 75, and graduate students - 125. Each slice must be a different color. You can use any colors you wish. Position the chart in the middle of the application window. You can do this by hard coding the x and y coordinates. You do not need to use binding properties. I will not resize your window.

The lower section of the application shows the legend for the pie chart. This indicates which color corresponds with which student class. This color must match the color of the matching slice in the pie chart. Also, display the percentage of students in each class as shown in the example above. Use the NumberFormat class and the getPercentInstance method to format the percentage.

You must create and use at least one Font object with a specific font name and point size.

3Student Class Analysis Percent of Students by Clas 18%-Freshman 36%-Sophmore 9%Junior 14%Senior 2396-Graduate

Explanation / Answer

//This code is implemented as per the given requirement.

package edu.arizonastate.studentanalysis;

import java.text.NumberFormat;

import java.util.Set;

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.geometry.Side;

import javafx.scene.Group;

import javafx.scene.Node;

import javafx.scene.Scene;

import javafx.scene.chart.PieChart;

import javafx.scene.control.Label;

import javafx.scene.paint.Color;

import javafx.scene.text.Font;

import javafx.stage.Stage;

public class StudentAnalysis extends Application {

//number of students in each category

private static int FRESHMAN=100;

private static int SOPHOMORE=200;

private static int JUNIOR=50;

private static int SENIOR=75;

private static int GRADUATE=125;

public static void main(String[] args) {

launch(args);

}

public void getPercentInstance(){

}

public void start(Stage stage) {

//find total number of students

int total = FRESHMAN+SOPHOMORE+JUNIOR+SENIOR+GRADUATE;

//calculation for percentage

double FRESHMANP = (double)FRESHMAN/total;

double SOPHOMOREP = (double)SOPHOMORE/total;

double JUNIORP = (double)JUNIOR/total;

double SENIORP = (double)SENIOR/total;

double GRADUATEP = (double)GRADUATE/total;

//NumberFormat.getPercentInstance() is used below

NumberFormat defaultFormat = NumberFormat.getPercentInstance();

defaultFormat.setMinimumFractionDigits(0);

String FRESHMANCAPTION = defaultFormat.format(FRESHMANP)+" - Freshman";

String SOPHOMORECAPTION = defaultFormat.format(SOPHOMOREP)+" - Sophomore";

String JUNIORCAPTION = defaultFormat.format(JUNIORP)+" - Junior";

String SENIORCAPTION = defaultFormat.format(SENIORP)+" - Senior";

String GRADUATECAPTION = defaultFormat.format(GRADUATEP)+" - Graduate";

//create the pie chart below

Scene scene = new Scene(new Group());

stage.setTitle("Student Class Analysis");

stage.setWidth(500);

stage.setHeight(500);

ObservableList<PieChart.Data> pieChartData =

FXCollections.observableArrayList(

new PieChart.Data(FRESHMANCAPTION, FRESHMAN),

new PieChart.Data(SOPHOMORECAPTION, SOPHOMORE),

new PieChart.Data(JUNIORCAPTION, JUNIOR),

new PieChart.Data(SENIORCAPTION, SENIOR),

new PieChart.Data(GRADUATECAPTION, GRADUATE));

final PieChart chart = new PieChart(pieChartData);

chart.setTitle("Percent of Students by Class");

chart.setLegendSide(Side.BOTTOM);

chart.setClockwise(false);

chart.setLabelsVisible(false);

//updating the pie chart color and legend text color.

Set<Node> items = chart.lookupAll("Label.chart-legend-item");

int i = 0;

// Any colors can be provided here. Make sure to match these with the pie chart color sequence while calling applyCustomColor()

Color[] colors = { Color.BLUE, Color.DEEPPINK, Color.PURPLE, Color.GREEN, Color.RED };

for (Node item : items) {

Label label = (Label) item;

//Font object is used below

label.setFont(new Font("Courier New", 10));

label.setTextFill(colors[i]);

label.setGraphic(null);

i++;

}

((Group) scene.getRoot()).getChildren().add(chart);

stage.setScene(scene);

stage.show();

applyCustomColor(

pieChartData,

"BLUE",

"DEEPPINK",

"PURPLE",

"GREEN",

"RED"

);

}

  

private void applyCustomColor(ObservableList<PieChart.Data> pieChartData, String... pieColors) {

int i = 0;

for (PieChart.Data data : pieChartData) {

data.getNode().setStyle("-fx-pie-color: " + pieColors[i % pieColors.length] + ";");

i++;

}

}

}