问题描述
我有四个班级: -ChartService -CSVReader -FileChooser -ChartPanel
I have four classes: - ChartService - CSVReader - FileChooser - ChartPanel
我的问题是,在将文件加载到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.
请帮助!!!
ChartService类:
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类:
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类:
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类:
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);
}
推荐答案
将图表添加到 org.jfree.chart.ChartPanel
,它扩展了JPanel
并在图表中注册以接收对图表中任何组成部分的更改通知."就像在示例中一样,数据集通常在更新后默认情况下会提供此通知.您的ChartPanel
似乎根本没有注册任何侦听器.
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.
这篇关于加载文件后如何刷新ChartPanel类中的图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!