Chart.java. The program shows data in both bar chart format and pie chart format.
public class Data {
private String name;
private double quantity;
public Data(String name, double quantity) {
this.name = name;
this.quantity = quantity;
}
public String getName() {
return name;
}
public double getQuantity() {
return quantity;
}
}
|
Data[] data) by a bar chart and pie chart.
There are 2 constructors
public Chart(Data[] data, Color[] color) : represents the data by a bar chart and pie chart. Use color[i] for
data[i]. If there are more data than the color, then use random color for the remaining data. public Chart(Data[] data): use random color for all the data.JFramemax length times (quantity of the data/total quantity)
quantity of the data/total quantity time 100%) and the name of the data360 times (quantity of the data/total quantity)
JFrame.
Test1data.length == color.length
import java.awt.*;
import javax.swing.*;
// same number of color and data
public class Test1 {
public static void main(String[] args) {
Data[] data = new Data[2];
data[0] = new Data("Own house", 491882);
data[1] = new Data("Rent", 783530);
Color[] color = {Color.blue, Color.red};
Chart chart = new Chart(data, color);
chart.setTitle("Los Angeles Housing");
chart.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
chart.setVisible(true);
}
}
|
Test2data.length < color.length
import java.awt.*;
import javax.swing.*;
// more color than data
public class Test2 {
public static void main(String[] args) {
Data[] data = new Data[4];
data[0] = new Data("Charles", 22222.5);
data[1] = new Data("Dianne", 32000);
data[2] = new Data("Eric", 10003.25);
data[3] = new Data("Florence", 5999.99);
Color[] color = {Color.red, Color.green, Color.yellow, Color.pink, Color.cyan};
Chart chart = new Chart(data, color);
chart.setTitle("Family Income");
chart.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
chart.setVisible(true);
}
}
|
Test3No color Use random color. Your result may be different from the output
// The United States Federal Budget for Fiscal Year 2004
// source : http://www.warresisters.org/piechart.htm
import java.awt.*;
import javax.swing.*;
// no color
public class Test3 {
public static void main(String[] args) {
// the unit of the money is in Billion
Data[] data = new Data[5];
data[0] = new Data("Current Military", 459);
data[1] = new Data("Past Military", 345);
data[2] = new Data("Human Resources", 593);
data[3] = new Data("General Government", 235);
data[4] = new Data("Physical Resources", 99);
Chart chart = new Chart(data);
chart.setTitle("The United States Federal Budget for Fiscal Year 2004");
chart.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
chart.setVisible(true);
}
}
|
Test4data.length > color.length Use random color for the remaining data Your result may be different from the output
// The United States Federal Budget for Fiscal Year 2004
// source : http://www.warresisters.org/piechart.htm
import java.awt.*;
import javax.swing.*;
// more data than color
public class Test4 {
public static void main(String[] args) {
// the unit of the money is in Billion
Data[] data = new Data[5];
data[0] = new Data("Current Military", 459);
data[1] = new Data("Past Military", 345);
data[2] = new Data("Human Resources", 593);
data[3] = new Data("General Government", 235);
data[4] = new Data("Physical Resources", 99);
Color[] color = {Color.orange, Color.magenta};
Chart chart = new Chart(data, color);
chart.setTitle("The United States Federal Budget for Fiscal Year 2004");
chart.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
chart.setVisible(true);
}
}
|
/** Name: Charles Li * Student ID : 000 000 000 * PIC ID: ccli * E-mail: ccli@math.ucla.edu * Discussion: 1A, John Baber * Assignment: hw3 * * I, Charles Li, pledge that this is my own independent work, which * conforms to the guidelines of academic honesty as described in the * course syllabus. * */