JAVA產生長條圖Image

來源:互聯網
上載者:User

本程式修改至http://blog.csdn.net/lazy_p/article/details/5400268 ,以前是用的applet來呈現效果的。修改為返回一張BufferedImage圖片,便於應用到項目中去,比如Web、C/S

架構的程式都能應用。

package com.image.histogram;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;/** * 柱狀圖 * * @author lazy_p * @date 2010-3-20 */public class PlaneHistogram {    private final int histogramWidth = 15;// 直條圖的寬度    private final int histogramPitch = 10;// 直條圖的間距    private float scaling = 1f;// 縮放的比例    private int maxStrWidth = 0; // 字串需要的最大寬度    /**     * <pre>     *   參數b[i]和str[i]必須對應     * </pre>     *     * @param g     * @param title     * @param v     * @param str     * @param color     *            可以為空白     */    public BufferedImage paintPlaneHistogram(String title, int[] v,            String[] str, Color[] color) {        int width = str.length * histogramWidth+str.length*histogramPitch+50;        int height = 255;        scaling = calculateScale(v, height);//計算縮放比例                BufferedImage bufferImage = new BufferedImage(width, height,                BufferedImage.TYPE_INT_RGB);        Graphics g = bufferImage.getGraphics();        g.setColor(Color.WHITE);        g.fillRect(0, 0, width, height);        FontMetrics metrics = null;        g.setFont(new Font(null, Font.BOLD, 18));        g.setColor(Color.RED);        g.drawString(title, (bufferImage.getWidth() - g.getFontMetrics()                .stringWidth(title)) >> 1, 30);// 畫標題        g.setFont(new Font(null, Font.PLAIN, 12));        metrics = g.getFontMetrics();        g.setColor(Color.BLACK);        g.drawLine(10, 0, 10, height - 15); // 畫Y座標        g.drawLine(10, height - 15, width, height - 15);// 畫X座標                int j = 0;        int colorCount=color.length;                for (int i = 0; i < v.length; ++i) {            if (color != null){                g.setColor(color[j]);// 設定前景色彩                if(j+1<colorCount){                    j++;                }else{                    j=0;                }            }else{                g.setColor(Color.RED);            }            int x = 20 + i                    * (histogramPitch + histogramWidth + (maxStrWidth >> 1));// 計算出X座標            int y = height - 16 - (int) (v[i] * scaling); // 計算出Y座標            // 畫占的比例            g.drawString(v[i] + "", x                    - ((metrics.stringWidth(v[i] + "") - histogramWidth) >> 1),                    y);            // 畫平面的柱狀圖            g.drawRect(x, y, histogramWidth, (int) (v[i] * scaling));            g.fillRect(x, y, histogramWidth, (int) (v[i] * scaling));            // 畫每一項表示的東西            g.drawString(str[i], x                    - ((metrics.stringWidth(str[i]) - histogramWidth) >> 1),                    height - 2);        }        return bufferImage;    }        /**     * 計算縮放比例     * @param v     * @param h 圖片的高度     * @return     */    public float calculateScale(int[] v , int h){        float scale = 1f;        int max = Integer.MIN_VALUE;        for(int i=0 , len=v.length ; i < len ;++i){            if(v[i]>h && v[i]>max){                max=v[i];            }        }        if(max > h){            scale=((int)(h*1.0f/max*1000))*1.0f/1000;        }        return scale;    }        public static void main(String[] args)  {        PlaneHistogram planeHistogram = new PlaneHistogram();        BufferedImage image = planeHistogram.paintPlaneHistogram("顏色長條圖",                        new int[]{100,200,300}, new String[]{"a" , "b" , "c"} ,  new Color[] {Color.RED, Color.GREEN, Color.BLACK, Color.BLUE });        File output = new File("G:/333.jpg");        try {            ImageIO.write(image, "jpg", output);        } catch (IOException e) {            e.printStackTrace();        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.