Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wrote a simple parabola plot using JFreeChart.

package parabolademo;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
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.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
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;


public class ParabolaDemo extends ApplicationFrame {

/*
 * @param title  the frame title.
 */
public ParabolaDemo(final String title) {

    super(title);
    double[] a = {0.0, 0.0, 3.0};
    Function2D p = new PolynomialFunction2D(a);
    XYDataset dataset = DatasetUtilities.sampleFunction2D(p, -20.0, 20.0, 100, "Function");
    final JFreeChart chart = ChartFactory.createXYLineChart(
        "Parabola",
        "X", 
        "Y", 
        dataset,
        PlotOrientation.VERTICAL,
        true,
        true,
        false
    );

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseClicked(ChartMouseEvent cme) {
            Point2D po = chartPanel.translateScreenToJava2D(cme.getTrigger().getPoint());
            Rectangle2D plotArea = chartPanel.getScreenDataArea();
            XYPlot plot = (XYPlot) chart.getPlot(); // your plot
            double chartX = plot.getDomainAxis().java2DToValue(po.getX(), plotArea, plot.getDomainAxisEdge());
            double chartY = plot.getRangeAxis().java2DToValue(po.getY(), plotArea, plot.getRangeAxisEdge());
            System.out.println("Clicked!");
            System.out.println("X:" + chartX + ", Y:" + chartY);
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent cme) {

        }
    });
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    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);
}

}

How to get coordinates of FUNCTION PLOT point (my chartMouseListener get coordinates of any point in window)? how to receive point coordinates after the user moved mouse and released the mouse button? I want that when clicking a mouse, the point of the plot followed a mouse, thus the plot will be rebuilt (for this purpose it is necessary to calculate again coefficients, knowing this coordinate and having taken any 2 other coordinates). How it can be done? How rebuilt plot with new coefficients?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
180 views
Welcome To Ask or Share your Answers For Others

1 Answer

Given a ChartMouseEvent named cmd, ignore entities of any type other than XYItemEntity. Once you know the entity, don't interpolate—just query the dataset.

ChartEntity ce = cme.getEntity();
if (ce instanceof XYItemEntity) {
    XYItemEntity e = (XYItemEntity) ce;
    XYDataset d = e.getDataset();
    int s = e.getSeriesIndex();
    int i = e.getItem();
    System.out.println("X:" + d.getX(s, i) + ", Y:" + d.getY(s, i));
}

Also consider invoking setBaseShapesVisible(true) on the plot's renderer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...