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

📄 jfreechart.java

📁 jfreechart安装程序和使用说明
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     *
     * @return A boolean.
     */
    public boolean isNotify() {
        return this.notify;
    }

    /**
     * Sets a flag that controls whether or not listeners receive {@link ChartChangeEvent}
     * notifications.
     *
     * @param notify  a boolean.
     */
    public void setNotify(boolean notify) {
        this.notify = notify;
        // if the flag is being set to true, there may be queued up changes...
        if (notify) {
            notifyListeners(new ChartChangeEvent(this));
        }
    }

    /**
     * Draws the chart on a Java 2D graphics device (such as the screen or a
     * printer).
     * <P>
     * This method is the focus of the entire JFreeChart library.
     *
     * @param g2  the graphics device.
     * @param area  the area within which the chart should be drawn.
     */
    public void draw(Graphics2D g2, Rectangle2D area) {
        draw(g2, area, null, null);
    }

    /**
     * Draws the chart on a Java 2D graphics device (such as the screen or a
     * printer).
     * <P>
     * This method is the focus of the entire JFreeChart library.
     *
     * @param g2  the graphics device.
     * @param area  the area within which the chart should be drawn.
     * @param info  records info about the drawing (null means collect no info).
     */
    public void draw(Graphics2D g2, Rectangle2D area, ChartRenderingInfo info) {
        draw(g2, area, null, info);
    }
    
    /**
     * Draws the chart on a Java 2D graphics device (such as the screen or a
     * printer).
     * <P>
     * This method is the focus of the entire JFreeChart library.
     *
     * @param g2  the graphics device.
     * @param chartArea  the area within which the chart should be drawn.
     * @param anchor  the anchor point (in Java2D space) for the chart (<code>null</code> 
     *                permitted).
     * @param info  records info about the drawing (null means collect no info).
     */
    public void draw(Graphics2D g2, 
                     Rectangle2D chartArea, Point2D anchor, ChartRenderingInfo info) {

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Entering draw() method, chartArea = " + chartArea.toString());   
        }
        notifyListeners(new ChartProgressEvent(this, this, ChartProgressEvent.DRAWING_STARTED, 0));

        // record the chart area, if info is requested...
        if (info != null) {
            info.clear();
            info.setChartArea(chartArea);
        }

        // ensure no drawing occurs outside chart area...
        Shape savedClip = g2.getClip();
        g2.clip(chartArea);

        g2.addRenderingHints(this.renderingHints);

        // draw the chart background...
        if (this.backgroundPaint != null) {
            g2.setPaint(this.backgroundPaint);
            g2.fill(chartArea);
        }

        if (this.backgroundImage != null) {
            Composite originalComposite = g2.getComposite();
            g2.setComposite(
                AlphaComposite.getInstance(AlphaComposite.SRC_OVER, this.backgroundImageAlpha)
            );
            Rectangle2D dest = new Rectangle2D.Double(
                0.0, 0.0, this.backgroundImage.getWidth(null), this.backgroundImage.getHeight(null)
            );
            Align.align(dest, chartArea, this.backgroundImageAlignment);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Drawing background image into " + dest.toString());   
            }
            g2.drawImage(
                this.backgroundImage, (int) dest.getX(), (int) dest.getY(),
                (int) dest.getWidth(), (int) dest.getHeight(), null
            );
            g2.setComposite(originalComposite);
        }

        if (isBorderVisible()) {
            Paint paint = getBorderPaint();
            Stroke stroke = getBorderStroke();
            if (paint != null && stroke != null) {
                Rectangle2D borderArea = new Rectangle2D.Double(
                    chartArea.getX(), chartArea.getY(),
                    chartArea.getWidth() - 1.0, chartArea.getHeight() - 1.0
                );
                g2.setPaint(paint);
                g2.setStroke(stroke);
                g2.draw(borderArea);
            }
        }

        // draw the title and subtitles...
        Rectangle2D nonTitleArea = new Rectangle2D.Double();
        nonTitleArea.setRect(chartArea);

        if (this.title != null) {
            drawTitle(this.title, g2, nonTitleArea);
        }

        Iterator iterator = this.subtitles.iterator();
        while (iterator.hasNext()) {
            Title currentTitle = (Title) iterator.next();
            drawTitle(currentTitle, g2, nonTitleArea);
        }

        // draw the legend - the draw method will return the remaining area
        // after the legend steals a chunk of the non-title area for itself
        Rectangle2D plotArea = nonTitleArea;
        if (this.legend != null) {
            plotArea.setRect(this.legend.draw(g2, nonTitleArea, info));
        }

        // draw the plot (axes and data visualisation)
        PlotRenderingInfo plotInfo = null;
        if (info != null) {
            plotInfo = info.getPlotInfo();
        }
        this.plot.draw(g2, plotArea, anchor, null, plotInfo);

        g2.setClip(savedClip);

        notifyListeners(
            new ChartProgressEvent(this, this, ChartProgressEvent.DRAWING_FINISHED, 100)
        );

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Leaving draw() method");   
        }
    }

    /**
     * Draws a title.  The title should be drawn at the top, bottom, left or right of the 
     * specified area, and the area should be updated to reflect the amount of space used by 
     * the title.
     *
     * @param title  the title (<code>null</code> not permitted).
     * @param g2  the graphics device (<code>null</code> not permitted).
     * @param area  the chart area, excluding any existing titles (<code>null</code> not permitted).
     */
    public void drawTitle(Title title, Graphics2D g2, Rectangle2D area) {

        if (title == null) {
            throw new IllegalArgumentException("Null 'title' argument.");   
        }
        if (area == null) {
            throw new IllegalArgumentException("Null 'area' argument.");   
        }
        Rectangle2D titleArea = new Rectangle2D.Double();
        double availableHeight = 0.0;
        double availableWidth = 0.0;
        
        RectangleEdge position = title.getPosition();
        if (position == RectangleEdge.TOP) {
            availableWidth = area.getWidth();
            availableHeight = Math.min(
                title.getPreferredHeight(g2, (float) availableWidth), area.getHeight()
            );
            titleArea.setRect(area.getX(), area.getY(), availableWidth, availableHeight);
            title.draw(g2, titleArea);
            area.setRect(
                area.getX(), Math.min(area.getY() + availableHeight, area.getMaxY()),
                availableWidth, Math.max(area.getHeight() - availableHeight, 0)
            );
        }
        else if (position == RectangleEdge.BOTTOM) {
            availableWidth = area.getWidth();
            availableHeight = Math.min(
                title.getPreferredHeight(g2, (float) availableWidth), area.getHeight()
            );
            titleArea.setRect(
                area.getX(), area.getMaxY() - availableHeight, availableWidth, availableHeight
            );
            title.draw(g2, titleArea);
            area.setRect(
                area.getX(), area.getY(), availableWidth, area.getHeight() - availableHeight
            );
        }
        else if (position == RectangleEdge.RIGHT) {
            availableHeight = area.getHeight();
            availableWidth = Math.min(
                title.getPreferredWidth(g2, (float) availableHeight), area.getWidth()
            );
            titleArea.setRect(
                area.getMaxX() - availableWidth, area.getY(), availableWidth, availableHeight
            );
            title.draw(g2, titleArea);
            area.setRect(
                area.getX(), area.getY(), area.getWidth() - availableWidth, availableHeight
            );
        }

        else if (position == RectangleEdge.LEFT) {
            availableHeight = area.getHeight();
            availableWidth = Math.min(
                title.getPreferredWidth(g2, (float) availableHeight), area.getWidth()
            );
            titleArea.setRect(area.getX(), area.getY(), availableWidth, availableHeight);
            title.draw(g2, titleArea);
            area.setRect(
                area.getX() + availableWidth, area.getY(),
                area.getWidth() - availableWidth, availableHeight
            );
        }
        else {
            throw new RuntimeException("JFreeChart.drawTitle(...): unknown title position.");
        }
        
    }

    /**
     * Creates and returns a buffered image into which the chart has been drawn.
     *
     * @param width  the width.
     * @param height  the height.
     *
     * @return a buffered image.
     */
    public BufferedImage createBufferedImage(int width, int height) {

        return createBufferedImage(width, height, null);

    }

    /**
     * Creates and returns a buffered image into which the chart has been drawn.
     *
     * @param width  the width.
     * @param height  the height.
     * @param info  carries back chart state information (<code>null</code> permitted).
     *
     * @return a buffered image.
     */
    public BufferedImage createBufferedImage(int width, int height, ChartRenderingInfo info) {
        return createBufferedImage(width, height, BufferedImage.TYPE_INT_RGB, info);
    }

    /**
     * Creates and returns a buffered image into which the chart has been drawn.
     *
     * @param width  the width.
     * @param height  the height.
     * @param imageType  the image type.
     * @param info  carries back chart state information (<code>null</code> permitted).
     *
     * @return a buffered image.
     */
    public BufferedImage createBufferedImage(int width, int height, int imageType, 
                                             ChartRenderingInfo info) {
        BufferedImage image = new BufferedImage(width, height, imageType);
        Graphics2D g2 = image.createGraphics();
        draw(g2, new Rectangle2D.Double(0, 0, width, height), null, info);
        g2.dispose();
        return image;
    }

    /**
     * Creates and returns a buffered image into which the chart has been drawn.
     *
     * @param imageWidth  the image width.
     * @param imageHeight  the image height.
     * @param drawWidth  the width for drawing the chart (will be scaled to fit image).
     * @param drawHeight  the height for drawing the chart (will be scaled to fit image).
     * @param info  optional object for collection chart dimension and entity information.
     *
     * @return a buffered image.
     */
    public BufferedImage createBufferedImage(int imageWidth, int imageHeight,
                                             double drawWidth, double drawHeight, 
                                             ChartRenderingInfo info) {

        BufferedImage image = new BufferedImage(
            imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB
        );
        Graphics2D g2 = image.createGraphics();
        double scaleX = imageWidth / drawWidth;
        double scaleY = imageHeight / drawHeight;
        AffineTransform st = AffineTransform.getScaleInstance(scaleX, scaleY);
        g2.transform(st);
        draw(g2, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), null, info);
        g2.dispose();
        return image;

    }

    /**
     * Handles a 'click' on the chart.
     * <P>
     * JFreeChart is not a UI component, so some other object (e.g. ChartPanel)
     * needs to capture the click event and pass it onto the JFreeChart object.
     * If you are not using JFreeChart in a client application, then this
     * method is not required (and hopefully it doesn't get in the way).
     *
     * @param x  x-coordinate of the click (in Java2D space).
     * @param y  y-coordinate of the click (in Java2D space).
     * @param info  contains chart dimension and entity information.
     */
    public void handleClick(int x, int y, ChartRenderingInfo info) {

        // pass the click on to the plot...
        // rely on the plot to post a plot change event and redraw the chart...
        this.plot.handleClick(x, y, info.getPlotInfo());

    }

    /**
     * Registers an object for notification of changes to the chart.
     *
     * @param listener  the listener (<code>null</code> not permitted).
     */
    public void addChangeListener(ChartChangeListener listener) {
        if (listener == null) {
            throw new IllegalArgumentException("Null 'listener' argument.");
        }
        this.changeListeners.add(ChartChangeListener.class, listener);
    }

    /**
     * Deregisters an object for notification of changes to the chart.
     *
     * @param listener  the listener (<code>null</code> not permitted)
     */
    public void removeChangeListener(ChartChangeListener listener) {
        if (listener == null) {
            throw new IllegalArgumentException("Null 'listener' argument.");
        }
        this.changeListeners.remove(ChartChangeListener.class, listener);
    }

    /**
     * Sends a default {@link ChartChangeEvent} to all registered listeners.
     * <P>
     * This method is for convenience only.
     */
    public void fireChartChanged() {
        ChartChangeEvent event = new ChartChangeEvent(this);
        notifyListeners(event);
    }

    /**
     * Sends a {@link ChartChangeEvent} to all registered listeners.
     *
     * @param event  information about the event that triggered the notification.
     */
    protected void notifyListeners(ChartChangeEvent event) {

        if (this.notify) {
            Object[] listeners = this.changeListeners.getListenerList();
            for (int i = listeners.length - 2; i >= 0; i -= 2) {
                if (listeners[i] == ChartChangeListener.class) {
                    ((ChartChangeListener) listeners[i + 1]).chartChanged(event);
                }

⌨️ 快捷键说明

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