做了一个生成JFreeChart的自定义标签SONIC Taglib。不过只能生成姘图和柱图


下载地址:http://www.javasp.net/jinxin/jinxin.rar (Sonic1.0)

       本来可以用cwolf的。但是cwolf不能做统计。返照他的功能自己做了一个。觉得还不错。开发的时候省略了很多JFreeChart的东西。直接做一个连数据的CLASS就可以了。下面是开发指南,希望对要用JFREECHART的人有帮助。

优点:
          简化了生成图形的繁琐代码,用TagLib封装了。只要数据对象就可以了

缺点:
          只能生成饼图 和 柱图 其他图还没有做

Sonic Chart Tutorial

SETUP for installing the tag library

(First time user)

Tutorial: Step1

How to Install (Making sure you have the Jar File)

To use the tag library, you must first include the jar file into your project.

Put the following JAR files of the /lib directory into your web applications /WEB-INF/lib directory:

 

l        Sonic. jar

l        jfreechart-0.9.20.jar

l        jcommon-0.9.5.jar

l        Commons-logging. jar

Install the sonic Servlet in your Web App

You must add the following code to the web.xml file t o let yours project know the location of the TLDS

<servlet>

 

<servlet>
   
<servlet-name>DisplayChart</servlet-name>
  
 <servlet-class>rg.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>

 

 

 
     

Additionally you should provide a servlet-mapping to tell the container which URL maps to your sonic rendering servlet.

<servlet-mapping>

 

<servlet-mapping>
    <servlet-name>DisplayChart</servlet-name>
    <url-pattern>servlet/DisplayChart</url-pattern>
</servlet-mapping>

 

 

Additionally you should proide a taglib to tell container which URL to find sonic.tld

<taglib>

  <taglib>

    <taglib-uri>/WEB-INF/sonic.tld</taglib-uri>

    <taglib-location>/WEB-INF/sonic.tld</taglib-location>

  </taglib>

二.Tutorial: Step 2

Provide a DatasetProducer

 

1.How to Create a PeChart DatasetProducer

 

l         Createa a java file such ByUsingPeople_PieDB.java

              Note:

1.       In ByUsingPeople_PieDB.java file you must implements ProductDataSetInterface, Serializable

 

Below is ByUsingPeople_PieDB.java detail

 

import org.jfree.data.DefaultPieDataset;

import java.util.ArrayList;

import net.javasp.www.chartfactorymethod.creator.ChartCreatorInterface.ProductDataSetInterface;import java.io.Serializable;

 

public class IndustryGroupRequire implements ProductDataSetInterface, Serializable{

 

   public ArrayList produceDataset() throws Exception {

// Create a DefaultPieDataset object

    DefaultPieDataset ds = new DefaultPieDataset();

    String total = null;

    String percent = null;

    ArrayList list = new ArrayList();

 

    // The data which need set into DefaultPieDataset object

    ds.setValue("第一产业比重", 36);

    ds.setValue("第二产业比重", 24);

    ds.setValue("第三产业比重", 20);

// Below chart show total info   

    String[] totalname = {"所占比重:", ""};

 

    list.add(ds);

    list.add("80"+"%");

    list.add("");

    list.add(totalname); //3

    list.add("other"); //

    return  list;

   }

}

 

Tutorial: Step 3

Define the chart in your JSP

1.       Create a jsp file such as piechart.jsp

Note:

1.     The taglib sonic attribute must same to <jsp useBean id> attribute Such as <sonic:plotchart id=”pieData” …> and <jsp:useBean id=”pieData” …>

2.     type="piechart" type attribute must be “piechart”

 

 

Below is piechart.jsp detail

piechart.jsp

<%@ page contentType="text/html; charset=GB2312" %>

<%@taglib uri='/WEB-INF/sonic.tld' prefix='sonic' %>

<jsp:useBean id="pieData" class=" net.javasp.www.piechartdataset.ByUsingPeople_PieDB "/>

<html>

<head>

<title>

按行业分组的需求人数

</title>

</head>

<body bgcolor="#ffffff">

<div align="center">

  <sonic:plotchart id="pieData" type="piechart" name="按行业分组的需求人数" hight="700" width="700" />

</div>

</body>

</html>

 

2.How to Create a Column Chart DatasetProducer

Note:

l         Createa a java file such SupplyTotalCount.java

 

In ByUsingPeople_PieDB.java file you must implements ProductDataSetInterface, Serializable

import net.javasp.www.chartfactorymethod.creator.ChartCreatorInterface.ProductDataSetInterface; import java.io.Serializable;

import java.util.ArrayList;

import org.jfree.data.CategoryDataset;

import org.jfree.data.DatasetUtilities;

 

/**

 * <p>Title: Colum3DproduceDataset</p>

 * <p>Description: </p>

 * <p>Copyright: Copyright (c) 2004</p>

 * <p>Company: www.javasp.net</p>

 * @author Jstar

 * @version 1.0

 */

 

public class SupplyTotalCount implements ProductDataSetInterface, Serializable {

  public SupplyTotalCount() {

  }

 

  public ArrayList produceDataset() throws Exception {

 

    int total = 0;

    int percent = 0;

    ArrayList list = new ArrayList();

    //---------------------------------需求人数-----求职人数

    double[][] date = new double[][] {{322, 261}, {128, 240}};

 

    String[] rowKeys = {"上期结转", "本期新增"};

    String[] columnKeys = {"需求人数", "求职人数"};

 

    String[] totalname = {"需求人数", "求职人数"};

 

    CategoryDataset ds = DatasetUtilities.createCategoryDataset(rowKeys,columnKeys, date);

    //total

    for (int i = 0; i < 1; i++) {

      for (int j = 0; j < date.length; j++) {

        total += date[j][i];

      }

    }

    //percent

    for (int a = 1; a < 2; a++) {

      for (int b = 0; b < date.length ; b++) {

        percent += date[b][a];

      }

    }

 

    list.add(ds);//0

    list.add(Integer.toString(total));//1

    list.add(Integer.toString(percent));//2

    list.add(totalname);//3

    list.add("other");//4

    return list;

 

  }

}

Define the chart in your JSP

2.      Create a jsp file such as columnchart.jsp

Note:

3.     The taglib sonic attribute must same to <jsp useBean id> attribute Such as <sonic:plotchart id=” columndata” …> and <jsp:useBean id=” columndata” …>

4.     type=" columndata " type attribute must be “columndata

 

 

Below is piechart.jsp detail

piechart.jsp

<%@ page contentType="text/html; charset=GB2312" %>

<%@taglib uri='/WEB-INF/sonic.tld' prefix='sonic' %>

<jsp:useBean id="columndata " class=" net.javasp.www.columndataset.SupplyTotalCount "/>

<html>

<head>

<title>

供求总体人数

</title>

</head>

<body bgcolor="#ffffff">

<div align="center">

  <sonic:plotchart id=" columndata " type=" columchart " name="供求总体人数" width="500" hight="350 " />

</div>

</body>

</html>

 

 


你可能感兴趣的:(jsp,String,servlet,File,jfreechart,import)