我的问题与Pan chart using mouse - Jfreechart有关,但是我面临一个非常具体的问题。
我的图表在DateAxis上显示了一些追溯性的TimeSeries编码数据和预测值。我希望用户能够在时间上平移图表上的数据,但要避免显示超出我的预测时间窗口的任何数据。我知道数据的最大日期,但是在平移事件期间需要以某种方式找出我的DateAxis对象是否尚未达到此最大日期。 PanHandlerFX的源代码为我提供了有关鼠标位置和拖动距离的线索,但是我如何才能知道DateAxis上显示的Range的含义呢?有人可以帮忙吗?先感谢您。

最佳答案

我就是这样做的:我检查了滚动的方向,并且如果DateAxis上的最大日期是在处理程序类的预定义最大日期之后,则该方法将返回。

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());
}

08-15 19:18