用java生成柱状图

3 篇文章 0 订阅
2 篇文章 0 订阅
package com.test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

public class CreateImage {
	private static Font mFont = new Font("宋体", Font.PLAIN, 12);
	
	public static void main(String[] args){
		double[] value = {0.57d,1.25d,0.83d,1.38d,2.35d};
		createimage(value,2.35d);
	}
	
	private static void createimage(double[] value,double maxvalue){
		try{
			int ranwidth = 33,ranheight = 112;
			int width = 764, height = 168;
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics g = image.getGraphics();
			g.setColor(new Color(255, 255,255));
			g.fillRect(1, 1, width - 1, height - 1);
			g.setColor(new Color(255, 255,255));
			g.drawRect(0, 0, width - 1, height - 1);
			
			g.setFont(mFont);
			g.setColor(new Color(255, 26,135));
			
			Color c = new Color(227, 227, 227);
			for(int i= 0 ;i<5;i++){
				if(i==0){
					g.setColor(c);
					g.fillRect(72, 54, 33, ranheight);
					g.setColor(c);
					g.drawRect(72, 54, 33, ranheight);
				}else{
					g.setColor(c);
					g.fillRect(72+(ranwidth+110)*i, 54, 33, ranheight);
					g.setColor(c);
					g.drawRect(72+(ranwidth+110)*i, 54, 33, ranheight);
				}
			}
			
			for(int i= 0 ;i<value.length;i++){
				Color myc = null;
				if(i == 0){
					myc = new Color(75,178,209);
				}else if(i==1){
					myc = new Color(243,152,1);
				}else if(i == 2){
					myc = new Color(90,159,105);
				}else if(i == 3){
					myc = new Color(144,130,189);
				}else{
					myc = new Color(154,190,66);
				}
				if(i==0){
					g.setColor(myc);
					g.fillRect(72, 54+(ranheight-(int)Math.round(ranheight*(value[i]/maxvalue))), 33, (int)Math.round(ranheight*(value[i]/maxvalue)));
					g.setColor(myc);
					g.fillRect(72, 54+(ranheight-(int)Math.round(ranheight*(value[i]/maxvalue))), 33, (int)Math.round(ranheight*(value[i]/maxvalue)));
				}else{
					g.setColor(myc);
					g.fillRect(72+(ranwidth+110)*i, 54+(ranheight-(int)Math.round(ranheight*(value[i]/maxvalue))), 33, (int)Math.round(ranheight*(value[i]/maxvalue)));
					g.setColor(myc);
					g.drawRect(72+(ranwidth+110)*i, 54+(ranheight-(int)Math.round(ranheight*(value[i]/maxvalue))), 33, (int)Math.round(ranheight*(value[i]/maxvalue)));
				}
			}
			
			c = new Color(181, 181, 181);
			for(int i = 0;i<5;i++){
				if(i==0){
					g.setColor(c);
					g.setFont(mFont);
					g.drawString(value[i]+"s", 72,48);
				}else{
					g.setColor(c);
					g.setFont(mFont);
					g.drawString(value[i]+"s", 72+(ranwidth+110)*i,48);
				}
			}
			
			g.dispose();
			String filename = "d:\\createimg.jpg";
			File file = new File(filename);
			if(!file.exists()){
				file.createNewFile();
			}
			FileOutputStream s = new FileOutputStream(file);
			ImageIO.write(image, "JPEG", s);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值