title: Reading in a CSV file, and plotting the values in a graph using MVC
body: <p>I am trying to plot values from a csv file into a graph, using Java, JFreeChart and using the MVC concept. At the minute, I have created a button, and when this button is clicked it adds a new plot to the graph, however instead of doing this, I would like it to read in the values from the csv file, which is read in by a csv file adapter and stored in the model. I wondering if anyone could help me with this. I would appreciate any help that could be given. Thank you</p>
  
  <p>//Main Class</p>
  
  <pre><code>public class Main
  {
      public static void main(String[] args) {           
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {                                           
                  Model model = new Model(0);
  
                  Controller controller = new Controller(model);
                  View view = new View(controller, "-");
  
  
                  view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  view.setVisible(true);
              }
          });  
      }
  }
  </code></pre>
  
  <p>//csv file adapter</p>
  
  <pre><code>public List&lt;Double&gt; readFile(final String filename)
      {
          List&lt;Double&gt; result = new ArrayList&lt;Double&gt;();
  
          String csvFile = filename;
          BufferedReader br = null;
          String line = "";
          String splitBy = ",";
  
          try {
  
              br = new BufferedReader(new FileReader(csvFile));
              while((line = br.readLine()) != null){
  
                  String[] test = line.split(splitBy);
                  System.out.println("csvFile [Value= " + test[0] + ", Value=" + test[1] + ", Value=" + test[2] + "]");
                  try
                  {
                      for (String val : test)
                      {
                          final Double valueToAdd = Double.parseDouble(val);
                          result.add(valueToAdd);
                      }
                  }
                  catch (NumberFormatException nfe)
                  {
                      System.out.println("Failed to parse line: " + line);
                  }
              }   
          }
          catch(FileNotFoundException e){
              e.printStackTrace();
          }catch (IOException e){
              e.printStackTrace();
          }finally{
              if (br != null){
                  try{
                      br.close();
                  } catch (IOException e){
                      e.printStackTrace();
                  }
              }
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>//model</p>
  
  <pre><code>public class Model {
  
    private int x;
  
      public Model(){
          x = 0;
      }
  
      public Model(int x){
          this.x = x;
      }
  
      public void incX(){
          x++;
      }
  
      public int getX(){
          return x;
      }
  
      public void addDataset(final List&lt;Double&gt; data)
      {
          System.out.println("Added data to model");
          for (Double d : data)
          {
              System.out.println("Value: " + d.toString());
          }
      }
  }
  </code></pre>
  
  <p>//view</p>
  
  <pre><code>public class View extends JFrame
  {
      private Controller controller;
  
      private JFrame frame;
      private JLabel label;
      private JButton button;
      private ChartDisplayWidget myChart;
  
      public View(Controller c, String text){
          this.controller = c;
          getContentPane().setLayout(new BorderLayout());                                          
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           
          setSize(1000,1000);        
  
          //label = new JLabel(text);
          //getContentPane().add(label, BorderLayout.CENTER);
  
          button = new JButton("Button");        
          getContentPane().add(button, BorderLayout.SOUTH); 
          button.addActionListener(new Action());
  
          myChart = new ChartDisplayWidget();
          getContentPane().add(myChart, BorderLayout.CENTER);
      }
  
      public class Action implements ActionListener {
  
          public void actionPerformed (ActionEvent e){
              System.out.println("I was clicked");
              controller.control();
              myChart.addTimeSeriesPerformancePlot("NewPlot", new Double[] {60.0, 40.0, 500.0, 10.0});    
  
                 /*this is where I would like to plot the values from the csv file*/
          }
     }
  
      public JButton getButton(){
          return button;
      }
  
      public void setText(String text){
          label.setText(text);
      }
  }
  
  //controller
  
  public class Controller {
  
      public Model model;
      public ActionListener actionListener;
  
      public Controller(Model model){
          this.model = model;
      }
  
      public void control(){        
          CSVFileAdapter c = new CSVFileAdapter();
          model.addDataset(c.readFile("C:/dstat.csv"));
  
      }
  }
  </code></pre>
  
  <p>//Chart Display Widget</p>
  
  <pre><code>public class ChartDisplayWidget extends JPanel
  {
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      private TimeSeriesCollection chartData;
      private JFreeChart chart;
  
      public ChartDisplayWidget()
      {
          init();
      }
  
      public void init()
      {
          final XYDataset dataset = getSampleData();
  
          chart = ChartFactory.createTimeSeriesChart(
                  "Our test chart",
                  "Time",
                  "Some Value",
                  dataset);
  
          final ChartPanel chartPanel = new ChartPanel(chart); 
  
          add(chartPanel);
      }
  
      public void addTimeSeriesPerformancePlot(final String plotName, final Double[] values)
      {
          final TimeSeries newSeries = new TimeSeries(plotName);
  
          int arrLen = values.length;
          int monthIndex = 2;
          int yearIndex = 2001;
          for (int index = 0; index &lt; arrLen; index++)
          {
              newSeries.add(new Month(monthIndex++, yearIndex++), values[index]);
          }
          chartData.addSeries(newSeries);
      }
  
      private XYDataset getSampleData()
      {
          TimeSeries s1 = new TimeSeries("Max CPU");
  
          s1.add(new Month(2, 2001), 181.5);
          s1.add(new Month(3, 2001), 20.5);
          s1.add(new Month(4, 2001), 1.1);
          s1.add(new Month(5, 2001), 81.5);
          s1.add(new Month(6, 2001), 1181.5);
          s1.add(new Month(7, 2001), 1081.5);
  
          TimeSeries s2 = new TimeSeries("Disk I/O");
  
          s2.add(new Month(2, 2001), 50.0);
          s2.add(new Month(3, 2001), 55.0);
          s2.add(new Month(4, 2001), 60.6);
          s2.add(new Month(5, 2001), 70.8);
          s2.add(new Month(6, 2001), 1000.1);
          s2.add(new Month(7, 2001), 1081.5);
  
          chartData = new TimeSeriesCollection();
          chartData.addSeries(s1);
          chartData.addSeries(s2);
  
          return chartData;
      }
  }
  </code></pre>