从csv文件向xchart显示数据

hlswsv35  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(197)

我创建了一个带有空 JTable 以及 XChart . 我点击的地方 input button ,它将获取csv文件并将数据显示到 JTable 以及 XChart . 我确实让jtable工作了,但是我很难在xchart上工作。我想使xchart数据流与jtable相似。
这是jtable显示数据时涉及的类和方法
主视图

public class Home extends JFrame {
     public Home() {
         super("Hello world");
         setLayout(new BorderLayout());
         tablePanel = new TablePanel();
         chartPanel = new ChartPanel();
         buttonPanel = new JPanel();
         input = new JButton("Input");
         fc = new FileController();
         fileChooser = new JFileChooser();

         tablePanel.setData(fc.getDataModels());
         chartPanel.METHOD_FOR_SETDATA_THE_CHART?();    /////////////

         input.addActionListener((ActionEvent e) -> {
             if (fileChooser.showOpenDialog(Home.this) ==JFileChooser.APPROVE_OPTION) {
               try {
                  fc.inputFile(fileChooser.getSelectedFile());
                  tablePanel.refresh();
                  chartPanel.METHOD_FOR_REFRESH_THE_CHART?(); //////////
               } catch (IOException ex) {
                  ex.printStackTrace();
               }
             }
          });
      }
  }

文件控制器

public class FileController {
     private ArrayList<RawDataModel> dataset;
     private Home home;
     private double tempInt = 0;

     public FileController(){
        dataset = new ArrayList<RawDataModel>();
     }

     public ArrayList<RawDataModel> getDataModels(){
        return dataset;
     }

     public void inputFile(File file) throws IOException {

        String line = "";
        String csvSplit = ";";
        int iter = 0;

        try {
           BufferedReader br = new BufferedReader(new FileReader(file));

           while ((line = br.readLine()) != null) {
             if (iter == 0) {
                iter++;
                continue;
             }

             String[] text = line.split(csvSplit);

             double tempInt = (double) Double.parseDouble(text[1]);               
             dataset.add(new RawDataModel(text[0], tempInt));

           }
         } catch (ArrayIndexOutOfBoundsException e) {
           JOptionPane.showMessageDialog(home, "couldn't load data from file", "error", JOptionPane.ERROR_MESSAGE);
        } 
     }
  }

桌面面板

Public class TablePanel extends JPanel{
   private JTable table;
   private TableModel tableModel;

   public TablePanel() {//Layout Setting}

   public void setData(ArrayList<RawDataModel> data){
       tableModel.setData(data);
   }

   public void refresh(){
       tableModel.fireTableDataChanged();
   }
}

原始数据模型

public class RawDataModel implements Serializable {
    private double price;
    private String time;

    public RawDataModel(String time, double price) {
        this.price = price;
        this.time = time;
    }

    public double getPrice()           {return price;}
    public void setPrice(double price) {this.price = price;}
    public String getTime()            {return time;}
    public void setTime(String time)   {this.time = time;}
}

表格模型

public class TableModel extends AbstractTableModel{
    private ArrayList<RawDataModel> list;
    private String[] colnames = {"Time", "Price"};

    public TableModel() {}

    public void setData(ArrayList<RawDataModel> list) {
       this.list = list;
    }

    @Override //all method below are overriden
    public int getRowCount() {return list.size(); }
    public int getColumnCount() {return 2;}
    public String getColumnName(int column) {return colnames[column];}

    public Object getValueAt(int row, int col) {
       RawDataModel dataModel = list.get(row);

       switch(col){
         case 0 -> {return dataModel.getTime();}
         case 1 -> {return dataModel.getPrice();}
       }
       return null;
   }  
}

到目前为止,这是我为xchart创建的唯一类和方法

public class ChartPanel extends JPanel {

   private final XYChart chart;
   //do i need chart model?    //////////

   public ChartPanel() {
     //Layout Setting
   }

   public void setData(param?){
      chart.addSeries("data", TIME DATA?, PRICE DATA?);   //////////
   }

   public void refresh(){
      statement?  //////////
   } 
}

我需要做“图表模型”像表有表模型吗?图表获取数据和显示数据的方法是什么?以及如何在输入文件时刷新图表?我在代码中用“///////”标记了混乱,非常感谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题