JavaWeb图形报表柱状图

步骤说明:
1,导入log4j.jar,jfreechart-0.9.18.jar,jcommon-0.9.3.jar三个jar包

2,在某个包中写入BarChart .java类

package com.mengya.util;

import java.awt.Color;
import java.io.PrintWriter;

import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.DefaultCategoryDataset;


public class BarChart {

 private DefaultCategoryDataset dataset = new DefaultCategoryDataset();

 public void setValue(int value, String rowKey, String columnKey) {
  dataset.setValue(value, rowKey, columnKey);
 }

 public String generateBarChart(String title, HttpSession session,PrintWriter pw,int width,int heigth) {
  String filename = null;
  try {
   JFreeChart chart = ChartFactory
     .createBarChart3D(title, null, null, dataset,
       PlotOrientation.VERTICAL, true, false, false);
   chart.setBackgroundPaint(Color.WHITE);
   CategoryPlot plot = chart.getCategoryPlot();

   CategoryAxis domainAxis = plot.getDomainAxis();
   //domainAxis.setVerticalCategoryLabels(false);
   
   plot.setDomainAxis(domainAxis);

   ValueAxis rangeAxis = plot.getRangeAxis();
   // 设置最高的一个 Item 与图片顶端的距离
   rangeAxis.setUpperMargin(0.15);
   // 设置最低的一个 Item 与图片底端的距离
   rangeAxis.setLowerMargin(0.15);
   plot.setRangeAxis(rangeAxis);

   BarRenderer3D renderer = new BarRenderer3D();
   renderer.setBaseOutlinePaint(Color.BLACK);
   // 设置 Wall 的颜色
   renderer.setWallPaint(Color.gray);
   // 设置每种水果代表的柱的颜色
   renderer.setSeriesPaint(0, new Color(0, 0, 255));
   renderer.setSeriesPaint(1, new Color(0, 100, 255));
   renderer.setSeriesPaint(2, Color.GREEN);
   // 设置每个地区所包含的平行柱的之间距离
   renderer.setItemMargin(0.1);
   // 显示每个柱的数值,并修改该数值的字体属性
   //renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
   
   renderer.setItemLabelsVisible(true);
   plot.setRenderer(renderer);

   // 设置柱的透明度
   plot.setForegroundAlpha(0.6f);
   // 设置地区、销量的显示位置
   plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
   plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
   
   ChartRenderingInfo info = new ChartRenderingInfo(
     new StandardEntityCollection());
   filename = ServletUtilities.saveChartAsPNG(chart, width, heigth,
     info, session);
   ChartUtilities.writeImageMap(pw, filename, info);
   pw.flush();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return filename;
 }
}

3,配置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>/DisplayChart</url-pattern>
 </servlet-mapping>

4,在要显示图片的jsp中添加内容:
  <%@ page language="java" import="java.util.*,com.mengya.util.BarChart"
 pageEncoding="gbk"%>
<%@ page import="java.io.PrintWriter"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>柱形图</title>
 </head>

 <body>
  <%
   BarChart bch = new BarChart();

   //double[] data = { 85, 156, 179.5, 211, 123 };
   //String[] labels = { "Mon", "Tue", "Wed", "Thu", "Fri" };
   //for (int i = 0; i < data.length; i++) {
   // bch.setValue((int) data[i], "张明学", labels[i]);
   //}
   
   //第一组
   bch.setValue(200,"苹果","武汉");
   bch.setValue(400,"香蕉","武汉");
   bch.setValue(500,"橘子","武汉");
   bch.setValue(300,"梨子","武汉");
   
   //第二组
   bch.setValue(400,"苹果","黄冈");
   bch.setValue(300,"香蕉","黄冈");
   bch.setValue(100,"橘子","黄冈");
   bch.setValue(300,"梨子","黄冈");
   
   //第三组
   bch.setValue(600,"苹果","A");
   bch.setValue(300,"香蕉","A");
   bch.setValue(400,"橘子","A");
   bch.setValue(200,"梨子","A");
           
   //说明:bch.setValue(int a,String b,String c)其中参数c是该组的名字。最组的名字最好是不一样的!
   
   String filename = bch.generateBarChart("水果比图", session,
     new PrintWriter(out),700,500);
   String url = request.getContextPath() + "/DisplayChart?filename="
     + filename;
  %>
  <table align="center" border="0">
   <tr>
    <td>
     <P ALIGN="CENTER">
      <img src="<%=url%>" width=700 height=500 border=1
       usemap="#<%=filename%>">
     </p>
    </td>
   </tr>
  </table>
 </body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值