Update jfreechart

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();
   // 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, 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 an action when the user clicks a button, as shown 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);
        }

    }


}      
+3
source share
1 answer

, . , , onDatasetChanged, . JFreeChart - , ( ). . , ( setDataset), ( dataSetChanged, ). (init , ), setVisible (false), ( ).

0
source

All Articles