簡體   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