57 lines
1.8 KiB
Java
57 lines
1.8 KiB
Java
package ch.psi.plot.util;
|
|
|
|
|
|
import java.awt.Point;
|
|
import java.awt.geom.Point2D;
|
|
import java.awt.geom.Rectangle2D;
|
|
|
|
import org.jfree.chart.ChartRenderingInfo;
|
|
import org.jfree.chart.axis.ValueAxis;
|
|
import org.jfree.chart.plot.XYPlot;
|
|
import org.jfree.chart.ChartPanel;
|
|
import org.jfree.ui.RectangleEdge;
|
|
|
|
import ch.psi.plot.plot.done.XYPoint;
|
|
|
|
|
|
/**
|
|
* Helper class transforms window (frame) coordinates to chart coordinates.
|
|
* @param mouseX, mouseY position of mouse click
|
|
* @param xyPoint (useless so far)
|
|
* @return true if mouse click was in Chart, false else (if click was in Legend area, etc.)
|
|
*/
|
|
|
|
|
|
public class CoordinateTransformer {
|
|
|
|
public static boolean chartCoordinates(ChartPanel chartPanel, int mouseX, int mouseY, XYPoint xyPoint) {
|
|
//calculate x and y value in chart at mouse position
|
|
Point2D p = chartPanel.translateScreenToJava2D(new Point(mouseX, mouseY));
|
|
XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();
|
|
ChartRenderingInfo info = chartPanel.getChartRenderingInfo();
|
|
Rectangle2D dataArea = info.getPlotInfo().getDataArea();
|
|
|
|
ValueAxis domainAxis = plot.getDomainAxis();
|
|
RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
|
|
ValueAxis rangeAxis = plot.getRangeAxis();
|
|
RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
|
|
double chartX = domainAxis.java2DToValue(p.getX(), dataArea, domainAxisEdge);
|
|
double chartY = rangeAxis.java2DToValue(p.getY(), dataArea,rangeAxisEdge);
|
|
|
|
// TODO need to remove this or make this as an return value
|
|
xyPoint.setX(chartX);
|
|
xyPoint.setY(chartY);
|
|
|
|
//Check if the click occurred in the plot region
|
|
if (chartX < domainAxis.getLowerBound() || chartX > domainAxis.getUpperBound() ||
|
|
chartY < rangeAxis.getLowerBound() || chartY > rangeAxis.getUpperBound()){
|
|
return false;
|
|
}
|
|
else{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
}
|