java - 更新 JFreeChart 中的绘图

标签 java plot jfreechart

我有一个抛物线图,抛物线方程的系数存储在数组a中。在mouseDragged(mousemotionlistener)中,抛物线系数发生了变化,我想用新系数实时更新抛物线图。我怎样才能做到这一点?

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"
    }
}

最佳答案

由此提示现已删除 question , JFreeChart 并不是为创建此类交互式 Swing 程序而设计的。您可以利用现有的事件基础架构来动态更新绘图。在下面的示例中,JSpinner 监听用于更新绘图数据集的更改。该程序依赖于图表构造函数中指定的 XYItemLabelGenerator,而不是 ChartMouseListener;将鼠标悬停在某个点上即可查看效果。如果您重构以添加其他参数,请考虑扩展 AbstractAction ,如下所示。

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

关于java - 更新 JFreeChart 中的绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20249765/

相关文章:

java - 字符串比较未返回正确结果

statistics - 在 Mathematica 中一次绘制不同的条件和主题

r - y轴上的绘图百分比

java - 将鼠标监听器坐标转换为图表坐标

java - 如何从另一个 netbeans 模块更新 jTable

java - X 不活动量后如何锁定用户?

java - 使用 + 符号连接字符串

java - 数据库服务使用什么技术?

python - 在 Mac OS X 10.8.5 (Mountain Lion) 上使用 Enthought Canopy python 编辑器运行 matplotlib.pyplot 时没有显示绘图窗口

java - JFreeChart:将图例嵌入箱线图中