java awt 打印柱状图、线图

很凌乱,简单记录,有空整理下




import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;

import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Hashtable;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class Test{
// 保存为文件名:设置
private String fileName = buildFileName("c:\");

private String PillarNum="1";
private int pillarnum=0;
private String pillarflag="1";//柱形图(1)或者线图
// 主要标题
private String topTitle = "主要标题";
// No set get Method.
private Color topTitleColor = Color.gray;
private Font topTitleFont = new Font("宋体", Font.BOLD, 20);
//标题位置
private int topTitleStart = 25;


// 左边标题
private String leftTitle = "左边标题";

private Color leftTitleColor = Color.red;
private Font leftTitleFont = new Font("宋体", Font.PLAIN, 14);
//标题位置
private int leftTitleStart = 25;
//数字长度
private int leftlength=2;

// 底部标题
private String bottomTitle = "底部标题";
private Color bottomTitleColor = Color.red;
private Font bottomTitleFont = new Font("宋体", Font.PLAIN, 14);
//标题位置
private int bottomTitleStart = 10;
//标题长度
private int bottomlength=2;

// 右边标题
private String[] PillarInfo={"","","","","","","","","","","","","","","","","","","","","","","","","","",};
private Color rightTitleColor = Color.black;
private Font rightTitleFont = new Font("宋体", Font.PLAIN, 14);
//标题位置
private int rightTitleStart = 10;
//标题长度
private int rightlength=2;

// 设置标识字体大小:设置
private Font drawLineFont = new Font("宋体", Font.PLAIN, 2); // 虚线字体
private Font rectHeadFont = new Font("宋体", Font.PLAIN, 12);// 柱头字体
private Font rowScaleFont = new Font("宋体", Font.PLAIN, 12);// 横坐标的字体
private Font colScaleFont = new Font("宋体", Font.PLAIN, 12);// 纵坐标字体

// 上面字体相对应的颜色:设置
private Color drawLineFontColor = Color.lightGray;

private Color rowScaleFontColor = Color.black;
private Color colScaleFontColor = Color.darkGray;
//private Color rectHeadFontColor = Color.black;
private Color[] rectHeadFontColor ={Color.black,Color.decode("#FF0000"),Color.decode("#0000FF"),Color.decode("#77CC50"),Color.decode("#FF6600"),Color.decode("#00FF00"),Color.decode("#cc99FF"),Color.decode("#000080")};
// 设置柱状图柱宽=颜色:#99CC00:设置
//private Color fillRectColor = Color.decode("#99CC00");// new
//设置柱状图柱宽=颜色:#99CC00:设置
//#99cc00,#FF0000,#0000FF,#77CC50,#FF6600,#00FF00,#cc99FF,#000080
private Color[] fillRectColor ={Color.decode("#99cc00"),Color.decode("#FF0000"),Color.decode("#0000FF"),Color.decode("#77CC50"),Color.decode("#FF6600"),Color.decode("#00FF00"),Color.decode("#cc99FF"),Color.decode("#000080")};
// Color(153,204,0);//Color.decode("#00C600");
// //Color.decode("#0099FF");
private Color frameRectColor = Color.GRAY; // 内框颜色

// 画布背景头颜色=默认白色:设置(大框的背景颜色)
private Color canvasBackgroundColor = Color.decode("#efefef"); // Color.white;//

// 设置画图区域边界:刻度:设置

private int topVerge = 20;// 上框距离
private int addtoptitleVerge=0;//如果有上标题,加上上标题的高度
private int bottomVerge = 0; // 下框距离
private int addbottomtitleVerge=0;//如果有下标题,加上下标题的高度
private int leftVerge = 0;//左边宽度
private int addlefttitleVerge=0;//如果有左标题,加上左标题的宽度
private int addrighttitleVerge=0;//如果有右标题,加上右标题的宽度
private int rightVerge = 20;//右边固定20
private int leftmargin=10;//左边空白
private int bottommargin=10;//下边空白
// 数据变量:计算
private Hashtable hb = null;

// 画图数据
private String dataList = null;

// 参数是否有效,决定是否要画出图形:计算
private boolean bPaint = true;
private String gradual="0";
// 画布大小=默认:宽:800;高:300:设置(大框的长度和高度)
private int canvasWidth = 800;
private int canvasHeight = 300;

// 画布刻度=默认:行:11;列:31:设置或计算(刻度数量)
private int rowCount = 10;
private int colCount = 200;


// 柱体伸出部分,柱脚部分高
private int rectFootLenght = 0;

// 柱状标头字串上移偏转量:px:设置
private int rectHeadFontOffset = 2;

// 画线的间隔:px[如果太小,则图体积大]:设置
private int drawLineInterval = 4;

// 放大比率:放大100倍[暂没有用]:设置用
private int zoomInRate = 100;

// 柱状图柱宽=单位:px:设置或计算
private int rectWidth = 16;


// 画区域长宽:计算
private int paintWidth = 0;
private int paintHeight = 0;

// 画线间隔:列间隔;行间隔:计算,初始化在构造函数中colinterval和柱子数量有关系
private int colInterval = 20*pillarnum;
private int rowInterval = 20;

public void paint(BufferedImage bi) {
if (bi == null){
bPaint = false;
}
if (bPaint) {
// 内存建图区域:生成画布
// BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight,
// BufferedImage.TYPE_INT_BGR);
Graphics g = bi.createGraphics();
// 设置画布底色
g.setColor(canvasBackgroundColor);
g.fillRect(0, 0, canvasWidth, canvasHeight);
//打印top
printTop(g);
//打印left
printLeft(g);
//打印主题数据
printMain(g);
//打印bottom
printBottom(g);
//打印right
if(pillarnum>1){
printRight(g);
}
// 清除画布
g.dispose();

} else {//错误提示的时候执行
if (bi == null){
bi = new BufferedImage(canvasWidth, canvasHeight,BufferedImage.TYPE_INT_BGR);
}
Graphics g = bi.createGraphics();
// 设置画布底色
g.setColor(canvasBackgroundColor);
g.fillRect(0,0,canvasWidth,canvasHeight);
g.setColor(Color.RED);
g.setFont(new Font("宋体", Font.BOLD, 20));
g.drawString("没有数据,图形不输出!",canvasWidth/2-11*10,canvasHeight/2+13-10);
// 清除画布
g.dispose();
}
}
public void printTop(Graphics g) {
// 打印头标题
int mainTitleLeftStart = canvasWidth / 2 - topTitle.length()* topTitleFont.getSize()* 10 / 23;
g.setColor(topTitleColor);
g.setFont(topTitleFont);
g.drawString(topTitle, mainTitleLeftStart, topTitleStart);
}
public void printLeft(Graphics g) {
//左标题居中显示,如果字数过多就会显示不全那么就从最顶部开始打印,最顶部是13
if(canvasHeight<=(leftTitle.length()*20+13)){
leftTitleStart=13;
}else{
leftTitleStart =canvasHeight/2 - leftTitle.length()*10+13;
}
// 打左标题
g.setColor(leftTitleColor);
g.setFont(leftTitleFont);

drawString(g,leftTitle,(leftmargin+addlefttitleVerge)/2-10,leftTitleStart,20);
}
public void printRight(Graphics g) {


rightTitleStart=canvasWidth-rightVerge-addrighttitleVerge+5;
for(int i=0;i<pillarnum;i++){
if(pillarflag.equalsIgnoreCase("1")){
//打右矩形gradual为1就渐变
if(gradual.equalsIgnoreCase("1")){
paintRect(g,rightTitleStart,canvasHeight/2+ (i-pillarnum)*17+13, rectWidth - 2,rectWidth - 2,fillRectColor[i]);
}else{
g.setColor(fillRectColor[i]);
g.fillRect(rightTitleStart,canvasHeight/2+ (i-pillarnum)*17+13, rectWidth - 2,rectWidth - 2);
}
}else{
g.setColor(fillRectColor[i]);
g.drawOval(rightTitleStart-2,canvasHeight/2+ (i-pillarnum)*17+11, 5, 5);
g.fillOval(rightTitleStart-2,canvasHeight/2+ (i-pillarnum)*17+11, 5, 5);
}
// 打右标题
g.setColor(rightTitleColor);
g.setFont(rightTitleFont);

g.drawString(PillarInfo[i],rightTitleStart+rectWidth,canvasHeight/2+ (i-pillarnum)*17+11+rectWidth);
}
}
public void printMain(Graphics g) {
double val_max = toDbl(hb.get("val_max"));
int rowScaleMax = round(val_max, 1);
int rowScale = rowScaleMax / rowCount;
boolean bCalc = (rowScale * rowCount == rowScaleMax);

// 输出竖标
String[] col_arr = (String[]) hb.get("col_arr");

double[][] val_arr = (double[][]) hb.get("val_arr");
// 画列标度:主要是整数
g.setFont(rowScaleFont);
g.setColor(rowScaleFontColor);
for (int i = 1; i < rowCount + 1; i++) {
g.drawString(fmtNum("" + rowScale * i, leftlength, ' '), leftmargin+addlefttitleVerge,(topVerge+addtoptitleVerge + paintHeight - i * rowInterval ));
}
// 先画外框架
g.setColor(frameRectColor);
g.drawRect(leftmargin+leftVerge+addlefttitleVerge, topVerge+addtoptitleVerge, paintWidth, paintHeight);
// 后画内框
g.setColor(Color.white);
g.fillRect(leftmargin+leftVerge+addlefttitleVerge + 1, topVerge+addtoptitleVerge + 1, paintWidth - 1, paintHeight - 1);
// 画虚横线
int offset = drawLineFont.getSize() / 2;
g.setFont(drawLineFont);
g.setColor(drawLineFontColor);
for (int i = 1; i < rowCount + 1; i++) {
for (int j = leftVerge+addlefttitleVerge + offset; j < (leftmargin+leftVerge+addlefttitleVerge + paintWidth); j += drawLineInterval) {
g.drawString(".", j,(topVerge+addtoptitleVerge + paintHeight - i * rowInterval + 2));
}
}
// 画行标度:行标识
offset = colScaleFont.getSize() / 2;
g.setFont(colScaleFont);
g.setColor(colScaleFontColor);
// 打印横坐标
for (int i = 1; i < colCount + 1; i++) {
drawString(g, col_arr[i - 1],leftmargin+ leftVerge+addlefttitleVerge + 20+(i-1) * colInterval+pillarnum*rectWidth / 2-rectWidth, canvasHeight - bottomVerge-addbottomtitleVerge-bottommargin+15, 12);
}

int rectHeight = (int) Math.round(Math.random() * 10);

if(pillarflag.equals("1")){
for (int i = 1; i < colCount + 1; i++) {
for(int firsti=0;firsti<pillarnum;firsti++){
if (bCalc){
rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight - topVerge), 0);
}else{
rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight), 0);
}
//gradual为1就渐变
if(gradual.equalsIgnoreCase("1")){
paintRect(g, leftmargin+leftVerge+addlefttitleVerge+ 20+(i-1) * colInterval+firsti*rectWidth - rectWidth / 2, (topVerge+addtoptitleVerge+ paintHeight - rectHeight), rectWidth, rectHeight+ rectFootLenght,fillRectColor[firsti]);
}else{
g.setColor(fillRectColor[firsti]);
g.fillRect(leftmargin+leftVerge+addlefttitleVerge+ 20+(i-1) * colInterval+firsti*rectWidth - rectWidth / 2, (topVerge+addtoptitleVerge+ paintHeight - rectHeight), rectWidth, rectHeight+ rectFootLenght);
}
}
}
}else{
for(int firsti=0;firsti<pillarnum;firsti++){
int[] x=new int[colCount];
int[] y=new int[colCount];
for(int i=1;i<colCount+1;i++){
if (bCalc){
rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight - topVerge), 0);
}else{
rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight), 0);
}
x[i-1]=leftmargin+leftVerge+addlefttitleVerge+ 20+(i-1) * colInterval+firsti*rectWidth+5- rectWidth / 2;
y[i-1]=(topVerge+addtoptitleVerge+ paintHeight - rectHeight);
g.setColor(fillRectColor[firsti]);
g.drawOval(x[i-1]-3, y[i-1]-3, 6, 6);
g.fillOval(x[i-1]-3, y[i-1]-3, 6, 6);
}
g.setColor(fillRectColor[firsti]);
g.drawPolyline(x, y, colCount);
}
}
for (int i = 1; i < colCount + 1; i++) {
double d=0;
for(int firsti=0;firsti<pillarnum;firsti++){
if(val_arr[i-1][firsti]!=d){
// 加柱头数据
g.setFont(rectHeadFont);
g.setColor(rectHeadFontColor[firsti]);
offset = String.valueOf(val_arr[i - 1][firsti]).length() / 2;
if (offset > 2){
offset += 2;
}
offset += rectWidth / 2;
rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight - topVerge), 0);
g.drawString(fmtOutStr(val_arr[i - 1][firsti]), leftmargin+leftVerge+addlefttitleVerge+20+ (i-1) * colInterval+firsti*rectWidth- offset,(topVerge+addtoptitleVerge + paintHeight - rectHeight - rectHeadFontOffset));
d=val_arr[i-1][firsti];
}
}
}

}
//打印渐变矩形
public void paintRect(Graphics g,float x, float y, int width, int height,Color color) {
/*
int r=color.getRed();
int gr=color.getGreen();
int b=color.getBlue();
int minusoffset=30;
int addoffset=10;
Color color1=new Color(RGBscope(r-minusoffset),RGBscope(gr-minusoffset),RGBscope(b-minusoffset));
Color color2=new Color(RGBscope(r+addoffset),RGBscope(gr+addoffset),RGBscope(b+addoffset));
*/
Graphics2D g2D = (Graphics2D) g;
Point2D.Float p1 = new Point2D.Float(x, y);
Point2D.Float p2 = new Point2D.Float(x,y+height);

GradientPaint g1 = new GradientPaint(p1, color, p2, Color.decode("#FFFFFF"),false);
Rectangle2D.Float rect1 = new Rectangle2D.Float(x,y,width,height);
g2D.setPaint(g1);
g2D.fill(rect1);
}
public int RGBscope(int value){
if(value<0){
value=0;
}else if(value>255){
value=255;
}else{

}
return value;
}
public void printBottom(Graphics g) {

// 打印底标题
int bottomTitleLeftStart = canvasWidth / 2 - bottomTitle.length()* bottomTitleFont.getSize() / 2;
g.setColor(bottomTitleColor);
g.setFont(bottomTitleFont);
g.drawString(bottomTitle, bottomTitleLeftStart + 10, canvasHeight-(addbottomtitleVerge+bottommargin)/2+5);

/*
// 打印底标题的图形
int bottomTitleLeftStart = canvasWidth / 2 - bottomTitle.length()
* bottomTitleFont.getSize() / 2;
g.setColor(fillRectColor);
// g.fillRect(bottomTitleLeftStart-6, canvasHeight -
// bottomTitleStart - rectWidth+4,
// rectWidth+bottomTitleFont.getSize()*bottomTitle.length(),
// rectWidth);
g.fillRect(bottomTitleLeftStart - 6, canvasHeight
- bottomTitleStart - rectWidth + 4, rectWidth - 2,
rectWidth - 2);
*/
}
// 重新计算定位数据
public void calculateScaleParameters() {
// 解析数据
// System.out.println("fdsafd"+dataList);
hb = parseData();
int data_len = toInt(hb.get("data_len"));
leftlength=hb.get("val_max").toString().trim().length()-2;
// 设置行列默认大小
if (colCount < 2){
colCount = 10;
}
if (rowCount < 2){
rowCount = 31;
}
if (data_len < colCount){
colCount = data_len;
}

//根据传进来字数的长度来动态调整bottomVerge
String[] s=(String[]) hb.get("col_arr");
int templength=0;
for(int i=0;i<s.length;i++){
if(templength<s[i].length()){
templength=s[i].length();
}
}
setBottomlength(templength);
//每个汉字高度13
bottomVerge=bottomlength*13;
if(getBottomTitle().equals("")){
addbottomtitleVerge=0;
}else{
//底部标题高度20
addbottomtitleVerge=20;
}
//每个数字宽度为6
leftVerge=leftlength*6;
if(!getLeftTitle().equals("")){
//左边字体宽度20+字体偏移量<=addlefttitleVerge
addlefttitleVerge=24;
}
if(!getTopTitle().equals("")){
//如果有上标题生加20的高度
addtoptitleVerge=20;
}
if(pillarnum>1){
rightVerge=10;
addrighttitleVerge=rightlength*20;
}
canvasWidth = colCount * colInterval + leftmargin+leftVerge +addlefttitleVerge+ addrighttitleVerge+rightVerge+20;
canvasHeight = rowCount * rowInterval + bottommargin+topVerge+addtoptitleVerge + bottomVerge+addbottomtitleVerge+20;

paintWidth = canvasWidth - leftVerge-leftmargin -addlefttitleVerge- rightVerge-addrighttitleVerge;
paintHeight = canvasHeight - topVerge-addtoptitleVerge-addbottomtitleVerge-bottommargin - bottomVerge;

// System.err.print(paintWidth+" "+paintHeight);
// 定义传入参数有误的可能
if (paintHeight < 20 || paintWidth < 20)
bPaint = false;

// 放大比率控制:暂时没有用
if (zoomInRate < 100)
zoomInRate = 100;
// colInterval = (paintWidth - rightMargin) / colCount;
// rowInterval = (paintHeight - topMargin) / rowCount;

// 如果柱宽大于等于行间隔,自动调整到其80%
if (rectWidth >= colInterval)
rectWidth = colInterval * 4 / 5;

// 柱宽最少2px
if (rectWidth < 2)
rectWidth = 2;

//参数输入错误的时候固定大小
if(!bPaint){
canvasWidth=300;
canvasHeight=100;
}
}

public Hashtable parseData() {
// String data =
// "'第部分',121330.22,'2',92333.84,'3',31110.4677,'4',72950,'5',19620,'6',11000,'7',38300,'8',57870,'9',34720,'10',0,'11',66810,'12',76030,'13',67190,'14',66950,'15',34370,'16',25400,'17',0,'18',44780,'19',108820,'20',53830,'21',57730,'22',134190,'23',6000,'24',0,'25',55600,'26',67620,'27',11030,'28',81370,'29',16850,'30',159650,'31',7000,'32',2000,'33',6000,'34',12300";
String data = "1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0";
if (dataList != null && !dataList.trim().equals("")){
data = dataList;
}else{
bPaint=false;
}
return parseData(data);
}
public Hashtable parseData(String data) {
if (data == null || data.length() < 2){
return null;
}
String[] arr_col = null;
double[][] arr_dbl = null;
String[] arr = replace(data, "'", "").split(",");
int len = 0;
if (arr != null){
len = arr.length;
}else{
return null;
}
double max = 0.0d;
Hashtable hb = new Hashtable(200);
arr_col = new String[len / (pillarnum+1)];
arr_dbl = new double[len / (pillarnum+1)][pillarnum];
hb.put("data_len", String.valueOf(len / (pillarnum+1)));
for (int i = 0; i < len; i++) {
if (arr[i] == null || arr[i].trim().equals("")){
arr[i] = "0";
}
if ((i % (pillarnum+1)) != 0 ){
if(max < Double.valueOf(arr[i]).doubleValue()){
max = Double.valueOf(arr[i]).doubleValue();
}
}
if ((i % (pillarnum+1)) == 0){
arr_col[i / (pillarnum+1)]=arr[i];
}else{
if((i % (pillarnum+1)) == 1){
for(int ii=0;ii<pillarnum;ii++){
arr_dbl[i / (pillarnum+1)][ii] = Double.valueOf(arr[i+ii]).doubleValue();
}
}
}
}
if (max < 10) {
max = 10;
}
hb.put("col_arr", arr_col);
hb.put("val_arr", arr_dbl);
hb.put("val_max", String.valueOf(max));

return hb;
}

public void drawString(Graphics g, String msg, int x, int y, int offset) {
if (g == null)
return;
if (x < 0 || y < 0)
return;
if (msg == null || msg.trim().equals(""))
return;
int len = msg.length();
if (toInt(msg) > 0) {
g.drawString(msg, x, y);
return;
}
for (int i = 0; i < len; i++) {
g.drawString(msg.substring(i, i + 1), x, y + i * offset);
}

}
public String getFileName(String file) {
String szRtn = ".";
if (file == null || file.trim().equals(""))
return File.separator + System.currentTimeMillis() + ".jpg";

szRtn = file.trim();
File f = new File(szRtn);
String path = f.getParent();
String name = f.getName();
int iPos = name.lastIndexOf(".");
if (iPos < 0)
name += ".jpg";
else
name = name.substring(0, iPos) + ".jpg";
f = new File(path);
if (!f.exists() || !f.isDirectory())
System.err.println("create dir ok! " + f.mkdirs());
szRtn = path + File.separator + name;
return szRtn;
}
public void paintToPic(String file) // 生成文件
{
fileName = getFileName(file);
System.err.println(fileName);

// 计算刻度数据
calculateScaleParameters();

// 定义一个画图空间
BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight,
BufferedImage.TYPE_INT_BGR);

// 输出内容到画图空间
paint(bi);

try {
FileOutputStream out = new FileOutputStream(fileName);
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(out);
en.encode(bi);
if (out != null) {
out.close();
out = null;
}
} catch (Exception e) {
System.err.println("Create pic err:" + e);
}
}
public void paintToStream(HttpServletResponse response) // 放入内存
{
// 计算刻度数据
calculateScaleParameters();

// 定义一个画图空间
BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight,
BufferedImage.TYPE_INT_BGR);

// 输出内容到画图空间
paint(bi);
/*
* response.setContentType("image/jpeg");
* response.addHeader("pragma","NO-cache");
* response.addHeader("Cache-Control","no-cache");
* response.addDateHeader("Expries",0);
*/
ServletOutputStream out = null;
try {
out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
if (false && out != null) {
out.close();
out = null;
}

} catch (Exception e) {
System.err.println("Create pic err:" + e);
}
}
/**
* 控制左对齐和右对齐的核心方法,其中w是控制的参数
* */
public String fmtNum(String src, int w, char ch) {
StringBuffer sb = new StringBuffer();
if (src == null || w < 0)
return src;
if (w <= src.length())
return src;
int len = w - src.length();
for (int i = 0; i < len; i++)
sb.append(ch);
sb.append(src);
return sb.toString();
}
public String fmtOutStr(double num) {
return fmtNumStr(roundDbl(num, -2));
}
public String fmtNumStr(String str) {
if (str == null || str.trim().equals(""))
return str;
int iPos = str.lastIndexOf(".");
if (iPos < 0)
return str;
String head = str.substring(0, iPos);
String tail = str.substring(iPos + 1);
iPos = tail.length();
while (head.startsWith("0"))
head = head.substring(1);
while (tail.endsWith("0"))
tail = tail.substring(0, tail.length() - 1);

if (tail.length() == iPos)
return str;
else if (tail.length() > 0)
return head + "." + tail;
else
return head;
}
public int round(double num, int w) {
if (num < 0.000001d || w < 1)
return (int) Math.round(num);
String szDbl = String.valueOf(num);
int iPos = szDbl.indexOf(".");
String szRate = "1";
for (int i = 0; i < w; i++)
szRate += "0";
int tmp = (int) Math.round(num);
int rate = Integer.valueOf(szRate).intValue();

if (iPos > 0) {
if (iPos <= w)
return tmp;
szDbl = szDbl.substring(iPos + 1);
if (0 < Integer.valueOf(szDbl).intValue())
return (int) (tmp / rate * rate + rate);
}

if (0 < (tmp % rate))
return (int) (tmp / rate * rate + rate);
return tmp;
}
public String buildFileName(String path) {
String dir = ".";
if (path != null || !path.trim().equals(""))
dir = path.trim();

File f = new File(dir);
if (f.isFile() || !f.exists())
System.err.println("create dir:" + f.mkdirs());
dir = f.getAbsolutePath() + File.separator + System.currentTimeMillis()
+ ".jpg";
return dir;
}
public double toDbl(Object obj) {
if (obj == null || obj.toString().trim().equals(""))
return 0.0d;

try {
return Double.parseDouble(obj.toString().trim());
} catch (Exception ex) {
return 0;
}
}
public int toInt(Object obj) {
if (obj == null || obj.toString().trim().equals(""))
return 0;

try {
return Integer.parseInt(obj.toString().trim());
} catch (Exception ex) {
return 0;
}
}
public String roundDbl(double num, int w) {
if(Double.valueOf(num).toString().contains("E")){
return Double.valueOf(num).intValue()+"";
}
int width = 0;
if (w > 0)
return String.valueOf(round(num, w));

width = Math.abs(w);
String szDbl = String.valueOf(num);
int iPos = szDbl.indexOf(".");
if (width >= iPos)
width = 2;
if (iPos < 0) {
szDbl += ".";
for (int i = 0; i < width; i++)
szDbl += "0";
return szDbl;
} else {
int len = szDbl.length();
if (width < (len - iPos - 1)) {
iPos = iPos + 1 + width; // 整数位+小数点+小数位
double d1 = Double.valueOf(szDbl.substring(0, iPos))
.doubleValue();
int k = Integer.valueOf(szDbl.substring(iPos, iPos + 1))
.intValue();
if (k < 5)
return String.valueOf(d1);
else {
String szTmp = "1";
for (int i = 0; i < width; i++)
szTmp += "0";
// System.err.println("width="+width+" szTmp="+szTmp);
int tmp = Integer.valueOf(szTmp).intValue();
d1 = (d1 * tmp + 1) / tmp;
szDbl = String.valueOf(d1);
}
}

iPos = szDbl.indexOf(".");
len = szDbl.length();
if (width >= (len - iPos - 1)) {
width = width - (len - iPos - 1);
for (int i = 0; i < width; i++)
szDbl += "0";
}
return szDbl;
}
}

public String replace(String szSrc, String szOld, String szNew) {
try {
if (szSrc == null || szSrc.trim().equals(""))
return "";

String szTmp = szSrc.trim();
int iOldLen = szOld.length();
int iSrcLen = szSrc.length();
int iStart = 0;
int iPos = -1;
StringBuffer sb = new StringBuffer(iSrcLen + iOldLen * 6);

if (szNew == null) {
return szTmp;
}
if (szOld == null || szOld.equals("")) {
return szTmp;
}

if ((iPos = szTmp.indexOf(szOld)) == -1) // 提前退出
{
return szTmp;
}

while ((iPos = szTmp.indexOf(szOld, iStart)) > -1) {
sb.append(szTmp.substring(iStart, iPos));
sb.append(szNew);
iStart = iPos + iOldLen;
}

if (iStart < iSrcLen) {
sb.append(szTmp.substring(iStart));
}

return sb.toString();
} catch (Exception ex) {
return "";
}
}
/**
* 转码(指定字符集)
*
* @param str
* String
* @return String
*/
public String toGBKStr(String str) {
try {
String strTmp = "";
if ((str == null) || str.trim().equals("")) {
return "";
} else {
strTmp = str.trim();
}



byte[] bytes = strTmp.getBytes("GBK");// ISO8859_1
return (new String(bytes, "GBK"));
} catch (Exception e) {
return "";
}
}




public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getTopTitle() {
return topTitle;
}
public void setTopTitle(String topTitle) {
this.topTitle = topTitle;
}
public Color getTopTitleColor() {
return topTitleColor;
}
public void setTopTitleColor(Color topTitleColor) {
this.topTitleColor = topTitleColor;
}
public Font getTopTitleFont() {
return topTitleFont;
}
public void setTopTitleFont(Font topTitleFont) {
this.topTitleFont = topTitleFont;
}
public int getTopTitleStart() {
return topTitleStart;
}
public void setTopTitleStart(int topTitleStart) {
this.topTitleStart = topTitleStart;
}
public String getLeftTitle() {
return leftTitle;
}
public void setLeftTitle(String leftTitle) {
this.leftTitle = leftTitle;
}
public Color getLeftTitleColor() {
return leftTitleColor;
}
public void setLeftTitleColor(Color leftTitleColor) {
this.leftTitleColor = leftTitleColor;
}
public Font getLeftTitleFont() {
return leftTitleFont;
}
public void setLeftTitleFont(Font leftTitleFont) {
this.leftTitleFont = leftTitleFont;
}
public int getLeftTitleStart() {
return leftTitleStart;
}
public void setLeftTitleStart(int leftTitleStart) {
this.leftTitleStart = leftTitleStart;
}
public String getBottomTitle() {
return bottomTitle;
}
public void setBottomTitle(String bottomTitle) {
this.bottomTitle = bottomTitle;
}
public Color getBottomTitleColor() {
return bottomTitleColor;
}
public void setBottomTitleColor(Color bottomTitleColor) {
this.bottomTitleColor = bottomTitleColor;
}
public Font getBottomTitleFont() {
return bottomTitleFont;
}
public void setBottomTitleFont(Font bottomTitleFont) {
this.bottomTitleFont = bottomTitleFont;
}
public int getBottomTitleStart() {
return bottomTitleStart;
}
public void setBottomTitleStart(int bottomTitleStart) {
this.bottomTitleStart = bottomTitleStart;
}
public int getBottomlength() {
return bottomlength;
}
public void setBottomlength(int bottomlength) {
this.bottomlength = bottomlength;
}
public Font getDrawLineFont() {
return drawLineFont;
}
public void setDrawLineFont(Font drawLineFont) {
this.drawLineFont = drawLineFont;
}
public Font getRectHeadFont() {
return rectHeadFont;
}
public void setRectHeadFont(Font rectHeadFont) {
this.rectHeadFont = rectHeadFont;
}
public Font getRowScaleFont() {
return rowScaleFont;
}
public void setRowScaleFont(Font rowScaleFont) {
this.rowScaleFont = rowScaleFont;
}
public Font getColScaleFont() {
return colScaleFont;
}
public void setColScaleFont(Font colScaleFont) {
this.colScaleFont = colScaleFont;
}
public Color getDrawLineFontColor() {
return drawLineFontColor;
}
public void setDrawLineFontColor(Color drawLineFontColor) {
this.drawLineFontColor = drawLineFontColor;
}
public Color[] getRectHeadFontColor() {
return rectHeadFontColor;
}
public void setRectHeadFontColor(String rectHeadFontColor) {
if(!rectHeadFontColor.equals("")){
String[] s=rectHeadFontColor.split(",");
for(int i=0;i<s.length;i++){
if(s[i].indexOf("#")==-1){
s[i]="#"+s[i];
}
this.rectHeadFontColor[i] =Color.decode(s[i]);
}
}
}
public Color getRowScaleFontColor() {
return rowScaleFontColor;
}
public void setRowScaleFontColor(Color rowScaleFontColor) {
this.rowScaleFontColor = rowScaleFontColor;
}
public Color getColScaleFontColor() {
return colScaleFontColor;
}
public void setColScaleFontColor(Color colScaleFontColor) {
this.colScaleFontColor = colScaleFontColor;
}
public Color[] getFillRectColor() {
return fillRectColor;
}
public void setFillRectColor(String fillRectColor) {
if(!fillRectColor.equals("")){
String[] s=fillRectColor.split(",");
for(int i=0;i<s.length;i++){
if(s[i].indexOf("#")==-1){
s[i]="#"+s[i];
}
this.fillRectColor[i] =Color.decode(s[i]);
}
}
}
public Color getFrameRectColor() {
return frameRectColor;
}
public void setFrameRectColor(Color frameRectColor) {
this.frameRectColor = frameRectColor;
}
public Color getCanvasBackgroundColor() {
return canvasBackgroundColor;
}
public void setCanvasBackgroundColor(Color canvasBackgroundColor) {
this.canvasBackgroundColor = canvasBackgroundColor;
}
public Hashtable getHb() {
return hb;
}
public void setHb(Hashtable hb) {
this.hb = hb;
}
public String getDataList() {
return dataList;
}
public void setDataList(String dataList) {
this.dataList = dataList;
}
public boolean isBPaint() {
return bPaint;
}
public void setBPaint(boolean paint) {
bPaint = paint;
}
public int getCanvasWidth() {
return canvasWidth;
}
public void setCanvasWidth(int canvasWidth) {
this.canvasWidth = canvasWidth;
}
public int getCanvasHeight() {
return canvasHeight;
}
public void setCanvasHeight(int canvasHeight) {
this.canvasHeight = canvasHeight;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getColCount() {
return colCount;
}
public void setColCount(int colCount) {
this.colCount = colCount;
}
public int getTopVerge() {
return topVerge;
}
public void setTopVerge(int topVerge) {
this.topVerge = topVerge;
}
public int getBottomVerge() {
return bottomVerge;
}
public void setBottomVerge(int bottomVerge) {
this.bottomVerge = bottomVerge;
}
public int getLeftVerge() {
return leftVerge;
}
public void setLeftVerge(int leftVerge) {
this.leftVerge = leftVerge;
}
public int getRightVerge() {
return rightVerge;
}
public void setRightVerge(int rightVerge) {
this.rightVerge = rightVerge;
}
public int getRectFootLenght() {
return rectFootLenght;
}
public void setRectFootLenght(int rectFootLenght) {
this.rectFootLenght = rectFootLenght;
}
public int getRectHeadFontOffset() {
return rectHeadFontOffset;
}
public void setRectHeadFontOffset(int rectHeadFontOffset) {
this.rectHeadFontOffset = rectHeadFontOffset;
}
public int getDrawLineInterval() {
return drawLineInterval;
}
public void setDrawLineInterval(int drawLineInterval) {
this.drawLineInterval = drawLineInterval;
}
public int getZoomInRate() {
return zoomInRate;
}
public void setZoomInRate(int zoomInRate) {
this.zoomInRate = zoomInRate;
}
public int getRectWidth() {
return rectWidth;
}
public void setRectWidth(int rectWidth) {
this.rectWidth = rectWidth;
}

public int getPaintWidth() {
return paintWidth;
}
public void setPaintWidth(int paintWidth) {
this.paintWidth = paintWidth;
}
public int getPaintHeight() {
return paintHeight;
}
public void setPaintHeight(int paintHeight) {
this.paintHeight = paintHeight;
}
public int getColInterval() {
return colInterval;
}
public void setColInterval(int colInterval) {
this.colInterval = colInterval;
}
public int getRowInterval() {
return rowInterval;
}
public void setRowInterval(int rowInterval) {
this.rowInterval = rowInterval;
}
public String[] getPillarInfo() {
return PillarInfo;
}
public void setPillarInfo(String pillarInfo) {
if(!pillarInfo.equals("")){
String[] s=pillarInfo.split(",");
for(int i=0;i<s.length;i++){
PillarInfo[i] = s[i];
if(this.rightlength<s[i].length()){
this.rightlength=s[i].length();
}
}
}
}
public String getPillarNum() {
return PillarNum;
}
public void setPillarNum(String pillarNum) {
PillarNum = pillarNum;
}
public int getPillarnum() {
return pillarnum;
}
public void setPillarnum(int pillarnum) {
this.pillarnum = pillarnum;
}
public int getLeftlength() {
return leftlength;
}
public void setLeftlength(int leftlength) {
this.leftlength = leftlength;
}
public int getAddtoptitleVerge() {
return addtoptitleVerge;
}
public void setAddtoptitleVerge(int addtoptitleVerge) {
this.addtoptitleVerge = addtoptitleVerge;
}
public int getAddbottomtitleVerge() {
return addbottomtitleVerge;
}
public void setAddbottomtitleVerge(int addbottomtitleVerge) {
this.addbottomtitleVerge = addbottomtitleVerge;
}
public int getAddlefttitleVerge() {
return addlefttitleVerge;
}
public void setAddlefttitleVerge(int addlefttitleVerge) {
this.addlefttitleVerge = addlefttitleVerge;
}
public int getLeftmargin() {
return leftmargin;
}
public void setLeftmargin(int leftmargin) {
this.leftmargin = leftmargin;
}
public int getBottommargin() {
return bottommargin;
}
public void setBottommargin(int bottommargin) {
this.bottommargin = bottommargin;
}
public void setRectHeadFontColor(Color[] rectHeadFontColor) {
this.rectHeadFontColor = rectHeadFontColor;
}
public void setFillRectColor(Color[] fillRectColor) {
this.fillRectColor = fillRectColor;
}

public String getGradual() {
return gradual;
}
public void setGradual(String gradual) {
this.gradual = gradual;
}
public String getPillarflag() {
return pillarflag;
}
public void setPillarflag(String pillarflag) {
this.pillarflag = pillarflag;
}
public Test() {
super();
}
public Test(String PillarNum) {
super();
if(PillarNum.equals("")){
PillarNum="1";
}
this.PillarNum=PillarNum;//得出柱子数量
this.pillarnum=Integer.valueOf(PillarNum);
this.colInterval=pillarnum*20;//根据柱子数量得间隔
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Color.blue);
Color c=new Color(-16776961);
System.out.println(c);
}



}




<%@ page language="java"%>
<%@ page contentType="text/html;charset=gbk"%>
<%@page import="Test"%>
<%@page import="java.awt.Color" %>
<title>Test Create Pic</title>

<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body>
<%
String strChartData=request.getParameter("chartData");
String Pillarflag="1";//是否是柱形图1是
//String strCoordColor="#99cc00,#0000FF,#FF0000,#FF99cc,#800000,#cc99FF,#000080,#808000";//柱体颜色
String strCoordColor="";//柱体颜色
//String RectHeadFontColor="#99cc00,#0000FF,#FF0000,#FF99cc,#800000,#cc99FF,#000080,#808000";//柱体字体颜色
String RectHeadFontColor="";//柱体字体颜色
String PillarNum="1";//柱子数量
String PillarInfo="";//柱子说明
String Gradual="1";//是否渐变
String TopTitle="";
String BottomTitle="";
String LeftTitle="";


if (request.getParameter("CoordColor")!=null)
{
strCoordColor=request.getParameter("CoordColor");
}
if (request.getParameter("Gradual")!=null)
{
Gradual=request.getParameter("Gradual");
}
if (request.getParameter("Pillarflag")!=null)
{
Pillarflag=request.getParameter("Pillarflag");
}
if (request.getParameter("RectHeadFontColor")!=null)
{
RectHeadFontColor=request.getParameter("RectHeadFontColor");
}
if (request.getParameter("PillarInfo")!=null)
{
PillarInfo=request.getParameter("PillarInfo");
}
if (request.getParameter("PillarNum")!=null)
{
PillarNum=request.getParameter("PillarNum");
}
if (request.getParameter("TopTitle")!=null)
{
TopTitle=request.getParameter("TopTitle");
}
if (request.getParameter("BottomTitle")!=null)
{
BottomTitle=request.getParameter("BottomTitle");
}
if (request.getParameter("LeftTitle")!=null)
{
LeftTitle=request.getParameter("LeftTitle");
}
out.clear();
//77CC50
//String list = "'销售十一部',31330.22,'销售十一部',22333.84,'销售十一部',131110.4677,'销售十一部',72950,'销售十一部',19620,'6',11000,'7',38300,'8',57870,'9',34720,'10',0,'11',66810";
//String list = "'销售十一部',31330.22,30000.11,'销售十一部',22333.84,20000.22,'销售十一部',131110.4677,130000.33,'销售十一部',72950,78000,'销售十一部',19620,15000,'6',11000,11000,'7',38300,40000,'8',57870,56000,'9',34720,34720,'10',0,1,'11',66810,66000";
//String list = "'销售十一部',31330.22,30000.11,30000.11,'销售十一部',22333.84,20000.22,30000.11,'销售十一部',131110.4677,130000.33,30000.11,'销售十一部',72950,78000,30000.11,'销售十一部',19620,15000,30000.11,'6',11000,11000,30000.11,'7',38300,40000,30000.11,'8',57870,56000,30000.11,'9',34720,34720,30000.11,'10',0,1,30000.11,'11',66810,66000,30000.11,";

Test bp = new Test(PillarNum);
bp.setPillarflag(Pillarflag);
//bp.setDataList(list);
bp.setDataList(strChartData);
//bp.setTopTitle("本月销售业绩");
//bp.setBottomTitle("日期");
bp.setTopTitle(TopTitle);
bp.setBottomTitle(BottomTitle);
bp.setLeftTitle(LeftTitle);
bp.setFillRectColor(strCoordColor);
bp.setRectHeadFontColor(RectHeadFontColor);
bp.setPillarInfo(PillarInfo);
bp.setGradual(Gradual);
bp.paintToStream(response);
//bp.paintToPic(null);
%>
</body>
</HTML>
<%
out.clear();
%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值