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 com; import com.csvreader.CsvReader; import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MultiHashtable; import java.awt.Color; import java.awt.Paint; import java.awt.SystemColor; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Hashtable; import java.util.logging.Level; import java.util.logging.Logger; 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.PiePlot3D; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.PieDataset; import org.jfree.util.Rotation; /** * * @author charmainerodrigues */ public class MainChart extends javax.swing.JFrame { private static Hashtable<String, Double> multiHashtable; /** * Creates new form Main */ public MainChart() { this.multiHashtable = new Hashtable<>(); initComponents(); } public MainChart(String applicationTitle, String chartTitle) throws IOException { super(applicationTitle); this.multiHashtable = new Hashtable<>(); // This will create the dataset // PieDataset dataset = createDataset(); // based on the dataset we create the chart final JFreeChart chart = ChartFactory.createBarChart("GPA Analysis By Country", // chart title "Country", // domain axis label "GPA", // range axis label createDataset(), // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? ); chart.setBackgroundPaint(Color.white); CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); plot.setBackgroundPaint(SystemColor.inactiveCaption);//change background color //set bar chart color //CategoryItemRenderer renderer = new CustomRenderer(); //plot.setRenderer(renderer); // we put the chart into a panel ChartPanel chartPanel = new ChartPanel(chart); // default size chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); // add it to our application setContentPane(chartPanel); } /** * 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() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(new java.awt.Color(102, 102, 102)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 725, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 552, Short.MAX_VALUE)); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents /** * @param args the command line arguments * @throws java.io.IOException */ public static void main(String args[]) throws IOException { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> //parseCSV(); MainChart demo = new MainChart("Comparison", "Which operating system are you using?"); demo.pack(); demo.setVisible(true); } private DefaultCategoryDataset createDataset() throws IOException { parseCSV(); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); Object[] countries = multiHashtable.keySet().toArray(); for (Object country : countries) { //System.out.println(country); if (country instanceof String) { //System.out.println(hashtable.get(country).toString() + (Comparable) country.toString()); dataset.addValue(multiHashtable.get(country), "Region", (Comparable) country); System.out.println("GPA: " + multiHashtable.get(country)); System.out.println("country: " + country); } } //System.out.println(dataset); return dataset; } private static boolean isDouble(String str) { try { Double.parseDouble(str); return true; } catch (NumberFormatException e) { System.out.println("data"); return false; } } private static boolean isInteger(String str) { try { Integer.parseInt(str); return true; } catch (NumberFormatException e) { System.out.println("int"); return false; } } public static void parseCSV() throws FileNotFoundException, IOException { CsvReader students = new CsvReader("CMU_Country_GPA_Sample.csv"); students.readHeaders(); while (students.readRecord()) { int id = Integer.parseInt(students.get(0)); String country = students.get(1); Double gpa = Double.parseDouble(students.get(2)); multiHashtable.put(country, gpa); // perform program logic here //System.out.println(id + ":" + country + ":" +gpa); } students.close(); } public static void readCsv() { try { System.out.println("Hi"); File file = new File("CMU_Country_GPA_Sample.csv"); System.out.println(file.getPath()); //String[] studentline = line.split(","); String line; //Reader csvFile = new InputStreamReader(Main.class.getResourceAsStream("CMU_Country_GPA_Sample.csv")); BufferedReader br = new BufferedReader(new FileReader("CMU_Country_GPA_Sample.csv")); ArrayList<Student> studentList = new ArrayList<>(); while ((line = br.readLine()) != null) { System.out.println(line); String[] studentline = line.split(","); for (String studentl : studentline) { System.out.println(studentl); } if (isDouble(studentline[2]) && isInteger(studentline[0])) { studentList.add(new Student(Integer.parseInt(studentline[0]), studentline[1], Double.parseDouble(studentline[2]))); //System.out.println(studentline[0] + studentline[1] + studentline[2]); } else { break; } } // BufferedReader br1 = new BufferedReader(new FileReader("CMU_Country_GPA_Sample.csv")); //System.out.println(csvFile); //CSVReader<Student> studentReader = new CSVReaderBuilder<Student>(csvFile).entryParser(new StudentEntryParser()).build(); // List<Student> studentList = studentReader.readAll(); for (Student student : studentList) { System.out.println(student.toString()); } //csvFile.close(); br.close(); } catch (IOException ex) { Logger.getLogger(MainChart.class.getName()).log(Level.SEVERE, null, ex); } } private JFreeChart createChart(PieDataset dataset, String title) { JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title dataset, // data true, // include legend true, false); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setStartAngle(290); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5f); return chart; } // Variables declaration - do not modify//GEN-BEGIN:variables // End of variables declaration//GEN-END:variables } class CustomRenderer extends BarRenderer { private Paint[] colors; public CustomRenderer() { this.colors = new Paint[] { Color.red, Color.blue, Color.green, Color.yellow, Color.orange, Color.cyan, Color.magenta, Color.blue }; } public Paint getItemPaint(final int row, final int column) { // returns color for each column return (this.colors[column % this.colors.length]); } }