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

在Struts中用JFreeChart生成圖表

2022-06-13   來源: Java開源技術 

  類中方法
  
  引入必要的包
  import javaxservlethttpHttpSession;
  import javaxservlethttpHttpServletRequest;
  import orgjfreechartJFreeChart;
  import orgjfreechartChartFactory;
  import orgjfreedatageneralDefaultPieDataset;
  import orgjfreechartplotPiePlot;
  import orgjfreechartChartRenderingInfo;
  import orgjfreechartentityStandardEntityCollection;import orgjfreechartservletServletUtilities;int n = orgCourseListsize();
  //List的長度int[] Num = new int[n];
  //存放人數String[] orgName = new String[n];
  //存放機構名稱String title = 參加人數統計圖;
  for (int i = ;
  i < n; i++) {RepVteachStatic temp = new RepVteachStatic();
  //臨時存放對象視情況可變RepVteachStatic是一個DTO(數據傳輸對象)temp = (RepVteachStatic) orgCourseListget(i);
  //取得List中的對象int personNum = repVteachStaticServicegetOrgCoursePersonNum(temp);
  //查詢出參加人數Num[i] = personNum;
  orgName[i] = tempgetOrg_name();
  }getOrgCoursePieChart(Num orgName title);
  //用三個參數調用圖表生成方法
  
  圖表生成方法
  public void getOrgCoursePieChart(int[] Num String[] orgName String title) {
  DefaultPieDataset data = new DefaultPieDataset();
  for (int i = n = Numlength; i < n; i++) {
  datasetValue(orgName[i] Num[i]);
  }
  JFreeChart chart = ChartFactorycreatePieChartD(title data truefalse false);
  chartsetBackgroundPaint(javaawtColorgreen);
  ChartRenderingInfo info = new ChartRenderingInfo(
  new StandardEntityCollection());
  try {
  HttpSession session = ActionContextgetActionContext()getRequest()getSession();
  //取session對象視情況取法會有變化
  HttpServletRequest request = ActionContextgetActionContext()getRequest();
  //取request對象視情況取法會有變化
  String filename = ServletUtilitiessaveChartAsPNG(chart info session);
  graphURL = requestgetContextPath()
  + /servlet/DisplayChart?filename= + filename;
  }catch (Exception e) {}}
  
  頁面
  <img src=<bean:writename=repVteachStaticBean property=graphURL/> >
From:http://tw.wingwit.com/Article/program/Java/ky/201311/11167.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.