chartutilities.java

来自「制作图表的好工具」· Java 代码 · 共 787 行 · 第 1/3 页

JAVA
787
字号
        if (scale) {
            AffineTransform saved = g2.getTransform();
            g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY));
            chart.draw(
                g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), 
                null, null
            );
            g2.setTransform(saved);
            g2.dispose();
        }
        else {
            chart.draw(
                g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight), 
                null, null
            );
        }
        out.write(encodeAsPNG(image));

    }

    /**
     * Saves a chart to the specified file in PNG format.
     *
     * @param file  the file name (<code>null</code> not permitted).
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void saveChartAsPNG(File file,
                                      JFreeChart chart,
                                      int width,
                                      int height) throws IOException {

        // defer argument checking...
        saveChartAsPNG(file, chart, width, height, null);

    }

    /**
     * Saves a chart to a file in PNG format.  This method allows you to pass 
     * in a {@link ChartRenderingInfo} object, to collect information about the 
     * chart dimensions/entities.  You will need this info if you want to 
     * create an HTML image map.
     *
     * @param file  the file (<code>null</code> not permitted).
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     * @param info  the chart rendering info (<code>null</code> permitted).
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void saveChartAsPNG(File file,
                                      JFreeChart chart,
                                      int width,
                                      int height,
                                      ChartRenderingInfo info) 
        throws IOException {

        if (file == null) {
            throw new IllegalArgumentException("Null 'file' argument.");
        }
        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        ChartUtilities.writeChartAsPNG(out, chart, width, height, info);
        out.close();
    }

    /**
     * Saves a chart to a file in PNG format.  This method allows you to pass 
     * in a {@link ChartRenderingInfo} object, to collect information about the 
     * chart dimensions/entities.  You will need this info if you want to 
     * create an HTML image map.
     *
     * @param file  the file (<code>null</code> not permitted).
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     * @param info  the chart rendering info (<code>null</code> permitted).
     * @param encodeAlpha  encode alpha?
     * @param compression  the PNG compression level (0-9).
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void saveChartAsPNG(File file,
                                      JFreeChart chart,
                                      int width,
                                      int height,
                                      ChartRenderingInfo info,
                                      boolean encodeAlpha,
                                      int compression) throws IOException {

        if (file == null) {
            throw new IllegalArgumentException("Null 'file' argument.");
        }
        if (chart == null) {
            throw new IllegalArgumentException("Null 'chart' argument.");
        }

        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        writeChartAsPNG(
            out, chart, width, height, info, encodeAlpha, compression
        );
        out.close();

    }

    /**
     * Writes a chart to an output stream in JPEG format.  Please note that
     * JPEG is a poor format for chart images, use PNG if possible.
     * 
     * @param out  the output stream (<code>null</code> not permitted).
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void writeChartAsJPEG(OutputStream out,
                                        JFreeChart chart,
                                        int width,
                                        int height) throws IOException {

        // defer argument checking...
        writeChartAsJPEG(out, chart, width, height, null);

    }

    /**
     * Writes a chart to an output stream in JPEG format.  Please note that
     * JPEG is a poor format for chart images, use PNG if possible.
     *
     * @param out  the output stream (<code>null</code> not permitted).
     * @param quality  the quality setting.
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void writeChartAsJPEG(OutputStream out,
                                        float quality,
                                        JFreeChart chart,
                                        int width,
                                        int height) throws IOException {

        // defer argument checking...
        ChartUtilities.writeChartAsJPEG(
            out, quality, chart, width, height, null
        );

    }

    /**
     * Writes a chart to an output stream in JPEG format. This method allows 
     * you to pass in a {@link ChartRenderingInfo} object, to collect 
     * information about the chart dimensions/entities.  You will need this 
     * info if you want to create an HTML image map.
     *
     * @param out  the output stream (<code>null</code> not permitted).
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     * @param info  the chart rendering info (<code>null</code> permitted).
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void writeChartAsJPEG(OutputStream out,
                                        JFreeChart chart,
                                        int width,
                                        int height,
                                        ChartRenderingInfo info) 
        throws IOException {

        if (chart == null) {
            throw new IllegalArgumentException("Null 'chart' argument.");
        }
        BufferedImage image = chart.createBufferedImage(width, height, info);
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);

    }

    /**
     * Writes a chart to an output stream in JPEG format.  This method allows 
     * you to pass in a {@link ChartRenderingInfo} object, to collect 
     * information about the chart dimensions/entities.  You will need this 
     * info if you want to create an HTML image map.
     *
     * @param out  the output stream (<code>null</code> not permitted).
     * @param quality  the output quality (0.0f to 1.0f).
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     * @param info  the chart rendering info (<code>null</code> permitted).
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void writeChartAsJPEG(OutputStream out,
                                        float quality,
                                        JFreeChart chart,
                                        int width,
                                        int height,
                                        ChartRenderingInfo info) 
        throws IOException {

        if (chart == null) {
            throw new IllegalArgumentException("Null 'chart' argument.");
        }
        BufferedImage image = chart.createBufferedImage(width, height, info);
        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality);

    }

    /**
     * Saves a chart to a file in JPEG format.
     *
     * @param file  the file (<code>null</code> not permitted).
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void saveChartAsJPEG(File file,
                                       JFreeChart chart,
                                       int width,
                                       int height) throws IOException {

        // defer argument checking...
        saveChartAsJPEG(file, chart, width, height, null);

    }

    /**
     * Saves a chart to a file in JPEG format.
     *
     * @param file  the file (<code>null</code> not permitted).
     * @param quality  the JPEG quality setting.
     * @param chart  the chart (<code>null</code> not permitted).
     * @param width  the image width.
     * @param height  the image height.
     *
     * @throws IOException if there are any I/O errors.
     */
    public static void saveChartAsJPEG(File file,
                                       float quality,
                                       JFreeChart chart,
                                       int width,
                                       int height) throws IOException {

        // defer argument checking...
        saveChartAsJPEG(file, quality, chart, width, height, null);

    }

    /**
     * Saves a chart to a file in JPEG format.  This method allows you to pass 
     * in a {@link ChartRenderingInfo} object, to collect information about the 
     * chart dimensions/entities.  You will need this info if you want to 
     * create an HTML image map.
     *
     * @param file  the file name (<code>null</code> not permitted).

⌨️ 快捷键说明

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