update a jfreechart
I have a jfreechart which appears after the user clicks a button. When the user clicks the button again with a different dataset the graph should be repainted. My problem is that even the new graph is displayed once the user clicks the button for the second time, it disappears and the previous graph appears when the user click on anywhere else in the graph.
Please help me on this.
This is where the graph is created.
public JFreeChart createChart() throws FileNotFoundException, IOException {
userAligner.userRecord();
userAligner.diffVoiceText();
// aligner.roggerRecord();
//aligner.userRecord();
final CategoryDataset dataset1 = aligner.roggerRecord();
final NumberAxis rangeAxis1 = new NumberAxis("Pace - Rogger(ms)");
rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
final LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
renderer1.setSeriesPaint(0, Color.blue); //int series, paint paint
renderer1.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
final CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1开发者_Python百科, renderer1);
subplot1.setDomainGridlinesVisible(true);
final CategoryDataset dataset2 = userAligner.createData();
dataset2.addChangeListener(subplot1);
final NumberAxis axis2 = new NumberAxis("Pace - User(ms)");
subplot1.setRangeAxis(1, axis2); //value axis
subplot1.setDataset(1, dataset2); //int index
//subplot1.mapDatasetToRangeAxis(1, 1); // int index, int axisindex
final CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
renderer2.setSeriesPaint(0, Color.red);
subplot1.setForegroundAlpha(0.7f);
subplot1.setRenderer(0, renderer1);
subplot1.setRenderer(1, renderer2);
final CategoryAxis domainAxis = new CategoryAxis("Words");
// domainAxis.setMaximumCategoryLabelLines(10);
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
final CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(domainAxis);
plot.add(subplot1, 1);
// plot.add(subplot2, 1);
final JFreeChart chart = new JFreeChart(
"Pace Graph", new Font("SansSerif", Font.BOLD, 14),
plot, true);
return chart;
}
public void datasetChanged(DatasetChangeEvent arg0) {
try {
createChart();
} catch (FileNotFoundException ex) {
Logger.getLogger(PaceChart.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(PaceChart.class.getName()).log(Level.SEVERE, null, ex);
}
}
And it is updated every time with the action when the user clicks a button as below.
private void viewPaceButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (file == null){
JOptionPane.showMessageDialog(VoiceTracker.f,"Save the Voice before View Pace Graph");
}
else {
//jScrollPane2.setLayout(new java.awt.BorderLayout());
// PaceChart pc = new PaceChart();
PaceChart dac = new PaceChart();
//SymbolAxisDemo1 demo = new SymbolAxisDemo1();
ChartPanel CP;
try {
CP = new ChartPanel(dac.createChart());
paceAnalyzePanel.add(CP, BorderLayout.CENTER);
paceAnalyzePanel.validate();
} catch (FileNotFoundException ex) {
Logger.getLogger(VoiceTracker.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(VoiceTracker.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
You are making new chart objects every time the user clicks and adding it to the same panel in the same place. Not sure exactly how that gets you the behavior your seeing but you are also doing it onDatasetChanged events which is a bad idea. The JFreeChart object is a panel and you should only need one (for sanity and performance). You can set up all your changes to the renderers etc at the same time and just reuse the same chart and plot objects. The datasets on the subplots should be the only thing changed when clicked (call setDataset) and the chart will be repainted automatically (drop the dataSetChanged listener unless its just for logging). You could either make the chart always be there (init at the same time as the containing panel) and call setVisible(false) to hide it until the first click or initialize it lazily (or even leave it there as an empty plot with no dataset).
精彩评论