⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chartcomposite.java

📁 java图形利器
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    case SWT.MouseMove:                        // handle axis trace                        if ( horizontalAxisTrace || verticalAxisTrace ) {                            horizontalTraceLineY = event.y;                            verticalTraceLineX = event.x;                            canvas.redraw();                        }                        // handle tool tips in a simple way                        if (displayToolTips) {                                                        String s = getToolTipText(new MouseEvent(event));                            if (s == null && canvas.getToolTipText() != null                        	    || s!=null && !s.equals(canvas.getToolTipText()))                                canvas.setToolTipText(s);                        }                        // handle zoom box                        if (zoomPoint == null) {                            return;                        }                        scaledDataArea = getScreenDataArea(zoomPoint.x, zoomPoint.y);                        org.eclipse.swt.graphics.Point movingPoint                             = getPointInRectangle(event.x, event.y, scaledDataArea);                        // handle zoom                        boolean hZoom = false;                        boolean vZoom = false;                        if (orientation == PlotOrientation.HORIZONTAL) {                            hZoom = rangeZoomable;                            vZoom = domainZoomable;                        }                        else {                            hZoom = domainZoomable;                                          vZoom = rangeZoomable;                        }                        if (hZoom && vZoom) {                            // selected rectangle shouldn't extend outside the data area...                            zoomRectangle = new Rectangle(zoomPoint.x, zoomPoint.y,                                     movingPoint.x - zoomPoint.x, movingPoint.y - zoomPoint.y);                                                    }                        else if (hZoom) {                            zoomRectangle = new Rectangle(zoomPoint.x, scaledDataArea.y,                                    movingPoint.x - zoomPoint.x, scaledDataArea.height);                        }                        else if (vZoom) {                            zoomRectangle = new Rectangle(                                    scaledDataArea.x, zoomPoint.y,                                    scaledDataArea.width, event.y - zoomPoint.y);                        }                        canvas.redraw();                        break;                    case SWT.MouseUp:                        if (zoomRectangle == null) {                            Rectangle screenDataArea = getScreenDataArea(event.x, event.y);                            if (screenDataArea != null) {                                zoomPoint = getPointInRectangle(event.x, event.y, screenDataArea);                            }                            if (popup != null && event.button == 3) {                                org.eclipse.swt.graphics.Point pt = canvas.toDisplay(event.x, event.y);                                displayPopupMenu(pt.x, pt.y);                            }                        }                        else {                            hZoom = false;                            vZoom = false;                            if (orientation == PlotOrientation.HORIZONTAL) {                                hZoom = rangeZoomable;                                vZoom = domainZoomable;                            }                            else {                                hZoom = domainZoomable;                                              vZoom = rangeZoomable;                            }                            boolean zoomTrigger1 = hZoom                                     && Math.abs(zoomRectangle.width)                                     >= zoomTriggerDistance;                            boolean zoomTrigger2 = vZoom                                     && Math.abs(zoomRectangle.height)                                     >= zoomTriggerDistance;                            if (zoomTrigger1 || zoomTrigger2) {                                // if the box has been drawn backwards, restore the auto bounds                                if ((hZoom && (zoomRectangle.x + zoomRectangle.width < zoomPoint.x))                                         || (vZoom && (zoomRectangle.y + zoomRectangle.height < zoomPoint.y)))                                     restoreAutoBounds();                                else zoom(zoomRectangle);                                canvas.redraw();                            }                        }                        zoomPoint = null;                        zoomRectangle = null;                        break;                    default:                        zoomPoint = null;                        zoomRectangle = null;                }            }        };        canvas.addListener(SWT.MouseDown, listener);        canvas.addListener(SWT.MouseMove, listener);        canvas.addListener(SWT.MouseUp, listener);                this.enforceFileExtensions = true;    }            /**     * Returns the X scale factor for the chart.  This will be 1.0 if no      * scaling has been used.     *      * @return The scale factor.     */    public double getScaleX() {        return this.scaleX;    }        /**     * Returns the Y scale factory for the chart.  This will be 1.0 if no      * scaling has been used.     *      * @return The scale factor.     */    public double getScaleY() {        return this.scaleY;    }        /**     * Returns the anchor point.     *      * @return The anchor point (possibly <code>null</code>).     */    public Point2D getAnchor() {        return this.anchor;       }        /**     * Sets the anchor point.  This method is provided for the use of      * subclasses, not end users.     *      * @param anchor  the anchor point (<code>null</code> permitted).     */    protected void setAnchor(Point2D anchor) {        this.anchor = anchor;       }    /**     * Returns the chart contained in the panel.     *     * @return The chart (possibly <code>null</code>).     */    public JFreeChart getChart() {        return this.chart;    }    /**     * Sets the chart that is displayed in the panel.     *     * @param chart  the chart (<code>null</code> permitted).     */    public void setChart(JFreeChart chart) {        // stop listening for changes to the existing chart        if (this.chart != null) {            this.chart.removeChangeListener(this);            this.chart.removeProgressListener(this);        }        // add the new chart        this.chart = chart;        if (chart != null) {            this.chart.addChangeListener(this);            this.chart.addProgressListener(this);            Plot plot = chart.getPlot();            this.domainZoomable = false;            this.rangeZoomable = false;            if (plot instanceof Zoomable) {                Zoomable z = (Zoomable) plot;                this.domainZoomable = z.isDomainZoomable();                this.rangeZoomable = z.isRangeZoomable();                this.orientation = z.getOrientation();            }        }        else {            this.domainZoomable = false;            this.rangeZoomable = false;        }        if (this.useBuffer) {            this.refreshBuffer = true;        }    }    /**     * Returns the zoom in factor.     *      * @return The zoom in factor.     *      * @see #setZoomInFactor(double)     */    public double getZoomInFactor() {        return this.zoomInFactor;       }        /**     * Sets the zoom in factor.     *      * @param factor  the factor.     *      * @see #getZoomInFactor()     */    public void setZoomInFactor(double factor) {        this.zoomInFactor = factor;    }        /**     * Returns the zoom out factor.     *      * @return The zoom out factor.     *      * @see #setZoomOutFactor(double)     */    public double getZoomOutFactor() {        return this.zoomOutFactor;       }        /**     * Sets the zoom out factor.     *      * @param factor  the factor.     *      * @see #getZoomOutFactor()     */    public void setZoomOutFactor(double factor) {        this.zoomOutFactor = factor;    }        /**     * Displays a dialog that allows the user to edit the properties for the     * current chart.     */    private void attemptEditChartProperties() {        SWTChartEditor editor = new SWTChartEditor(canvas.getDisplay(), this.chart);            //ChartEditorManager.getChartEditor( canvas.getDisplay(), this.chart );        editor.open();    }    /**     * Returns <code>true</code> if file extensions should be enforced, and      * <code>false</code> otherwise.     *     * @return The flag.     */    public boolean isEnforceFileExtensions() {        return this.enforceFileExtensions;    }    /**     * Sets a flag that controls whether or not file extensions are enforced.     *     * @param enforce  the new flag value.     */    public void setEnforceFileExtensions(boolean enforce) {        this.enforceFileExtensions = enforce;    }    /**     * Opens a file chooser and gives the user an opportunity to save the chart     * in PNG format.     *     * @throws IOException if there is an I/O error.     */    public void doSaveAs() throws IOException {        FileDialog fileDialog = new FileDialog(canvas.getShell(), SWT.SAVE);        String[] extensions = { "*.png" };        fileDialog.setFilterExtensions(extensions);        String filename = fileDialog.open();        if (filename != null) {            if (isEnforceFileExtensions()) {                if (!filename.endsWith(".png")) {                    filename = filename + ".png";                }            }            //TODO replace getSize by getBounds ?            ChartUtilities.saveChartAsPNG(new File(filename), this.chart,                     canvas.getSize().x, canvas.getSize().y);        }    }    /**     * Returns a point based on (x, y) but constrained to be within the bounds     * of the given rectangle.  This method could be moved to JCommon.     *      * @param x  the x-coordinate.     * @param y  the y-coordinate.     * @param area  the rectangle (<code>null</code> not permitted).     *      * @return A point within the rectangle.     */    private org.eclipse.swt.graphics.Point getPointInRectangle(int x, int y, Rectangle area) {        x = (int) Math.max(area.x, Math.min(x, area.x + area.width));           y = (int) Math.max(area.y, Math.min(y, area.y + area.height));        return new org.eclipse.swt.graphics.Point(x, y);    }    /**     * 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

⌨️ 快捷键说明

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