jfreechart和springMVC+maven整合
来源:互联网 发布:网络分为哪几类 编辑:程序博客网 时间:2024/06/07 12:21
转自:http://blog.csdn.net/y_love_f/article/details/43120815
高校的项目中一个功能是对统计的成绩进行图表分析,查找了一些资料之后感觉jfreechar上手挺快的,简单的做了几个demo也都很好实现,也能够满足项目中的需求,所以就决定使用这个工具。这里就将学习到的一些关于jfreechar的知识整理一下。
1. 引入jar包
使用这个工具一定要将他的包引入的到项目,在高校的项目中使采用maven管理这些外部文件的,所以对于引入jar包这一步就变得特别简单了,只要在maven项目的pom.xml文件中添加jfreechar的依赖就可以(maven会自动在仓库中检索jar包然后下载到本地)
- <dependency>
- lt;span style="white-space:pre"> </span> <groupId>jfreechart</groupId>
- <artifactId>jfreechart</artifactId>
- <version>1.0.0</version>
- </dependency>
之后我们就会在Maven Depenencies中看到jfreechar需要的jar包了(如果没有的话可以update project—快捷键是ALT+F5)。
2. 修改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>/chart</url-pattern>
- </servlet-mapping>
上面两步就是jfreechar和springMVC+maven的整合。
3. 实现一个柱形图
下面就做一个小例子来熟悉一个这个工具—创建一个柱形图
web层中controller中代码
- package com.tgb.itoo.chart.controller;
-
- import java.awt.Font;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.jfree.chart.ChartColor;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.axis.CategoryAxis;
- import org.jfree.chart.axis.ValueAxis;
- import org.jfree.chart.plot.CategoryPlot;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.chart.renderer.category.BarRenderer;
- import org.jfree.chart.servlet.ServletUtilities;
- import org.jfree.chart.title.TextTitle;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.category.DefaultCategoryDataset;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
- @Controller
- public class ColumnChartController {
-
- @RequestMapping("/resultmap")
- public String resultmap(){
- return "/resultmap";
- }
-
-
- @RequestMapping(value = "/getColumnChart")
- public ModelAndView getColumnChart(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception{
-
- CategoryDataset dataset = getDataSet();
-
- JFreeChart chart = ChartFactory.createBarChart3D("学生对教师授课满意度",
- "课程名",
- "百分比",
- dataset,
- PlotOrientation.VERTICAL,
- false,
- false,
- false
- );
-
- chart.setBackgroundPaint(ChartColor.WHITE);
-
-
- CategoryPlot p = chart.getCategoryPlot();
- p.setBackgroundPaint(ChartColor.lightGray);
- p.setRangeGridlinePaint(ChartColor.WHITE);
-
-
- BarRenderer renderer = (BarRenderer)p.getRenderer();
- renderer.setMaximumBarWidth(0.06);
-
-
- getChartByFont(chart);
-
-
- String fileName = ServletUtilities.saveChartAsJPEG(chart, 700, 400, null, request.getSession());
- String chartURL=request.getContextPath() + "/chart?filename="+fileName;
- modelMap.put("chartURL", chartURL);
- return new ModelAndView("resultmap",modelMap);
- }
-
-
- private static void getChartByFont(JFreeChart chart) {
-
- TextTitle textTitle = chart.getTitle();
- textTitle.setFont(new Font("宋体",Font.BOLD,20));
-
-
- CategoryPlot plot = (CategoryPlot) chart.getPlot();
- CategoryAxis axis = plot.getDomainAxis();
- axis.setLabelFont(new Font("宋体",Font.BOLD,22));
- axis.setTickLabelFont(new Font("宋体",Font.BOLD,15));
-
-
- ValueAxis valueAxis = plot.getRangeAxis();
- valueAxis.setLabelFont(new Font("宋体",Font.BOLD,15));
- valueAxis.setTickLabelFont(new Font("sans-serif",Font.BOLD,12));
- }
-
-
- private static CategoryDataset getDataSet() {
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
- dataset.addValue(40, "", "普通动物学");
- dataset.addValue(50, "", "生物学");
- dataset.addValue(60, "", "动物解剖学");
- dataset.addValue(70, "", "生物理论课");
- dataset.addValue(80, "", "动物理论课");
- return dataset;
- }
- }
spring-mvc中的配置
- <bean name="columnChartController" class="com.tgb.itoo.chart.controller.ColumnChartController" />
web层中jsp页面 resultmap.jsp- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- </head>
- <body>
- <div style="text-align:center">
- jfreechart _3D柱状图 <br><br>
- 点击显示柱状图<a href="getColumnChart">getMajorChart</a>
- <br><br>
- <img src="${chartURL}" width=600 height=400 border=0 color=gray>
- </div>
-
- </body>
- </html>
结果展示: