JFreeChart 之一:柱状图实现
注: 项目使用SSH , jfreechart-1.0.13 版本
准备工作:
1:JFreeChart Jar包 可去官方下载JFreeChart ,此处我引用了 jcommon-1.0.16.jar , jfreechart-1.0.13.jar
放入WEB-INF\lib
2:添加servlet 支持,打开web.xml 文件 添加以下支持
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/servlet/DisplayChart</url-pattern> </servlet-mapping>
3: 编写报表
/* Action: */
// 从数据库中查询 需要显示的数据
List list = commonService.findBySql(query);
int count=0;
DefaultCategoryDataset data = new DefaultCategoryDataset();
if(list!=null && list.size()>0){
int len = list.size();
// 组建数据
for(int i=0;i<len;i++){
Object[] obj = (Object[]) list.get(i);
count+=new Double (obj[1].toString());
data.addValue(new Double(obj[1].toString()), "", obj[0].toString()+"月");
}
CategoryDataset dataset = data;
JFreeChart chart = ChartFactory.createBarChart3D("",year+"年","人数",dataset,PlotOrientation.VERTICAL,true,true,false);
//设置字体,不然会中文乱码的
Font font = new Font("宋体", Font.BOLD, 16);
TextTitle title = new TextTitle("每月录入统计", font);
//副标题
TextTitle subtitle = new TextTitle("总人数:"+count, new Font("宋体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setTitle(title); //标题
//////////////////////////
JFreeChartUtil.barChart3DStyle(chart);
String filename = ServletUtilities.saveChartAsPNG(chart, 800, 600,null, request.getSession());
String pieUrl = request.getContextPath()+ "/servlet/DisplayChart?filename=" + filename;
request.setAttribute("chartUrl", pieUrl);
JFreeChartUtil / barChart3DStyle
/* 柱状图样式 */
public static void barChart3DStyle(JFreeChart chart) {
CategoryPlot plot = chart.getCategoryPlot();
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
CategoryAxis domainAxis = plot.getDomainAxis();
/*------设置X轴坐标上的文字-----------*/
// domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 11));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
/*------设置Y轴坐标上的文字-----------*/
// numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
numberaxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
/*------这句代码解决了底部汉字乱码的问题-----------*/
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
//设置网格背景颜色
plot.setBackgroundPaint(Color.white);
//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);
//设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);
//显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
//设置 底部分类 不显示
renderer.setBaseSeriesVisibleInLegend(false);
//默认的数字显示在柱子中,通过如下两句可调整数字的显示
//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));
renderer.setItemLabelsVisible(true);
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.3);
plot.setRenderer(renderer);
//设置地区、销量的显示位置
//将下方的“年”放到上方
// plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
//将默认放在左边的“人数”放到右方
// plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
}
JSP
<img src="${chartUrl}" mce_src="${chartUrl}" />