package com.dao;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DrawPicture {
private String pictureTitle = "无标题"; //图片标题
private int pictureWidth = 460; //图片宽度
private int pictureHeight = 320; //图片高度
private String[] xTitle = {"x1", "x2", "x3", "x4", "x5", "x6"}; //x轴标题
private String[] yTitle = {"y1", "y2", "y3", "y4", "y5", "y6"}; //y轴标题
private String valueOrPercent = "percent";
private double[] data; //绘图数据
public DrawPicture() {
}
public void draw(HttpServletResponse response) {
double percent = pictureWidth / 460.0; //绘图百分比
// 清空缓冲区
response.reset();
// 参数image的意思是设置返回客户端的响应数据类型为图象,参数pictureType为图片格式
response.setContentType("image/png");
// 创建一个指定大小的图像
BufferedImage image = new BufferedImage(pictureWidth, pictureHeight,BufferedImage.TYPE_INT_RGB);
// 创建Java2D对象,Java2D即对二维图表的支持
Graphics2D g2d = image.createGraphics();
//绘制图片背景
g2d.setPaint(Color.WHITE); //设置颜色
g2d.fillRect(0, 0, pictureWidth, pictureHeight); //参数含义(x,y,width,height)
//绘制图框
g2d.setPaint(Color.ORANGE);
int roundSize = (int) (40 * percent);
g2d.fillRoundRect(0, 0, pictureWidth, pictureHeight, roundSize,
roundSize);
//绘制绘图区
g2d.setPaint(Color.WHITE);
int a = (int) (30 * percent);
int b = (int) (50 * percent);
g2d.fillRect(a, b, pictureWidth - a * 2, pictureHeight - (a + b));
//绘制坐标轴
g2d.setPaint(Color.BLACK);
g2d.drawLine(a + (int) (40 * percent), pictureHeight - a * 2,
pictureWidth - (a + 10), pictureHeight - a * 2); //x
g2d.drawLine(a + (int) (40 * percent), b + 10, a + (int) (40 * percent),
pictureHeight - a * 2); //y
// //绘制坐标原点
// g2d.setColor(Color.BLACK);
// g2d.setFont(new Font("华文隶书", Font.BOLD, 10)); //设置字体及大小
// g2d.drawString("0", a + (int) (40 * percent) - (int) (20 * percent),
// pictureHeight - a * 2 + (int) (16 * percent)); //设置输出内容及输出位置
// 创建虚线笔划,设置线条颜色
float[] dashes = {3.f};
BasicStroke bs = new BasicStroke(1.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 10, dashes, 0);
g2d.setStroke(bs);
g2d.setPaint(Color.BLACK);
//绘制标准线
int y = pictureHeight - a * 2;
//前面加的1是为了弥补取整时的误差用的
int ySpacing = 1 + ((pictureHeight - a * 2) - (a + (int) (40 * percent)) -
(int) (10 * percent)) / yTitle.length;
for (int i = 0; i < yTitle.length; i++) {
y = y - ySpacing;
g2d.drawLine(a + (int) (40 * percent), y,
pictureWidth - (a + 10), y);
}
//绘制坐标轴文字
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("华文隶书", Font.BOLD, 10));
//x轴
int xTitleLengthAll = 0;
for (int i = 0; i < xTitle.length; i++) {
xTitleLengthAll = xTitleLengthAll + xTitle[i].length();
}
int xSpacing = ((pictureWidth - (a + 10)) - (a + (int) (40 * percent)) -
(int) (10 * percent) - (xTitleLengthAll * 11)) /
(xTitle.length * 2);
int x = a + (int) (40 * percent);
for (int i = 0; i < xTitle.length; i++) {
x = x + xSpacing;
g2d.drawString(xTitle[i], x,
pictureHeight - a * 2 + (int) (16 * percent));
x = x + xTitle[i].length() * 11 + xSpacing;
}
//y轴
y = pictureHeight - a * 2 + 5;
int yTitleLengthMax = 0;
for (int i = 0; i < yTitle.length; i++) {
if (yTitle[i].length() > yTitleLengthMax) {
yTitleLengthMax = yTitle[i].length();
}
}
for (int i = 0; i < yTitle.length; i++) {
y = y - ySpacing;
g2d.drawString(yTitle[i],
a + (int) (40 * percent) - yTitleLengthMax * 6 - 2, //注意:如果Y轴标题包含汉字,则乘11,否则乘6
y);
}
//定义标题
g2d.setColor(Color.RED);
g2d.setFont(new Font("宋体", Font.BOLD, 16));
//确定标题开始输出的位置,确保居中显示
int outputTitleInX = 0;
int titleLength = pictureTitle.length();
if (titleLength % 2 == 0) {
outputTitleInX = (pictureWidth - titleLength * 17) / 2;
} else {
outputTitleInX = (pictureWidth - titleLength * 17) / 2 + 2;
}
g2d.drawString(pictureTitle, outputTitleInX, (int) (36 * percent));
//绘制柱状图
//判断数据类型
int[] histogramHigh = new int[data.length];
if (valueOrPercent.equals("percent")) {
//求百分比
double dataSum = 0;
for (int i = 0; i < data.length; i++) {
dataSum = dataSum + data[i];
}
for (int i = 0; i < data.length; i++) {
histogramHigh[i] = (int) (data[i] *
((pictureHeight - a * 2) -
(a + (int) (40 * percent)) -
(int) (10 * percent)) / dataSum);
}
} else {
//求值
int yTitleMin = Integer.parseInt(yTitle[0]);
int yUnit = Integer.parseInt(yTitle[1]) - yTitleMin;
for (int i = 0; i < data.length; i++) {
if (data[i] > yTitleMin) {
histogramHigh[i] = (int) (data[i] - yTitleMin + yUnit) *
ySpacing / yUnit;
} else {
histogramHigh[i] = (int) data[i] * ySpacing / yTitleMin;
}
}
}
//绘制柱状图
g2d.setPaint(Color.GREEN);
int xMiddle = 0;
int xAll = 0;
for (int i = 0; i < histogramHigh.length; i++) {
xMiddle = xAll + xSpacing + xTitle[i].length() * 11 / 2;
xAll = xAll + xSpacing * 2 + xTitle[i].length() * 11;
g2d.fillRect(a + (int) (40 * percent) + xMiddle - 6,
pictureHeight - a * 2 - histogramHigh[i], 12,
histogramHigh[i]);
}
// 部署图形
g2d.dispose();
// 利用ImageIO类的write方法对图像进行编码,生成png格式的图象
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
} catch (IOException ex) {
}
}
public void setPictureTitle(String pictureTitle) {
this.pictureTitle = pictureTitle;
}
public void setPictureWidth(int pictureWidth) {
this.pictureWidth = pictureWidth;
}
public void setPictureHeight(int pictureHeight) {
this.pictureHeight = pictureHeight;
}
public void setXTitle(String[] xTitle) {
this.xTitle = xTitle;
}
public void setYTitle(String[] yTitle) {
this.yTitle = yTitle;
}
public void setData(double[] data) {
this.data = data;
}
public void setValueOrPercent(String valueOrPercent) {
this.valueOrPercent = valueOrPercent;
}
}