【问题标题】:Update a plot in JFreeChart更新 JFreeChart 中的绘图
【发布时间】:2013-11-27 18:00:20
【问题描述】:

我有一个抛物线图,抛物线方程的系数存储在数组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"
    }
}

【问题讨论】:

    标签: java plot jfreechart


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果数据是DefaultCategoryDataset,只需在您的数据集上使用 setValue。如果是时间序列数据,则使用 TimeSeriesCollection.add() 。应该为您自动调用图表的侦听器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多