jfreechart生成折线图 | 饼图 | 柱状图 | 堆栈柱状图 (五)

2014-11-24 10:11:45 · 作者: · 浏览: 4
hartAsPNG(fos_jpg, chart, 500, 500, true, 10);
return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 饼状图
*
* @param dataset
* 数据集
* @param chartTitle
* 图标题
* @param charName
* 生成图的名字
* @param pieKeys
* 分饼的名字集
* @return
*/
public String createva lidityComparePimChar(PieDataset dataset,
String chartTitle, String charName, String[] pieKeys) {
JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, // chart
// title
dataset,// data
true,// include legend
true, false);

// 使下说明标签字体清晰,去锯齿类似于
// chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);的效果
chart.setTextAntiAlias(false);
// 图片背景色
chart.setBackgroundPaint(Color.white);
// 设置图标题的字体重新设置title
Font font = new Font("隶书", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);

PiePlot3D plot = (PiePlot3D) chart.getPlot();
// 图片中显示百分比:默认方式

// 指定饼图轮廓线的颜色
// plot.setBaseSectionOutlinePaint(Color.BLACK);
// plot.setBaseSectionPaint(Color.BLACK);

// 设置无数据时的信息
plot.setNoDataMessage("无对应的数据,请重新查询。");

// 设置无数据时的信息显示颜色
plot.setNoDataMessagePaint(Color.red);

// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})"));

plot.setLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12));

// 指定图片的透明度(0.0-1.0)
plot.setForegroundAlpha(0.65f);
// 指定显示的饼图上圆形(false)还椭圆形(true)
plot.setCircular(false, true);

// 设置第一个 饼块section 的开始位置,默认是12点钟方向
plot.setStartAngle(90);

// // 设置分饼颜色
plot.setSectionPaint(pieKeys[0], new Color(244, 194, 144));
plot.setSectionPaint(pieKeys[1], new Color(144, 233, 144));

FileOutputStream fos_jpg = null;
try {
// 文件夹不存在则创建
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);
// 高宽的设置影响椭圆饼图的形状
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 230);

return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
System.out.println("create pie-chart.");
} catch (Exception e) {
e.printStackTrace();
}
}

}

/**
* 判断文件夹是否存在,如果不存在则新建
*
* @param chartPath
*/
private void isChartPathExist(String chartPath) {
File file = new File(chartPath);
if (!file.exists()) {
file.mkdirs();
// log.info("CHART_PATH="+CHART_PATH+"create.");
}
}

/**
* 折线图
*
* @param chartTitle
* @param x
* @param y
* @param xyDataset
* @param charName
* @return
*/
public String createTimeXYChar(String chartTitle, String x, String y,
CategoryDataset xyDataset, String charName) {

JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y,
xyDataset, PlotOrientation.VERTICAL, true, true, false);

chart.setTextAntiAlias(false);
chart.setBackgrou