spring mvc-jfreeChart 柱图(1)

第一步:下载jfreeChart包,注意是jfreeChart文件lib目录下的,jcommon-1.0.23.jar和jfreechart-1.0.19.jar两个包即可;
第二步:配置web.xml;
web.xml代码如下
<servlet>
    <servlet-name>jfreechart</servlet-name>
    <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>jfreechart</servlet-name>
    <url-pattern>/servlet/DisplayChart</url-pattern>
  </servlet-mapping>

创建CreateJFreechartRegisterUtil类,用于作图

package com.practise.sword.util;
import java.awt.Color;
import java.awt.Font;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

public class CreateJFreechartRegisterUtil {
public String setImages(HttpServletRequest request, HttpServletResponse response)  throws Exception {
     CategoryDataset dataset = getDataSet();
     JFreeChart chart = ChartFactory.createBarChart3D(
         "网站注册用户统计图", //图表标题
         "注册用户", //目录轴的显示标签
         "人数", //数值轴的显示标签
         dataset, //数据集
         PlotOrientation.VERTICAL, //图表方向:水平、垂直
         true, //是否显示图例(对于简单的柱状图必须是false)
         false, //是否生成工具
         false //是否生成URL链接
     );
     CategoryPlot plot = chart.getCategoryPlot();
     BarRenderer renderer = new BarRenderer();
     plot.setRangeGridlinePaint(Color.blue);   //设置网格线颜色
     renderer.setBaseOutlinePaint(Color.ORANGE); //边框颜色
     renderer.setDrawBarOutline(true);
     renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//设置 柱子显示数值
     renderer.setBaseItemLabelsVisible(true);
     renderer.setBaseItemLabelPaint(Color.BLUE);//设置数值颜色,默认黑色
     plot.setBackgroundAlpha(0.5f);  //设置背景色
     plot.setRenderer(renderer);
     setImageFont(chart);  //解决乱码问题
     String filename = ServletUtilities.saveChartAsPNG(chart, 800, 400, null, request.getSession());//生成图片
     ServletContext servlet = request.getSession().getServletContext();
     String imagePath_area = servlet.getContextPath() + "/servlet/DisplayChart?filename=" + filename;//得到功能路径    
     return imagePath_area;
}

/**
  * 解决乱码问题
  */
public void setImageFont(JFreeChart chart){
CategoryPlot plot = (CategoryPlot)chart.getPlot();
     CategoryAxis domainAxis = plot.getDomainAxis();
     ValueAxis numberaxis = plot.getRangeAxis(); 
     chart.getTitle().setFont(new Font("宋体", Font.BOLD, 18));//设置标题文字    
     domainAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 7));//设置X轴坐标上的文字 
     domainAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));//设置X 轴的标题文字   
     numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));//设置Y 轴坐标上的文字
     numberaxis.setLabelFont(new Font("黑体", Font.BOLD, 14));//设置Y 轴的标题文字
     chart.getLegend().setVisible(false);  //不显示底部文字
}

/**
  * 获取一个组合数据集对象
  * @return
  */
public CategoryDataset getDataSet(){
     double[][] data = new double[][] { { 100,120,350,230,200,400,350,260,410,500,390,450 } }; 
     String[] rowKeys = { "" }; 
     String[] columnKeys = { "一月", "二月", "三月", "四月", "五月", "六月","七月","八月","九月","十月","十一月","十二月" }; 
     CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); 
     return dataset;
}
}

创建BarRenderer,用于给柱子加颜色

package com.practise.sword.util;
import java.awt.Color;
import java.awt.Paint;
import org.jfree.chart.renderer.category.BarRenderer3D;

public class BarRenderer extends BarRenderer3D {
private static final long serialVersionUID = 784630226449158436L; 
    private Paint[] colors; 
    //初始化柱子颜色 
    private String[] colorValues = { "#AAA8F8", "#F6BD0F", "#8BBA00", "#FF8E46", "#008E8E", "#D98628",
    "#ABD8F8", "#F9CD0F", "#1ABA00", "#BB8E46", "#009A8B", "#C08628" }; 
 
    public BarRenderer() { 
        colors = new Paint[colorValues.length]; 
        for (int i = 0; i < colorValues.length; i++) { 
            colors[i] = Color.decode(colorValues[i]); 
        } 
    } 
 
    //每根柱子以初始化的颜色不断轮循 
    public Paint getItemPaint(int i, int j) { 
        return colors[j % colors.length]; 
    } 
}

创建StatistickController类,用于请求

@Controller
@RequestMapping(value="${adminPath}/statistick")
public class StatistickController {
     /**
* 注册用户统计
*/
@RequestMapping(value="registerStatistick")
public String redisterStatistick(@RequestParam Map<String, Object> paramMap,HttpServletRequest request,HttpServletResponse response,Model model) throws Exception {  

CreateJFreechartRegisterUtil jfreeChart = new CreateJFreechartRegisterUtil();
String imagePath_area=jfreeChart.setImages(request, response);
request.setAttribute("imagePath_area", imagePath_area);//把得到的图片放到request范围里面
        return "modules/statistick/registerStatistick";
}
}

最后写jsp(部分代码)

<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center" class="table  table-bordered table-condensed">
<tr>
<td width="30%">
<img alt="abc" src="${imagePath_area}">
</td> </tr></table>

[img][/img]

你可能感兴趣的:(jfreechart)