服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用java从xml文件中获取业务图表

java关键字导航


网络 j2me 手机游戏 javacard struts 游戏 分析器 jaas ejb javamail 设计模式 j2ee


  数据的图解表示法是一个热门的话题。 我们翻阅杂志,可以看到很多公司提供复杂的图形程序包,可以让你处理你的数据并使之可视化。

  这些程序包有一个公共的问题:它们要求在它们可以把你的数据形成图表之前,你必须把你的xml数据格式化成一种它们能够读懂的特定的格式。 这样的话,xml有力的灵活性就被埋没了,因为你必须改换数据格式--通常使用xslt。 这并不总是想看上去那么微不足道的,因为有时开发者必须从第三方的数据源获得xml文件,然后在把这些xml文件发送到客户之前给它们添加图表。 举例来说吧,一个金融服务公司可能必须从一个租用的数据库中取得一家公司的基本资料,然后在把它转化成xsl格式之前需要分析这些数据。

  本文将介绍如何一步一步的使用java开发一个业务图表服务器。 服务器调用你的数据源,然后按照你设置来生成数据序列和种类的xpath变量分析数据。 它然后把数据载入制图表引擎,一个名为jfreechart的出色的源码开放程序包。

  最后返回到服务器的调用者的图表是一个jpeg文件,这样它就可以被发送到一个基于浏览器的产品而不需要下载额外的软件了。

  在这边文章中我开发的服务器(起名为chartserver),是一个初级版本,调用固定的xml文件作为数据源。 在本文的结尾,我将讨论如何把这个服务器扩展成可以调用自己设置的参数需要的数据服务器。 另外,这个例程使用开放源码的jfreechart引擎在服务器端构造它的图表。 而且想要把它修改成使用任何其它的制图表引擎的话,也是一件很简单的事情。
  图表的元素

  大部分的图表可以分解成一系列公共的对象,其中最主要的两个是数据系列和数据种类。 数据系列正像它名称所蕴含的意义---一系列计划放在一起组成一种关系的数据。 举例来说,在一个线状图表中数据系列是线内的点;在一个圆饼图中,数据系列是组成饼图的每个表示数量的"块"。 另一方面数据种类是描述数据系列的点。 举例来说,在一个线状图中,如果数据系列是股票收盘价格,那么通常的匹配这些数据的日期类别将是股票在这个价位收盘的日期。

  本文中的例子是基于一个包含一个虚拟的球队的赛季赛况统计数据的xml文件。在下面的例子中,球员的得分情况被制成了一张圆饼图。 得分组成数据系列,球员姓名组成类别系列。

  某些图表需要多种数据系列,比如你想比较两组数据的图表,最现实的例子就是预算收入和。 实际收入,或者进行的比赛项目和取得的成绩, 它然后使用这些值作为参数来构造图表对象。

  这些值连同其它的用于每个图表的配置信息一起保存在一个charts.xml文件中。

<chart id="points">
<url>http://localhost/players.xml</url>
<config>http://localhost/pmconfig.xml</config>
<series>//players/player[points>0 and minutes>1000]/points</series>
<categories>//players/player[points>0 and minutes>1000]/name</categories>
<xsize>600</xsize>
<ysize>400</ysize>
</chart>

  这段xml文本告诉服务器用于生成图表的数据源储存在什么位置,生成图表的配置信息储存在什么位置以及用于数据系列和数据种类的xpath是什么。 我们可以看出,xpath值可以相当的复杂,在这个例子中,我们用一些条件过滤这个xml文件,找到那些球员有得分,那些球员上场时间超过1000分钟。 他们的分数组成数据系列,他们的姓名组成类别系列。

  现在,用于单独图表的配置文件包含设定图表类型,图表标题,种类标题,值标题和每个系列的标题。 这样就能够轻而易举的使用数据系列被定义的颜色来表示这个系列,而不是使用某种代码来表示了。

<chart>
<type>1</type>
<title>points by player</title>
<cattitle>player name</cattitle>
<valtitle>points</valtitle>
<seriestitles>points scored</seriestitles>
</chart>

  servlet是如何工作的

  代码段1中的doget函数取得参数值(包括图表、宽度和高度),载入配置xml文件,分析这个文件取得正确的数据文件,然后设置所有的dochart(见代码段2)函数所需要的变量,而dochart函数是执行实际的制表功能的。

  配置图表对象
 
  配置图表对象是一个两步操作。 第一步是设置图表的外观属性。 第二步是将这些数据装载入图表。
这些都是由dochart函数来完成的。 它分析配置文件并取得一个图表类型,然后以此构造出11个图表中的一个。

  在下面的例子中,球员的得分情况被制成了一张圆饼图。 得分组成数据系列,球员姓名组成类别系列。
这个函数可以很容易地扩展,以便用户增加新的设定到配置xml文件然后分析它们,这样就可以让用户很有效的知道并更改制表引擎(在本例中为jfreechart)的属性模型,当然这一切都只要通过一个属性文件。
现在,配置变得相当简化了,与图表关联的唯一的图表类型和各种各样文本标题都可用了。

  createdataset函数(见代码段3)把这些数据装载入制图引擎。 这个函数的核心目的就是把xpath应用到xml文件中来产生一维的结点列表。 它然后使用这些结点列表来生成vector。 这些矢量被用于构造defaultcategorydataset对象(请参看jfreechart文件),制表引擎使用这些defaultcategorydataset对象来绘制图表。

  除了上面我介绍的圆饼图外,我还提供了其它的两个使用charts.xml文件构造的图表的例子(参见图3和4)。 你可以修改这个文件,然后用它来增添自己的图表。


  由xml引发的数据交换革命还是刚刚开始。 每天都有新的技术出现,让我们可以用一些新鲜的并且激动人心的方法来思考我们要处理的信息。 这个图表服务器只是正在到来的激动人心的新技术的一个简单的例子而已。

  代码段1 doget函数

public void doget(httpservletrequest request, httpservletresponse response) throws
servletexception, ioexception
{
string strchart = "points";
string strx = "0";
string stry = "0";
// 读取参数
try
{
strchart = request.getparameter("chart").tolowercase();
strx = request.getparameter("width");
stry = request.getparameter("height");
if(strx==null)
strx = "0";
if(stry==null)
stry = "0";
// 读取配置文件
dfactory = documentbuilderfactory.newinstance();
dfactory.setnamespaceaware(true);
urlconnection con = new url(config_loc).openconnection();
con.connect();
inputstream newin = con.getinputstream();
xmlconfig = dfactory.newdocumentbuilder().parse(newin);
// 从配置文件中取得值
// 从xml文件中取得条目
strxpath = "/root/chart[@id='" + strchart + "']";
ninodelist = xpathapi.selectnodeiterator(xmlconfig, strxpath);
ndeitem = ninodelist.nextnode();
elemitem = (element) ndeitem;
// 我们然后为数据服务取得特定的参数
// 接着使用这个参数来构建一个到数据源的url
strdataurl = getchilditem(elemitem, "url");
strdataurl = strdataurl.replace('_','&');
// 然后从数据源中导入xml文档
loaddatadoc(strdataurl);
// 取得配置文件的位置
strchartconfigurl = getchilditem(elemitem, "config");
loadconfigdoc(strchartconfigurl);
// 然后取得用于数据系列的xpath列表
strdummy = getchilditem(elemitem, "series");
loadseries(strdummy);
// 取得用于数据类别的xpath列表
strdummy = getchilditem(elemitem, "categories");
loadcategories(strdummy);
// 取得图像的尺寸
// 首先检查strx和stry参数.
// 如果没有参数,从配置文件中读取。
nxsize = new integer(strx).intvalue();
if (nxsize==0)
{
strdummy = getchilditem(elemitem, "xsize");
nxsize = new integer(strdummy).intvalue();
}
nysize = new integer(stry).intvalue();
if (nysize==0)
{
strdummy = getchilditem(elemitem, "ysize");
nysize = new integer(strdummy).intvalue();
}

// 开始制图:
dochart();
// 输出图表
response.setcontenttype("image/jpeg");
outputstream out = response.getoutputstream();
chartutilities.writechartasjpeg(out, chart, nxsize, nysize);
out.close();

}
catch(exception e)
{
e.printstacktrace();
}

}

  代码段2 dochart函数

private void dochart()
{
node nroot;
element nelem;
int ntype;
string strtitle;
string strcattitle;
string strvaltitle;
string strstitlelist;
nroot = xmlconfig.getdocumentelement();
nelem = (element) nroot;
ntype = new integer(getchilditem(nelem,"type")).intvalue();
strtitle = getchilditem(nelem,"title");
strcattitle = getchilditem(nelem,"cattitle");
strvaltitle = getchilditem(nelem, "valtitle");
strstitlelist = getchilditem(nelem, "seriestitles");
loadseriestitles(strstitlelist);

// 取得数据并把它导入正确的数据结构
categorydata = createdataset();
// 分析图表类型然后创建适合的图表
switch(ntype){
case 0:
{
// 垂直条状图
chart = chartfactory.createverticalbarchart
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 1:
{
// 饼状图
piedataset piedata = datasetutilities.createpiedataset
(categorydata, 0); chart = chartfactory.createpiechart
(strtitle, piedata, true);
break;
}
case 2:
{
// 水平条状图
chart = chartfactory.createhorizontalbarchart
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 3:
{
// 线状图
chart = chartfactory.createlinechart
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 4:
{
// 面积图
chart = chartfactory.createareachart
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 5:
{
// 3d水平条状图
chart = chartfactory.createhorizontalbarchart3d
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 6:
{
// 3d 饼状图
piedataset piedata = datasetutilities.createpiedataset(categorydata, 0);
chart = chartfactory.createpie3dchart(strtitle, piedata, true);
break;
}
case 7:
{
// 堆积水平条状图
chart = chartfactory.createstackedhorizontalbarchart
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 8:
{
// 堆积垂直条状图
chart = chartfactory.createstackedverticalbarchart
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 9:
{
// 3d堆积垂直条状图
chart = chartfactory.createstackedverticalbarchart3d
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 10:
{
//3d垂直条状图
chart = chartfactory.createverticalbarchart3d
(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
}
// 设置合适的颜色
setupchart();
}


  代码段3

private categorydataset createdataset()
{

int lp,ncurrent;
string strxpath;
string strval;
vector[] victor= new vector[ndataxpathcount];
//装载数据
try{

for(lp=0;lp<ndataxpathcount;lp++)
{
victor[lp] = new vector();
strxpath = strdataxpaths.elementat(lp).tostring();
ninodelist = xpathapi.selectnodeiterator(xmldata,strxpath);
elemitem = (element) ninodelist.nextnode();
do{
if(elemitem.getfirstchild()!=null)
{
strval = elemitem.getfirstchild().getnodevalue();
victor[lp].add(strval);
}
elemitem = (element) ninodelist.nextnode();
} while(elemitem!=null);
}
double[][] ndata = new double[ndataxpathcount][victor[0].size()];
for(lp=0;lp<ndataxpathcount;lp++)
{
for(ncurrent=0;ncurrent<victor[0].size();ncurrent++)
{
try{
ndata[lp][ncurrent] = new double(victor[lp].elementat
(ncurrent).tostring());
}
catch (exception e){
ndata[lp][ncurrent] = new double(0);
}
}
}

//装载类别
vector vcategories = new vector();
strxpath = strcategoryxpaths.elementat(0).tostring();
ninodelist = xpathapi.selectnodeiterator(xmldata,strxpath);
elemitem = (element) ninodelist.nextnode();


for(lp=0;lp<victor[0].size();lp++)
{
strval = elemitem.getfirstchild().getnodevalue();
vcategories.add(strval);
elemitem = (element) ninodelist.nextnode();
}
defaultcategorydataset dset = new defaultcategorydataset(ndata);

// 装载系列标题
string[] strt = new string[strstitles.size()];

for(lp=1;lp<=strstitles.size();lp++)
{
strt[lp-1] = strstitles.elementat(lp-1).tostring();
}
dset.setseriesnames(strt);
dset.setcategories(vcategories.toarray());



return dset;

}
catch (exception e)
{
e.printstacktrace();
}


double[][] ndata = new double[][]{{new double(0)},{new double(0)}};
return new defaultcategorydataset(ndata);

}

  代码段4 bizcharter.java

import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.color;
import java.awt.paint;
import java.awt.gradientpaint;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jrefinery.chart.pieplot;
import com.jrefinery.chart.jfreechart;
import com.jrefinery.chart.chartfactory;
import com.jrefinery.chart.chartutilities;
import com.jrefinery.chart.plot;
import com.jrefinery.chart.categoryplot;
import com.jrefinery.chart.xyplot;
import com.jrefinery.chart.axis;
import com.jrefinery.chart.horizontalcategoryaxis;
import com.jrefinery.chart.numberaxis;
import com.jrefinery.chart.verticalnumberaxis;
import com.jrefinery.chart.data.plotfit;
import com.jrefinery.chart.data.linearplotfitalgorithm;
import com.jrefinery.chart.data.movingaverageplotfitalgorithm;
import com.jrefinery.data.categorydataset;
import com.jrefinery.data.defaultcategorydataset;
import com.jrefinery.data.piedataset;
import com.jrefinery.data.highlowdataset;
import com.jrefinery.data.xydataset;
import com.jrefinery.data.defaultxydataset;
import com.jrefinery.data.datasetutilities;
import java.awt.geom.rectangle2d;
import org.apache.xerces.parsers.domparser;
import org.apache.xpath.xpathapi;
import org.apache.xml.utils.treewalker;
import org.apache.xml.utils.dombuilder;
import org.w3c.dom.document;
import org.w3c.dom.element;
import org.w3c.dom.documentfragment;
import org.w3c.dom.node;
import org.w3c.dom.nodelist;
import org.w3c.dom.traversal.nodeiterator;
import org.xml.sax.saxexception;
import org.xml.sax.inputsource;
import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.parsers.parserconfigurationexception;
import org.w3c.dom.document;
import org.w3c.dom.domimplementation;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;

public class bizcharter extends httpservlet
{
private static final string content_type = "image/jpeg";
private static final string doc_type;
private static final string config_loc = "http://localhost/charts.xml";

private string version = "0.1.2";

document xmlconfig;
document xmldata;
document xmlchartsetup;

vector strdataxpaths = new vector();
vector strcategoryxpaths = new vector();
vector strstitles = new vector();

protected string strxpath;

private int ncategoryxpathcount=0;
private string strdataurl;
private string strchartconfigurl;
private string strservice;
private nodeiterator ninodelist;
private nodeiterator niservicelist;
private node ndeitem;
private node ndeservice;
private element elemitem;
private element elemservice;
private string strdummy;
private documentbuilderfactory dfactory;
private int nxsize = 0;
private int nysize = 0;
private categorydataset categorydata;
private jfreechart chart;
private boolean bfirsttime=false;
private int ndataxpathcount=0;

public void init(servletconfig config) throws servletexception
{
super.init(config);
}


public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
{
string strchart = "points";
string strx = "0";
string stry = "0";
try
{
strchart = request.getparameter("chart").tolowercase();
strx = request.getparameter("width");
stry = request.getparameter("height");
if(strx==null)
strx = "0";
if(stry==null)
stry = "0";
dfactory = documentbuilderfactory.newinstance();
dfactory.setnamespaceaware(true);
urlconnection con = new url(config_loc).openconnection();
con.connect();
inputstream newin = con.getinputstream();
xmlconfig = dfactory.newdocumentbuilder().parse(newin);
strxpath = "/root/chart[@id='" + strchart + "']";
ninodelist = xpathapi.selectnodeiterator(xmlconfig, strxpath);
ndeitem = ninodelist.nextnode();
elemitem = (element) ndeitem;
strdataurl = getchilditem(elemitem, "url");
strdataurl = strdataurl.replace('_','&');
loaddatadoc(strdataurl);
strchartconfigurl = getchilditem(elemitem, "config");
loadconfigdoc(strchartconfigurl);
strdummy = getchilditem(elemitem, "series");
loadseries(strdummy);
strdummy = getchilditem(elemitem, "categories");
loadcategories(strdummy);
nxsize = new integer(strx).intvalue();
if (nxsize==0)
{
strdummy = getchilditem(elemitem, "xsize");
nxsize = new integer(strdummy).intvalue();
}
nysize = new integer(stry).intvalue();
if (nysize==0)
{
strdummy = getchilditem(elemitem, "ysize");
nysize = new integer(strdummy).intvalue();
}

dochart();
response.setcontenttype("image/jpeg");
outputstream out = response.getoutputstream();
chartutilities.writechartasjpeg(out, chart, nxsize, nysize);
out.close();

}
catch(exception e)
{
e.printstacktrace();
}

}

private string getchilditem(element elemitem, string strelemname)
{
nodelist nl;
string strreturn;
nl = elemitem.getelementsbytagname(strelemname);
if(nl.getlength()>0)
{
node ndeitem = nl.item(0);
strreturn = ndeitem. getfirstchild().getnodevalue();
}
else
{
strreturn = "null";
}
return strreturn ;
}


private void loaddatadoc(string strdocurl)
{
try{
urlconnection urldataconn = new url(strdataurl).openconnection();
urldataconn.connect();
inputstream indata = urldataconn.getinputstream();
xmldata = dfactory.newdocumentbuilder().parse(indata);
}
catch (exception e){
}
}

private void loadconfigdoc(string strconfigurl)
{
try{
urlconnection urldataconn = new url(strconfigurl).openconnection();
urldataconn.connect();
inputstream indata = urldataconn.getinputstream();
xmlconfig = dfactory.newdocumentbuilder().parse(indata);
}
catch (exception e){
}
}

private void loadseries(string strseries)
{
int ncurrentloc = 0;
int nloc = 1;
string chtofind = ",";
string strcurrent;
strdataxpaths.clear();
ndataxpathcount = 0;
while(nloc>0)
{
nloc = strseries.indexof(chtofind,ncurrentloc);

if(nloc>0)
{
strcurrent = strseries.substring(ncurrentloc,nloc);
strseries = strseries.substring(nloc+1,strseries.length());

}
else
{
strcurrent = strseries;
}
strdataxpaths.add(strcurrent);
ndataxpathcount++;

}

}



private void loadcategories(string strcategories)
{
int ncurrentloc = 0;
int nloc = 1;
ncategoryxpathcount = 0;
string chtofind = ",";
string strcurrent;
strcategoryxpaths.clear();
while(nloc>0)
{
nloc = strcategories.indexof(chtofind,ncurrentloc);

if(nloc>0)
{
strcurrent = strcategories.substring(ncurrentloc,nloc);
strcategories = strcategories.substring(nloc+1,strcategories.length());

}
else
{
strcurrent = strcategories;
}
strcategoryxpaths.add(strcurrent);
ncategoryxpathcount++;

}

}


private void loadseriestitles(string strseriestitles)
{
int ncurrentloc = 0;
int nloc = 1;
string chtofind = ",";
string strcurrent;
strstitles.clear();
while(nloc>0)
{
nloc = strseriestitles.indexof(chtofind,ncurrentloc);

if(nloc>0)
{
strcurrent = strseriestitles.substring(ncurrentloc,nloc);
strseriestitles = strseriestitles.substring(nloc+1,strseriestitles.length());

}
else
{
strcurrent = strseriestitles;
}
strstitles.add(strcurrent);

}
}

private void dochart()
{
node nroot;
element nelem;
int ntype;
string strtitle;
string strcattitle;
string strvaltitle;
string strstitlelist;
nroot = xmlconfig.getdocumentelement();
nelem = (element) nroot;
ntype = new integer(getchilditem(nelem,"type")).intvalue();
strtitle = getchilditem(nelem,"title");
strcattitle = getchilditem(nelem,"cattitle");
strvaltitle = getchilditem(nelem, "valtitle");
strstitlelist = getchilditem(nelem, "seriestitles");
loadseriestitles(strstitlelist);

categorydata = createdataset();
switch(ntype){
case 0:
{
chart = chartfactory.createverticalbarchart(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 1:
{
piedataset piedata = datasetutilities.createpiedataset(categorydata, 0);
chart = chartfactory.createpiechart(strtitle, piedata, true);
break;
}
case 2:
{
chart = chartfactory.createhorizontalbarchart(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 3:
{
chart = chartfactory.createlinechart(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 4:
{
chart = chartfactory.createareachart(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 5:
{
chart = chartfactory.createhorizontalbarchart3d(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 6:
{
piedataset piedata = datasetutilities.createpiedataset(categorydata, 0);
chart = chartfactory.createpie3dchart(strtitle, piedata, true);
break;
}
case 7:
{
chart = chartfactory.createstackedhorizontalbarchart(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 8:
{
chart = chartfactory.createstackedverticalbarchart(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 9:
{
chart = chartfactory.createstackedverticalbarchart3d(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
case 10:
{
chart = chartfactory.createverticalbarchart3d(strtitle, strcattitle, strvaltitle, categorydata, true);
break;
}
}
setupchart();
}



private void setupchart()
{
plot theplot = chart.getplot();
float[][] pnthsbarray = new float[4][3];
pnthsbarray[0] = color.rgbtohsb(57,69,148,pnthsbarray[0]);
pnthsbarray[1] = color.rgbtohsb(255,215,49,pnthsbarray[1]);
pnthsbarray[2] = color.rgbtohsb(206,207,206,pnthsbarray[2]);
pnthsbarray[3] = color.rgbtohsb(99,150,255,pnthsbarray[3]);
paint[] mypaintarray = new paint[] { color.gethsbcolor(pnthsbarray[0][0],pnthsbarray[0][1],pnthsbarray[0][2]),
color.gethsbcolor(pnthsbarray[1][0],pnthsbarray[1][1],pnthsbarray[1][2]),
color.gethsbcolor(pnthsbarray[2][0],pnthsbarray[2][1],pnthsbarray[2][2]),
color.gethsbcolor(pnthsbarray[3][0],pnthsbarray[3][1],pnthsbarray[3][2]),
};
theplot.setseriespaint(mypaintarray);
chart.setbackgroundpaint(color.white);
}


private categorydataset createdataset()
{

int lp,ncurrent;
string strxpath;
string strval;
vector[] victor= new vector[ndataxpathcount];
try{

for(lp=0;lp<ndataxpathcount;lp++)
{
victor[lp] = new vector();
strxpath = strdataxpaths.elementat(lp).tostring();
ninodelist = xpathapi.selectnodeiterator(xmldata,strxpath);
elemitem = (element) ninodelist.nextnode();
do{
if(elemitem.getfirstchild()!=null)
{
strval = elemitem.getfirstchild().getnodevalue();
victor[lp].add(strval);
}
elemitem = (element) ninodelist.nextnode();
} while(elemitem!=null);
}
double[][] ndata = new double[ndataxpathcount][victor[0].size()];
for(lp=0;lp<ndataxpathcount;lp++)
{
for(ncurrent=0;ncurrent<victor[0].size();ncurrent++)
{
try{
ndata[lp][ncurrent] = new double(victor[lp].elementat(ncurrent).tostring());
}
catch (exception e){
ndata[lp][ncurrent] = new double(0);
}
}
}

vector vcategories = new vector();
strxpath = strcategoryxpaths.elementat(0).tostring();
ninodelist = xpathapi.selectnodeiterator(xmldata,strxpath);
elemitem = (element) ninodelist.nextnode();


for(lp=0;lp<victor[0].size();lp++)
{
strval = elemitem.getfirstchild().getnodevalue();
vcategories.add(strval);
elemitem = (element) ninodelist.nextnode();
}
defaultcategorydataset dset = new defaultcategorydataset(ndata);

string[] strt = new string[strstitles.size()];

for(lp=1;lp<=strstitles.size();lp++)
{
strt[lp-1] = strstitles.elementat(lp-1).tostring();
}
dset.setseriesnames(strt);
dset.setcategories(vcategories.toarray());



return dset;

}
catch (exception e)
{
e.printstacktrace();
}


double[][] ndata = new double[][]{{new double(0)},{new double(0)}};
return new defaultcategorydataset(ndata);

}

}

  代码段5 charts.xml

<?xml version="1.0"?>
<root>
 <chart id='points'>
  <url>http://localhost/players.xml</url>
  <config>http://localhost/pmconfig.xml</config>
  <series>//players/player[points>0 and minutes>1000]/points</series>
  <categories>//players/player[points>0 and minutes>1000]/name</categories>
  <xsize>600</xsize>
  <ysize>400</ysize>
 </chart>
 <chart id='defenders'>
  <url>http://localhost/players.xml</url>
  <config>http://localhost/dfconfig.xml</config>
  <series>//players/player[position='def' or position='def-mid']/points</series>
  <categories>//players/player[position='def' or position='def-mid']/name</categories>
  <xsize>600</xsize>
  <ysize>400</ysize>
 </chart>
 <chart id='makeup'>
  <url>http://localhost/players.xml</url>
  <config>http://localhost/muconfig.xml</config>
  <series>//players/player/goals,//players/player/points</series>
  <categories>//players/player/number</categories>
  <xsize>600</xsize>
  <ysize>400</ysize>
 </chart>
</root>

  代码段6 dfconfig.xml

<?xml version="1.0"?>
<chart>
 <type>10</type>
 <title>points by defender</title>
 <cattitle>player name</cattitle>
 <valtitle>points</valtitle>
 <seriestitles>points scored</seriestitles>
</chart>

  代码段7 muconfig.xml

<?xml version="1.0"?>
<chart>
 <type>9</type>
 <title>points breakdown</title>
 <cattitle>player number</cattitle>
 <valtitle>points</valtitle>
 <seriestitles>goals, assists</seriestitles>
</chart>

  代码段8 players.xml

<players team="new york power">
 <player>
  <number>30</number>
  <position>mid</position>
  <name>justi baumgardt-yamada</name>
  <gamesplayed>5</gamesplayed>
  <gamesstarted>2</gamesstarted>
  <minutes>233</minutes>
  <goals>0</goals>
  <assists>1</assists>
  <points>1</points>
  <shots>6</shots>
  <shotsongoal>2</shotsongoal>
  <blocks>0</blocks>
  <foulscommitted>2</foulscommitted>
  <foulssuffered>5</foulssuffered>
  <cautions>0</cautions>
  <ejections>0</ejections>
 </player>
 <player>
  <number>12</number>
  <position>mid</position>
  <name>kirsta davey</name>
  <gamesplayed>15</gamesplayed>
  <gamesstarted>1</gamesstarted>
  <minutes>357</minutes>
  <goals>2</goals>
  <assists>0</assists>
  <points>4</points>
  <shots>8</shots>
  <shotsongoal>5</shotsongoal>
  <blocks>0</blocks>
  <foulscommitted>6</foulscommitted>
  <foulssuffered>7</foulssuffered>
  <cautions>1</cautions>
  <ejections>0</ejections>
 </player>

  代码段 9 pmconfig.xml

<?xml version="1.0"?>
<chart>
 <type>1</type>
 <title>points by player</title>
 <cattitle>player name</cattitle>
 <valtitle>points</valtitle>
 <seriestitles>points scored</seriestitles>
</chart>

扫描关注微信公众号