JFreeChart示例

准备工作:

MyEclipse5.5

JFreeChart的JAR包:gnujaxp.jar、jcommon-1.0.0.jar、jfreechart-1.0.1.jar

一、柱形图

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("image/png"); 
  // 1、创建柱形数据集
  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.setValue(59, "计算机软件", "Spring2.0");
  dataset.setValue(55, "计算机软件", "Hibernate3.0");
  dataset.setValue(70, "计算机软件", "EJB2.0");
  dataset.setValue(100, "小说", "红楼梦");
  dataset.setValue(80, "小说", "三国演义");
  dataset.setValue(70, "小说", "水浒传");
  dataset.setValue(80, "小说", "西游记");
  dataset.setValue(50, "计算机网络", "局域网技术");
  dataset.setValue(60, "计算机网络", "网络安全");
  dataset.setValue(70, "计算机网络", "网络布线");
  // 2、创建JFreeChart对象
  JFreeChart jFreeChart = ChartFactory.createBarChart("图书单价统计", "图书名", "图书价格",
dataset,

                                                                         PlotOrientation.VERTICALtruetruetrue);
  // 3、输出柱形图
  ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, 800,600);
 }

运行结果:

JFreeChart示例

二、饼图

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("image/png");
  // 1、创建默认饼数据集
  DefaultPieDataset dataset = new DefaultPieDataset();
  dataset.setValue("本科生", 8310);
  dataset.setValue("大专生", 6100);
  dataset.setValue("硕士生", 3520);
  dataset.setValue("高中生", 1620);
  dataset.setValue("高中以下", 380);
  dataset.setValue("博士生", 180);
  // 2、为JFreeChart创建3D图形
  PiePlot3D plot = new PiePlot3D(dataset);
  // 3、为图形添加工具条
  plot.setToolTipGenerator(new StandardPieToolTipGenerator());
  
  JFreeChart chart = ChartFactory.createPieChart3D("程序员学历情况调查表", dataset,truetrue,

                                                                                                                       true);
  // 设置JFreeChart背景色
  chart.setBackgroundPaint(Color.gray);
  // 4、输出饼图
  ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 600, chart,800, 400, null);
 }

运行结果:

JFreeChart示例

三、线形图

public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       response.setContentType("image/JPEG");

       // 1、将封装数据到DefaultCategoryDataset对象中

       DefaultCategoryDataset dataset = new DefaultCategoryDataset();

       dataset.setValue(-4.7, "北京", "1月");

       dataset.setValue(-2.3, "北京", "2月");

       dataset.setValue(4.4, "北京", "3月");

       dataset.setValue(13.2, "北京", "4月");

       dataset.setValue(20.2, "北京", "5月");

       dataset.setValue(24.2, "北京", "6月");

       dataset.setValue(26.0, "北京", "7月");

       dataset.setValue(24.6, "北京", "8月");

       dataset.setValue(19.5, "北京", "9月");

       dataset.setValue(12.5, "北京", "10月");

       dataset.setValue(-4.0, "北京", "11月");

       dataset.setValue(-2.8, "北京", "12月");

       dataset.setValue(5.3, "成都", "1月");

       dataset.setValue(7.2, "成都", "2月");

       dataset.setValue(11.5, "成都", "3月");

       dataset.setValue(16.7, "成都", "4月");

       dataset.setValue(21.1, "成都", "5月");

       dataset.setValue(23.1, "成都", "6月");

       dataset.setValue(25.5, "成都", "7月");

       dataset.setValue(25.1, "成都", "8月");

       dataset.setValue(21.1, "成都", "9月");

       dataset.setValue(16.7, "成都", "10月");

       dataset.setValue(11.5, "成都", "11月");

       dataset.setValue(7.2, "成都", "12月");

       dataset.setValue(-0.5, "西安", "1月");

       dataset.setValue(2.2, "西安", "2月");

       dataset.setValue(7.9, "西安", "3月");

       dataset.setValue(14.1, "西安", "4月");

       dataset.setValue(19.4, "西安", "5月");

       dataset.setValue(24.8, "西安", "6月");

       dataset.setValue(26.3, "西安", "7月");

       dataset.setValue(25.3, "西安", "8月");

       dataset.setValue(19.4, "西安", "9月");

       dataset.setValue(13.8, "西安", "10月");

       dataset.setValue(6.8, "西安", "11月");

       dataset.setValue(0.9, "西安", "12月");

       // 2、创建JFreeChart对象

       JFreeChart jFreeChart = ChartFactory.createLineChart("中国主要城市每月平均气温变化曲线", "月份", "温度", dataset,PlotOrientation.VERTICALtruetruefalse);

       // 3、输出线形图

       ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 10,jFreeChart, 800, 600, null);

运行结果:

JFreeChart示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值