jsp添加扇形图,jsp添加饼图

来源:互联网 发布:亚信数据 编辑:程序博客网 时间:2024/05/01 12:31

在网上搜了很多关于扇形图(饼图)的代码,但是很多都是客户端的,很少有jsp的,所以花了一些时间做成了jsp页面的扇形图。

1、          添加相关的jar包:主要有三个

gnujaxp.jar   jfreechart-1.0.13.jar     jcommon-1.0.17.jar

还有其他的相关包:(这些包可加可不加,因为现在我加了,没出现问题,所以就暂时把这些包添加在项目中)

iText-2.1.5.jar       jfreechart-1.0.14-experimental.jar

jfreechart-1.0.14-swt.jar      servlet.jar    swtgraphics2d.jar

junit.jar

2、          我这里是直接把生成扇形图的代码写到jsp页面中,也就是直接写到前台。   在这一步需要在jsp页面头部分引入jar包。

下面这些是我引用到的jar包:

<%@pageimport="java.awt.Color"%>

<%@pageimport="javax.swing.text.Document"%>

<%@pageimport="com.sun.java.swing.plaf.windows.resources.windows"%>

<%@ page import="java.io.PrintWriter"%>

<%@ page import="com.nantian.tool.JKPieChart"%>

<%@ page import="org.jfree.chart.JFreeChart"%>

<%@ page import="org.jfree.chart.plot.PiePlot"%>

<%@ page import="org.jfree.chart.ChartRenderingInfo"%>

<%@ page import="org.jfree.chart.servlet.ServletUtilities"%>

<%@ page import="org.jfree.chart.urls.StandardPieURLGenerator"%>

<%@ page import="org.jfree.chart.entity.StandardEntityCollection"%>

<%@ page import="org.jfree.data.DefaultKeyedValues2D"%>

<%@ page import="org.jfree.data.DataUtilities"%>

<%@ page import="org.jfree.data.general.DefaultPieDataset"%>

<%@ page import="org.jfree.chart.ChartFactory"%>

<%@ page import="java.text.DecimalFormat"%>

<%@ page import="java.text.NumberFormat"%>

<%@ page import="org.jfree.chart.labels.StandardPieSectionLabelGenerator"%>

<%@ pageimport="java.awt.Font"%>

 

3、          添加扇形图的代码。 在这里演示2种扇形代码,一是固定扇形的代码,算是示例,另一个则是我在项目中的代码。

A)         这里的扇形中的内容是固定的,只是普通示例。

jsp页面中添加以下java代码:

<%

    DefaultPieDataset data = new DefaultPieDataset();

    data.setValue("苹果", 500);

    data.setValue("香蕉", 560);

    data.setValue("葡萄", 800);

    data.setValue("橘子", 880);

 

    PiePlot plot = new PiePlot(data);

   

JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT,plot,true);

    chart.setBackgroundPaint(Color.white);

    chart.setTitle("进度监控统计扇形表");

   

ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

   

  String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);

    String graphURL = request.getContextPath() +"/servlet/DisplayChart?filename="+filename;

   

%>

 

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>进度监控扇形演示图</title>

</head>

<body>

    <palign="center">

       <imgsrc="<%=graphURL%>"width=500height=300border=0usemap="#<%=filename%>">

   

    </p>

</body>

</html>

 

 

 

B)    这里扇形中的内容是先从后台获取数据,然后再扇形图中展示出来。

 

<%

 

      // ResultSet res = (ResultSet)request.getAttribute("res");//ResultSet拿到从后台传过来的查询结果。

         String totalSQL = " select (select count(stat) from t_batch_part_step where stat='0') batch0,(select count(stat) from t_batch_part_step where stat='1') batch1,(select count(stat) from t_batch_part_step where stat='2') batch2,(select count(stat) from t_batch_part_step where stat='3') batch3 from t_batch_part_step";

         Statement stmt1=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

         ResultSet res=stmt1.executeQuery(totalSQL);//由于页面中含有列表数据,要进行翻页(这里的翻页是在前台写的,同时进行数据库的操作),所以查询语句也在这里写。如果在后台写在传到这里也可以,但是在翻页的时候就会出现空指针异常。所以在翻页的同时,扇形图也进行数据库的查询。

        

        

         int batch_nums = 0;

         String batchNums = null;

         String batch00 = null;

         String batch11 = null;

         String batch22 = null;

         String batch33 = null;

          DefaultPieDataset data =new DefaultPieDataset();

         while(res.next()){

         // batchNums = res.getString("batchNum");

              batch00 = res.getString("batch0");

              batch11 = res.getString("batch1");

              batch22 = res.getString("batch2");

              batch33 = res.getString("batch3");

              

         // data.setValue("总步骤数", Integer.valueOf(batchNums));

             data.setValue("待就绪", Integer.valueOf(batch00));

             data.setValue("完成", Integer.valueOf(batch33));

             data.setValue("运行", Integer.valueOf(batch22));

             data.setValue("就绪", Integer.valueOf(batch11));

         }

            

        

             PiePlot plot = new PiePlot(data);

            

             JFreeChart chart = new JFreeChart("进度监控统计扇形表",JFreeChart.DEFAULT_TITLE_FONT,plot,true);

      //    JFreeChart chart = ChartFactory.createPieChart("进度监控统计扇形表",data,true,true,false);

             chart.setBackgroundPaint(Color.white);

             chart.setTitle("进度监控扇形统计表");

            

             //设置百分比

             plot = (PiePlot)chart.getPlot();

             DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象,主要是设置小数问题

             NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象

             StandardPieSectionLabelGenerator sp1 =new StandardPieSectionLabelGenerator("{0} {2}", nf, df);//获得StandardPieSectionLabelGenerator对象

             plot.setLabelGenerator(sp1);//设置饼图显示百分比

             plot.setNoDataMessage("无数据显示");

             plot.setCircular(false);

             plot.setLabelGap(0.02D);

             plot.setIgnoreNullValues(true);//设置不显示空值

             plot.setIgnoreZeroValues(true);//设置不显示负值

             plot.setLabelFont(new Font("宋体",Font.BOLD,20));//解决乱码

             chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体

             chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,12));

            

             ChartRenderingInfo info =new ChartRenderingInfo(new StandardEntityCollection());

            

             String filename = ServletUtilities.saveChartAsPNG(chart, 800,600, info, session);

             String graphURL = request.getContextPath() +"/servlet/DisplayChart?filename="+filename;

            

         %>

         </table>

        

         <tr>

             <td></td>

             <tdcolspan="6">

                <palign="center">

                    <imgsrc="<%=graphURL%>"width=800height=600border=0usemap="#<%=filename%>">

                </p>

             </td>

             <td></td>

         </tr>

 

 

 

Jsp页面代码完成

 

 

 

4、        web.xml的配置:

这个纠结了一段时间,不知道这里配置的内容是否要修改,这些为什么这么写,是否要写成普通servlet那样,把DisplayChart改成自己的servlet等。其实这些是不用修改的,/servlet/DisplayChartjsp页面中对应

<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>/servlet/DisplayChart</url-pattern>

       </servlet-mapping>

 

现在jsp页面的代码完成,扇形图也就出现了。

下面的图是我的程序中的截图