Java Programming Pie Chart Display

Not able to diplay pie percentages correctly

Page 1 of 1

0 Replies - 1534 Views - Last Post: 27 November 2007 - 12:53 PM Rate Topic: -----

#1 UtahStar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-October 07

Java Programming Pie Chart Display

Post icon  Posted 27 November 2007 - 12:53 PM

I am new to Java programming and I am confused as to how to correct my code so that the interest and principle display in my pie chart properly. When I click on the button to diplay the pie chart all I get is red pie piece and not the blue pie piece. If anyone can point me in the right direction I sure would appreciate it. I am not certain what to fix at this point. I have tried several changes but the pie chart still displays wrong or not at all.

Here is my code:
*/
import java.text.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;


/**
 *
 * @author  javachallenged
 */
public class PieChart extends javax.swing.JFrame {
	
	double Princ = 0.0;		//Loan amount requested
	double TotalInt = 0.0;	//Total of all interest payments made.
	
	/** Creates new form Week3 */
	public PieChart() {
		initComponents();
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">						  
	private void initComponents() {
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jComboBox1 = new javax.swing.JComboBox();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jTextField2 = new javax.swing.JTextField();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jTextField3 = new javax.swing.JTextField();
		jTextField4 = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jButton3 = new javax.swing.JButton();
		jPanel1 = new javax.swing.JPanel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		jLabel1.setFont(new java.awt.Font("Papyrus", 1, 18));
		jLabel1.setText("Pie Chart Calculator");
		jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(51, 102, 255)));

		jLabel2.setFont(new java.awt.Font("MS Reference Sans Serif", 1, 10));
		jLabel2.setText("Enter Loan Amount : $");

		jLabel3.setFont(new java.awt.Font("MS Reference Sans Serif", 1, 10));
		jLabel3.setText("Select Loan Option:");

		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "7 Years @ 5.35%", "15 Years @ 5.50%", "30 Years @ 5.75%", "Select Term and Rate" }));

		jButton1.setText("Calculate Loan");
		jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton1MouseClicked(evt);
			}
		});

		jButton2.setText("Exit");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		jTextField2.setBorder(null);

		jTextArea1.setRows(5);
		jTextArea1.setSelectedTextColor(new java.awt.Color(51, 255, 255));
		jScrollPane1.setViewportView(jTextArea1);

		jLabel4.setFont(new java.awt.Font("MS Reference Sans Serif", 1, 10));
		jLabel4.setText("Enter % Rate:");

		jLabel5.setFont(new java.awt.Font("MS Reference Sans Serif", 1, 10));
		jLabel5.setText("Enter Term (Years):");

		jButton3.setText("	   View Pie Chart		  ");
		jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jButton3MouseClicked(evt);
			}
		});

		jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jPanel1MouseClicked(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
			jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGap(0, 197, Short.MAX_VALUE)
		);
		jPanel1Layout.setVerticalGroup(
			jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGap(0, 199, Short.MAX_VALUE)
		);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGap(94, 94, 94)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 706, Short.MAX_VALUE)
					.addGroup(layout.createSequentialGroup()
						.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(19, 19, 19)
						.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
							.addGroup(layout.createSequentialGroup()
								.addComponent(jButton1)
								.addGap(16, 16, 16)
								.addComponent(jButton2))
							.addComponent(jLabel3))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
							.addGroup(layout.createSequentialGroup()
								.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
								.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(155, 155, 155))
							.addGroup(layout.createSequentialGroup()
								.addGap(28, 28, 28)
								.addComponent(jButton3)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jLabel5)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(115, 115, 115)))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
					.addGroup(layout.createSequentialGroup()
						.addComponent(jLabel2)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
				.addContainerGap())
			.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
				.addContainerGap(271, Short.MAX_VALUE)
				.addComponent(jLabel1)
				.addGap(191, 191, 191))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jLabel1)
				.addGap(18, 18, 18)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(jLabel2)
					.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(16, 16, 16)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
					.addComponent(jLabel4)
					.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(jButton1)
						.addComponent(jButton2)
						.addComponent(jButton3))
					.addGroup(layout.createSequentialGroup()
						.addGap(6, 6, 6)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
							.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
							.addComponent(jLabel5))))
				.addGap(17, 17, 17)
				.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
					.addGroup(layout.createSequentialGroup()
						.addGap(15, 15, 15)
						.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
				.addContainerGap())
		);
		pack();
	}// </editor-fold>						

	private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {									  
// TODO add your handling code here:
			
	   
			DefaultPieDataset data = new DefaultPieDataset();
			data.setValue("Interest",TotalInt);
			data.setValue("Principal",Princ);
			
	 
			JFreeChart chart = ChartFactory.createPieChart("Loan Chart",data,true,true,false);
	 
			ChartPanel p = new ChartPanel(chart);
			jPanel1.add(p);
			p.setSize(300,200);
			p.setVisible(true);
	}									 

	private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {									 
// TODO add your handling code here:
			
	}									

	private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {									  
	// TODO add your handling code here:
		
	DecimalFormat Currency = new DecimalFormat("$#,##0.00"); //Formats output to US Currency
		
	//Declared variables for list of pre-determined loan(s):
	double Principal = 0.0;					//Loan amount requested
	double [] Rates = {5.35, 5.50, 5.75, 0.0}; //Listed rates available or user rate can be entered 
	double [] Terms = {7.0, 15.0, 30.0, 0.0};  //Listed term in years of available term or user terms can be entered
	int [] Payments = {85, 181, 361, 0};	   //Listed amount of monthly payments or user monthly payments
	int count = 0;							 //Payment counter set to zero
	TotalInt = 0.0;							//Interest counter reset to zero	
	Princ = 0.0;							   //Principal counter reset to zero			
	   
	DecimalFormat dec = new DecimalFormat("$0.00"); // Converts to decimal format
	 
	   
		//Start of Array methods
		int i;
		int x;
			   
		Principal = Double.parseDouble(jTextField1.getText()); //Gets the information from the header
		
		i = jComboBox1.getSelectedIndex();	//Gets the information from the four options in the combo box.
		
		if (i ==3 ){  //If statement if fourth combobox is selected
			Rates[3]=Double.parseDouble(jTextField3.getText());
			Terms[3]=Double.parseDouble(jTextField4.getText());
			Payments[3]=(int)(Terms[3] * 12 + 1);
		}		
		
		x = Payments[i];  //Gets the payments from the Array
		
		double [][] loan = new double [x][5]; //Creates the new Array from the loan.java file
		
		Rates [i] = Rates [i] / 100; //Gets the rate(s) and divides by 100 to get percentage
		
		loan sum;   //This is where the loan file is declared
		sum = new loan (Principal, Terms[i], Rates[i]); //Gets the data from the loan.java file
		
		jTextField2.setText("Month\t"+"Payment\t"+"Interest\t"+"Principal\t"+"Balance"); //This is what displays in the header field
		
		jTextArea1.setText(""); //Gets the text from the loan.java file and diplays in the text box
		
		loan[0][1]= sum.payment(); //This array sums the payments
		loan[0][4]= Principal;	 //This array keeps track of the principal balances
		
		//Creates the for statement
		for (count = 1; count < x; count ++)
		{
			loan[count][0] =  count; //Payment number
			loan[count][1] = (loan[count-1][1]); //Interest amount
			TotalInt  += loan[count][1];
			Princ += loan[count][1];
			loan[count][2] = (loan[count-1][4] * (Rates[i]/12.0)); //Amount * Rate/100
			loan[count][3] = (loan[count][1])-loan[count][2];  //Payment minus Interest
			loan[count][4] = (loan[count-1][4] - (loan[count][1] - loan[count][2])); //Balance
									
			
			//Extracts the data from loan and displplays in currency format.
			jTextArea1.append(Math.round(loan[count][0])+"\t");
			jTextArea1.append(Currency.format(loan[count][1])); 
			jTextArea1.append("\t"+Currency.format(loan[count][2]));
			jTextArea1.append("\t"+Currency.format(loan[count][3]));
			jTextArea1.append("\t"+Currency.format(loan[count][4])+"\n");
					
						  
	}									 
	

	}									 

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {										 
// TODO add your handling code here:
		System.exit(0);
	}										
	
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Week3().setVisible(true);
			}
		});
	}
	
	// Variables declaration - do not modify					 
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	private javax.swing.JTextField jTextField3;
	private javax.swing.JTextField jTextField4;
	// End of variables declaration				   
	
}





Is This A Good Question/Topic? 0
  • +

Page 1 of 1