首先要用到 JFreeChart.jar、jcommon-1.0.15.jar、log4j.jar
servlet代码:
package com.framework.test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.Color;
import java.awt.Font;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
import org.jfree.data.category.*;
import org.jfree.chart.*;
import org.jfree.data.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.*;
import com.framework.AppSession;
public class Test extends HttpServlet{
private static final String CONTENT_TYPE = "text/html;charset=utf-8";
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException {
response.setContentType( "image/jpeg ");
response.setCharacterEncoding("utf-8");
response.reset();
CategoryDataset data=(CategoryDataset) request.getSession().getAttribute("catedataset");//从session中取到CategoryDataset 对象
//CategoryDataset data=getDataSet2();
JFreeChart chart=ChartFactory.createBarChart( "", "", "点击率",data,PlotOrientation.VERTICAL,true,true,true);
//设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis
org.jfree.chart.axis.CategoryAxis domainAxis = chart.getCategoryPlot().getDomainAxis();
domainAxis.setLowerMargin(0.1);//设置距离图片左端距离此时为10%
domainAxis.setUpperMargin(0.1);//设置距离图片右端距离此时为百分之10
domainAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离(10像素)
domainAxis.setCategoryMargin(0.2);//横轴标签之间的距离20%
//取得纵轴
NumberAxis numberAxis = (NumberAxis)chart.getCategoryPlot().getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
//设置最高的一个柱与图片顶端的距离
numberAxis.setUpperMargin(0.1);
//numberAxis.setFixedAutoRange(100);
//设置整数显示
//numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//numberAxis.setNegativeArrowVisible(true);
//取最大数Math.max(supportCount, blackballCount)
int totalcount= (Integer) request.getSession().getAttribute("totalcount");
numberAxis.setUpperBound(totalcount);//动态设置纵坐标高度
numberAxis.setLowerBound(0.01);
//设置百分比显示
// numberAxis.setNumberFormatOverride(new DecimalFormat("0%"));
//numberAxis.setNumberFormatOverride(new DecimalFormat("0.00%"));
//设置最小显示数,小于的话会显示在中间(正负)
//numberAxis.setAutoRangeMinimumSize(1);
Font font=new Font("宋体" , Font.BOLD , 12);
String tit=(String) request.getSession().getAttribute("title");
TextTitle title = new TextTitle(tit, font);//动态设置主标题
chart.setTitle(title);
//设定柱子的属性
org.jfree.chart.axis.ValueAxis rangeAxis = chart.getCategoryPlot().getRangeAxis();
rangeAxis.setUpperMargin(0.1);//设置最高的一个柱与图片顶端的距离(最高柱的10%)
org.jfree.chart.renderer.category.BarRenderer3D renderer;
renderer = new org.jfree.chart.renderer.category.BarRenderer3D();
renderer.setItemMargin(0.4);//组内柱子间隔为组宽的10%
//显示每个柱的数值,并修改该数值的字体属性
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelFont(new Font("黑体",Font.BOLD,10));//12号黑体加粗
renderer.setItemLabelPaint(Color.black);//字体为黑色
renderer.setItemLabelsVisible(true);
chart.getCategoryPlot().setRenderer(renderer);
chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,12));//设置图下面框内中文字体不为乱码
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),1,chart,400,300,null);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
private static CategoryDataset getDataSet2() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(0.25, "beijing", "test");
dataset.addValue(0.5, "shanghai", "test");
dataset.addValue(0.8, "guangzhou", "test");
dataset.addValue(0.1, "shanxi", "test");
return dataset;
}
public void destroy() {
}
}
Action代码:
DefaultCategoryDataset catedataset = new DefaultCategoryDataset();
catedataset.addValue();//动态添加,参考Test中getDataSet2()
AppContext.getServletRequest().getSession(). setProperty("catedataset",catedataset);
AppContext.getServletRequest().getSession(). setProperty("totalcount", totalcount);
AppContext.getServletRequest().getSession(). setProperty("title", investigate.getTitle());
jsp页面代码:
<img src="${pageContext.request.contextPath}/s/draw.htm" alt="" />
web.xml中配置:
<servlet>
<servlet-name>draw</servlet-name>
<servlet-class>com.framework.test.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>draw</servlet-name>
<url-pattern>/s/draw.htm</url-pattern>
</servlet-mapping>
——————————————————————————————————————————————
其实可以不用这个,完全可以根据生成的数据在jsp页面动态控制一张图片的高度或宽度来显示比例