chartChanged invokes twice

A free public discussion forum for the JFreeChart class library.

chartChanged invokes twice

Postby 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);

    }
}

PollerJava
 
Posts: 81
Joined: Wed Jul 25, 2007 10:40 am

Postby 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: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11352
Joined: Fri Mar 14, 2003 10:29 am

Postby 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: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11352
Joined: Fri Mar 14, 2003 10:29 am


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 4 guests