开发者

JFreechart & Servlet: How to compute datapoint from mouse position

开发者 https://www.devze.com 2023-02-12 18:24 出处:网络
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

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 开发者_JAVA百科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());
0

精彩评论

暂无评论...
验证码 换一张
取 消