使用 jfreechart 生成 曲线、柱状图、饼状图、分布图 展示到JSP

虽然现在JS做报表和图形展示已经非常普遍和漂亮了,但是不能忽略有jfreechart 这样一种东西!

这些翻阅资料,在看以前写的示例时发现了关于jfreechart 的简单示例,不管怎样发上来分享一下!

 

 

这个示例使用JSP和Servlet做后台和前台展示,下面有源代码可以直接运行

生产线型趋势图:

 

Java代码 复制代码 收藏代码
  1. package com.xidian.servlet;   
  2. import java.awt.Color;   
  3. import java.awt.Font;   
  4. import java.io.*;   
  5. import java.text.SimpleDateFormat;   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.http.HttpServlet;   
  8. import javax.servlet.http.HttpServletRequest;   
  9. import javax.servlet.http.HttpServletResponse;   
  10. import org.jfree.chart.*;   
  11. import org.jfree.chart.axis.DateAxis;   
  12. import org.jfree.chart.axis.ValueAxis;   
  13. import org.jfree.chart.plot.XYPlot;   
  14. import org.jfree.chart.renderer.xy.XYItemRenderer;   
  15. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;   
  16. import org.jfree.chart.title.TextTitle;   
  17. import org.jfree.data.time.Day;   
  18. import org.jfree.data.time.TimeSeries;   
  19. import org.jfree.data.time.TimeSeriesCollection;   
  20. import org.jfree.data.xy.XYDataset;   
  21. /**  
  22.  * 生产线型趋势图  
  23.  * @说明   
  24.  * @author cuisuqiang  
  25.  * @version 1.0  
  26.  * @since  
  27.  */  
  28. @SuppressWarnings("serial")   
  29. public class LineServlet extends HttpServlet {   
  30.     @SuppressWarnings("deprecation")   
  31.     @Override  
  32.     protected void service(HttpServletRequest request,   
  33.             HttpServletResponse response) throws ServletException, IOException {   
  34.         response.setContentType("text/html");   
  35.         // 在Mysql中使用 select   
  36.         // year(accessdate),month(accessdate),day(accessdate),count(*)   
  37.         // 其中accessdate 是一个date类型的时间   
  38.         // 时间序列对象集合   
  39.         TimeSeriesCollection chartTime = new TimeSeriesCollection();   
  40.         // 时间序列对象,第1个参数表示时间序列的名字,第2个参数是时间类型,这里为天   
  41.         // 该对象用于保存前count天每天的访问次数   
  42.         TimeSeries timeSeries = new TimeSeries("日访问", Day.class);   
  43.         // 为了演示,直接拼装数据   
  44.         // Day的组装格式是day-month-year 访问次数   
  45.         timeSeries.add(new Day(112010), 50);   
  46.         timeSeries.add(new Day(212010), 47);   
  47.         timeSeries.add(new Day(312010), 82);   
  48.         timeSeries.add(new Day(412010), 95);   
  49.         timeSeries.add(new Day(512010), 104);   
  50.         timeSeries.add(new Day(612010), 425);   
  51.         chartTime.addSeries(timeSeries);   
  52.         XYDataset date = chartTime;   
  53.         try {   
  54.             // 使用ChartFactory来创建时间序列的图表对象   
  55.             JFreeChart chart = ChartFactory.createTimeSeriesChart(   
  56.                     "网站每天访问统计"// 图形标题   
  57.                     "日期"// X轴说明   
  58.                     "访问量"// Y轴说明   
  59.                     date, // 数据   
  60.                     true// 是否创建图例   
  61.                     true// 是否生成Tooltips   
  62.                     false // 是否生产URL链接   
  63.             );   
  64.             // 设置整个图片的背景色   
  65.             chart.setBackgroundPaint(Color.PINK);   
  66.             // 设置图片有边框   
  67.             chart.setBorderVisible(true);   
  68.             // 获得图表区域对象   
  69.             XYPlot xyPlot = (XYPlot) chart.getPlot();   
  70.             // 设置报表区域的背景色   
  71.             xyPlot.setBackgroundPaint(Color.lightGray);   
  72.             // 设置横 纵坐标网格颜色   
  73.             xyPlot.setDomainGridlinePaint(Color.GREEN);   
  74.             xyPlot.setRangeGridlinePaint(Color.GREEN);   
  75.             // 设置横、纵坐标交叉线是否显示   
  76.             xyPlot.setDomainCrosshairVisible(true);   
  77.             xyPlot.setRangeCrosshairVisible(true);   
  78.             // 获得数据点(X,Y)的render,负责描绘数据点   
  79.             XYItemRenderer xyItemRenderer = xyPlot.getRenderer();   
  80.             if (xyItemRenderer instanceof XYLineAndShapeRenderer) {   
  81.                 XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyItemRenderer;   
  82.                 xyLineAndShapeRenderer.setShapesVisible(true); // 数据点可见   
  83.                 xyLineAndShapeRenderer.setShapesFilled(true); // 数据点是实心点   
  84.                 xyLineAndShapeRenderer.setSeriesFillPaint(0, Color.RED); // 数据点填充为蓝色   
  85.                 xyLineAndShapeRenderer.setUseFillPaint(true);// 将设置好的属性应用到render上   
  86.             }   
  87.             // 配置以下内容方可解决乱码问题   
  88.             // 配置字体   
  89.             Font xfont = new Font("宋体", Font.PLAIN, 12);    // X轴   
  90.             Font yfont = new Font("宋体", Font.PLAIN, 12);    // Y轴   
  91.             Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部   
  92.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题   
  93.             // 图片标题   
  94.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));   
  95.             // 底部   
  96.             chart.getLegend().setItemFont(kfont);              
  97.             // X 轴   
  98.             ValueAxis domainAxis = xyPlot.getDomainAxis();   
  99.             domainAxis.setLabelFont(xfont);// 轴标题   
  100.             domainAxis.setTickLabelFont(xfont);// 轴数值   
  101.             domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色   
  102.             // Y 轴   
  103.             ValueAxis rangeAxis = xyPlot.getRangeAxis();   
  104.             rangeAxis.setLabelFont(yfont);   
  105.             rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色   
  106.             rangeAxis.setTickLabelFont(yfont);   
  107.             // 定义坐标轴上日期显示的格式   
  108.             DateAxis dateAxis = (DateAxis) xyPlot.getDomainAxis();   
  109.             // 设置日期格式   
  110.             dateAxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));   
  111.             // 向客户端输出生成的图片   
  112.             ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,   
  113.                     chart, 500300null);   
  114.         } catch (Exception e) {   
  115.             e.printStackTrace();   
  116.         }   
  117.     }   
  118. }  
package com.xidian.servlet;
import java.awt.Color;
import java.awt.Font;
import java.io.*;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
/**
 * 生产线型趋势图
 * @说明 
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
@SuppressWarnings("serial")
public class LineServlet extends HttpServlet {
	@SuppressWarnings("deprecation")
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		// 在Mysql中使用 select
		// year(accessdate),month(accessdate),day(accessdate),count(*)
		// 其中accessdate 是一个date类型的时间
		// 时间序列对象集合
		TimeSeriesCollection chartTime = new TimeSeriesCollection();
		// 时间序列对象,第1个参数表示时间序列的名字,第2个参数是时间类型,这里为天
		// 该对象用于保存前count天每天的访问次数
		TimeSeries timeSeries = new TimeSeries("日访问", Day.class);
		// 为了演示,直接拼装数据
		// Day的组装格式是day-month-year 访问次数
		timeSeries.add(new Day(1, 1, 2010), 50);
		timeSeries.add(new Day(2, 1, 2010), 47);
		timeSeries.add(new Day(3, 1, 2010), 82);
		timeSeries.add(new Day(4, 1, 2010), 95);
		timeSeries.add(new Day(5, 1, 2010), 104);
		timeSeries.add(new Day(6, 1, 2010), 425);
		chartTime.addSeries(timeSeries);
		XYDataset date = chartTime;
		try {
			// 使用ChartFactory来创建时间序列的图表对象
			JFreeChart chart = ChartFactory.createTimeSeriesChart(
					"网站每天访问统计", // 图形标题
					"日期", // X轴说明
					"访问量", // Y轴说明
					date, // 数据
					true, // 是否创建图例
					true, // 是否生成Tooltips
					false // 是否生产URL链接
			);
			// 设置整个图片的背景色
			chart.setBackgroundPaint(Color.PINK);
			// 设置图片有边框
			chart.setBorderVisible(true);
			// 获得图表区域对象
			XYPlot xyPlot = (XYPlot) chart.getPlot();
			// 设置报表区域的背景色
			xyPlot.setBackgroundPaint(Color.lightGray);
			// 设置横 纵坐标网格颜色
			xyPlot.setDomainGridlinePaint(Color.GREEN);
			xyPlot.setRangeGridlinePaint(Color.GREEN);
			// 设置横、纵坐标交叉线是否显示
			xyPlot.setDomainCrosshairVisible(true);
			xyPlot.setRangeCrosshairVisible(true);
			// 获得数据点(X,Y)的render,负责描绘数据点
			XYItemRenderer xyItemRenderer = xyPlot.getRenderer();
			if (xyItemRenderer instanceof XYLineAndShapeRenderer) {
				XYLineAndShapeRenderer xyLineAndShapeRenderer = (XYLineAndShapeRenderer) xyItemRenderer;
				xyLineAndShapeRenderer.setShapesVisible(true); // 数据点可见
				xyLineAndShapeRenderer.setShapesFilled(true); // 数据点是实心点
				xyLineAndShapeRenderer.setSeriesFillPaint(0, Color.RED); // 数据点填充为蓝色
				xyLineAndShapeRenderer.setUseFillPaint(true);// 将设置好的属性应用到render上
			}
			// 配置以下内容方可解决乱码问题
			// 配置字体
			Font xfont = new Font("宋体", Font.PLAIN, 12);    // X轴
			Font yfont = new Font("宋体", Font.PLAIN, 12);    // Y轴
			Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部
			Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题
			// 图片标题
			chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
			// 底部
			chart.getLegend().setItemFont(kfont);			
			// X 轴
			ValueAxis domainAxis = xyPlot.getDomainAxis();
			domainAxis.setLabelFont(xfont);// 轴标题
			domainAxis.setTickLabelFont(xfont);// 轴数值
			domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色
			// Y 轴
			ValueAxis rangeAxis = xyPlot.getRangeAxis();
			rangeAxis.setLabelFont(yfont);
			rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色
			rangeAxis.setTickLabelFont(yfont);
			// 定义坐标轴上日期显示的格式
			DateAxis dateAxis = (DateAxis) xyPlot.getDomainAxis();
			// 设置日期格式
			dateAxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
			// 向客户端输出生成的图片
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,
					chart, 500, 300, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

生产柱状图:

 

Java代码 复制代码 收藏代码
  1. package com.xidian.servlet;   
  2. import java.awt.Color;   
  3. import java.awt.Font;   
  4. import java.io.IOException;   
  5. import javax.servlet.ServletException;   
  6. import javax.servlet.http.HttpServlet;   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9. import org.jfree.chart.ChartFactory;   
  10. import org.jfree.chart.ChartUtilities;   
  11. import org.jfree.chart.JFreeChart;   
  12. import org.jfree.chart.axis.CategoryAxis;   
  13. import org.jfree.chart.axis.NumberAxis;   
  14. import org.jfree.chart.plot.CategoryPlot;   
  15. import org.jfree.chart.plot.PlotOrientation;   
  16. import org.jfree.chart.renderer.category.BarRenderer;   
  17. import org.jfree.chart.title.TextTitle;   
  18. import org.jfree.data.category.DefaultCategoryDataset;   
  19.   
  20. /**  
  21.  * 生产柱状图  
  22.  * @说明   
  23.  * @author cuisuqiang  
  24.  * @version 1.0  
  25.  * @since  
  26.  */  
  27. @SuppressWarnings("serial")   
  28. public class PillarServlet extends HttpServlet {   
  29.     @Override  
  30.     protected void service(HttpServletRequest request,   
  31.             HttpServletResponse response) throws ServletException, IOException {   
  32.         response.setContentType("text/html");   
  33.         // 使用普通数据集   
  34.         DefaultCategoryDataset chartDate = new DefaultCategoryDataset();   
  35.         // 增加测试数据,第一个参数是访问量,最后一个是时间,中间是显示用不考虑   
  36.         chartDate.addValue(55"访问量""2010-01");   
  37.         chartDate.addValue(65"访问量""2010-02");   
  38.         chartDate.addValue(59"访问量""2010-03");   
  39.         chartDate.addValue(156"访问量""2010-04");   
  40.         chartDate.addValue(452"访问量""2010-05");   
  41.         chartDate.addValue(359"访问量""2010-06");   
  42.         try {   
  43.             // 从数据库中获得数据集   
  44.             DefaultCategoryDataset data = chartDate;   
  45.                
  46.             // 使用ChartFactory创建3D柱状图,不想使用3D,直接使用createBarChart   
  47.             JFreeChart chart = ChartFactory.createBarChart3D(   
  48.                     "网站月访问量统计"// 图表标题   
  49.                     "时间"// 目录轴的显示标签   
  50.                     "访问量"// 数值轴的显示标签   
  51.                     data, // 数据集   
  52.                     PlotOrientation.VERTICAL, // 图表方向,此处为垂直方向   
  53.                     // PlotOrientation.HORIZONTAL, //图表方向,此处为水平方向   
  54.                     true// 是否显示图例   
  55.                     true// 是否生成工具   
  56.                     false // 是否生成URL链接   
  57.                     );             
  58.             // 设置整个图片的背景色   
  59.             chart.setBackgroundPaint(Color.PINK);   
  60.             // 设置图片有边框   
  61.             chart.setBorderVisible(true);   
  62.             Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部   
  63.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题   
  64.             // 图片标题   
  65.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));   
  66.             // 底部   
  67.             chart.getLegend().setItemFont(kfont);   
  68.             // 得到坐标设置字体解决乱码   
  69.             CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();   
  70.             categoryplot.setDomainGridlinesVisible(true);   
  71.             categoryplot.setRangeCrosshairVisible(true);   
  72.             categoryplot.setRangeCrosshairPaint(Color.blue);   
  73.             NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();   
  74.             numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());   
  75.             BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();   
  76.             barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));   
  77.             barrenderer.setSeriesItemLabelFont(1new Font("宋体", Font.PLAIN, 12));   
  78.             CategoryAxis domainAxis = categoryplot.getDomainAxis();            
  79.             /*------设置X轴坐标上的文字-----------*/  
  80.             domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));   
  81.             /*------设置X轴的标题文字------------*/  
  82.             domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));   
  83.             /*------设置Y轴坐标上的文字-----------*/  
  84.             numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));   
  85.             /*------设置Y轴的标题文字------------*/  
  86.             numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));   
  87.             /*------这句代码解决了底部汉字乱码的问题-----------*/  
  88.             chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));   
  89.             // 生成图片并输出   
  90.             ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,   
  91.                     chart, 500300null);   
  92.         } catch (Exception e) {   
  93.             e.printStackTrace();   
  94.         }   
  95.     }   
  96. }  
package com.xidian.servlet;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * 生产柱状图
 * @说明 
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
@SuppressWarnings("serial")
public class PillarServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		// 使用普通数据集
		DefaultCategoryDataset chartDate = new DefaultCategoryDataset();
		// 增加测试数据,第一个参数是访问量,最后一个是时间,中间是显示用不考虑
		chartDate.addValue(55, "访问量", "2010-01");
		chartDate.addValue(65, "访问量", "2010-02");
		chartDate.addValue(59, "访问量", "2010-03");
		chartDate.addValue(156, "访问量", "2010-04");
		chartDate.addValue(452, "访问量", "2010-05");
		chartDate.addValue(359, "访问量", "2010-06");
		try {
			// 从数据库中获得数据集
			DefaultCategoryDataset data = chartDate;
			
			// 使用ChartFactory创建3D柱状图,不想使用3D,直接使用createBarChart
			JFreeChart chart = ChartFactory.createBarChart3D(
					"网站月访问量统计", // 图表标题
					"时间", // 目录轴的显示标签
					"访问量", // 数值轴的显示标签
					data, // 数据集
					PlotOrientation.VERTICAL, // 图表方向,此处为垂直方向
					// PlotOrientation.HORIZONTAL, //图表方向,此处为水平方向
					true, // 是否显示图例
					true, // 是否生成工具
					false // 是否生成URL链接
					);			
			// 设置整个图片的背景色
			chart.setBackgroundPaint(Color.PINK);
			// 设置图片有边框
			chart.setBorderVisible(true);
			Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部
			Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题
			// 图片标题
			chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
			// 底部
			chart.getLegend().setItemFont(kfont);
			// 得到坐标设置字体解决乱码
			CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
			categoryplot.setDomainGridlinesVisible(true);
			categoryplot.setRangeCrosshairVisible(true);
			categoryplot.setRangeCrosshairPaint(Color.blue);
			NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
			numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
			barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));
			barrenderer.setSeriesItemLabelFont(1, new Font("宋体", Font.PLAIN, 12));
			CategoryAxis domainAxis = categoryplot.getDomainAxis();			
			/*------设置X轴坐标上的文字-----------*/
			domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
			/*------设置X轴的标题文字------------*/
			domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
			/*------设置Y轴坐标上的文字-----------*/
			numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
			/*------设置Y轴的标题文字------------*/
			numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
			/*------这句代码解决了底部汉字乱码的问题-----------*/
			chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
			// 生成图片并输出
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,
					chart, 500, 300, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

生成饼状统计图:

 

Java代码 复制代码 收藏代码
  1. package com.xidian.servlet;   
  2. import java.awt.Color;   
  3. import java.awt.Font;   
  4. import java.io.IOException;   
  5. import javax.servlet.ServletException;   
  6. import javax.servlet.http.HttpServlet;   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9. import org.jfree.chart.ChartUtilities;   
  10. import org.jfree.chart.JFreeChart;   
  11. import org.jfree.chart.plot.PiePlot3D;   
  12. import org.jfree.chart.title.TextTitle;   
  13. import org.jfree.data.general.DefaultPieDataset;   
  14. /**  
  15.  * 生成饼状统计图  
  16.  * @说明   
  17.  * @author cuisuqiang  
  18.  * @version 1.0  
  19.  * @since  
  20.  */  
  21. @SuppressWarnings("serial")   
  22. public class CakeServlet extends HttpServlet {   
  23.     protected void service(HttpServletRequest request,   
  24.             HttpServletResponse response) throws ServletException, IOException {   
  25.         response.setContentType("text/html");   
  26.         // 默认数据类型   
  27.         DefaultPieDataset dataType = new DefaultPieDataset();   
  28.         // 数据参数 内容,数量   
  29.         dataType.setValue("IE6"156);   
  30.         dataType.setValue("IE7"230);   
  31.         dataType.setValue("IE8"45);   
  32.         dataType.setValue("火狐"640);   
  33.         dataType.setValue("谷歌"245);   
  34.         try {   
  35.             DefaultPieDataset data = dataType;   
  36.             // 生成普通饼状图除掉 3D 即可   
  37.             // 生产3D饼状图   
  38.             PiePlot3D plot = new PiePlot3D(data);   
  39.             JFreeChart chart = new JFreeChart(   
  40.                     "用户使用的浏览器类型",            // 图形标题   
  41.                     JFreeChart.DEFAULT_TITLE_FONT, // 标题字体   
  42.                     plot,                          // 图标标题对象   
  43.                     true                           // 是否显示图例   
  44.             );   
  45.             // 设置整个图片的背景色   
  46.             chart.setBackgroundPaint(Color.PINK);   
  47.             // 设置图片有边框   
  48.             chart.setBorderVisible(true);   
  49.             // 配置字体   
  50.             Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部   
  51.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题   
  52.             // 图片标题   
  53.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));   
  54.             // 底部   
  55.             chart.getLegend().setItemFont(kfont);   
  56.             ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,   
  57.                     chart, 500300null);   
  58.         } catch (Exception e) {   
  59.             e.printStackTrace();   
  60.         }   
  61.     }   
  62. }  
package com.xidian.servlet;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
/**
 * 生成饼状统计图
 * @说明 
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
@SuppressWarnings("serial")
public class CakeServlet extends HttpServlet {
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		// 默认数据类型
		DefaultPieDataset dataType = new DefaultPieDataset();
		// 数据参数 内容,数量
		dataType.setValue("IE6", 156);
		dataType.setValue("IE7", 230);
		dataType.setValue("IE8", 45);
		dataType.setValue("火狐", 640);
		dataType.setValue("谷歌", 245);
		try {
			DefaultPieDataset data = dataType;
			// 生成普通饼状图除掉 3D 即可
			// 生产3D饼状图
			PiePlot3D plot = new PiePlot3D(data);
			JFreeChart chart = new JFreeChart(
					"用户使用的浏览器类型",            // 图形标题
					JFreeChart.DEFAULT_TITLE_FONT, // 标题字体
					plot,                          // 图标标题对象
					true                           // 是否显示图例
			);
			// 设置整个图片的背景色
			chart.setBackgroundPaint(Color.PINK);
			// 设置图片有边框
			chart.setBorderVisible(true);
			// 配置字体
			Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部
			Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题
			// 图片标题
			chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
			// 底部
			chart.getLegend().setItemFont(kfont);
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,
					chart, 500, 300, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

柱状分布统计图:

 

Java代码 复制代码 收藏代码
  1. package com.xidian.servlet;   
  2. import java.awt.Color;   
  3. import java.awt.Font;   
  4. import java.io.IOException;   
  5. import javax.servlet.ServletException;   
  6. import javax.servlet.http.HttpServlet;   
  7. import javax.servlet.http.HttpServletRequest;   
  8. import javax.servlet.http.HttpServletResponse;   
  9. import org.jfree.chart.ChartFactory;   
  10. import org.jfree.chart.ChartUtilities;   
  11. import org.jfree.chart.JFreeChart;   
  12. import org.jfree.chart.axis.CategoryAxis;   
  13. import org.jfree.chart.axis.NumberAxis;   
  14. import org.jfree.chart.plot.CategoryPlot;   
  15. import org.jfree.chart.plot.PlotOrientation;   
  16. import org.jfree.chart.renderer.category.BarRenderer;   
  17. import org.jfree.chart.title.TextTitle;   
  18. import org.jfree.data.category.DefaultCategoryDataset;   
  19. /**  
  20.  * 柱状分布统计图  
  21.  * @说明   
  22.  * @author cuisuqiang  
  23.  * @version 1.0  
  24.  * @since  
  25.  */  
  26. @SuppressWarnings("serial")   
  27. public class ParagraphsServlet extends HttpServlet {   
  28.     protected void service(HttpServletRequest request,   
  29.             HttpServletResponse response) throws ServletException, IOException {   
  30.         response.setContentType("text/html");   
  31.         DefaultCategoryDataset dataTime = new DefaultCategoryDataset();   
  32.         // 这是一组数据   
  33.         dataTime.addValue(52"0-6""2010-1-1");   
  34.         dataTime.addValue(86"6-12""2010-1-1");   
  35.         dataTime.addValue(126"12-18""2010-1-1");   
  36.         dataTime.addValue(42"18-24""2010-1-1");   
  37.         // 这是一组数据   
  38.         dataTime.addValue(452"0-6""2010-1-2");   
  39.         dataTime.addValue(96"6-12""2010-1-2");   
  40.         dataTime.addValue(254"12-18""2010-1-2");   
  41.         dataTime.addValue(126"18-24""2010-1-2");   
  42.         // 这是一组数据   
  43.         dataTime.addValue(256"0-6""2010-1-3");   
  44.         dataTime.addValue(86"6-12""2010-1-3");   
  45.         dataTime.addValue(365"12-18""2010-1-3");   
  46.         dataTime.addValue(24"18-24""2010-1-3");   
  47.         try {   
  48.             DefaultCategoryDataset data = dataTime;   
  49.             // 使用ChartFactory创建3D柱状图,不想使用3D,直接使用createBarChart   
  50.             JFreeChart chart = ChartFactory.createBarChart3D(   
  51.                     "网站时间段访问量统计",    
  52.                     "时间",    
  53.                     "访问量",    
  54.                     data,   
  55.                     PlotOrientation.VERTICAL,    
  56.                     true,    
  57.                     false,    
  58.                     false  
  59.             );   
  60.             // 设置整个图片的背景色   
  61.             chart.setBackgroundPaint(Color.PINK);   
  62.             // 设置图片有边框   
  63.             chart.setBorderVisible(true);   
  64.             Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部   
  65.             Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题   
  66.             // 图片标题   
  67.             chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));   
  68.             // 底部   
  69.             chart.getLegend().setItemFont(kfont);   
  70.             // 得到坐标设置字体解决乱码   
  71.             CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();   
  72.             categoryplot.setDomainGridlinesVisible(true);   
  73.             categoryplot.setRangeCrosshairVisible(true);   
  74.             categoryplot.setRangeCrosshairPaint(Color.blue);   
  75.             NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();   
  76.             numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());   
  77.             BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();   
  78.             barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));   
  79.             barrenderer.setSeriesItemLabelFont(1new Font("宋体", Font.PLAIN, 12));   
  80.             CategoryAxis domainAxis = categoryplot.getDomainAxis();            
  81.             /*------设置X轴坐标上的文字-----------*/  
  82.             domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));   
  83.             /*------设置X轴的标题文字------------*/  
  84.             domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));   
  85.             /*------设置Y轴坐标上的文字-----------*/  
  86.             numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));   
  87.             /*------设置Y轴的标题文字------------*/  
  88.             numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));   
  89.             /*------这句代码解决了底部汉字乱码的问题-----------*/  
  90.             chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));     
  91.             ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,   
  92.                     chart, 500300null);   
  93.         } catch (Exception es) {   
  94.             es.printStackTrace();   
  95.         }   
  96.     }   
  97. }  
package com.xidian.servlet;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
/**
 * 柱状分布统计图
 * @说明 
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
@SuppressWarnings("serial")
public class ParagraphsServlet extends HttpServlet {
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		DefaultCategoryDataset dataTime = new DefaultCategoryDataset();
		// 这是一组数据
		dataTime.addValue(52, "0-6", "2010-1-1");
		dataTime.addValue(86, "6-12", "2010-1-1");
		dataTime.addValue(126, "12-18", "2010-1-1");
		dataTime.addValue(42, "18-24", "2010-1-1");
		// 这是一组数据
		dataTime.addValue(452, "0-6", "2010-1-2");
		dataTime.addValue(96, "6-12", "2010-1-2");
		dataTime.addValue(254, "12-18", "2010-1-2");
		dataTime.addValue(126, "18-24", "2010-1-2");
		// 这是一组数据
		dataTime.addValue(256, "0-6", "2010-1-3");
		dataTime.addValue(86, "6-12", "2010-1-3");
		dataTime.addValue(365, "12-18", "2010-1-3");
		dataTime.addValue(24, "18-24", "2010-1-3");
		try {
			DefaultCategoryDataset data = dataTime;
			// 使用ChartFactory创建3D柱状图,不想使用3D,直接使用createBarChart
			JFreeChart chart = ChartFactory.createBarChart3D(
					"网站时间段访问量统计", 
					"时间", 
					"访问量", 
					data,
					PlotOrientation.VERTICAL, 
					true, 
					false, 
					false
			);
			// 设置整个图片的背景色
			chart.setBackgroundPaint(Color.PINK);
			// 设置图片有边框
			chart.setBorderVisible(true);
			Font kfont = new Font("宋体", Font.PLAIN, 12);    // 底部
			Font titleFont = new Font("宋体", Font.BOLD, 25); // 图片标题
			// 图片标题
			chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
			// 底部
			chart.getLegend().setItemFont(kfont);
			// 得到坐标设置字体解决乱码
			CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
			categoryplot.setDomainGridlinesVisible(true);
			categoryplot.setRangeCrosshairVisible(true);
			categoryplot.setRangeCrosshairPaint(Color.blue);
			NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
			numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
			barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));
			barrenderer.setSeriesItemLabelFont(1, new Font("宋体", Font.PLAIN, 12));
			CategoryAxis domainAxis = categoryplot.getDomainAxis();			
			/*------设置X轴坐标上的文字-----------*/
			domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
			/*------设置X轴的标题文字------------*/
			domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
			/*------设置Y轴坐标上的文字-----------*/
			numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
			/*------设置Y轴的标题文字------------*/
			numberaxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
			/*------这句代码解决了底部汉字乱码的问题-----------*/
			chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));	
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 1.0f,
					chart, 500, 300, null);
		} catch (Exception es) {
			es.printStackTrace();
		}
	}
}

 

 

JSP上使用IMG图片来请求Servlet显示图片:

 

Java代码 复制代码 收藏代码
  1. <%@ page language="java" pageEncoding="UTF-8"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  7. <html>   
  8.   <head>   
  9.     <base href="<%=basePath%>">   
  10.     <title></title>   
  11.     <meta http-equiv="pragma" content="no-cache">   
  12.     <meta http-equiv="cache-control" content="no-cache">   
  13.     <meta http-equiv="expires" content="0">       
  14.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  15.     <meta http-equiv="description" content="This is my page">   
  16.   </head>   
  17.   <body>   
  18.     <center>   
  19.         <img src="LineServlet">   
  20.         <img src="PillarServlet">   
  21.         <img src="ParagraphsServlet">   
  22.         <img src="CakeServlet">   
  23.     </center>   
  24.   </body>   
  25. </html>  
<%@ page language="java" 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%>">
    <title></title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  <body>
    <center>
    	<img src="LineServlet">
    	<img src="PillarServlet">
    	<img src="ParagraphsServlet">
    	<img src="CakeServlet">
    </center>
  </body>
</html>

 

 

WEB.xml配置:

 

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  6.     <filter>   
  7.         <filter-name>EncodingFilter</filter-name>   
  8.         <filter-class>org.filter.EncodingFilter</filter-class>   
  9.     </filter>   
  10.     <filter-mapping>   
  11.         <filter-name>EncodingFilter</filter-name>   
  12.         <url-pattern>/*</url-pattern>   
  13.     </filter-mapping>   
  14.     <servlet>   
  15.         <servlet-name>LineServlet</servlet-name>   
  16.         <servlet-class>com.xidian.servlet.LineServlet</servlet-class>   
  17.     </servlet>   
  18.     <servlet>   
  19.         <servlet-name>PillarServlet</servlet-name>   
  20.         <servlet-class>com.xidian.servlet.PillarServlet</servlet-class>   
  21.     </servlet>   
  22.     <servlet>   
  23.         <servlet-name>ParagraphsServlet</servlet-name>   
  24.         <servlet-class>   
  25.             com.xidian.servlet.ParagraphsServlet   
  26.         </servlet-class>   
  27.     </servlet>   
  28.     <servlet>   
  29.         <servlet-name>CakeServlet</servlet-name>   
  30.         <servlet-class>com.xidian.servlet.CakeServlet</servlet-class>   
  31.     </servlet>   
  32.     <servlet-mapping>   
  33.         <servlet-name>LineServlet</servlet-name>   
  34.         <url-pattern>/LineServlet</url-pattern>   
  35.     </servlet-mapping>   
  36.     <servlet-mapping>   
  37.         <servlet-name>PillarServlet</servlet-name>   
  38.         <url-pattern>/PillarServlet</url-pattern>   
  39.     </servlet-mapping>   
  40.     <servlet-mapping>   
  41.         <servlet-name>ParagraphsServlet</servlet-name>   
  42.         <url-pattern>/ParagraphsServlet</url-pattern>   
  43.     </servlet-mapping>   
  44.     <servlet-mapping>   
  45.         <servlet-name>CakeServlet</servlet-name>   
  46.         <url-pattern>/CakeServlet</url-pattern>   
  47.     </servlet-mapping>   
  48.     <welcome-file-list>   
  49.         <welcome-file>index.jsp</welcome-file>   
  50.     </welcome-file-list>   
  51. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<filter>
		<filter-name>EncodingFilter</filter-name>
		<filter-class>org.filter.EncodingFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>EncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>LineServlet</servlet-name>
		<servlet-class>com.xidian.servlet.LineServlet</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>PillarServlet</servlet-name>
		<servlet-class>com.xidian.servlet.PillarServlet</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>ParagraphsServlet</servlet-name>
		<servlet-class>
			com.xidian.servlet.ParagraphsServlet
		</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>CakeServlet</servlet-name>
		<servlet-class>com.xidian.servlet.CakeServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>LineServlet</servlet-name>
		<url-pattern>/LineServlet</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>PillarServlet</servlet-name>
		<url-pattern>/PillarServlet</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>ParagraphsServlet</servlet-name>
		<url-pattern>/ParagraphsServlet</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>CakeServlet</servlet-name>
		<url-pattern>/CakeServlet</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

 

为了解决乱码问题,我配置了一个过滤器:

 

Java代码 复制代码 收藏代码
  1. package org.filter;   
  2. import java.io.IOException;   
  3. import javax.servlet.Filter;   
  4. import javax.servlet.FilterChain;   
  5. import javax.servlet.FilterConfig;   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.ServletRequest;   
  8. import javax.servlet.ServletResponse;   
  9. /**  
  10.  * 处理乱码  
  11.  * @说明   
  12.  * @author cuisuqiang  
  13.  * @version 1.0  
  14.  * @since  
  15.  */  
  16. public class EncodingFilter implements Filter {   
  17.     public void destroy() {   
  18.     }   
  19.     public void doFilter(ServletRequest request, ServletResponse response,   
  20.             FilterChain chain) throws IOException, ServletException {   
  21.         request.setCharacterEncoding("gbk");   
  22.         response.setCharacterEncoding("gbk");   
  23.         chain.doFilter(request, response);   
  24.     }   
  25.     public void init(FilterConfig arg0) throws ServletException {   
  26.   
  27.     }   
  28. }  
package org.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * 处理乱码
 * @说明 
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
public class EncodingFilter implements Filter {
	public void destroy() {
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("gbk");
		chain.doFilter(request, response);
	}
	public void init(FilterConfig arg0) throws ServletException {

	}
}

 

posted @ 2013-12-05 09:42  zewentdcq  阅读(2243)  评论(0编辑  收藏  举报