Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package userinterface.StateNetworkAdminRole; import Business.Hospital.Patient; import Business.Hospital.PatientDirectory; import Business.RegCenter.Donor; import Business.RegCenter.DonorDirectory; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author ApoorvaLakhmani */ public class StateReportsJPanel extends javax.swing.JPanel { private JPanel userProcessContainer; private DonorDirectory donorDirectory; private PatientDirectory patientList; /** * Creates new form StateReportsJPanel */ StateReportsJPanel(JPanel userProcessContainer, DonorDirectory donorDirectory, PatientDirectory patientList) { initComponents(); this.userProcessContainer = userProcessContainer; this.donorDirectory = donorDirectory; this.patientList = patientList; final CategoryDataset datasetDonors = createDataSetForDonorReports(); final JFreeChart donorChart = createDonorsReportsChart(datasetDonors); final ChartPanel donorChartPanel = new ChartPanel(donorChart); donorReportsJPanel.setLayout(new BorderLayout()); donorReportsJPanel.add(donorChartPanel, BorderLayout.CENTER); donorReportsJPanel.validate(); final CategoryDataset datasetPatients = createDataSetForPatientsReports(); final JFreeChart patientChart = createPatientReportsChart(datasetPatients); final ChartPanel patientChartPanel = new ChartPanel(patientChart); patientReportJPanel.setLayout(new BorderLayout()); patientReportJPanel.add(patientChartPanel, BorderLayout.CENTER); patientReportJPanel.validate(); final CategoryDataset patientDonordataSet = createDataSetForpatientDonorReports(); final JFreeChart patientDonorChart = createpatientDonorReportsChart(patientDonordataSet); final ChartPanel patientDonorChartPanel = new ChartPanel(patientDonorChart); patientDonorPanel.setLayout(new BorderLayout()); patientDonorPanel.add(patientDonorChartPanel, BorderLayout.CENTER); patientDonorPanel.validate(); } /** * 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { patientReportJPanel = new javax.swing.JPanel(); donorReportsJPanel = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); patientDonorPanel = new javax.swing.JPanel(); setBackground(new java.awt.Color(204, 255, 255)); javax.swing.GroupLayout patientReportJPanelLayout = new javax.swing.GroupLayout(patientReportJPanel); patientReportJPanel.setLayout(patientReportJPanelLayout); patientReportJPanelLayout.setHorizontalGroup(patientReportJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 404, Short.MAX_VALUE)); patientReportJPanelLayout.setVerticalGroup(patientReportJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 303, Short.MAX_VALUE)); javax.swing.GroupLayout donorReportsJPanelLayout = new javax.swing.GroupLayout(donorReportsJPanel); donorReportsJPanel.setLayout(donorReportsJPanelLayout); donorReportsJPanelLayout.setHorizontalGroup(donorReportsJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 434, Short.MAX_VALUE)); donorReportsJPanelLayout.setVerticalGroup(donorReportsJPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 303, Short.MAX_VALUE)); jButton1.setText("<< Back"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout patientDonorPanelLayout = new javax.swing.GroupLayout(patientDonorPanel); patientDonorPanel.setLayout(patientDonorPanelLayout); patientDonorPanelLayout.setHorizontalGroup(patientDonorPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 490, Short.MAX_VALUE)); patientDonorPanelLayout.setVerticalGroup(patientDonorPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 309, Short.MAX_VALUE)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(34, 34, 34) .addComponent(patientReportJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE) .addComponent(donorReportsJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(43, 43, 43)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(69, 69, 69).addComponent(jButton1)) .addGroup(layout.createSequentialGroup().addGap(230, 230, 230).addComponent( patientDonorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(56, 56, 56).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(patientReportJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(donorReportsJPanel, 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(patientDonorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(1, 1, 1).addComponent(jButton1).addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed userProcessContainer.remove(this); CardLayout layout = (CardLayout) userProcessContainer.getLayout(); layout.previous(userProcessContainer); }//GEN-LAST:event_jButton1ActionPerformed private CategoryDataset createDataSetForDonorReports() { ArrayList<String> cityList = new ArrayList<>(); DefaultCategoryDataset barChartData = new DefaultCategoryDataset(); for (Donor donor : donorDirectory.getDonorList()) { if (!cityList.contains(donor.getDonorAddress()) && !cityList.contains(donor.getDonorAddress() + " ")) { cityList.add(donor.getDonorAddress()); } } for (String city : cityList) { Map<Integer, Integer> yearDonorMap = new HashMap<>(); for (Donor donor : donorDirectory.getDonorList()) { if (donor.getDonorAddress().trim().equalsIgnoreCase(city.trim())) { int counter = 1; Date regDate = donor.getDonorRegisterationDate(); Calendar cal = Calendar.getInstance(); cal.setTime(regDate); int year = cal.get(Calendar.YEAR); if (yearDonorMap.containsKey(year)) { counter = yearDonorMap.get(year); counter++; yearDonorMap.put(year, counter); } else { yearDonorMap.put(year, counter); } } } for (Map.Entry<Integer, Integer> entryset : yearDonorMap.entrySet()) { barChartData.addValue(entryset.getValue(), city, entryset.getKey()); } } return barChartData; } private JFreeChart createDonorsReportsChart(CategoryDataset dataset) { JFreeChart barChart = ChartFactory.createBarChart("No Of Registered Donors", "Year", "Registered Donors", dataset, PlotOrientation.VERTICAL, true, true, false); barChart.setBackgroundPaint(Color.white); // Set the background color of the chart barChart.getTitle().setPaint(Color.DARK_GRAY); barChart.setBorderVisible(true); // Adjust the color of the title CategoryPlot plot = barChart.getCategoryPlot(); plot.getRangeAxis().setLowerBound(0.0); // Get the Plot object for a bar graph plot.setBackgroundPaint(Color.white); plot.setRangeGridlinePaint(Color.blue); CategoryItemRenderer renderer = plot.getRenderer(); renderer.setSeriesPaint(0, Color.decode("#00008B")); //return chart; return barChart; } private CategoryDataset createDataSetForPatientsReports() { ArrayList<String> cityList = new ArrayList<>(); ArrayList<Integer> yearList = new ArrayList<>(); DefaultCategoryDataset barChartData = new DefaultCategoryDataset(); for (Patient patient : patientList.getPatientDirectory()) { if (!cityList.contains(patient.getPatientLocation())) { cityList.add(patient.getPatientLocation()); } } for (Patient patient : patientList.getPatientDirectory()) { Date reqDate = patient.getTransplantRequestDate(); Calendar cal = Calendar.getInstance(); cal.setTime(reqDate); int year = cal.get(Calendar.YEAR); if (!yearList.contains(year)) { yearList.add(year); } } for (String city : cityList) { Map<Integer, Double> yearPatientMap = new HashMap<>(); for (int reqYear : yearList) { double sum = 0; int count = 0; double avg = 0.0; for (Patient patient2 : patientList.getPatientDirectory()) { if (patient2.getPatientLocation().equals(city)) { //if patient ka add == city //if patient ka reg year = year Date compDate = patient2.getTransplantCompletionDate(); if (compDate != null) { Date reqDate = patient2.getTransplantRequestDate(); Calendar cal = Calendar.getInstance(); cal.setTime(reqDate); int year = cal.get(Calendar.YEAR); double diff = 0; if (year == reqYear) { // diff = (patient2.getTransplantCompletionDate().getTime() - patient2.getTransplantRequestDate().getTime()) / ((1000 * 60 * 60 * 24 * 30)); // sum = sum+diff; // count++; Calendar startCalendar = new GregorianCalendar(); startCalendar.setTime(reqDate); Calendar endCalendar = new GregorianCalendar(); endCalendar.setTime(patient2.getTransplantCompletionDate()); int diffyear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR); int monthDiff = diffyear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH); sum += monthDiff; count++; } } } } avg = sum / count; yearPatientMap.put(reqYear, avg); } //putting in data set for (Map.Entry<Integer, Double> entryset : yearPatientMap.entrySet()) { barChartData.addValue(entryset.getValue(), city, entryset.getKey()); } } return barChartData; } private JFreeChart createPatientReportsChart(CategoryDataset dataset) { JFreeChart barChart = ChartFactory.createBarChart("Average Waiting period of Patients", "Year", "Avg Waiting period(months)", dataset, PlotOrientation.VERTICAL, true, true, false); barChart.setBackgroundPaint(Color.white); // Set the background color of the chart barChart.getTitle().setPaint(Color.DARK_GRAY); barChart.setBorderVisible(true); // Adjust the color of the title CategoryPlot plot = barChart.getCategoryPlot(); plot.getRangeAxis().setLowerBound(0.0); // Get the Plot object for a bar graph plot.setBackgroundPaint(Color.white); plot.setRangeGridlinePaint(Color.blue); CategoryItemRenderer renderer = plot.getRenderer(); renderer.setSeriesPaint(0, Color.decode("#00008B")); //return chart; return barChart; } private CategoryDataset createDataSetForpatientDonorReports() { DefaultCategoryDataset barChartData = new DefaultCategoryDataset(); ArrayList<String> array = new ArrayList<>(); array.add("patient"); array.add("donor"); for (String item : array) { HashMap<Integer, Integer> map = new HashMap<>(); if (item.equals("patient")) { for (Patient patient : patientList.getPatientDirectory()) { { int counter = 1; Date regDate = patient.getTransplantRequestDate(); Calendar cal = Calendar.getInstance(); cal.setTime(regDate); int year = cal.get(Calendar.YEAR); if (map.containsKey(year)) { counter = map.get(year); counter++; map.put(year, counter); } else { map.put(year, counter); } } } for (Map.Entry<Integer, Integer> entryset : map.entrySet()) { barChartData.addValue(entryset.getValue(), item, entryset.getKey()); } } else { for (Donor donor : donorDirectory.getDonorList()) { { int counter = 1; Date regDate = donor.getDonorRegisterationDate(); Calendar cal = Calendar.getInstance(); cal.setTime(regDate); int year = cal.get(Calendar.YEAR); if (map.containsKey(year)) { counter = map.get(year); counter++; map.put(year, counter); } else { map.put(year, counter); } } } for (Map.Entry<Integer, Integer> entryset : map.entrySet()) { barChartData.addValue(entryset.getValue(), item, entryset.getKey()); } } } return barChartData; } private JFreeChart createpatientDonorReportsChart(CategoryDataset dataset) { JFreeChart barChart = ChartFactory.createBarChart("Patients:Donor in the state", "Year", "Number of Donors/Patients", dataset, PlotOrientation.VERTICAL, true, true, false); barChart.setBackgroundPaint(Color.white); // Set the background color of the chart barChart.getTitle().setPaint(Color.DARK_GRAY); barChart.setBorderVisible(true); // Adjust the color of the title CategoryPlot plot = barChart.getCategoryPlot(); plot.getRangeAxis().setLowerBound(0.0); // Get the Plot object for a bar graph plot.setBackgroundPaint(Color.white); plot.setRangeGridlinePaint(Color.blue); CategoryItemRenderer renderer = plot.getRenderer(); renderer.setSeriesPaint(0, Color.decode("#00008B")); //return chart; return barChart; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel donorReportsJPanel; private javax.swing.JButton jButton1; private javax.swing.JPanel patientDonorPanel; private javax.swing.JPanel patientReportJPanel; // End of variables declaration//GEN-END:variables }