您的位置:首页 > 编程语言 > Java开发

Eclipse上GIT插件EGIT使用手册

2013-05-16 09:45 489 查看
/*
* @(#)DrawBar.java
* All rights reserved.
*/
package com.system.jfreechart;

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAnchor;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

/**
* Class description goes here
*
* @author Fly Liu
* @date Jan 8, 2010
* @description
*/
public class DrawBar {

private String title = ""; // 图片标题
private String XTitle = ""; // 图片横坐标标题
private String YTitle = ""; // 图片垂直坐标标题
private int Xsz = 10; // X轴标尺字体大小
private int Ysz = 10; // X轴标尺字体大小
private Color bgcolor = null; // 图片背景颜色
private int width = 800; // 要生成的图片的宽度
private int height = 600; // 要生成的图片的高度
private double margin = 0.2; // 每组柱间的间距 0--1之间
private boolean isV = true; // 柱图显示方式:0:垂直 1:水平显示
private String fileName = ""; // 图片名称(可以加路经)
private float diaphaneity = 0.8f ;//柱体透明度
private double poleSpaceBetween = 0 ;//组内柱间距
private Color wallPaintColor = Color.white;//3D墙的颜色
private Color rangeGridline = Color.black;//外围背景颜色
private Color rangeGridInside = Color.white;//网格背景色
private DefaultCategoryDataset dataset = null; // 显示图片需用的数据集
private FileOutputStream fosJpg = null; // 生成图片是用到的输出流
private Font xFont = new Font("宋体", Font.PLAIN, 14);//x轴 坐标 字体 样式
private Font yFont = new Font("宋体", Font.PLAIN, 14);//y轴 坐标 字体 样式
private Font xTitleFont = new Font("黑体",Font.PLAIN,14);//x轴 标题 字体 样式
private Font yTitleFont = new Font("黑体",Font.PLAIN,14);//y轴 标题 字体 样式
private Color xPaint = Color.black;//横坐标字体颜色
private Color yPaint = Color.black;//纵坐标字体颜色
private Color viewNumberColor = Color.black;//图上显示数字的颜色
private Font viewNumberFont = new Font("宋体",Font.PLAIN,10);////图上显示数字的颜色
private double xCircumgyratePercent = 0.0d;//x轴坐标旋转角度
private double yTitleCircumgyratePercent = 0.0d;//y轴标题旋转角度
private static DrawBar instance = null;

/**
* 单态模式生成类对象
*
* @return 该类的一个对象
*/
public static synchronized DrawBar getInstance() {
if (instance == null)
instance = new DrawBar();
return instance;
}
/**
*  柱间距
* @param d
*/
public void setPoleSpaceBetween(double d){
this.poleSpaceBetween = d ;
}
/**
*  3D墙的颜色
* @param d
*/
public void setWallPaintColor(Color color){
this.wallPaintColor = color ;
}
/**
* 3D墙的颜色
*
* @param red
*            红色色素值
* @param green
*            绿色色素值
* @param blue
*            兰色色素值
*/
public void setWallPaintColor(int red, int green, int blue){
this.wallPaintColor = new Color(red, green, blue);
}
/**
*  网格内部背景的颜色
* @param d
*/
public void setRangeGridInside(Color color){
this.rangeGridInside = color ;
}
/**
* 网格内部背景的颜色
*
* @param red
*            红色色素值
* @param green
*            绿色色素值
* @param blue
*            兰色色素值
*/
public void setRangeGridInside(int red, int green, int blue){
this.rangeGridInside = new Color(red, green, blue);
}
/**
*  网格背景的颜色
* @param d
*/
public void setRangeGridline(Color color){
this.rangeGridline = color ;
}
/**
* 网格背景的颜色
*
* @param red
*            红色色素值
* @param green
*            绿色色素值
* @param blue
*            兰色色素值
*/
public void setRangeGridline(int red, int green, int blue){
this.rangeGridline = new Color(red, green, blue);
}
/**
* 柱状图透明度
*
* @param f
*/
public void setDiaphaneity(float f){
this.diaphaneity = f ;
}
/**
* 改变 图表标题
*
* @param str
*            图表标题
*/
public void setTitle(String str) {
this.title = str;
}

/**
* 改变 目录轴的显示标签
*
* @param str
*            目录轴的显示标签
*/
public void setXTitle(String str) {
this.XTitle = str;
}

/**
* 改变 数值轴的显示标签
*
* @param str
*            数值轴的显示标签
*/
public void setYTitle(String str) {
this.YTitle = str;
}

/**
* 改变 轴的显示标签
*
* @param i
*            轴的显示标签
*/
public void setXFontSize(int i) {
this.Xsz = i;
}

/**
* 改变 轴的显示标签
*
* @param i
*            轴的显示标签
*/
public void setYFontSize(int i) {
this.Ysz = i;
}
/**
* 设置背景颜色
*
* @param red
*            红色色素值
* @param green
*            绿色色素值
* @param blue
*            兰色色素值
*/
public void setBgcolor(int red, int green, int blue) {
this.bgcolor = new Color(red, green, blue);
}

/**
* 改变背景颜色
*
* @param str
*            背景颜色描述 比如:BLACK black blue Blue 等
*/
public void setBgcolor(Color color) {
this.bgcolor = color;
}

/**
* 改变背景颜色
*
* @param str
*            背景颜色描述 比如:BLACK black blue Blue 等
*/
public void setBgColor(String str) {
this.bgcolor = ChangeColor.getColor(str);
}

/**
* 改变图片宽度
*
* @param width
*            图片宽度
*/
public void setWidth(int width) {
this.width = width;
}

/**
* 改变图片高度
*
* @param height
*            图片高度
*/
public void setHeight(int height) {
this.height = height;
}

/**
* 设置每组柱间的距离
*
* @param margin
*            间距
*/
public void setMargin(double margin) {
this.margin = margin;
}

/**
* 改变图片显示方式
*
* @param str
*            图片显示方式 垂直显示 水平显示
*/
public void setIsV(boolean str) {
this.isV = str;
}

/**
* 改变文件名称
*
* @param str
*            文件名称
*/
private void setFileName(String str) {
this.fileName = str;
}
/**
* x轴坐标字体
* @param font
*/
public void setXFont(Font font) {
xFont = font;
}
/**
* y轴坐标字体
* @param font
*/
public void setYFont(Font font) {
yFont = font;
}
/**
* x轴标题字体
* @param titleFont
*/
public void setXTitleFont(Font titleFont) {
xTitleFont = titleFont;
}
/**
* x轴坐标 字体 颜色
* @param red
* @param green
* @param blue
*/
public void setXPaint(int red, int green, int blue) {
xPaint = new Color(red, green, blue);
}
/**
* y轴坐标 字体 颜色
* @param red
* @param green
* @param blue
*/
public void setYPaint(int red, int green, int blue) {
yPaint = new Color(red, green, blue);
}
/**
* x轴坐标 字体 颜色
* @param paint
*/
public void setXPaint(Color paint) {
xPaint = paint;
}
/**
* y坐标 字体 颜色
* @param paint
*/
public void setYPaint(Color paint) {
yPaint = paint;
}
/**
* y轴标题 字体 样式
* @param titleFont
*/
public void setYTitleFont(Font titleFont) {
yTitleFont = titleFont;
}
/**
* 图示 显示数字 颜色
* @param red
* @param green
* @param blue
*/
public void setViewNumberColor(int red, int green, int blue) {
this.viewNumberColor = new Color(red, green, blue);
}
/**
* 图示 显示数字 颜色
* @param viewNumberColor
*/
public void setViewNumberColor(Color viewNumberColor) {
this.viewNumberColor = viewNumberColor;
}
/**
* 图示 显示数字 样式
* @param viewNumberFont
*/
public void setViewNumberFont(Font viewNumberFont) {
this.viewNumberFont = viewNumberFont;
}
/**
* x轴坐标旋转百分比
* @param circumgyratePercent
*/
public void setXCircumgyratePercent(double circumgyratePercent) {
xCircumgyratePercent = circumgyratePercent;
}
/**
* y轴标题旋转百分比
* @param titleCircumgyratePercent
*/
public void setYTitleCircumgyratePercent(double titleCircumgyratePercent) {
yTitleCircumgyratePercent = titleCircumgyratePercent;
}
/**
* 初始化参数
*/
public void init() {
setTitle("柱状图");
setXTitle("横标题");
setYTitle("纵标题");
setXFontSize(10);
setYFontSize(10);
setWidth(800);
setHeight(600);
setMargin(0.2);
setIsV(true);
setBgcolor(255, 255, 255);
setFileName("temp.jpg");
}

/**
* 添加要进行画柱状图的数据
*
* @param value
*            单元值
* @param name
*            单元项名称
* @param group
*            该单元项所属的组
*/
public void addData(String name, int value, String group) {
if (dataset != null) {
dataset.addValue(value, group, name);
} else {
dataset = new DefaultCategoryDataset();
dataset.addValue(value, group, name);
}
}

/**
* 使用servlet生成图片
* @param request
* @param servletName /servlet/DisplayChart?filename=
* @return 图片路径
*/
public String getUrl(HttpServletRequest request,String servletName){
if("".equals(servletName)||servletName == null){
servletName = "/servlet/DisplayChart?filename=" ;
}
if (dataset == null) {
return "";
} else {
JFreeChart chart = null;
if (isV == true) {
chart = ChartFactory.createBarChart3D(this.title, this.XTitle,
this.YTitle, dataset, PlotOrientation.VERTICAL, false,
true, false);
// ChartFactory.createBarChart3D( 图表标题, 目录轴的显示标签, 数值轴的显示标签,数据集, 图表方向:水平、垂直, 是否显示图例(对于简单的柱状图必须是false),  是否生成工具, 是否生成URL链接);
} else {
chart = ChartFactory.createBarChart3D(this.title, this.XTitle,
this.YTitle, dataset, PlotOrientation.HORIZONTAL, true,
false, false);
}

CategoryPlot categoryplot = chart.getCategoryPlot();
categoryplot.setRangeGridlinePaint(this.rangeGridline);// 设置网格横线颜色
categoryplot.setForegroundAlpha(this.diaphaneity);// 柱体透明度
//categoryplot.setDomainGridlinesVisible(true);//垂直网格线
chart.setBackgroundPaint(this.bgcolor); // 设置外围背景颜色

//categoryplot.setNoDataMessage("当前无数据可供显示!");
//取得横轴
CategoryAxis categoryaxis = categoryplot.getDomainAxis();//取得横轴
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(this.xCircumgyratePercent)); //横坐标旋转
categoryaxis.setLowerMargin(0.01);// 设置标签距离图片左端距离比为2%
categoryaxis.setUpperMargin(0.01);
categoryaxis.setCategoryMargin(this.margin);
categoryaxis.setTickLabelFont(this.xFont); //-设置X轴坐标上的文字
categoryaxis.setLabelFont(this.xTitleFont); //设置X轴的标题文字
categoryaxis.setLabelPaint(this.xPaint);//设置X轴的标题文字颜色
categoryaxis.setTickLabelPaint(this.xPaint);//-设置X轴坐标上的文字颜色

//取得纵轴
NumberAxis verticalAxis = (NumberAxis) categoryplot.getRangeAxis();//取得纵轴
verticalAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
verticalAxis.setVisible(true);
verticalAxis.setAutoRange(true);// 自动设置数据轴数据范围

categoryplot.setBackgroundPaint(this.rangeGridInside);//设置网格背景色
categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);// 底部组标签(靠近x轴)

categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);// 左部标签(y轴)
categoryplot.setDomainGridlinePosition(CategoryAnchor.MIDDLE);// 设置格线位置

ValueAxis rangeAxis = categoryplot.getRangeAxis();
rangeAxis.setUpperMargin(0.15);// 标签距离图片顶端距离
rangeAxis.setLowerMargin(0.15);
rangeAxis.setTickLabelFont(this.yFont);//设置Y轴坐标上的文字
rangeAxis.setLabelFont(this.yTitleFont);//设置Y轴坐标上的标题
rangeAxis.setTickLabelPaint(this.yPaint);//设置Y轴坐标上的文字
rangeAxis.setLabelPaint(this.yPaint);//设置Y轴坐标上的标题
rangeAxis.setLabelAngle(this.yTitleCircumgyratePercent);//纵坐标 旋转
categoryplot.setRangeAxis(rangeAxis);

CustomBarRenderer custombarrenderer = new CustomBarRenderer();
custombarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
custombarrenderer.setBaseItemLabelsVisible(true);
// 按组设置柱体颜色
custombarrenderer.setWallPaint(this.wallPaintColor);// 3d墙的颜色
custombarrenderer.setItemMargin(this.poleSpaceBetween);// 组内柱间距
custombarrenderer.setMaximumBarWidth(0.2); // 設定最大寬度
custombarrenderer.setMinimumBarLength(0.1); // 設定最小長度
// 显示柱体数字
custombarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
custombarrenderer.setBaseItemLabelsVisible(true);
// 默认的数字显示在柱子中,通过如下两句可调整数字的显示
custombarrenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
custombarrenderer.setItemLabelPaint(this.viewNumberColor);//设置柱体数字颜色
custombarrenderer.setItemLabelAnchorOffset(18D);

//custombarrenderer.setItemURLGenerator(new StandardCategoryURLGenerator());//MAP中钻取链接格式
categoryplot.setRenderer(custombarrenderer);

String url = "" ;
try {
String filename = ServletUtilities.saveChartAsPNG(chart, width, height, request.getSession());
url = request.getContextPath() + servletName+ filename ;
} catch (Exception e) {
e.printStackTrace();
} finally {
this.dataset.clear();
}
return url;
}
}
/**
* @return 要显示的文件的名称(包括文件路径)
*/
public String show() {
return fileName;
}

/**
* 恢复成员变量为初试状态
*/
public void reset() {
dataset.clear();
init();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: