Denis
Denis

Reputation: 503

Update a plot in JFreeChart

I have a parabola plot, coefficients of parabola equation are stored in array a. In mouseDragged (mousemotionlistener), coefficients of parabola were changed and I want to update a parabola plot with new coefficients in realtime. How can I make this happen?

public class ParabolaDemo extends ApplicationFrame {
    int flag = 0;
    double px = 0.0, py = 0.0, chartpx = 0.0, chartpy = 0.0, 
    chartX = 0.0, chartY = 0.0;
    int windowheight = 270;
    ChartPanel chartPanel;
    PolynomialFunction2D p;
    double lrange = -20.0;
    double rrange = 20.0;
    double[] a;

    public ParabolaDemo(final String title) {

        super(title);
        double[] tmp = {0.0, 0.0, 1.0};
        a = tmp; // coeffcients of parabola (a[0] + a[1]*x + a[2]*x^2)
        p = new PolynomialFunction2D(a);
        XYDataset dataset = DatasetUtilities.sampleFunction2D(p, lrange, rrange, 1000, "y = f(x)");

        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Parabola",
            "X", 
            "Y", 
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );

        chartPanel = new ChartPanel(chart);

        chartPanel.addMouseMotionListener(new MotionListener());

        //some code...

        chartPanel.setPreferredSize(new java.awt.Dimension(500, windowheight));
        chartPanel.setDomainZoomable(false);
        chartPanel.setRangeZoomable(false);
        setContentPane(chartPanel);
    }

    public static void main(final String[] args) {
        final ParabolaDemo demo = new ParabolaDemo("Parabola Plot Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }


    public class MotionListener implements MouseMotionListener {

        @Override
        public void mouseDragged(MouseEvent me) {

            //some code...

            a = calculate(graphx, graphy);
        }

        @Override
        public void mouseMoved(MouseEvent me) {

        }

    }

    private double[] calculate(double x, double y) {
        //some code...
        //it is function that changes coefficients of array "a"
    }
}

Upvotes: 1

Views: 1143

Answers (2)

trashgod
trashgod

Reputation: 205785

Prompted by this now deleted question, JFreeChart was not designed to create interactive Swing programs of this sort. You can leverage the existing event infrastructure to dynamically update a plot. In the example below, a JSpinner listens for changes that it uses to update the plot's dataset. Instead of a ChartMouseListener, the program relies on the XYItemLabelGenerator specified in the chart' constructor; hover over a point to see the effect. If you re-factor to add other parameters, consider extending AbstractAction, as shown here.

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.function.Function2D;
import org.jfree.data.function.PolynomialFunction2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/*
 * @see https://stackoverflow.com/a/20249795/230513
 * @see https://stackoverflow.com/a/20107935/230513
 * @see https://stackoverflow.com/q/20081801/230513
 */
public class ParabolaDemo extends ApplicationFrame {

    private Integer value = Integer.valueOf(-1);

    public ParabolaDemo(final String title) {
        super(title);
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Parabola", "X", "Y", createDataset(value),
            PlotOrientation.VERTICAL, true, true, false);
        final XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
        r.setBaseShapesVisible(true);
        r.setSeriesShape(0, new Rectangle(-6, -6, 12, 12));
        final ChartPanel chartPanel = new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        add(chartPanel, BorderLayout.CENTER);
        JPanel p = new JPanel();
        JSpinner s = new JSpinner();
        s.setValue(value);
        s.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner s = (JSpinner) e.getSource();
                int v = ((Number) s.getValue()).intValue();
                plot.setDataset(createDataset(v));
            }
        });
        p.add(new JLabel("a"));
        p.add(s);
        add(p, BorderLayout.SOUTH);
    }

    private XYDataset createDataset(int a) {
        double[] array = {0.0, 0.0, a};
        Function2D p = new PolynomialFunction2D(array);
        return DatasetUtilities.sampleFunction2D(
            p, -20.0, 20.0, 20, "y = " + a + "x² {-20…20}");

    }

    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                final ParabolaDemo demo = new ParabolaDemo("Parabola Demo");
                demo.pack();
                RefineryUtilities.centerFrameOnScreen(demo);
                demo.setVisible(true);
            }
        });
    }
}

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38521

If the data is DefaultCategoryDataset just use setValue on your dataSet. If its time series data then use TimeSeriesCollection.add() . The listeners for the charts should be called automatically for you.

Upvotes: 1

Related Questions