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

时间:2011-02-23 16:27:53

标签: java servlets jfreechart

我在一个servlet中绘制一个ScatterPlot并将其提供给浏览器。 用户现在可以点击图上的某个地方,我想确定一下 用户指出的散点图的数据点。从鼠标点击 用户我可以确定他点击的图像的哪个像素,但是怎么样 我从这个信息到域和范围轴上的坐标?

我发现tipps如何做,它使用ChartPanel。但是为了直接服务它 到浏览器我只使用JFreeChar对象的实例。

任何人都有线索或示例如何做到这一点?

谢谢, 迪特

1 个答案:

答案 0 :(得分:1)

我想我找到了解决方案。对于解决方案,我需要再次获取我的图表, 所以我要么必须创建一个新的或以某种方式保存它。但是当我有一个参考 该图表解决方案如下:


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());