繁体   English   中英

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

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

我正在Servlet中绘制一个ScatterPlot,并将其提供给浏览器。 用户现在可以单击绘图上的某个位置,我想确定用户指向的散点图的哪个数据点。 通过用户的鼠标单击,我可以确定他单击了图像的哪个像素,但是如何从该信息获取域和范围轴上的坐标?

我发现了使用ChartPanel的小技巧。 但是,为了直接将其提供给浏览器,我仅使用JFreeChar对象的实例。

任何人都有线索或例子怎么做?

谢谢,Dieter

我想我已经找到了解决方案。 对于解决方案,我需要再次获取图表,因此我必须重新创建图表或以某种方式保存它。 但是,当我参考该图表时,解决方案如下:


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