DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(SsrRatingResultDetailVO vo : volist)
{
dataset.addValue(Double.valueOf(vo.getPercentage()), vo.getLevelResultStr(), vo.getDrName());
}
JFreeChart chart = ChartFactory.createBarChart("级别统计柱状图","总监名称","比例(%)",dataset
,PlotOrientation.VERTICAL,true,false,false);
//背景色
chart.setBackgroundPaint(new Color(235,239,249));
CategoryPlot plot = chart.getCategoryPlot();
//设置柱子的透明度,3D时使用
plot.setForegroundAlpha(0.80f);
//没有数据的时候显示的内容
plot.setNoDataMessage("无数据显示");
//设置柱子的颜色(按rowKey)
BarRenderer3D renderer = new BarRenderer3D();
renderer.setSeriesPaint(0, new Color(241,9,14)); //9级
renderer.setSeriesPaint(1, new Color(243,92,0)); //8级
renderer.setSeriesPaint(2, new Color(100,0,220));
renderer.setSeriesPaint(3, new Color(2,93,220));
renderer.setSeriesPaint(4, new Color(142,204,241));
renderer.setSeriesPaint(5, new Color(15,160,0));
renderer.setSeriesPaint(6, new Color(173,220,0));
//设置柱子上是否显示柱子的数值
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
//设置柱子上数值的显示位置,当柱子的比例过小时,柱子的数值将无法显示,可以更改数值的显示位置在柱子上方。当数值过长时,可设置数值的偏移度,最后一个参数(-1.57D)为旋转的角度值(90°)/3.14.
renderer.setPositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_LEFT,TextAnchor.HALF_ASCENT_LEFT,-1.57D));
renderer.setItemLabelFont(new Font("黑体",Font.BOLD,12));
//使用设置
plot.setRenderer(renderer);
CategoryAxis caxis = plot.getDomainAxis();
//设置组间间隔
caxis.setCategoryMargin(0.15);
//设置标签轴第一个图例距数值轴的距离
caxis.setLowerMargin(0.03);
caxis.setUpperMargin(0.03);
//设置标签的字体
caxis.setLabelFont(new Font("SansSerif",Font.BOLD,13));
//设置组名称的字体
caxis.setTickLabelFont(new Font("SansSerif",Font.TRUETYPE_FONT,12));
NumberAxis naxis = (NumberAxis) plot.getRangeAxis();
//设置数值轴最大值和最小值
naxis.setUpperBound(100);
naxis.setLowerBound(0);
naxis.setLabelFont(new Font("SansSerif",Font.BOLD,13));
//保存图片到指定路径
try {
ChartUtilities.saveChartAsPNG(new File(picPath), chart, 600 , 400);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new BusinessException("生成级别统计比例图失败!");
}
参考:http://younglibin.javaeye.com/blog/528310
http://www.blogjava.net/amigoxie/archive/2007/09/30/149765.html