fcGraphServlet.java
资源名称:OA.rar [点击查看]
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:14k
源码类别:
OA系统
开发平台:
Java
- //package dom;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.sql.*;
- import java.io.*;
- import dom.fcGraph;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.util.Enumeration;
- import java.awt.*;
- import org.xml.sax.* ;
- import org.apache.xerces.dom.TextImpl;
- import org.w3c.dom.Attr;
- import org.w3c.dom.Document;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import dom.DOMParserWrapper;
- import dom.wrappers.DOMParser;
- import java.lang.Boolean;
- public class fcGraphServlet extends HttpServlet {
- private String strXml="";
- private Document document=null;
- public void service( HttpServletRequest req, HttpServletResponse res )
- throws IOException, ServletException {
- //res.setContentType("text/html");
- res.setContentType("image/jpeg");
- /*
- fcGraph sg = new fcGraph();
- try {
- ServletOutputStream out = null;
- out = res.getOutputStream();
- int iType=1;
- */
- String strKey = req.getParameter( "key" );
- if (strKey.equals("send"))
- {
- //送参数
- //计算前台SEND来的XML串 存入session中
- BufferedReader in = req.getReader();
- String line = null;
- strXml="";
- while ((line = in.readLine()) != null) {
- strXml=strXml+line;
- }
- // strXml=encodeTrans(strXml,"8859") ;
- HttpSession session = req.getSession(true);
- session.setAttribute("graphdata", strXml);
- }
- if (strKey.equals("show"))
- {
- HttpSession session = req.getSession(true);
- strXml=session.getAttribute("graphdata").toString();
- //strKey为?后的值
- //String strXml=new String(req.getQueryString());
- strXml=Trans(strXml);
- //parse XML串
- try {
- DOMParserWrapper parser =(DOMParserWrapper)Class.forName("dom.wrappers.DOMParser").newInstance();
- document = parser.parseXml(strXml);
- }
- catch (Exception e) {
- }
- //--------------------------------------------------------
- //--------------------------------------------------------
- fcGraph sg = new fcGraph();
- try {
- ServletOutputStream out = null;
- out = res.getOutputStream();
- //图形类型1 2 3
- String sType = req.getParameter( "type" );
- int iType=Integer.parseInt(sType,10);
- //行数
- String sRows = req.getParameter( "rows" );
- int iRows=Integer.parseInt(sRows,10);
- //列数
- String sCols = req.getParameter( "cols" );
- int iCols=Integer.parseInt(sCols,10);
- //标题
- String sTitle="";
- String sWidth="";
- String sHeight="";
- String strMargin="";
- String strLabeltext="";//X轴标题的内容
- String strLabelleft="";//标题的横坐标
- String strLabeltop="";//标题的纵坐标
- String strArcCenterleft="100";//圆心的横坐标
- String strArcCentertop="150";//圆心的纵坐标
- String strArcRadius="70";//圆的半径
- String strPieHigh="30";
- String strExchangeXY="";//当strExchangeXY设置为true时,报表的行,列的数据要进行调换;接收时其值为true或false
- String strWriteNumeric="";//每个柱形图上是否写上相应的数值
- boolean bWriteNumeric=false;//将strWriteNumeric转化为boolean型值后,再传到fcGraph.class中
- String strSplit="";
- String strEffectCols="";//实际列数
- try {
- sTitle = req.getParameter( "title" );
- }
- catch( Exception e ) { }
- try {
- sWidth = req.getParameter( "width" );
- sHeight = req.getParameter( "height" );
- strMargin=req.getParameter( "ImgMargin" );
- //System.out.println("strMargin="+strMargin);
- }
- catch( Exception e ) { }
- try {
- strLabeltext = req.getParameter( "Label" );
- }
- catch( Exception e ) { }
- try {
- strLabelleft = req.getParameter( "LabelLeft" );
- }
- catch( Exception e ) { }
- try {
- strLabeltop = req.getParameter( "LabelTop" );
- }
- catch( Exception e ) { }
- try {
- strArcCenterleft = req.getParameter( "ArcLeft" );
- }
- catch( Exception e ) { }
- try {
- strArcCentertop = req.getParameter( "ArcTop" );
- }
- catch( Exception e ) { }
- try {
- strArcRadius = req.getParameter( "ArcRadius" );
- }
- catch( Exception e ) { }
- try {
- strPieHigh = req.getParameter( "PieHigh" );
- }
- catch( Exception e ) { }
- try {
- strExchangeXY=req.getParameter("ExchangeXY");
- }
- catch(Exception e){}
- try {
- strWriteNumeric=req.getParameter("StrWriteNumeric");
- }
- catch(Exception e){}
- try {
- strSplit=req.getParameter("StrSplit");
- }
- catch(Exception e){}
- try {
- strEffectCols=req.getParameter("EffectCols");
- }
- catch(Exception e){}
- int iWidth=Integer.parseInt(sWidth,10);
- int iHeight=Integer.parseInt(sHeight,10);
- int iMargin=Integer.parseInt(strMargin);
- //System.out.println("iMargin="+iMargin);
- int Labelleft=Integer.parseInt(strLabelleft);
- int Labeltop=Integer.parseInt(strLabeltop);
- int strlength=strLabeltext.length();
- int PieHigh=Integer.parseInt(strPieHigh);
- if (Labelleft==0){
- Labelleft=iWidth-(strlength*12+30);
- }
- if (Labeltop==0){
- Labeltop=iHeight-10;
- }
- int Arcleft=Integer.parseInt(strArcCenterleft);
- int Arctop=Integer.parseInt(strArcCentertop);
- int ArcRadius=Integer.parseInt(strArcRadius);
- if (strWriteNumeric.equals("true")){
- bWriteNumeric=true;
- }
- else {
- bWriteNumeric=false;
- }
- int iSplit=Integer.parseInt(strSplit);
- int effectCols=Integer.parseInt(strEffectCols);
- //数据输入
- Node root = document.getChildNodes().item(0);
- Hashtable data= new Hashtable();
- int i=0;
- int j=0;
- Vector V=new Vector(iRows);//行的KEY
- Vector V2=new Vector(iCols);//列的KEY
- if (strExchangeXY.equals("true")){
- //当strExchangeXY="true"时,表示报表行和列的数据要进行对换
- if (iCols>2) {
- for (i=1;i<iCols;i++)
- {
- String s2 =root.getChildNodes().item(0).getChildNodes().item(i).getChildNodes().item(0).getNodeValue();
- //if (iCols>2) {
- //多列
- Hashtable datas1= new Hashtable();
- for (j=1;j<iRows;j++)
- {
- String ss =root.getChildNodes().item(j).getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
- String s3=root.getChildNodes().item(j).getChildNodes().item(i).getChildNodes().item(0).getNodeValue();
- datas1.put(ss,new Double(s3));
- if (i==1)
- V2.addElement(new String(ss));
- }
- data.put(s2,datas1);
- V.addElement(s2);
- }
- }
- else {
- Hashtable datas1= new Hashtable();
- String s2 =root.getChildNodes().item(0).getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
- for (j=1;j<iRows;j++)
- {
- String ss =root.getChildNodes().item(j).getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
- String s3=root.getChildNodes().item(j).getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
- datas1.put(ss,new Double(s3));
- V2.addElement(new String(ss));
- }
- data.put(s2,datas1);
- V.addElement(s2);
- }
- }
- else {
- //此else块是处理,strExchangeXY不等于"true"时,报表行和列的数据不进行对换,/////============================
- for (i=1;i<iRows;i++)
- {
- String s2 =root.getChildNodes().item(i).getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
- if (iCols>2) {
- //多列
- Hashtable datas1= new Hashtable();
- for (j=1;j<iCols;j++)
- {
- String ss =root.getChildNodes().item(0).getChildNodes().item(j).getChildNodes().item(0).getNodeValue();
- String s3=root.getChildNodes().item(i).getChildNodes().item(j).getChildNodes().item(0).getNodeValue();
- datas1.put(ss,new Double(s3));
- if (i==1)
- V2.addElement(new String(ss));
- }
- data.put(s2,datas1);
- }
- else
- {
- //单列
- String s4=root.getChildNodes().item(i).getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
- data.put(s2,new Double(s4));
- }
- V.addElement(s2);
- }
- }
- //----------------------------------------------------
- /*
- Hashtable data= new Hashtable();
- Hashtable datas1= new Hashtable();
- datas1.put("一车间",new Double(10));
- datas1.put("二车间",new Double(30));
- datas1.put("三车间",new Double(150));
- datas1.put("四车间",new Double(20));
- datas1.put("五车间",new Double(30));
- data.put("一分厂",datas1);
- Hashtable datas2= new Hashtable();
- datas2.put("一车间",new Double(40));
- datas2.put("二车间",new Double(70));
- datas2.put("三车间",new Double(90));
- datas2.put("四车间",new Double(10));
- datas2.put("五车间",new Double(20));
- data.put("二分厂",datas2);
- Hashtable datas3= new Hashtable();
- datas3.put("一车间",new Double(86));
- datas3.put("二车间",new Double(40));
- datas3.put("三车间",new Double(50));
- datas3.put("四车间",new Double(150));
- datas3.put("五车间",new Double(50));
- data.put("三分厂",datas3);
- Hashtable datas4= new Hashtable();
- datas4.put("一车间",new Double(66));
- datas4.put("二车间",new Double(100));
- datas4.put("三车间",new Double(36));
- datas4.put("四车间",new Double(26));
- //datas4.put("五车间",new Double(56));
- data.put("四分厂",datas4);
- Hashtable datas5= new Hashtable();
- datas5.put("一车间",new Double(66));
- datas5.put("二车间",new Double(100));
- datas5.put("三车间",new Double(36));
- datas5.put("四车间",new Double(26));
- //datas4.put("五车间",new Double(56));
- data.put("五分厂",datas5);
- Hashtable datas6= new Hashtable();
- datas6.put("一车间",new Double(66));
- datas6.put("二车间",new Double(100));
- datas6.put("三车间",new Double(36));
- datas6.put("四车间",new Double(26));
- //datas4.put("五车间",new Double(56));
- data.put("六分厂",datas6);
- Hashtable datas7= new Hashtable();
- datas7.put("一车间",new Double(66));
- datas7.put("二车间",new Double(100));
- datas7.put("三车间",new Double(36));
- datas7.put("四车间",new Double(26));
- //datas4.put("五车间",new Double(56));
- data.put("七分厂",datas7);
- Vector V=new Vector(8);
- V.addElement(new String("一分厂"));
- V.addElement(new String("二分厂"));
- V.addElement(new String("三分厂"));
- V.addElement(new String("四分厂"));
- V.addElement(new String("五分厂"));
- V.addElement(new String("六分厂"));
- V.addElement(new String("七分厂"));
- V.addElement(new String("八分厂"));
- Vector V2=new Vector(8);
- V2.addElement(new String("一车间"));
- V2.addElement(new String("二车间"));
- V2.addElement(new String("三车间"));
- V2.addElement(new String("四车间"));
- V2.addElement(new String("五车间"));
- //----------------------------------------------------
- */
- sg.SetHashtableKey(V);
- sg.SetDataSource(data);
- sg.SetbgColor(Color.white);//white);//底色
- sg.SetDisplayType(iType); //设置图形风格
- if (iWidth>0 && iHeight>0)
- sg.SetScreen(iWidth,iHeight,iMargin);//设置显示区域大小
- else
- sg.SetScreen(600,300,iMargin);//设置显示区域大小
- //sg.SetTitleFont(new Font("楷体_GB2312",Font.BOLD,24));//设置标题字体
- sg.SetTitle(sTitle); //设置标题
- sg.SetTitlefontcolor(Color.blue);//设置标题颜色
- sg.SetcooColor(Color.black); //坐标系(x轴y轴)颜色
- sg.SetlineColor(Color.black);//折线颜色
- //sg.SetCategoryFont(new Font("楷体_GB2312",Font.BOLD,18));//设置X轴标签字体
- sg.SetCategoryfontcolor(Color.blue);//设置X轴标签颜色
- sg.SetCategory(strLabeltext,Labelleft,Labeltop); //设置X轴标签内容、位置
- sg.Setxnodefontcolor(Color.black);//设置X轴节点字体的颜色
- //sg.Setxnodefont(new Font("楷体_GB2312",Font.BOLD,16)); //设置X轴节点字体
- //sg.SetValueFont(new Font("楷体_GB2312",Font.BOLD,18));//设置Y轴标题字体
- sg.SetValueFontcolor(Color.green);//设置Y轴标题颜色
- //sg.Setynodefont(new Font("楷体_GB2312",Font.BOLD,16));//设置Y轴节点字体
- sg.Setynodefontcolor(Color.black);//设置Y轴节点字体、颜色
- sg.SetPieCenter(Arcleft,Arctop);//设置圆饼圆心
- sg.Setradius(ArcRadius);//半径
- sg.SetPieHeight(PieHigh);
- //boolean WriteNumeric=true;
- sg.SetWriteNumeric(bWriteNumeric);
- sg.SetSplitData(iSplit);
- sg.SetEffectCols(effectCols);
- //sg.SetPieFont(new Font("楷体_GB2312",Font.BOLD,14));//圆饼字体
- if (strExchangeXY.equals("true")){
- //if (iCols>2)
- //{
- sg.SetexistSeries(1);
- sg.SetHashtableKey2(V2);
- //}
- //else
- //{
- //sg.SetexistSeries(1);
- //sg.SetValue(root.getChildNodes().item(0).getChildNodes().item(1).getChildNodes().item(0).getNodeValue());//设置Y轴标题内容、位置
- //sg.SetHashtableKey2(V2);
- //}
- }
- else {
- if (iCols>2)
- {
- sg.SetexistSeries(1);
- sg.SetHashtableKey2(V2);
- }
- else
- {
- sg.SetexistSeries(0);
- sg.SetValue(root.getChildNodes().item(0).getChildNodes().item(1).getChildNodes().item(0).getNodeValue());//设置Y轴标题内容、位置
- }
- }
- sg.DrawGraph( out );//绘制输出
- }
- catch( Exception e ) {
- throw new ServletException( e );
- }
- }
- }
- private String encodeTrans( String src, String encode ) {
- if( src == null ) return null;
- try {
- src = new String( src.getBytes( encode ), "gb2312" );
- }
- catch( Exception e ) {}
- return src;
- }
- private String Trans(String str1) {
- String sss="";
- try {
- byte [] by = str1.getBytes("8859_1");
- sss = new String(by,"UTF-8");
- }
- catch( Exception e ) { }
- return sss;
- }
- }
- //Fontname="Dialog","SansSerif","Monospaced","Helvetica"
- //"TimesRoman","Courier","DiagInput","ZapfDiagbats"
- //Fonttype="Plain""Bold""Italic""BoldItalic"
- //Fontsize="12", "14", "16", "18", "24", "36"