簡體   English   中英

使用鼠標平移圖表-Jfreechart

[英]Pan chart using mouse - Jfreechart

我們可以在JfreeChart中將平移功能實現為鼠標拖動事件嗎? 現在,我按CTRL並拖動鼠標以平移圖表。 我只想通過拖動鼠標來實現平移功能。 那可能嗎 ?

這顯然是不可能改變當前的JFreeChart API修正鍵,如討論在這里 (但它是在管道)。

但是,所有內容都可以通過編程方式平移圖表,因此您可以嘗試以下操作:

  • 在您的ChartPanel添加一個MouseMotionListener ,以跟蹤mouseDragged()事件。
  • 從這些事件中,計算圖表的請求移動。
  • 直接調用XYPlot.panDomainAxes()XYPlot.panRangeAxis()此處為 API)。

ChartPanel 源代碼中 ChartPanel靈感:

/** 
 * Temporary storage for the width and height of the chart 
 * drawing area during panning.
 */
private double panW, panH;

/** The last mouse position during panning. */
private Point panLast;

/**
 * The mask for mouse events to trigger panning.
 *
 * @since 1.0.13
 */
private int panMask = InputEvent.CTRL_MASK;

...

/**
 * Handles a 'mouse pressed' event.
 * <P>
 * This event is the popup trigger on Unix/Linux.  For Windows, the popup
 * trigger is the 'mouse released' event.
 *
 * @param e  The mouse event.
 */
@Override
public void mousePressed(MouseEvent e) {
    if (this.chart == null) {
        return;
    }
    Plot plot = this.chart.getPlot();
    int mods = e.getModifiers();
    if ((mods & this.panMask) == this.panMask) {
        // can we pan this plot?
        if (plot instanceof Pannable) {
            Pannable pannable = (Pannable) plot;
            if (pannable.isDomainPannable() || pannable.isRangePannable()) {
                Rectangle2D screenDataArea = getScreenDataArea(e.getX(),
                        e.getY());
                if (screenDataArea != null && screenDataArea.contains(
                        e.getPoint())) {
                    this.panW = screenDataArea.getWidth();
                    this.panH = screenDataArea.getHeight();
                    this.panLast = e.getPoint();
                    setCursor(Cursor.getPredefinedCursor(
                            Cursor.MOVE_CURSOR));
                }
            }
            // the actual panning occurs later in the mouseDragged() 
            // method
        }
    }
    else if (this.zoomRectangle == null) {
        ...
    }
}

...

/**
 * Handles a 'mouse dragged' event.
 *
 * @param e  the mouse event.
 */
@Override
public void mouseDragged(MouseEvent e) {

    // if the popup menu has already been triggered, then ignore dragging...
    if (this.popup != null && this.popup.isShowing()) {
        return;
    }

    // handle panning if we have a start point
    if (this.panLast != null) {
        double dx = e.getX() - this.panLast.getX();
        double dy = e.getY() - this.panLast.getY();
        if (dx == 0.0 && dy == 0.0) {
            return;
        }
        double wPercent = -dx / this.panW;
        double hPercent = dy / this.panH;
        boolean old = this.chart.getPlot().isNotify();
        this.chart.getPlot().setNotify(false);
        Pannable p = (Pannable) this.chart.getPlot();
        if (p.getOrientation() == PlotOrientation.VERTICAL) {
            p.panDomainAxes(wPercent, this.info.getPlotInfo(),
                    this.panLast);
            p.panRangeAxes(hPercent, this.info.getPlotInfo(),
                    this.panLast);
        }
        else {
            p.panDomainAxes(hPercent, this.info.getPlotInfo(),
                    this.panLast);
            p.panRangeAxes(wPercent, this.info.getPlotInfo(),
                    this.panLast);
        }
        this.panLast = e.getPoint();
        this.chart.getPlot().setNotify(old);
        return;
    }

    ...

}

...

/**
 * Handles a 'mouse released' event.  On Windows, we need to check if this
 * is a popup trigger, but only if we haven't already been tracking a zoom
 * rectangle.
 *
 * @param e  information about the event.
 */
@Override
public void mouseReleased(MouseEvent e) {

    // if we've been panning, we need to reset now that the mouse is 
    // released...
    if (this.panLast != null) {
        this.panLast = null;
        setCursor(Cursor.getDefaultCursor());
    }

    ...

}

編輯:注意到當前API的唯一問題是panMask是私有的,為什么不嘗試通過反射來破解該字段:

Field mask = ChartPanel.class.getDeclaredField("panMask");
mask.setAccessible(true);
mask.set(yourChartPanel, Integer.valueOf(0)); // The "0" mask is equivalent to no mask. You could also set a different modifier.

新版本仍未提供解決方案,因此我可以完美地工作,滾動時它也會自動縮放范圍軸:

    ChartPanel panel = new ChartPanel(chart);
    panel.setMouseZoomable(false);
    panel.setMouseWheelEnabled(true);
    panel.setDomainZoomable(true);
    panel.setRangeZoomable(false);
    panel.setPreferredSize(new Dimension(1680, 1100));
    panel.setZoomTriggerDistance(Integer.MAX_VALUE);
    panel.setFillZoomRectangle(false);
    panel.setZoomOutlinePaint(new Color(0f, 0f, 0f, 0f));
    panel.setZoomAroundAnchor(true);
    try {
        Field mask = ChartPanel.class.getDeclaredField("panMask");
        mask.setAccessible(true);
        mask.set(panel, 0);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    panel.addMouseWheelListener(arg0 -> panel.restoreAutoRangeBounds());

現在,JFreeChart的行為類似於任何其他專業的制圖軟件。

這是我的解決方案。

關於TA

 ChartPanel cp = new ChartPanel(chart)
  {
     /**
      * A hack to change the zoom and panning function.
      * With this override we can pan around by dragging.
      * If SHIFT is pressed we get the zoom rectangle.
      * @param e
      */
     @Override
     public void mousePressed(MouseEvent e)
     {
        int mods = e.getModifiers();

        int panMask = MouseEvent.BUTTON1_MASK;

        if (mods == MouseEvent.BUTTON1_MASK+MouseEvent.SHIFT_MASK)
        {
           panMask = 255; //The pan test will match nothing and the zoom rectangle will be activated.
        }

        try
        {
           Field mask = ChartPanel.class.getDeclaredField("panMask");
           mask.setAccessible(true);
           mask.set(this, panMask);

        }
        catch (Exception ex)
        {
           ex.printStackTrace();
        }

        super.mousePressed(e);
     }
  };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM