[英]JFreeChart interactive chart editing: transforming mouse coordinates into series values
我有一個用JFreeChart構建的XYLineChart。 考慮到該圖表和一個ChartMouseEvent,我需要檢索最接近單擊鼠標的點的displayde系列的X值。
多虧了上一篇文章,我能夠使用以下方法檢索灰色圖表的偏移量(圖像中綠色點的坐標)及其尺寸:
Rectangle2D greyChartArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
我也知道顯示的系列的最大X值:
double maxXValue = seriesCollection.getDomainUpperBound(true); //where seriesCollection is an XYSeriesCollection object
現在的問題是,要將鼠標坐標(點)轉換為圖表中的相應值,我需要知道屏幕上的像素對應多少個單位(兩倍)。 不幸的是,最大X值(在這種情況下為60)與灰色圖表寬度(請看大藍線)之間存在間隙,因此我無法實現完美的轉換。
然后我有兩個問題:
回顧此示例 ,您可以從ChartProgressListener
的十字准線值獲取模型坐標。 十字線不必可見。
chartPanel.getChart().addProgressListener(new ChartProgressListener() {
@Override
public void chartProgress(ChartProgressEvent e) {
XYPlot xyPlot = (XYPlot) chartPanel.getChart().getPlot();
System.out.println(e.getType()
+ ": " + xyPlot.getDomainCrosshairValue()
+ ", " + xyPlot.getRangeCrosshairValue());
}
});
final XYPlot plot = getChart().getXYPlot();
final ValueAxis domainAxis = plot.getDomainAxis();
final ValueAxis rangeAxis = plot.getRangeAxis();
final Rectangle2D plotRectangle = SWTUtils.toAwtRectangle(getScreenDataArea());
final double chartX = domainAxis.java2DToValue(relativeX, plotRectangle, plot.getDomainAxisEdge());
final double chartY = rangeAxis.java2DToValue(relativeY, plotRectangle, plot.getRangeAxisEdge());
我們已經使用它來從鼠標坐標中獲取數據坐標。
看看這個JFreeChart獲取鼠標坐標 。 如果知道坐標,則可以從圖中獲取x和y坐標,並從軸獲取相應的值:
JFreeChart chart = yourChart;
Rectangle2D greyChartArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
XYPlot plot = (XYPlot) chart.getPlot();
double valueX = ((NumberAxis) plot.getRangeAxis()).java2DToValue(chartY,plot.getRangeAxisEdge();
double valueY = ((NumberAxis) plot.getDomainAxis()).java2DToValue(chartX,plot.getDomainAxisEdge();
應該這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.