package com.bstd.sys.server;
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.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import javax.imageio.ImageIO;
public class emp {
private final int histogramWidth = 60;// 柱形图的宽度
private final int histogramPitch = 60;// 柱形图的间距
private float scaling = 1f;// 缩放的比例
private int maxStrWidth = 0; // 字符串需要的最大宽度
private static Font mFont = new Font("微软雅黑体", Font.BOLD , 24);
/**
* <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 width=500;
int height = 500;
scaling = calculateScale(v, height);//计算缩放比例
BufferedImage bufferImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = bufferImage.getGraphics();
// g.setColor(Color.WHITE);
g.setColor(new Color(240,255,240));// 设置背景色
g.fillRect(0, 0, width, height);
g.fillRect(1, 1, width - 1, height - 1);
FontMetrics metrics = null;
g.setFont(mFont);
g.setColor(new Color(100,149,237)); // 设置标题颜色
g.drawString(title, (bufferImage.getWidth() - g.getFontMetrics()
.stringWidth(title)) >> 1, 30);// 画标题
g.setFont(mFont);
metrics = g.getFontMetrics();
g.setColor(new Color(100,149,237));
g.drawLine(10, 0, 10, height - 30); // 画Y坐标
g.drawLine(10, height - 30, width, height - 30);// 画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 - 30 - (int) (v[i] * scaling); // 计算出Y坐标
// 画占的比例
g.drawString(v[i] + "口", x
- ((metrics.stringWidth(v[i] + "") - histogramWidth) >> 10),
y-10); // 这是数值与图之间的空隙。
// 画平面的柱状图
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) {
emp planeHistogram = new emp();
Color[] colorArr=new Color[] {new Color(132,112,255),new Color(100,149,237),new Color(255,222,173),new Color(127,255,0) };
BufferedImage image = planeHistogram.paintPlaneHistogram("颜色直方图",
new int[]{100,200,300,400}, new String[]{"评价井" , "预探井" , "生产井","其他"} , colorArr);
// paintPlaneHistogram(String title, int[] v, String[] str, Color[] color)
// int width = str.length * histogramWidth+str.length*histogramPitch+50;
File output = new File("E:\3331.png");
try {
ImageIO.write(image, "png", output);
} catch (IOException e) {
e.printStackTrace();
}
}
}