public class ChartUtil { |
|
/** |
* 产生报表折线图的方法,可以产生一张1或者两张折线图供前台页面展示 |
* |
* |
* @param condVo |
* @param list |
* @return JFreeChart[]对象数组 |
* @throws IOException |
*/ |
private static final Logger logger = Logger.getLogger(ReportDao. class ); |
|
public JFreeChart[] createLineChart(ReportCondVo condVo, List list) |
throws IOException, Exception { |
|
// 创建一个解析配置文件类的对象 |
ParseProperties parse = new ParseProperties(String.valueOf(condVo |
.getReportId())); |
|
// 新建一个二维JfreeChart数组 |
JFreeChart[] jfreechart = new JFreeChart[ 2 ]; |
String ChartTitle = "" ; |
ChartTitle = parse.getName(); |
JFreeChart chart = ChartFactory.createLineChart(ChartTitle, // 图标题(第一张图的标题) |
"" , // x 轴标题 |
"" , // y 轴标题 |
getLineDataset(condVo, list, parse), // 数据源 |
PlotOrientation.VERTICAL, // 显示方向 |
true , // 显示图例 |
true , false ); |
|
logger.debug( "第一张图形名字:" + parse.getName()); |
// 设置背景色 |
chart.setBackgroundPaint( new Color( 218 , 235 , 255 )); |
|
CategoryPlot plot = chart.getCategoryPlot(); |
// 设置数据区(中间部分背景色) |
plot.setBackgroundPaint( new Color( 255 , 255 , 206 )); |
// x轴 // 分类轴网格是否可见 |
plot.setDomainGridlinesVisible( false ); |
plot.setDomainGridlinePaint(Color.WHITE); // 虚线色彩 |
// y轴 //数据轴网格是否可见 |
plot.setRangeGridlinesVisible( true ); |
plot.setRangeGridlinePaint(Color.BLACK); // 虚线色彩 |
|
// 设置轴和面板之间的距离 |
// plot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); |
|
// 获取 x 轴操作 |
CategoryAxis domainAxis = plot.getDomainAxis(); |
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // X轴45度斜 |
// 设置距离图片左端距离 |
domainAxis.setLowerMargin( 0.0 ); |
// 设置距离图片右端距离 |
domainAxis.setUpperMargin( 0.0 ); |
|
// 获取 y 轴操作: |
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); |
// 设置y轴最小值 |
yAxis.setLowerBound( 0 .0D); |
// 设置Legend的位置 图例的位置 |
// ((JFreeChart) chart).getLegend().setPosition(RectangleEdge.RIGHT); |
|
// 显示百分比 |
NumberFormat nf = new DecimalFormat( "00.00" ); |
yAxis.setNumberFormatOverride(nf); // 设置y轴以百分比方式显示 |
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) plot |
.getRenderer(); |
|
//lineandshaperenderer.setSeriesPaint(31, new Color(16,98,204));//天蓝 |
|
|
// 设置Series颜色(线条的颜色) |
// lineandshaperenderer.setSeriesPaint(0, new Color(0,0,128));//蓝 1 |
// lineandshaperenderer.setSeriesPaint(1, new Color(255,0,255));//桃红2 |
// lineandshaperenderer.setSeriesPaint(2, new Color(255,255,128));//黄色3 |
// lineandshaperenderer.setSeriesPaint(3, new Color(0,255,255));//蓝4 |
// lineandshaperenderer.setSeriesPaint(4, new Color(128,0,128));//紫5 |
// lineandshaperenderer.setSeriesPaint(5, new Color(0,0,255));//墨绿6 |
// lineandshaperenderer.setSeriesPaint(6, new Color(0,0,128));//蓝7 |
// lineandshaperenderer.setSeriesPaint(7, new Color(0,204,255));//草绿 |
// lineandshaperenderer.setSeriesPaint(8, new Color(192,192,192));//天蓝 |
// lineandshaperenderer.setSeriesPaint(9, new Color(204,255,204));//天蓝 |
|
lineandshaperenderer.setBaseShapesVisible( true ); // series 点(即数据点)可见 |
lineandshaperenderer.setBaseLinesVisible( true ); // series 点(即数据点)间有连线可见 |
|
// 显示折点数据 |
lineandshaperenderer |
.setBaseItemLabelGenerator( new StandardCategoryItemLabelGenerator()); |
lineandshaperenderer.setBaseItemLabelsVisible( false ); |
|
jfreechart[ 0 ] = chart; |
|
// 是否包含第二张图 |
boolean isRate = false ; |
isRate = parse.isIncludeRate(); |
|
logger.debug( "是否包含比率图 :" + parse.isIncludeRate()); |
|
if ( true == isRate) { // 是否包含第二张图 |
// 比率图的标题 |
String rateTitle = "" ; |
rateTitle = parse.getRateImgName(); |
|
JFreeChart chart2 = ChartFactory.createLineChart(rateTitle, // 图标题(第二张图的标题) |
"" , // x 轴标题 |
"" , // y 轴标题 |
getLineRateDataset(condVo, list, parse), // 数据源 |
PlotOrientation.VERTICAL, // 显示方向 |
true , // 显示图例 |
true , false ); |
|
logger.debug( "第二张比率图名称:" + parse.getRateImgName()); |
// 设置背景色 |
chart2.setBackgroundPaint( new Color( 218 , 235 , 255 )); |
|
CategoryPlot plot2 = chart2.getCategoryPlot(); |
|
// 获取 x 轴操作 |
// 设置数据区(中间部分背景色) |
plot2.setBackgroundPaint( new Color( 255 , 255 , 206 )); |
// x轴 // 分类轴网格是否可见 |
plot2.setDomainGridlinesVisible( false ); |
plot2.setDomainGridlinePaint(Color.WHITE); // 虚线色彩 |
// y轴 //数据轴网格是否可见 |
plot2.setRangeGridlinesVisible( true ); |
plot2.setRangeGridlinePaint(Color.BLACK); // 虚线色彩 |
|
CategoryAxis axis2 = plot2.getDomainAxis(); |
axis2.setCategoryLabelPositions(CategoryLabelPositions.UP_45); |
// 设置距离图片左端距离 |
axis2.setLowerMargin( 0.0 ); |
// 设置距离图片右端距离 |
axis2.setUpperMargin( 0.0 ); |
|
// 获取 y 轴操作: |
NumberAxis numAxis2 = (NumberAxis) plot2.getRangeAxis(); |
// 设置y轴最小值 |
numAxis2.setLowerBound( 0 .0D); |
// 设置Legend的位置 图例的位置(默认位置在图形的下面) |
// ((JFreeChart) |
// chart).getLegend().setPosition(RectangleEdge.RIGHT); |
// 显示百分比 |
NumberFormat nf22 = new DecimalFormat( "00.00%" ); |
numAxis2.setNumberFormatOverride(nf22); // 设置y轴以百分比方式显示 |
|
// 设置y显示方式 就是y轴间距(一般自动就可以) |
// numAxis2.setAutoTickUnitSelection(false);// 数据轴的数据标签是否自动确定 |
// double rangetick = 1000D; |
// numAxis2.setTickUnit(new NumberTickUnit(rangetick)); // |
// y轴单位间隔为0.1 |
// 下面使纵坐标的最小单位格为整数 |
// numAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); |
|
// 自动设置数据轴数据范围时数据范围的最小跨度(未确定) |
// void setAutoRangeMinimumSize(double size) |
|
// void setTickUnit(NumberTickUnit unit)数据轴的数据标签(未确定) |
// (需要将AutoTickUnitSelection设false) |
// numAxis2.setTickUnit(new NumberTickUnit(5)); |
// numAxis2.setAutoTickUnitSelection(false); |
|
LineAndShapeRenderer lineRenderer2 = (LineAndShapeRenderer) plot2 |
.getRenderer(); |
|
// series点(即数据点)可见 |
lineRenderer2.setBaseShapesVisible( true ); |
// series点(即数据点)间有连线可见 |
lineRenderer2.setBaseLinesVisible( true ); |
// 显示折点数据 |
// lineRenderer2.setBaseItemLabelGenerator(new |
// StandardCategoryItemLabelGenerator()); |
// 设置ItemLabel点对应的数据是否显示 |
lineRenderer2.setBaseItemLabelsVisible( false ); |
|
// 设置Series颜色 |
// lineRenderer2.setSeriesPaint(0, new Color(0,0,128));//蓝 |
// lineRenderer2.setSeriesPaint(0, new Color(255,0,255));//桃红 |
// lineRenderer2.setSeriesPaint(0, new Color(255,255,128));//黄色 |
// lineRenderer2.setSeriesPaint(0, new Color(0,0,128));//蓝 |
// lineRenderer2.setSeriesPaint(0, new Color(0,0,128));//蓝 |
// lineRenderer2.setSeriesPaint(0, new Color(0,0,128));//蓝 |
// lineRenderer2.setSeriesPaint(0, new Color(0,0,128));//蓝 |
|
// 设置第一条曲线数据点"填充"为红色,如果一个图表有多条曲线可分别设置 |
// lineRenderer2.setSeriesFillPaint(0, new Color(155,155,155)); //红 |
// lineRenderer2.setUseFillPaint(true); //应用 |
|
jfreechart[ 1 ] = chart2; // 将创建好的比率图JFreeChart 对象放入JfreeChart数组 |
|
} |
logger.debug( "这个JfreeChart对象数组大小:" + jfreechart.length); |
|
return jfreechart; |
} |
|
/** |
* 产生折线图所需要的数据,供createLineChart()方法调用 |
* |
* @param condVo |
* @param list |
* @return |
* @throws UnsupportedEncodingException |
*/ |
private DefaultCategoryDataset getLineDataset(ReportCondVo condVo, |
List list, ParseProperties parse) |
throws UnsupportedEncodingException, Exception { |
|
logger.debug( "创建折线图数据开始..." ); |
DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset(); |
|
// 取出字段类型 |
String[] rate = parse.getFieldName(); |
|
for ( int i = 0 ; i < rate.length; i++) { |
logger.debug( " ##包含统计类型:" + rate[i].toString()); |
// 第一步,先初始化一次数据,每个类型默认值0.0 |
// 产生时间格式样式 |
String datam = "000000" ; // 格式YYYYMM |
// 获取当月天数 |
ReportParams params = ReportService.getReportParams(condVo); |
// 取得当前循环类型 |
String Type = rate[i]; |
for ( int h = 1 ; h <= params.getColumns(); h++) { |
datam = condVo.getMonth(); |
String end = "" ; |
if ( 10 > h) { |
end = 0 + Integer.toString(h); |
} else { |
end = Integer.toString(h); |
} |
datam = datam + end; |
categoryDataset.addValue(getDouble( "0" ), Type, getDay(datam) |
.toString()); |
} |
// 把数据库取出来的List 赋值给categoryDataset对象 |
int size = list.size(); |
for ( int y = 0 ; y < size; y++) { |
RsVo rsVo = (RsVo) list.get(y); |
if (!( "" .equals(Type))) { |
if (Type.equals(rsVo.getType())) { |
categoryDataset.setValue(getDouble(rsVo.getValue()), |
Type, getDay(rsVo.getCurDate()).toString()); |
} |
} |
} |
} |
logger.debug( "创建折线图数据结束..." ); |
return categoryDataset; |
} |
|
/** |
* 产生比率图所需要数据,供createLineChart()方法调用 |
* |
* @param condVo |
* @param list |
* @param parse |
* @return |
* @throws UnsupportedEncodingException |
*/ |
|
private CategoryDataset getLineRateDataset(ReportCondVo condVo, List list, |
ParseProperties parse) throws UnsupportedEncodingException, |
Exception { |
logger.debug( "创建折线图(比率图)数据开始..." ); |
DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset(); |
|
// 取出比率图的字段类型(重要) |
String[] rate = parse.getRateFieldName(); |
|
// 开始创建数据 |
|
for ( int i = 0 ; i < rate.length; i++) { |
|
logger.debug( " ##包含统计类型(比率图):" + rate[i].toString()); |
|
// 产生时间格式 |
String datam = "000000" ; // 格式YYYYMM |
// 获取当月天数 |
ReportParams params = ReportService.getReportParams(condVo); |
// 先初始化一次值 |
|
// 取得当前循环类型 |
String Type = rate[i]; |
|
// 第一步:初始化折现图标比率图数据,给每日数据都0.00D |
for ( int h = 1 ; h <= params.getColumns(); h++) { |
datam = condVo.getMonth(); |
String end = "" ; |
if (h < 10 ) { |
end = 0 + Integer.toString(h); |
} else { |
end = Integer.toString(h); |
} |
datam = datam + end; |
categoryDataset.addValue( 0 .00d, rate[i], getDay(datam)); |
} |
|
// 第二步:赋值给比率图 |
|
int size = list.size(); |
logger.debug( "比率图中所含数据List的size值:" + list.size()); |
for ( int y = 0 ; y < size; y++) { |
RsVo rsVo = (RsVo) list.get(y); |
// 判断当前类型是否与rsVo.getType()类型一致 |
|
if (!( "" .equals(Type))) { |
if (Type.equals(rsVo.getType())) { |
categoryDataset.setValue( |
getRateDouble(rsVo.getValue()), Type, getDay( |
rsVo.getCurDate()).toString()); |
} |
} |
} |
|
} |
logger.debug( "创建折线图(比率图)数据成功..." ); |
return categoryDataset; |
} |
|
/** |
* X轴时间的产生 例:将字符串"20090103"转化为"1月03日"的字符串 |
* |
* @param dayStr |
* @return |
*/ |
private String getDay(String dayStr) { |
// 如果为空或者长度不等于8位,直接返回 |
if ( "" .equals(dayStr)) { |
return dayStr; |
} else if ( 8 != dayStr.length()) { |
return dayStr; |
} else { |
String month = dayStr.substring( 4 , 6 ); |
if (month.startsWith( "0" )) { |
month = month.substring( 1 , 2 ); |
} |
month = month + "月" ; |
String date = dayStr.substring( 6 , 8 ) + "日" ; |
String day = month + date; |
return day; |
} |
|
} |
|
/** |
* 将传入字符串转化为double类型 |
* |
* @param valueStr |
* @return |
*/ |
private double getDouble(String valueStr) { |
if (valueStr.endsWith( "%" )) { |
valueStr = valueStr.substring( 0 , valueStr.length() - 1 ); |
} |
if ( "" .equals(valueStr)) { |
return 0 .0d; |
} |
if ( null == valueStr) { |
return 0 .0d; |
} |
double value = Double.parseDouble(valueStr); |
return value; |
} |
|
/** |
* 将传入字符串转化为double类型 (比率图) |
* |
* @param valueStr |
* @return |
*/ |
private double getRateDouble(String valueStr) { |
if (valueStr.endsWith( "%" )) { |
valueStr = valueStr.substring( 0 , valueStr.length() - 1 ); |
} |
if ( "" .equals(valueStr)) { |
return 0 .0d; |
} |
if ( null == valueStr) { |
return 0 .0d; |
} |
|
double value = Double.parseDouble(valueStr); |
value = value / 100 ; |
return value; |
} |
|
/** |
* 创建饼图对象的JFreeChart 对象 |
* |
* @param condVo |
* @param list |
* @return |
* @throws Exception |
*/ |
public JFreeChart[] createPieChart(ReportCondVo condVo, List list) |
throws Exception { |
|
// 创建一个解析配置文件类的对象 |
ParseProperties parse = new ParseProperties(String.valueOf(condVo |
.getReportId())); |
|
JFreeChart[] jfreechart = new JFreeChart[ 2 ]; |
|
// 饼图的标题 |
String ChartTitle = "" ; |
ChartTitle = parse.getName(); |
|
// 用工厂类创建饼图 |
JFreeChart pieChart = ChartFactory.createPieChart(ChartTitle, |
createPieDataset(condVo, list, parse), true , true , false ); |
|
// RenderingHints做文字渲染参数的修改 |
// VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭. |
pieChart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, |
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); |
|
// 设置Legend的位置 图例的位置 |
// ((JFreeChart) pieChart).getLegend().setPosition(RectangleEdge.RIGHT); |
|
// 设置图片背景色 |
pieChart.setBackgroundPaint( new Color( 191 , 191 , 255 )); |
|
// 设置图标题的字体重新设置title |
Font font = new Font( "隶书" , Font.BOLD, 25 ); |
TextTitle title = new TextTitle(ChartTitle); |
title.setFont(font); |
// pieChart.setTitle(title); //用来控制Chart标题的 |
|
// 得到饼图的Plot对象 |
PiePlot piePlot = (PiePlot) pieChart.getPlot(); |
|
// 指定饼图轮廓线的颜色 |
piePlot.setBaseSectionOutlinePaint(Color.BLACK); |
piePlot.setBaseSectionPaint(Color.BLACK); |
|
// 设置背景区域背景色 |
piePlot.setBackgroundPaint( new Color( 191 , 191 , 255 )); |
setSection(piePlot); |
setLabel(piePlot); |
|
// 设置没数据显示信息 |
setNoDataMessage(piePlot); |
// 设置对于null数据和0数据的处理 |
setNullAndZeroValue(piePlot); |
jfreechart[ 0 ] = pieChart; |
return jfreechart; |
} |
|
/** |
* 创建饼图对象的数据源 DefaultPieDataset |
* |
* @param condVo |
* @param list |
* @param parse |
* @return |
* @throws UnsupportedEncodingException |
* @throws Exception |
*/ |
public DefaultPieDataset createPieDataset(ReportCondVo condVo, List list, |
ParseProperties parse) throws UnsupportedEncodingException, |
Exception { |
logger.debug( "创建饼图数据开始..." ); |
int size = list.size(); |
logger.debug( "饼图list数据大小" + size); |
DefaultPieDataset pieDataset = new DefaultPieDataset(); |
|
String[] TypeName = parse.getFieldName(); |
for ( int i = 0 ; i < TypeName.length; i++) { |
logger.debug( " ##饼图包含统计类型:" + TypeName[i].toString()); |
pieDataset.setValue(TypeName[i].toString(), 0 ); |
String keyWord = "" ; |
keyWord = TypeName[i].toString(); |
// 设置数据 |
for ( int y = 0 ; y < size; y++) { |
RsVo rsVo = (RsVo) list.get(y); |
if (!( "" .equals(keyWord))) { |
if (keyWord.equals(rsVo.getType())) { |
|
pieDataset.setValue(rsVo.getType(), Integer |
.parseInt(rsVo.getValue())); |
} |
} |
} |
} |
logger.debug( "创建饼图数据成功..." ); |
return pieDataset; |
} |
|
/** |
* plot对象设置的方法,提供配置饼图扇区颜色的方法 |
* |
* @param pieplot |
*/ |
@SuppressWarnings ( "deprecation" ) |
public void setSection(PiePlot pieplot) { |
// 扇区颜色设置 |
pieplot.setSectionPaint( 0 , new Color( 153 , 153 , 255 )); |
pieplot.setSectionPaint( 1 , new Color( 153 , 53 , 102 )); |
pieplot.setSectionPaint( 2 , new Color( 255 , 255 , 204 )); |
pieplot.setSectionPaint( 3 , new Color( 204 , 255 , 255 )); |
pieplot.setSectionPaint( 4 , new Color( 102 , 0 , 102 )); // 紫色 |
pieplot.setSectionPaint( 5 , new Color( 255 , 128 , 128 )); // 桃红 |
pieplot.setSectionPaint( 6 , new Color( 0 , 102 , 204 )); // 蓝 |
pieplot.setSectionPaint( 7 , new Color( 204 , 204 , 255 )); |
|
// 设置扇区分离显示 |
// pieplot.setExplodePercent("测试数据1", 0.2D); |
// 设置扇区边框不可见 |
// pieplot.setSectionOutlinesVisible(false); |
} |
|
/** |
* 设置扇区标题格式的方法类 |
* |
* @param pieplot |
*/ |
public void setLabel(PiePlot pieplot) { |
// 设置扇区标签显示格式:关键字:值(百分比) |
// pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator( |
// "{0}:{1}({2} percent)")); |
pieplot.setLabelGenerator( new StandardPieSectionLabelGenerator( |
"{0} {2}" , NumberFormat.getNumberInstance(), new DecimalFormat( |
"0.00%" ))); |
// 设置扇区标签颜色 |
pieplot.setLabelBackgroundPaint( new Color( 220 , 220 , 220 )); |
pieplot.setLabelFont(( new Font( "宋体" , Font.PLAIN, 15 ))); // 设置扇区标签字体大小 |
|
} |
|
/** |
* 设置饼图对象没数据时所显示内容和字体 |
* |
* @param pieplot |
*/ |
public void setNoDataMessage(PiePlot pieplot) { |
// 设置没有数据时显示的信息 |
pieplot.setNoDataMessage( "无数据" ); |
// 设置没有数据时显示的信息的字体 |
pieplot.setNoDataMessageFont( new Font( "宋体" , Font.BOLD, 14 )); |
// 设置没有数据时显示的信息的颜色 |
pieplot.setNoDataMessagePaint(Color.red); |
} |
|
/** |
* 设置饼图对象数据是否忽略0和null值 |
* |
* @param piePlot |
*/ |
public void setNullAndZeroValue(PiePlot piePlot) { |
// 设置是否忽略0和null值 |
piePlot.setIgnoreNullValues( false ); |
piePlot.setIgnoreZeroValues( false ); |
} |
|
} |
中级程序员
by: ykai160542 发表于:2013-09-26 15:35:04 顶(0) | 踩(0) 回复
这个不错啊啊啊
回复评论