How to to refresh the chart in ChartPanel class af

2019-08-20 08:02发布

问题:

I have four classes: - ChartService - CSVReader - FileChooser - ChartPanel

My problem is that I don't know how to to refresh the chart in ChartPanel class after loading the file in the FileChooser class. I tried in different ways but the chart doesn't appear.

Please help !!!

ChartService Class :

   public void initializeChart() {
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis("Time"));
        this.datasets = new TimeSeriesCollection[SUBPLOT_COUNT];

        for (int i = 0; i < SUBPLOT_COUNT; i++) {
            final TimeSeries series = sensorXseries;

            this.datasets[i] = new TimeSeriesCollection(series);
            final NumberAxis rangeAxis = new NumberAxis(Y_AXIS_TITLES[i]);
            rangeAxis.setAutoRangeIncludesZero(false);
            final XYPlot subplot = new XYPlot(
                    this.datasets[i], null, rangeAxis, new StandardXYItemRenderer()
            );

            subplot.setBackgroundPaint(Color.lightGray);
            subplot.setDomainGridlinePaint(Color.white);
            subplot.setRangeGridlinePaint(Color.white);
            plot.add(subplot);
        }

        final JFreeChart chart = new JFreeChart("Charts", plot);

        chart.setBorderPaint(Color.black);
        chart.setBorderVisible(true);
        chart.setBackgroundPaint(Color.white);

        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        final ValueAxis axis =  plot.getDomainAxis();
        axis.setAutoRange(false);

        chartPanel = new ChartPanel(chart);

        chartPanel.setPreferredSize(new java.awt.Dimension(790, 620));
        chartPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        this.add(chartPanel);
    }
    public TimeSeries createFirstDataSet(ArrayList<DataObject> data) {
        sensorXseries = new TimeSeries("Acceleration X");
        for(int i=0; i < data.size(); i++) {
            sensorXseries.add(new Millisecond(convertToTimestamp(data.get(i).getStoper())), data.get(i).getAccelerationX());
        }
        return sensorXseries;
    }

    public TimeSeries createSecondDataSet(ArrayList<DataObject> data) {
        TimeSeries sensorYseries = new TimeSeries("Acceleration Y");
        for(int i=0; i < data.size(); i++) {
            sensorYseries.add(new Millisecond(convertToTimestamp(data.get(i).getStoper())), data.get(i).getAccelerationY());
        }
        return sensorYseries;
    }

    public TimeSeries createThirdDataSet(ArrayList<DataObject> data) {
        TimeSeries sensorZseries = new TimeSeries("Acceleration Z");
        for(int i=0; i < data.size(); i++) {
            sensorZseries.add(new Millisecond(convertToTimestamp(data.get(i).getStoper())), data.get(i).getAccelerationZ());
        }
        return sensorZseries;
    }

    public void refreshChartPanel() {
        chartPanel.repaint();
    }

FileChooser class:

public void loadFile() throws Exception {
    fileChooser = new JFileChooser();
    fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("CSV Files", "csv"));

    int returnVal = fileChooser.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        fileName= file.getAbsolutePath();
        CSVReader csvReader = new CSVReader(fileName);
        chartService = new ChartService();
        chartService.createFirstDataSet(csvReader.getLinesFromFile());
    }

}

CSVReader class:

public CSVReader(String fileName) {
        this.fileName = fileName;
    }

    public ArrayList getLinesFromFile() throws Exception {
        inFile = new CsvBeanReader(new FileReader(fileName), CsvPreference.EXCEL_PREFERENCE);
        final String[] header = inFile.getCSVHeader(true);

        CellProcessor [] processors = {null, new ParseDouble(), new ParseDouble(), new ParseDouble(), new ParseInt(), new ParseInt()};

        dataObjectArray = new ArrayList<DataObject>();
        while((dataObject = inFile.read(DataObject.class, header, processors)) != null) {
            dataObjectArray.add(dataObject);
        }
        return dataObjectArray;
    }

ChartPanel class:

public ChartPanel() {
         initializeChartPanel();
    }

    private void initializeChartPanel() {

        setBorder(BorderFactory.createTitledBorder("Chart:"));
        setPreferredSize(new Dimension(824, 0));
        ChartService chartService = new ChartService();
        chartService.initializeChart();

        this.add(chartService);
    }

回答1:

Add your chart to a org.jfree.chart.ChartPanel, which extends JPanel and "registers with the chart to receive notification of changes to any component of the chart." As in this example, data sets typically provide this notification by default when updated. Your ChartPanel does not appear to register any listeners at all.