私は4つのクラスを持っています: - ChartService - CSVReader - FileChooser - ChartPanel
私の問題は、FileChooser クラスでファイルをロードした後、ChartPanel クラスでグラフを更新する方法がわからないことです。いろいろ試しましたがグラフが表示されません。
助けてください !!!
ChartService クラス :
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;
}
ChartPanel クラス:
public ChartPanel() {
initializeChartPanel();
}
private void initializeChartPanel() {
setBorder(BorderFactory.createTitledBorder("Chart:"));
setPreferredSize(new Dimension(824, 0));
ChartService chartService = new ChartService();
chartService.initializeChart();
this.add(chartService);
}