我在只实现了柱状图,其它的图形可以参看我的JavaWeb部门的中图形报表说明!
步骤说明:
1,导入log4j.jar,jfreechart-0.9.18.jar,jcommon-0.9.3.jar三个jar包
2,在某个包中写入BarChart.java类
private DefaultCategoryDataset dataset = new DefaultCategoryDataset();
public void setValue(int value, String rowKey, String columnKey) {
dataset.setValue(value, rowKey, columnKey);
}
public String generateBarChart(String title, HttpSession session,PrintWriter pw,int width,int heigth) {
String filename = null;
try {
JFreeChart chart = ChartFactory
.createBarChart3D(title, null, null, dataset,
PlotOrientation.VERTICAL, true, false, false);
chart.setBackgroundPaint(Color.WHITE);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
//domainAxis.setVerticalCategoryLabels(false);
plot.setDomainAxis(domainAxis);
ValueAxis rangeAxis = plot.getRangeAxis();
// 设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
// 设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
// 设置 Wall 的颜色
renderer.setWallPaint(Color.gray);
// 设置每种水果代表的柱的颜色
renderer.setSeriesPaint(0, new Color(0, 0, 255));
renderer.setSeriesPaint(1, new Color(0, 100, 255));
renderer.setSeriesPaint(2, Color.GREEN);
// 设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.1);
// 显示每个柱的数值,并修改该数值的字体属性
//renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelsVisible(true);
plot.setRenderer(renderer);
// 设置柱的透明度
plot.setForegroundAlpha(0.6f);
// 设置地区、销量的显示位置
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, width, heigth,
info, session);
ChartUtilities.writeImageMap(pw, filename, info);
//pw.flush();
//若你的访问数据库的代码和显示图片同在一个jsp中,就里就不能去掉**********
//否则你就在这要注释掉
} catch (Exception e) {
e.printStackTrace();
}
return filename;
}
3,配置web.xml,在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>/DisplayChart</url-pattern>
</servlet-mapping>
4,在要显示图片的jsp中添加内容:
在action中去访问数据库,我这就不访问了
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
BarChart bch = new BarChart();
//第一组
bch.setValue(200,"苹果","武汉");
bch.setValue(400,"香蕉","武汉");
bch.setValue(500,"橘子","武汉");
bch.setValue(300,"梨子","武汉");
//第二组
bch.setValue(400,"苹果","黄冈");
bch.setValue(300,"香蕉","黄冈");
bch.setValue(100,"橘子","黄冈");
bch.setValue(300,"梨子","黄冈");
//第三组
bch.setValue(600,"苹果","A");
bch.setValue(300,"香蕉","A");
bch.setValue(400,"橘子","A");
bch.setValue(200,"梨子","A");
HttpSession session=request.getSession();
//System.out.println("默认的格式:"+response.getCharacterEncoding());
//这里在改成gbk格式,否则在显示图片的jsp中不能显示中文
response.setCharacterEncoding("gbk");
String filename=null;
try {
filename = bch.generateBarChart("水果比图", session,
new PrintWriter(response.getWriter()),700,500);
} catch (IOException e) {
e.printStackTrace();
}
String url = request.getContextPath() + "/DisplayChart?filename="
+ filename;
request.setAttribute("filename", filename);
request.setAttribute("url", url);
try {
request.setCharacterEncoding("gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return mapping.findForward("show");
}
5,有两点要注意:
***************第一:在工具类BarChart.java:把pw.flush();去掉或注释掉
***************第二:在filename = bch.generateBarChart("水果比图", session,
new PrintWriter(response.getWriter()),700,500);
之前一定要//System.out.println("默认的格式:"+response.getCharacterEncoding());
//这里在改成gbk格式,否则在显示图片的jsp中不能显示中文
response.setCharacterEncoding("gbk");
6,在jsp中:
<table align="center" border="0">
<tr>
<td>
<P ALIGN="CENTER">
<img src=${url } width=700 height=500 border=1 usemap=${filename }>
</p>
</td>
</tr>
</table>