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

📄 jfreechart.java

📁 Web图形化的Java库
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * @param alignment  the alignment.
     */
    public void setBackgroundImageAlignment(int alignment) {
        if (this.backgroundImageAlignment != alignment) {
            this.backgroundImageAlignment = alignment;
            fireChartChanged();
        }
    }

    /**
     * Returns the alpha-transparency for the chart's background image.
     *
     * @return the alpha-transparency.
     */
    public float getBackgroundImageAlpha() {

        return this.backgroundImageAlpha;

    }

    /**
     * Sets the alpha-transparency for the chart's background image.
     * Registered listeners are notified that the chart has been changed.
     *
     * @param alpha  the alpha value.
     */
    public void setBackgroundImageAlpha(float alpha) {

        if (this.backgroundImageAlpha != alpha) {
            this.backgroundImageAlpha = alpha;
            fireChartChanged();
        }

    }

    /**
     * Returns a flag that controls whether or not change events are sent to registered listeners.
     *
     * @return <code>true</code> or <code>false</code>.
     */
    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);
    }

    /**
     * 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 info  records info about the drawing (null means collect no info).
     */
    public void draw(Graphics2D g2, Rectangle2D chartArea, ChartRenderingInfo info) {

        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);

        // set anti-alias...
        if (antialias) {
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
        }
        else {
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_OFF);
        }
        
        // draw the chart background...
        if (backgroundPaint != null) {
            g2.setPaint(backgroundPaint);
            g2.fill(chartArea);
        }

        if (backgroundImage != null) {
            Composite originalComposite = g2.getComposite();
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                                                       this.backgroundImageAlpha));
            Rectangle2D dest = new Rectangle2D.Double(0.0, 0.0,
                                                      backgroundImage.getWidth(null),
                                                      backgroundImage.getHeight(null));
            Align.align(dest, chartArea, this.backgroundImageAlignment);
            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()) {
            AbstractTitle currentTitle = (AbstractTitle) 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 (legend != null) {
            plotArea.setRect(legend.draw(g2, nonTitleArea, info));
        }

        // draw the plot (axes and data visualisation)
        plot.draw(g2, plotArea, info);

        g2.setClip(savedClip);

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

    }

    /**
     * Draws a title.
     * <P>
     * The title should be drawn at the top, bottom, left or right of the nonTitleArea, and
     * the area should be updated to reflect the amount of space used by the title.
     *
     * @param title  the title.
     * @param g2  the graphics device.
     * @param nonTitleArea  the area.
     */
    public void drawTitle(AbstractTitle title, Graphics2D g2, Rectangle2D nonTitleArea) {

        Rectangle2D titleArea = new Rectangle2D.Double();
        double availableHeight = 0.0;
        double availableWidth = 0.0;
        switch (title.getPosition()) {

            case AbstractTitle.TOP : 
                availableHeight = Math.min(title.getPreferredHeight(g2), nonTitleArea.getHeight());
                availableWidth = nonTitleArea.getWidth();
                titleArea.setRect(nonTitleArea.getX(), nonTitleArea.getY(),
                                  availableWidth, availableHeight);
                title.draw(g2, titleArea);
                nonTitleArea.setRect(nonTitleArea.getX(),
                    Math.min(nonTitleArea.getY() + availableHeight,
                             nonTitleArea.getMaxY()),
                             availableWidth,
                             Math.max(nonTitleArea.getHeight() - availableHeight, 0));
                break;

            case AbstractTitle.BOTTOM : 
                availableHeight = Math.min(title.getPreferredHeight(g2), nonTitleArea.getHeight());
                availableWidth = nonTitleArea.getWidth();
                titleArea.setRect(nonTitleArea.getX(),
                                  nonTitleArea.getMaxY() - availableHeight,
                                  availableWidth, availableHeight);
                title.draw(g2, titleArea);
                nonTitleArea.setRect(nonTitleArea.getX(), nonTitleArea.getY(),
                                     availableWidth,
                                     nonTitleArea.getHeight() - availableHeight);
                break;

            case AbstractTitle.RIGHT : 
                availableHeight = nonTitleArea.getHeight();
                availableWidth = Math.min(title.getPreferredWidth(g2), nonTitleArea.getWidth());
                titleArea.setRect(nonTitleArea.getMaxX() - availableWidth,
                                  nonTitleArea.getY(), availableWidth, availableHeight);
                title.draw(g2, titleArea);
                nonTitleArea.setRect(nonTitleArea.getX(), nonTitleArea.getY(),
                                     nonTitleArea.getWidth() - availableWidth,
                                     availableHeight);
                break;

            case AbstractTitle.LEFT : 
                availableHeight = nonTitleArea.getHeight();
                availableWidth = Math.min(title.getPreferredWidth(g2), nonTitleArea.getWidth());
                titleArea.setRect(nonTitleArea.getX(), nonTitleArea.getY(),
                                  availableWidth, availableHeight);
                title.draw(g2, titleArea);
                nonTitleArea.setRect(nonTitleArea.getX() + availableWidth,
                                     nonTitleArea.getY(),
                                     nonTitleArea.getWidth() - availableWidth,
                                     availableHeight);
                break;

            default :
                throw new RuntimeException("JFreeChart.draw(...): 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  optional object for collection chart dimension and entity information.
     *
     * @return a buffered image.
     */
    public BufferedImage createBufferedImage(int width, int height, ChartRenderingInfo info) {

        BufferedImage image = new BufferedImage(width , height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        draw(g2, new Rectangle2D.Double(0, 0, width, height), 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.
     * @param y  y-coordinate of the click.
     * @param info  optional object for collection 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);

    }

    /**
     * Registers an object for notification of changes to the chart.
     *
     * @param listener  the object being registered.
     */
    public void addChangeListener(ChartChangeListener listener) {
        this.changeListeners.add(ChartChangeListener.class, listener);
    }

    /**
     * Deregisters an object for notification of changes to the chart.
     *
     * @param listener  the object being deregistered.
     */
    public void removeChangeListener(ChartChangeListener listener) {
        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.

⌨️ 快捷键说明

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