我的问题与Pan chart using mouse - Jfreechart有关,但我面临一个非常具体的问题。 我的图表显示了一些追溯的TimeSeries编码数据和DateAxis的预测值。我希望用户能够及时平移图表上的数据,但是阻止显示超出预测时间窗口的任何数据。我知道我的数据的最大日期,但在平移事件期间我需要以某种方式查明我的DateAxis对象是否已达到此最大日期。 PanHandlerFX的源代码为我提供了关于鼠标位置和拖动距离的线索,但我怎么知道DateAxis上显示的范围意味着什么呢?有人可以帮忙吗?先感谢您。
答案 0 :(得分:1)
这就是我所做的:我检查了滚动的方向,如果public class PanHandlerFXWithDateLimit extends PanHandlerFX {
public PanHandlerFXWithDateLimit(String id, boolean altKey, boolean ctrlKey, boolean metaKey, boolean shiftKey) {
super(id, altKey, ctrlKey, metaKey, shiftKey);
}
/** The last mouse location seen during panning. */
private Point2D panLast;
private double panW;
private double panH;
private Date panMaxTo;
/**
* Handles a mouse pressed event by recording the initial mouse pointer
* location.
*
* @param canvas the JavaFX canvas (<code>null</code> not permitted).
* @param e the mouse event (<code>null</code> not permitted).
*/
@Override
public void handleMousePressed(ChartCanvas canvas, MouseEvent e) {
Plot plot = canvas.getChart().getPlot();
if (!(plot instanceof Pannable)) {
canvas.clearLiveHandler();
return;
}
Pannable pannable = (Pannable) plot;
if (pannable.isDomainPannable() || pannable.isRangePannable()) {
Point2D point = new Point2D.Double(e.getX(), e.getY());
Rectangle2D dataArea = canvas.findDataArea(point);
if (dataArea != null && dataArea.contains(point)) {
this.panW = dataArea.getWidth();
this.panH = dataArea.getHeight();
this.panLast = point;
canvas.setCursor(javafx.scene.Cursor.MOVE);
}
}
// the actual panning occurs later in the mouseDragged() method
}
/**
* Handles a mouse dragged event by calculating the distance panned and
* updating the axes accordingly.
*
* @param canvas the JavaFX canvas (<code>null</code> not permitted).
* @param e the mouse event (<code>null</code> not permitted).
*/
@Override
public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) {
if (this.panLast == null) {
//handle panning if we have a start point else unregister
canvas.clearLiveHandler();
return;
}
JFreeChart chart = canvas.getChart();
double dx = e.getX() - this.panLast.getX();
double dy = e.getY() - this.panLast.getY();
if (dx == 0.0 && dy == 0.0) {
return;
}
/** if dx is negative, the scroll is to the right side,
* therefore we must check if displayed axis end is after panMax date - then stop panning
*/
if (dx < 0.0) {
XYPlot p = (XYPlot) chart.getPlot();
if (((DateAxis)p.getDomainAxis()).getMaximumDate().after(panMaxTo))
return;
}
double wPercent = -dx / this.panW;
double hPercent = dy / this.panH;
boolean old = chart.getPlot().isNotify();
chart.getPlot().setNotify(false);
Pannable p = (Pannable) chart.getPlot();
PlotRenderingInfo info = canvas.getRenderingInfo().getPlotInfo();
if (p.getOrientation().isVertical()) {
p.panDomainAxes(wPercent, info, this.panLast);
p.panRangeAxes(hPercent, info, this.panLast);
}
else {
p.panDomainAxes(hPercent, info, this.panLast);
p.panRangeAxes(wPercent, info, this.panLast);
}
this.panLast = new Point2D.Double(e.getX(), e.getY());
chart.getPlot().setNotify(old);
}
@Override
public void handleMouseReleased(ChartCanvas canvas, MouseEvent e) {
//if we have been panning reset the cursor
//unregister in any case
if (this.panLast != null) {
canvas.setCursor(javafx.scene.Cursor.DEFAULT);
}
this.panLast = null;
canvas.clearLiveHandler();
}
/**
* @param panMaxTo the panMaxTo to set
*/
public final void setPanMaxTo(Date panMaxTo) {
if (panMaxTo == null) this.panMaxTo = Date.from(Instant.now());
else this.panMaxTo = panMaxTo;
//System.out.println(this.panMaxTo.toString());
}
上的最大日期是在我的处理程序类的预定义最大日期之后,则该方法只返回。
accessibilityValue