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

由现在删除的问题提示, JFreeChart并非旨在创建此类交互式Swing程序。 您可以利用现有的事件基础结构来动态更新绘图。 在下面的示例中, JSpinner侦听其用于更新绘图数据集的更改。 该程序不依赖于ChartMouseListener ,而是依赖于图表构造函数中指定的XYItemLabelGenerator ; 将鼠标hover在一个点以查看效果。 如果您重新考虑添加其他参数,请考虑扩展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); } }); } } 

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