欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaWeb实现图形报表折线图的方法

程序员文章站 2023-11-13 15:39:22
本文实例讲述了javaweb实现图形报表折线图的方法。分享给大家供大家参考,具体如下: 步骤说明: 1. 导入log4j.jar,jfreechart-0.9.18.j...

本文实例讲述了javaweb实现图形报表折线图的方法。分享给大家供大家参考,具体如下:

步骤说明:

1. 导入log4j.jar,jfreechart-0.9.18.jar,jdom.jar,jcommon-0.9.3.jar四个jar包

2. 在某个包中写入tuxin.java类

package com.mengya.util;
import java.awt.color;
import java.awt.font;
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.standardlegend;
import org.jfree.chart.axis.numberaxis;
import org.jfree.chart.entity.standardentitycollection;
import org.jfree.chart.plot.categoryplot;
import org.jfree.chart.plot.plotorientation;
import org.jfree.chart.renderer.lineandshaperenderer;
import org.jfree.chart.servlet.servletutilities;
import org.jfree.data.defaultcategorydataset;
public class line
{
  private defaultcategorydataset dataset = new defaultcategorydataset();
  public void setvalue(int sum, string line, string wfield)
  {
   dataset.addvalue(sum, line, wfield);
  }
  public string generatelinechart(string title,string wfield, string hfield, httpsession session, printwriter pw,int wphoto,int hphoto)
  {
   string filename = null;
  try
   {
     final jfreechart chart = chartfactory.createlinechart
      (
       title, // 图表标题
       wfield, // 横轴的显示标签
       hfield, // 纵轴的显示标签
       dataset, //数据集
       plotorientation.vertical, // 图表方向:水平、垂直
       true, // 是否显示图例
       true, // 是否生成提示工具 tooltips
       false // 是否生成url链接
     );
     standardlegend legend = (standardlegend) chart.getlegend();//生成图例
     legend.setdisplayseriesshapes(true);//显示图例形状
     legend.setshapescalex(1.5);//设置图例x轴的大小
     legend.setshapescaley(1.5);//设置图例y轴的大小
     legend.setdisplayserieslines(true);//显示图示项的横线
     //设置图片的背景色
     chart.setbackgroundpaint(new java.awt.color(189,235,255));
     categoryplot plot = (categoryplot) chart.getplot();
     plot.setbackgroundpaint(new color(239,251,255));//生成图片中墙体的背景色
     plot.setrangegridlinepaint(color.black);//生成图片中格子线的颜色
     // 没有数据时显示的消息
     plot.setnodatamessage("没有相关统计数据");
     plot.setnodatamessagefont(new font("黑体", font.center_baseline, 16));
     plot.setnodatamessagepaint(color.red);
     numberaxis rangeaxis = (numberaxis) plot.getrangeaxis();
     rangeaxis.setstandardtickunits(numberaxis.createintegertickunits());//设置图中的刻度线的单位
     rangeaxis.setautorangeincludeszero(true);//强制在自动选择的数据范围中包含0
     lineandshaperenderer renderer = (lineandshaperenderer) plot.getrenderer();
     renderer.setdrawshapes(true);//折线的数据点根据分类使用不同的形状
     renderer.setitemlabelsvisible(true);//显示每个点上的数据值的提示工具,数据标签是否可见
     chartrenderinginfo info = new chartrenderinginfo(new
       standardentitycollection());
     //500是图片长度,300是图片高度
     filename = servletutilities.savechartaspng(chart, wphoto, hphoto, 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.line,com.mengya.bean.stubean" pageencoding="gbk"%>
<%@page import="java.io.printwriter;"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <title>折线图</title>
 </head>
 <body>
 <%
  line tt = new line();
  //第一条折线
  list ar = (arraylist) request.getattribute("arr1");
  for (int i = 0; i < ar.size(); i++) {
  stubean bean = (stubean) ar.get(i);
  tt.setvalue((int) bean.gets_money(), "张明学个人消费比例图", bean.gets_month()+ "");
  }
  //条二条折线
  ar = (arraylist) request.getattribute("arr2");
  for (int i = 0; i < ar.size(); i++) {
  stubean bean = (stubean) ar.get(i);
  tt.setvalue((int) bean.gets_money(), "梦娅个人消费比例图", bean.gets_month()+ "");
  }
  //说明:tt.setvalue(int a,string b,string c)
  //其中的第二个参数b是该条折线的名字,每条折线的名字最好不要一样.
  string filename = tt.generatelinechart("个人消费比例图", "月   份",
   "费   用", session, new printwriter(out), 800, 550);
  string graphurl = request.getcontextpath()
   + "/displaychart?filename=" + filename;
 %>
 <p align="center">
 <img src="<%=graphurl %>" width=800 height=550 border=1 usemap="#<%=filename%>">
 </body>
</html>

希望本文所述对大家jsp程序设计有所帮助。