当前位置: 首页 > 知识库问答 >
问题:

Java jfreechart-plot未更新

宋典
2023-03-14

当我计算抛物线的新系数时,抛物线的图没有更新。当老鼠是一条抛物线和移动时,计算出新的系数。将显示图形上的新系数,但绘图保持不变。为什么这样?

package parabolademo;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.DecompositionSolver;
import org.apache.commons.math3.linear.LUDecomposition;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.linear.RealVector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
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;


public class ParabolaDemo extends ApplicationFrame {

    /*
     * @param title  the frame title.
     */
    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;
    XYPlot plot;
    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;
        p = new PolynomialFunction2D(a);
        XYDataset dataset = DatasetUtilities.sampleFunction2D(p, lrange, rrange, 20, "y = f(x)");

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

        plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer render = (XYLineAndShapeRenderer) plot.getRenderer();
        render.setBaseShapesVisible(true);
        render.setSeriesShape(0, new Rectangle(-6, -6, 12, 12));

        chartPanel = new ChartPanel(chart);

        chartPanel.addMouseMotionListener(new MotionListener());

        chartPanel.addChartMouseListener(new ChartMouseListener() {

            @Override
            public void chartMouseClicked(ChartMouseEvent cme) {

            }

            @Override
            public void chartMouseMoved(ChartMouseEvent cme) {
                ChartEntity ce = cme.getEntity();
                if (ce instanceof XYItemEntity) {
                    flag = 1;
                    XYItemEntity e = (XYItemEntity) ce;
                    XYDataset d = ((XYItemEntity) ce).getDataset();
                    int i = ((XYItemEntity) ce).getItem();
                    chartpx = d.getXValue(0, i);
                    chartpy = d.getYValue(0, i);
                    //System.out.println("X:" + chartpx + ", Y:" + chartpy);
                }
                else
                    flag = 0;
                Point2D po = chartPanel.translateScreenToJava2D(cme.getTrigger().getPoint());
                Rectangle2D plotArea = chartPanel.getScreenDataArea();
                XYPlot plot = (XYPlot) chart.getPlot(); // your plot
                chartX = plot.getDomainAxis().java2DToValue(po.getX(), plotArea, plot.getDomainAxisEdge());
                chartY = plot.getRangeAxis().java2DToValue(po.getY(), plotArea, plot.getRangeAxisEdge());
                //System.out.println("X:" + chartX + ", Y:" + chartY);
            }
        });
        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) {
            double dx = me.getX();
            double dy = windowheight - me.getY();
            double graphx, graphy;
            if (dx < 83)
                dx = 83;
            else if (dx > 468)
                dx = 468;
            if (dy < 74)
                dy = 74;
            else if (dy > 468)
                dy = 468;
            dx -= 83;
            dy -= 74;
            graphx = 40.0 / 385.0 * dx - 20;
            graphy = 400.0 / 153.0 * dy;
            if (flag == 1) {
                //System.out.println("Dragged! x = " + dx + ", y = " + dy + ", xplot = " + graphx + ", yplot = " + graphy);
            }
            a = calculate(graphx, graphy);
            plot.setDataset(createDataset(a));
        }

        @Override
        public void mouseMoved(MouseEvent me) {

        }

    }

    private double[] calculate(double x, double y) {
        double px1, px2, py1, py2;
        double[] coef;
        coef = p.getCoefficients();
        if (x == (-coef[1])/2*coef[2]) {
            px1 = lrange;
            py1 = p.getValue(px1);
            px2 = rrange;
            py2 = p.getValue(px2);
        }
        else if (x == lrange) {
            px1 = (-coef[1])/2*coef[2];
            py1 = p.getValue(px1);
            px2 = rrange;
            py2 = p.getValue(px2);
        }
        else if (x == rrange) {
            px1 = lrange;
            py1 = p.getValue(px1);
            px2 = (-coef[1])/2*coef[0];
            py2 = p.getValue(px2);
        }
        else if (x > (-coef[1])/2*coef[2]){
            px1 = lrange;
            py1 = p.getValue(px1);
            px2 = (-coef[1])/2*coef[2];
            py2 = p.getValue(px2);
        }
        else {
            px1 = (-coef[1])/2*coef[2];
            py1 = p.getValue(px1);
            px2 = rrange;
            py2 = p.getValue(px2);
        }
        System.out.println("("+px1+","+py1+") ("+px2+","+py2+") ("+x+","+y+")");
        RealMatrix coefficients = new Array2DRowRealMatrix(new double[][] { { px1*px1, px1, 1 }, { px2*px2, px2, 1 },
            { x*x, x, 1 } }, false);
        DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();
        RealVector constants = new ArrayRealVector(new double[] { py1, py2, y }, false);
        RealVector solution = solver.solve(constants);
        coef[2] = solution.getEntry(0);
        coef[1] = solution.getEntry(1);
        coef[0] = solution.getEntry(2);
        System.out.println(coef[0] + ", " + coef[1] + ", " + coef[2]);
        return coef;
    }

    private XYDataset createDataset(double[] a) {
        double[] array = a;
        return DatasetUtilities.sampleFunction2D(
            p, -20.0, 20.0, 20, "y = " + array[2] + "x²+" + array[1] + "x+" + array[0] + " {-20…20}");

    }
}

共有1个答案

贾成天
2023-03-14

您的createDataSet()方法使用的是原始的PolynomialFunction2D;您可以按如下所示更新它:

private XYDataset createDataset(double[] a) {
    p = new PolynomialFunction2D(a);
    return DatasetUtilities.sampleFunction2D(
        p, -20.0, 20.0, 20, "y = " + a[2] + "x²+" + a[1] + "x+" + a[0] + " {-20…20}");
}
 类似资料:
  • 我遇到了一个奇怪的行为——我在Plotly论坛和Stackoverflow上看到了类似的问题,但没有解决方案。基本上,我试图将中间值(在其他回调中重用)存储在隐藏的div“data storage json”中,但将其作为输入的回调似乎没有发生。后端没有错误。在前端,我得到“更新plot-div.children时出现回调错误”(这是指定为输出的组件) UPD:事件_df类似于: 我还在下面的答案

  • plot 可用于数据绘制与可视化,它提供了用于在 Go 中构建和绘制图的 API。 gonum/plot  被分成几个开发包: plot 包提供简单的界面布局的接口,并绘制它提供了原函数。 绘图仪包提供了一组标准的绘图仪,它使用由 plot 提供的原函数绘制线,散点图,箱线图,误差线等。 你不需要通过使用绘图仪包来利用 gonum/plot,自定义绘图仪的教程,可在维基中查看。 plotutil

  • 绘制曲线图、饼状图。可以在图形上标注数据。 [Code4App.com]

  • pandoc-plot A Pandoc filter to generate figures from code blocks in documents pandoc-plot turns code blocks present in your documents (Markdown, LaTeX, etc.) into embedded figures, using your plotting

  • Function Plot 是一个基于 D3.js 开发的 2D 图表库,用来绘制各种函数图。 示例代码: functionPlot({  title: 'y = x * x',  target: '#linear-with-options',  width: 580,  height: 400,  disableZoom: true,  xLabel: 'x - axis',  yLabel: 

  • PowerPlot是一个iOS可视化类库,可以用于科学和商业领域的可视化图表绘制。可以绘制柱状图、折线图等等,是一个可以高度定制化的类库,满足开发者大部分可视化绘制需求。 [Code4App.com]