【问题标题】:Restrict panning when a certain limit on DateAxis has been reached达到 DateAxis 的某个限制时限制平移
【发布时间】:2017-08-25 14:06:46
【问题描述】:

我的问题与Pan chart using mouse - Jfreechart 有关,但我面临一个非常具体的问题。 我的图表显示了沿 DateAxis 的一些回顾性 TimeSeries 编码数据和预测值。我希望用户能够及时平移图表上的数据,但防止显示比我的预测时间窗口更远的任何数据。我知道我的数据的最大日期,但不知何故,我需要在平移事件期间找出我的 DateAxis 对象是否尚未达到这个最大日期。 PanHandlerFX 的源代码为我提供了有关鼠标位置和拖动距离的线索,但我怎么知道这在 DateAxis 上显示的范围方面意味着什么?任何人都可以帮忙吗?先感谢您。

【问题讨论】:

  • 为什么不限制模型(数据集)中的投影?
  • 投影时长限制为 4 小时,但无需修改就可以随意平移到未来。我有一个肮脏的解决方案,不是很好,因为它只有在用户以合理的速度平移时才有效。高速时,轴的平移超出了我的上一个投影日期。

标签: java jfreechart pan


【解决方案1】:

这就是我所做的:我检查了滚动的方向,如果我在 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());
}

【讨论】:

  • 提示:使用标记为{}的编辑按钮格式化代码片段;使用 反引号 格式化内联标识符。
猜你喜欢
  • 2015-07-06
  • 1970-01-01
  • 2019-06-30
  • 2014-06-29
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 2011-03-25
  • 1970-01-01
相关资源
最近更新 更多