I have jfreechart that appears after a user clicks a button. When the user again presses a button with a different data set, the graph should be repainted. My problem is that even a new graph is displayed, as soon as the user presses the button a second time, it disappears, and the previous graph appears when the user clicks anywhere on the graph.
Please help me with this.
This creates a graph.
public JFreeChart createChart() throws FileNotFoundException, IOException {
userAligner.userRecord();
userAligner.diffVoiceText();
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);
renderer1.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
final CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1, renderer1);
subplot1.setDomainGridlinesVisible(true);
final CategoryDataset dataset2 = userAligner.createData();
dataset2.addChangeListener(subplot1);
final NumberAxis axis2 = new NumberAxis("Pace - User(ms)");
subplot1.setRangeAxis(1, axis2);
subplot1.setDataset(1, dataset2);
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.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
final CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(domainAxis);
plot.add(subplot1, 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 an action when the user clicks a button, as shown below.
private void viewPaceButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (file == null){
JOptionPane.showMessageDialog(VoiceTracker.f,"Save the Voice before View Pace Graph");
}
else {
PaceChart dac = new PaceChart();
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);
}
}
}