Note that there are some explanatory texts on larger screens.

plurals
  1. POJava JFreeChart - plot isn't updated
    primarykey
    data
    text
    <p>Plot of parabola isn't updated, when I calculate new coefficients of parabola. New coefficients of a parabola are calculated when the mouse is зкуыыув and moves. New coefficients on a graphics are displayed, but the plot remains the same. Why so?</p> <pre><code>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 &lt; 83) dx = 83; else if (dx &gt; 468) dx = 468; if (dy &lt; 74) dy = 74; else if (dy &gt; 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 &gt; (-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}"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload