最近在研究jFreeChart,做了几个报表,也是通过别人的blog学习的。
public class HotMap{
private static DefaultCategoryDataset dataset = new DefaultCategoryDataset();
private DefaultCategoryDataset getDataset() {// 取数据
dataset.addValue(100,"北京", "苹果");
dataset.addValue(200,"上海", "橘子");
dataset.addValue(180,"南京", "西瓜");
dataset.addValue(230,"江西", "香蕉");
dataset.addValue(400,"深圳", "菠萝");
dataset.addValue(190,"珠海", "葡萄");
return dataset;
}
public String drawPic(HttpSession session, JspWriter out) {
String fileName = null;
JFreeChart chart = ChartFactory.createBarChart3D("各地水果价格", "类型",
"金额(单位:元)", getDataset(), PlotOrientation.VERTICAL, true, true,
true);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();// 获取绘图区
plot.setBackgroundPaint(new Color(255, 255, 255)); // 设置绘图区背景色
plot.setRangeGridlinePaint(Color.gray); // 设置水平方向背景线颜色
plot.setRangeGridlinesVisible(true); // 设置是否显示水平方向背景线,默认值为True
plot.setDomainGridlinePaint(Color.black); // 设置垂直方向背景线颜色
// plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为False
CategoryAxis domainAxis = plot.getDomainAxis();// 获取统计种类轴标题(X轴)
plot.setDomainAxis(domainAxis);// 添加X轴
BarRenderer3D renderer = new BarRenderer3D();// 获得BarRenderer3D类的实例,目的是设置柱形的绘制属性
renderer
.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());//
//renderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator(
// "index2.jsp"));// 生成热点,用于链接
renderer.setItemMargin(0.1);// 设置每个组所包含的平行柱的之间距离
renderer.setSeriesPaint(0, Color.GREEN);// 设置柱子的颜色
renderer.setSeriesPaint(1, Color.blue);// 设置柱子的颜色
renderer.setBaseOutlinePaint(Color.BLACK);
renderer.setWallPaint(Color.gray);// 设置 Wall 的颜色
renderer.setItemLabelAnchorOffset(10D);// 设置柱形图上的文字偏离值
renderer.setBaseItemLabelFont(new Font("arial", Font.PLAIN, 10), true);// 设置柱形图上的文字
renderer
.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());// //显示每个柱的数值,并修改该数值的字体属性
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
renderer.setBaseItemLabelsVisible(true);
renderer.setMaximumBarWidth(0.050000000000000003D);
plot.setRenderer(renderer);
plot.setForegroundAlpha(0.80f);// 设置柱的透明度
// plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);//设置显示位置
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);// 设置显示位置
try {
PrintWriter pw = new PrintWriter(out);
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
fileName = ServletUtilities.saveChartAsPNG(chart, 640, 400, info,
session);
// ChartUtilities.writeChartAsPNG(op,chart, 640, 400, info,true,0);
ChartUtilities.writeImageMap(pw, fileName, info, true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileName;
}
}