JAVA生成直方图Image

本程序修改至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();
        }
    }

}





  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值