简体   繁体   English

JFreechart和Servlet:如何从鼠标位置计算数据点

[英]JFreechart & Servlet: How to compute datapoint from mouse position

I'm drawing within a servlet a ScatterPlot and serve it to the browser. 我正在Servlet中绘制一个ScatterPlot,并将其提供给浏览器。 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. 我发现了使用ChartPanel的小技巧。 But for serving it directly to the browser I only use an instance of a JFreeChar object. 但是,为了直接将其提供给浏览器,我仅使用JFreeChar对象的实例。

Anybody has a clue or an example how to do it? 任何人都有线索或例子怎么做?

Thanks, Dieter 谢谢,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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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