0

我有四个类: - ChartService - CSVReader - FileChooser - ChartPanel

我的问题是,在 FileChooser 类中加载文件后,我不知道如何刷新 ChartPanel 类中的图表。我尝试了不同的方式,但图表没有出现。

请帮忙 !!!

图表服务类:

   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 类:

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 类:

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;
    }

图表面板类:

public ChartPanel() {
         initializeChartPanel();
    }

    private void initializeChartPanel() {

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

        this.add(chartService);
    }
4

1 回答 1

4

将您的图表添加到org.jfree.chart.ChartPanel,它扩展JPanel并“注册到图表以接收图表任何组件更改的通知”。如本例所示,数据集通常在更新时默认提供此通知。您ChartPanel似乎根本没有注册任何听众。

于 2012-04-19T10:36:44.973 回答