chartpanel.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 1,880 行 · 第 1/5 页

JAVA
1,880
字号
            );        }        if (this.zoomRectangle != null) {            // use XOR to draw the new zoom rectangle...            if (this.fillZoomRectangle) {                g2.fill(this.zoomRectangle);            }            else {                g2.draw(this.zoomRectangle);            }        }        g2.dispose();    }    /**     * 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.     */    public void mouseReleased(MouseEvent e) {        if (this.zoomRectangle != null) {            boolean hZoom = false;            boolean vZoom = false;            if (this.orientation == PlotOrientation.HORIZONTAL) {                hZoom = this.rangeZoomable;                vZoom = this.domainZoomable;            }            else {                hZoom = this.domainZoomable;                              vZoom = this.rangeZoomable;            }                        boolean zoomTrigger1 = hZoom && Math.abs(e.getX()                 - this.zoomPoint.getX()) >= this.zoomTriggerDistance;            boolean zoomTrigger2 = vZoom && Math.abs(e.getY()                 - this.zoomPoint.getY()) >= this.zoomTriggerDistance;            if (zoomTrigger1 || zoomTrigger2) {                if ((hZoom && (e.getX() < this.zoomPoint.getX()))                     || (vZoom && (e.getY() < this.zoomPoint.getY()))) {                    restoreAutoBounds();                }                else {                    double x, y, w, h;                    Rectangle2D screenDataArea = getScreenDataArea(                        (int) this.zoomPoint.getX(),                         (int) this.zoomPoint.getY()                    );                    // for mouseReleased event, (horizontalZoom || verticalZoom)                    // will be true, so we can just test for either being false;                    // otherwise both are true                    if (!vZoom) {                        x = this.zoomPoint.getX();                        y = screenDataArea.getMinY();                        w = Math.min(                            this.zoomRectangle.getWidth(),                            screenDataArea.getMaxX() - this.zoomPoint.getX()                        );                        h = screenDataArea.getHeight();                    }                    else if (!hZoom) {                        x = screenDataArea.getMinX();                        y = this.zoomPoint.getY();                        w = screenDataArea.getWidth();                        h = Math.min(                            this.zoomRectangle.getHeight(),                            screenDataArea.getMaxY() - this.zoomPoint.getY()                        );                    }                    else {                        x = this.zoomPoint.getX();                        y = this.zoomPoint.getY();                        w = Math.min(                            this.zoomRectangle.getWidth(),                            screenDataArea.getMaxX() - this.zoomPoint.getX()                        );                        h = Math.min(                            this.zoomRectangle.getHeight(),                            screenDataArea.getMaxY() - this.zoomPoint.getY()                        );                    }                    Rectangle2D zoomArea = new Rectangle2D.Double(x, y, w, h);                    zoom(zoomArea);                }                this.zoomPoint = null;                this.zoomRectangle = null;            }            else {                Graphics2D g2 = (Graphics2D) getGraphics();                g2.setXORMode(java.awt.Color.gray);                if (this.fillZoomRectangle) {                    g2.fill(this.zoomRectangle);                }                else {                    g2.draw(this.zoomRectangle);                }                g2.dispose();                this.zoomPoint = null;                this.zoomRectangle = null;            }        }        else if (e.isPopupTrigger()) {            if (this.popup != null) {                displayPopupMenu(e.getX(), e.getY());            }        }    }    /**     * Receives notification of mouse clicks on the panel. These are     * translated and passed on to any registered chart mouse click listeners.     *     * @param event  Information about the mouse event.     */    public void mouseClicked(MouseEvent event) {        Insets insets = getInsets();        int x = (int) ((event.getX() - insets.left) / this.scaleX);        int y = (int) ((event.getY() - insets.top) / this.scaleY);        // old 'handle click' code...        //chart.handleClick(x, y, this.info);        this.anchor = new Point2D.Double(x, y);        this.chart.setNotify(true);  // force a redraw         // new entity code...        if (this.chartMouseListeners.isEmpty()) {            return;        }        ChartEntity entity = null;        if (this.info != null) {            EntityCollection entities = this.info.getEntityCollection();            if (entities != null) {                entity = entities.getEntity(x, y);            }        }        ChartMouseEvent chartEvent = new ChartMouseEvent(            getChart(), event, entity        );        Iterator iterator = this.chartMouseListeners.iterator();        while (iterator.hasNext()) {            ChartMouseListener listener = (ChartMouseListener) iterator.next();            listener.chartMouseClicked(chartEvent);        }    }    /**     * Implementation of the MouseMotionListener's method.     *     * @param e  the event.     */    public void mouseMoved(MouseEvent e) {        if (this.horizontalAxisTrace) {            drawHorizontalAxisTrace(e.getX());        }        if (this.verticalAxisTrace) {            drawVerticalAxisTrace(e.getY());        }        if (this.chartMouseListeners.isEmpty()) {            return;        }        Insets insets = getInsets();        int x = (int) ((e.getX() - insets.left) / this.scaleX);        int y = (int) ((e.getY() - insets.top) / this.scaleY);        ChartEntity entity = null;        if (this.info != null) {            EntityCollection entities = this.info.getEntityCollection();            if (entities != null) {                entity = entities.getEntity(x, y);            }        }        ChartMouseEvent event = new ChartMouseEvent(getChart(), e, entity);        Iterator iterator = this.chartMouseListeners.iterator();        while (iterator.hasNext()) {            ChartMouseListener listener = (ChartMouseListener) iterator.next();            listener.chartMouseMoved(event);        }    }    /**     * Zooms in on an anchor point (specified in screen coordinate space).     *     * @param x  the x value (in screen coordinates).     * @param y  the y value (in screen coordinates).     */    public void zoomInBoth(double x, double y) {        zoomInDomain(x, y);        zoomInRange(x, y);    }    /**     * Decreases the length of the domain axis, centered about the given     * coordinate on the screen.  The length of the domain axis is reduced     * by the value of {@link #getZoomInFactor()}.     *     * @param x  the x coordinate (in screen coordinates).     * @param y  the y-coordinate (in screen coordinates).     */    public void zoomInDomain(double x, double y) {        Plot p = this.chart.getPlot();        if (p instanceof Zoomable) {            Zoomable plot = (Zoomable) p;            plot.zoomDomainAxes(                this.zoomInFactor, this.info.getPlotInfo(),                 translateScreenToJava2D(new Point((int) x, (int) y))            );        }    }    /**     * Decreases the length of the range axis, centered about the given     * coordinate on the screen.  The length of the range axis is reduced by     * the value of {@link #getZoomInFactor()}.     *     * @param x  the x-coordinate (in screen coordinates).     * @param y  the y coordinate (in screen coordinates).     */    public void zoomInRange(double x, double y) {        Plot p = this.chart.getPlot();        if (p instanceof Zoomable) {            Zoomable z = (Zoomable) p;            z.zoomRangeAxes(                this.zoomInFactor, this.info.getPlotInfo(),                 translateScreenToJava2D(new Point((int) x, (int) y))            );        }    }    /**     * Zooms out on an anchor point (specified in screen coordinate space).     *     * @param x  the x value (in screen coordinates).     * @param y  the y value (in screen coordinates).     */    public void zoomOutBoth(double x, double y) {        zoomOutDomain(x, y);        zoomOutRange(x, y);    }    /**     * Increases the length of the domain axis, centered about the given     * coordinate on the screen.  The length of the domain axis is increased     * by the value of {@link #getZoomOutFactor()}.     *     * @param x  the x coordinate (in screen coordinates).     * @param y  the y-coordinate (in screen coordinates).     */    public void zoomOutDomain(double x, double y) {        Plot p = this.chart.getPlot();        if (p instanceof Zoomable) {            Zoomable z = (Zoomable) p;            z.zoomDomainAxes(                this.zoomOutFactor, this.info.getPlotInfo(),                 translateScreenToJava2D(new Point((int) x, (int) y))            );        }    }    /**     * Increases the length the range axis, centered about the given     * coordinate on the screen.  The length of the range axis is increased     * by the value of {@link #getZoomOutFactor()}.     *     * @param x  the x coordinate (in screen coordinates).     * @param y  the y-coordinate (in screen coordinates).     */    public void zoomOutRange(double x, double y) {        Plot p = this.chart.getPlot();        if (p instanceof Zoomable) {            Zoomable z = (Zoomable) p;            z.zoomRangeAxes(                this.zoomOutFactor, this.info.getPlotInfo(),                 translateScreenToJava2D(new Point((int) x, (int) y))            );        }    }    /**     * Zooms in on a selected region.     *     * @param selection  the selected region.     */    public void zoom(Rectangle2D selection) {        // get the origin of the zoom selection in the Java2D space used for        // drawing the chart (that is, before any scaling to fit the panel)        Point2D selectOrigin = translateScreenToJava2D(            new Point(                (int) Math.ceil(selection.getX()),                 (int) Math.ceil(selection.getY())            )        );        PlotRenderingInfo plotInfo = this.info.getPlotInfo();        Rectangle2D scaledDataArea = getScreenDataArea(            (int) selection.getCenterX(), (int) selection.getCenterY()        );        if ((selection.getHeight() > 0) && (selection.getWidth() > 0)) {            double hLower = (selection.getMinX() - scaledDataArea.getMinX())                 / scaledDataArea.getWidth();            double hUpper = (selection.getMaxX() - scaledDataArea.getMinX())                 / scaledDataArea.getWidth();            double vLower = (scaledDataArea.getMaxY() - selection.getMaxY())                 / scaledDataArea.getHeight();            double vUpper = (scaledDataArea.getMaxY() - selection.getMinY())                 / scaledDataArea.getHeight();            Plot p = this.chart.getPlot();            if (p instanceof Zoomable) {                Zoomable z = (Zoomable) p;                if (z.getOrientation() == PlotOrientation.HORIZONTAL) {                    z.zoomDomainAxes(vLower, vUpper, plotInfo, selectOrigin);                    z.zoomRangeAxes(hLower, hUpper, plotInfo, selectOrigin);                }                else {                    z.zoomDomainAxes(hLower, hUpper, plotInfo, selectOrigin);                    z.zoomRangeAxes(vLower, vUpper, plotInfo, selectOrigin);                }            }        }    }    /**     * Restores the auto-range calculation on both axes.     */    public void restoreAutoBounds() {        restoreAutoDomainBounds();        restoreAutoRangeBounds();    }    /**     * Restores the auto-range calculation on the domain axis.     */    public void restoreAutoDomainBounds() {        Plot p = this.chart.getPlot();        if (p instanceof Zoomable) {            Zoomable z = (Zoomable) p;            z.zoomDomainAxes(0.0, this.info.getPlotInfo(), this.zoomPoint);        }    }    /**     * Restores the auto-range calculation on the range axis.     */    public void restoreAutoRangeBounds() {        Plot p = this.chart.getPlot();        if (p instanceof ValueAxisPlot) {            Zoomable z = (Zoomable) p;            z.zoomRangeAxes(0.0, this.info.getPlotInfo(), this.zoomPoint);        }    }    /**     * Returns the data area for the chart (the area inside the axes) with the     * current scaling applied (that is, the area as it appears on screen).     *     * @return The scaled data area.     */    public Rectangle2D getScreenDataArea() {        Rectangle2D dataArea = this.info.getPlotInfo().getDataArea();        Insets insets = getInsets(); 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?