コード例 #1
0
  public String generatePieChart(
      String hitOrdNum, HttpSession session, PrintWriter pw, String courseId, int studentId) {
    /* int groupId=0;
                if (groupName.equals("All")){
                    groupId=0;
                }else{
                     groupId = studStatisticBean.getGroupIdByName(groupName);
    }*/
    String filename = null;
    try {
      //  Retrieve list of WebHits
      StudentsConceptChartDataSet whDataSet =
          new StudentsConceptChartDataSet(studStatisticBean, courseId, studentId);
      ArrayList list = whDataSet.getDataBySection(hitOrdNum);

      //  Throw a custom NoDataException if there is no data
      if (list.size() == 0) {
        System.out.println("No data has been found");
        throw new NoDataException();
      }

      //  Create and populate a PieDataSet
      DefaultPieDataset data = new DefaultPieDataset();
      Iterator iter = list.listIterator();
      while (iter.hasNext()) {
        StudentsConceptHit wh = (StudentsConceptHit) iter.next();
        data.setValue(wh.getSection(), wh.getHitDegree());
      }

      //  Create the chart object
      PiePlot plot = new PiePlot(data);
      plot.setInsets(new Insets(0, 5, 5, 5));
      plot.setURLGenerator(new StandardPieURLGenerator("xy_chart.jsp", "section"));
      plot.setToolTipGenerator(new StandardPieItemLabelGenerator());
      JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
      chart.setBackgroundPaint(java.awt.Color.white);

      //  Write the chart image to the temporary directory
      ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
      filename = ServletUtilities.saveChartAsPNG(chart, 600, 400, info, session);

      //  Write the image map to the PrintWriter
      ChartUtilities.writeImageMap(pw, filename, info);
      pw.flush();

    } catch (NoDataException e) {
      System.out.println(e.toString());
      filename = "public_nodata_500x300.png";
    } catch (Exception e) {
      System.out.println("Exception - " + e.toString());
      e.printStackTrace(System.out);
      filename = "public_error_500x300.png";
    }

    return filename;
  }