chartChanged invokes twice

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
PollerJava
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

chartChanged invokes twice

Post by PollerJava » Mon Jan 12, 2009 11:19 am

Hello,

I use the class XYSeriesDemo1 and I added a linstener to the JFreeChart- panel (see at the bottom).

I recognized that if I zoom into the JFreechart- panel, the method chartChanged is invoked twice instead of once.
My problem is that if somebody zooms into the JFreechart- panel, I load data from a database and if chartChanged is invoked twice than I load the data twice - that is not what I wont.
what can I do. is this a normal behavior or is there an fault?

Thanks a lot,
All the best,

Code: Select all

package trendtest;

import org.jfree.chart.event.ChartChangeEvent;
import org.jfree.chart.event.ChartChangeListener;


public class TrendZoomListener implements ChartChangeListener {

    public TrendZoomListener() {      
        }

    public void chartChanged(ChartChangeEvent arg0) {
        System.out.println("this is invoked twice");
        }
}

Code: Select all

package trendtest;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.event.ChartChangeListener;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class XYSeriesDemo1 extends ApplicationFrame {

    public XYSeriesDemo1(String title) {

        super(title);
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }

    private static JFreeChart createChart(XYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYLineChart(
            "XY Series Demo",
            "X",
            "Y",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );       
        chart.addChangeListener(new TrendZoomListener());
//        java.awt.BasicStroke stroke = new java.awt.BasicStroke(1f,
//                java.awt.BasicStroke.CAP_SQUARE,
//                java.awt.BasicStroke.JOIN_MITER,
//                10.0f,
//                new float[] {6f, 3f, 2f, 3f},
//                0f);
//
//        chart.getXYPlot().getRenderer().setStroke(stroke);
        return chart;
    }

    /**
     * Creates a sample dataset.
     *
     * @return A sample dataset.
     */
    private static XYDataset createDataset() {
        XYSeries series = new XYSeries("Random Data");
        series.add(1.0, 500.2);
        series.add(5.0, 694.1);
        series.add(4.0, 100.0);
        series.add(12.5, 734.4);
        series.add(17.3, 453.2);
        series.add(21.2, 500.2);
        series.add(21.9, null);
        series.add(25.6, 734.4);
        series.add(30.0, 453.2);
        return new XYSeriesCollection(series);
    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());       
        return new ChartPanel(chart);
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {

        XYSeriesDemo1 demo = new XYSeriesDemo1("XY Series Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }
}


david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Jan 12, 2009 9:07 pm

I think this may be a bug. Zooming will update the axes independently, and I think each one is triggering a change event...I will confirm that and then fix the problem.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jan 13, 2009 11:41 am

I've committed the fix to Subversion for inclusion in the 1.0.13 release. Thanks for reporting the problem.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked