简体   繁体   中英

JFreechart & Servlet: How to compute datapoint from mouse position

I'm drawing within a servlet a ScatterPlot and serve it to the browser. The user can now click somewhere on the plot and I want to determine what datapoint of the scatter plot the user has pointed. From the mouse click of the user I can determine on which pixel of the image he has clicked, but how can I get from this info to the coordinates on the domain and range axis?

I found tipps how to do it, which uses the ChartPanel. But for serving it directly to the browser I only use an instance of a JFreeChar object.

Anybody has a clue or an example how to do it?

Thanks, Dieter

I think I have found a solution. For the solution I need to get my chart again, so I either have to create it a new or to save it somehow. But when I have a reference to that chart the solution is as following:


JFreeChart chart = functionWhichRetrievesTheChart();
ChartRenderingInfo info = new ChartRenderingInfo();
// PLOT_SIZE is the size if the graph and has to be the same size as the original drawn chart.createBufferedImage(PLOT_SIZE, PLOT_SIZE, info); 
graph, otherwise the pixel position points to somewhere else
PlotRenderingInfo plotInfo = info.getPlotInfo();


XYPlot plot = (XYPlot)chart.getPlot();
Point p = new Point(x,y); // x and y are the pixel positions

// this is the domain value which belongs to the pixel position x
double domain = plot.getDomainAxis().java2DToValue(p.getX(), plotInfo.getDataArea(), plot.getDomainAxisEdge()); 

// this is the range value which belongs to the pixel position y
double range = plot.getRangeAxis().java2DToValue(p.getY(), plotInfo.getDataArea(), plot.getRangeAxisEdge());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM