熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JFreeChart封裝示例

2013-11-23 18:47:55  來源: Java核心技術 

  以前用JFreeChart的時候每次都需要修改一些顯示樣式和代碼前段時間項目組要用於是就進行了一次封裝把幾個常用的樣式都調整好了包括三種常用的圖表類型都封裝到一起了項目組的其他人員只要處理好要顯示的數據就行了

  webxml設置

  <servlet>

  <servletname>DisplayChart</servletname>

  <servletclass>    orgjfreechartservletDisplayChart   </servletclass>

  </servlet>

  <servletmapping>

  <servletname>DisplayChart</servletname>

  <urlpattern>/servlet/DisplayChart</urlpattern>

  </servletmapping>

  柱狀圖封裝和設置

  public static JFreeChart getLineChart(String title String xSubject String ySubject CategoryDataset dataset) {

  JFreeChart chart = ChartFactorycreateLineChart(title xSubject ySubject dataset PlotOrientationVERTICAL true true false);

  LineRendererD renderer = new LineRendererD();

  renderersetItemLabelGenerator(new StandardCategoryItemLabelGenerator());

  renderersetItemLabelsVisible(true);

  renderersetPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchorOUTSIDE TextAnchorBOTTOM_CENTER));

  CategoryPlot plot = chartgetCategoryPlot();

  CategoryAxis domainAxis = plotgetDomainAxis();   //domainAxissetVerticalCategoryLabels(false);

  plotsetDomainAxis(domainAxis);

  ValueAxis rangeAxis = plotgetRangeAxis();   //設置最高的一個 Item 與圖片頂端的距離

  rangeAxissetUpperMargin();   //設置最低的一個 Item 與圖片底端的距離

  rangeAxissetLowerMargin();

  plotsetRangeAxis(rangeAxis);

  plotsetRenderer(renderer);   //設置透明度

  plotsetForegroundAlpha(f);   //設置背景透明度

  plotsetBackgroundAlpha(f);      //設置背景顏色

  chartsetBackgroundPaint(new Color());

  chartsetBackgroundImageAlpha(f);

  return chart;

  }

  調用代碼

  public JFreeChart getSingleChart(String chartType)  {

  //構造數據

  double[][] data = new double[][];

  for(int i = ; i < ; i ++)

  {    data[][i] = Mathround(Mathrandom()*);   }

  //根據類型顯示不同類型圖表

  String[] rowKeys = new String[]{Rule};

  String[] columnKeys = new String[]{};

  CategoryDataset dataset = DatasetUtilitiescreateCategoryDataset(rowKeys columnKeys data);

  if(chartTypeequals(pole))

  return comutilMyChartFactorygetBarChart(校驗規則統計 日期 案例(個) dataset);

  else if(chartTypeequals(line))

  return comutilMyChartFactorygetLineChart(校驗規則統計 日期 案例(個) dataset);

  else //if(chartTypeequals(area))

  return comutilMyChartFactorygetAreaChart(校驗規則統計 日期 案例(個) dataset);

  }

  頁面代碼

  <%@ page contentType=text/html; charset=gb language=java errorPage=%>
<%@ page import=orgjfreechart*orgjfreechartservletServletUtilities%>
<jsp:useBean scope=page id=chartTest class=testChartTest />
<%  String chartType = requestgetParameter(chartType);
  if(chartType == null) chartType = line;
   JFreeChart chart = chartTestgetSingleChart(chartType); String filename = ;
String graphURL = /images/nodatagif; if(chart != null)
{
  filename = ServletUtilitiessaveChartAsPNG(chart null session);
  graphURL = requestgetContextPath() + /servlet/DisplayChart?filename= + filename;
}
%>
<HTML>
  <HEAD>
   <title></title>
   <LINK type=text/css rel=stylesheet>
  </HEAD>
  <body bottomMargin= leftMargin= topMargin= rightMargin=>
   <form name=myform method=post action=>
    <table width=% border= align=center cellpadding=     cellspacing=>
     <tr>
      <td valign=top>
       <TABLE class=td
        cellSpacing= cellPadding= border=>
        <TR>
         <TD class=tttable colspan=>
          <TABLE id=Table            cellSpacing= cellPadding= width=% align=right           border=>
           <TR>
            <TD vAlign=middle noWrap>
             <TABLE id=Table
              cellSpacing= cellPadding= width=% border=>
              <TR>
               <TD noWrap align=center noWrap> 
               圖表類型
               </TD>
               <TD noWrap>
                <select name=chartType>
                 <option value=line <%if(chartTypeequals(line))outprint(selected);%>>折線圖</option>
                 <option value=pole <%if(chartTypeequals(pole))outprint(selected);%>>柱狀圖</option>                 <option value=area <%if(chartTypeequals(area))outprint(selected);%>>面積圖</option>
                </select>
               </TD>
               <TD>
                &nbsp;<a # onclick=chkFrm();>
                <img src=/images/btnQuerygif border= align=absmiddle></a>
                </TD>
              </TR>
             </TABLE>
            </TD>
           </TR>
          </TABLE>
         </TD>
        </TR>
        <TR>
         <TD class=tttable colspan=>
          <table width=% border= class=mainFunctionTable  cellpadding= cellspacing=>
           <tr height=>
            <td>
              <img src=<%=graphURL%> border= usemap=#<%=filename%> align=center>
            </td>
           </tr>
          </table>
         </TD>
        </TR>
       </TABLE>
      </td>
     </tr>
    </table>
   </form>
   <script>
function chkFrm()
{
  documentmyformsubmit();
}
</script> </body> </HTML>


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25744.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.