private PdfPCell createKPGSPieChartCell(KeyValue[] values, String title)
      throws IOException, BadElementException {
    DefaultPieDataset dataset = new DefaultPieDataset();
    for (int n = 0; n < values.length; n++) {
      dataset.setValue(values[n].getKey(), new Double(values[n].getValue()).doubleValue());
    }
    // create chart
    final JFreeChart chart =
        ChartFactory.createPieChart(
            title, // chart title
            dataset, // data
            true, // legend
            false, // tooltips
            false // urls
            );
    // customize chart
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(java.awt.Color.WHITE);
    if (values.length > 0) {
      if (values[0].getKey().equalsIgnoreCase("?") && values.length > 1) {
        plot.setExplodePercent(values[1].getKey(), 0.2);
      } else {
        plot.setExplodePercent(values[0].getKey(), 0.2);
      }
    }
    plot.setLegendLabelGenerator(new KPGSLegendGenerator());
    plot.setLabelGenerator(new KPGSLabelGenerator(values));
    plot.setOutlineVisible(false);
    chart.setAntiAlias(true);
    LegendTitle legendTitle = (LegendTitle) chart.getSubtitle(0);
    legendTitle.setFrame(BlockBorder.NONE);
    legendTitle.setBackgroundPaint(null);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ChartUtilities.writeChartAsPNG(
        os,
        chart,
        MedwanQuery.getInstance().getConfigInt("stats.piechartwidth", 640),
        MedwanQuery.getInstance().getConfigInt("stats.piechartheight", 480));
    cell = new PdfPCell();
    cell.setColspan(50);
    cell.setImage(Image.getInstance(os.toByteArray()));
    cell.setBorder(PdfPCell.NO_BORDER);
    cell.setPaddingLeft(5);
    cell.setPaddingRight(5);
    return cell;
  }
Example #2
0
  /**
   * Creates a chart.
   *
   * @param dataset the dataset.
   * @return a chart.
   */
  private static JFreeChart createChart(PieDataset dataset) {

    JFreeChart chart =
        ChartFactory.createPieChart(
            "Pie Chart Demo 4", // chart title
            dataset, // data
            true, // include legend
            true,
            false);

    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setNoDataMessage("No data available");
    plot.setCircular(false);
    plot.setLabelGap(0.02);
    plot.setExplodePercent("Section D", 0.50);
    plot.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
    return chart;
  }
  private BufferedImage generarGraficoTorta(
      String titulo, double[] valores, String[] funciones, int anchoImagen, int altoImagen) {
    if (valores.length != funciones.length) {
      return null;
    }
    DefaultPieDataset pieDataset = new DefaultPieDataset();
    for (int i = 0; i < valores.length; i++) {
      pieDataset.setValue(funciones[i], valores[i]);
    }
    JFreeChart chart = ChartFactory.createPieChart(titulo, pieDataset, true, true, false);
    chart.setBackgroundPaint(null);
    chart.getTitle().setPaint(LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL);
    chart.getTitle().setFont(LookAndFeelEntropy.FUENTE_TITULO_GRANDE);
    chart.setBorderVisible(false);
    chart.getLegend().setItemFont(LookAndFeelEntropy.FUENTE_REGULAR);
    chart.getLegend().setBackgroundPaint(LookAndFeelEntropy.COLOR_TABLA_PRIMARIO);

    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(null);
    plot.setBackgroundImageAlpha(0.0f);
    plot.setSimpleLabels(true);
    plot.setOutlineVisible(false);
    for (int i = 0; i < valores.length; i++) {
      plot.setSectionPaint(
          funciones[i],
          generarColorAleatorio(Color.ORANGE)); // LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL));
      plot.setExplodePercent(funciones[i], 0.10);
    }
    PieSectionLabelGenerator gen =
        new StandardPieSectionLabelGenerator(
            "{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0%"));
    plot.setLabelGenerator(gen);

    this.lastChart = chart;

    // Generamos una imagen
    return chart.createBufferedImage(anchoImagen, altoImagen);
  }
  public JFreeChart getChart() {
    JFreeChart chart = null;
    try {
      Font font = new Font("宋体", 0, 20); // 字体
      QuestionStatisticsModel qsm = ss.statistics(qid);
      DefaultPieDataset pieds = null; // 饼图的数据集
      DefaultCategoryDataset cateds = null; // 种类数据集

      if (chartType < 2) {
        pieds = new DefaultPieDataset();
        for (OptionStatisticsModel os : qsm.getOsms()) {
          pieds.setValue(os.getOptionLabel(), os.getCount());
        }
      } else {
        cateds = new DefaultCategoryDataset();
        for (OptionStatisticsModel os : qsm.getOsms()) {
          cateds.setValue(os.getCount(), os.getOptionLabel(), "");
        }
      }

      switch (chartType) {
        case CHARTTYPE_PIE_2D:
          chart =
              ChartFactory.createPieChart(qsm.getQuestion().getTitle(), pieds, true, false, false);
          break;
        case CHARTTYPE_PIE_3D:
          chart =
              ChartFactory.createPieChart3D(qsm.getQuestion().getTitle(), pieds, true, true, true);
          // 设置前景色透明度
          chart.getPlot().setForegroundAlpha(0.6f);
          break;
        case CHARTTYPE_BAR_2D_H: // 平面条形图
          chart =
              ChartFactory.createBarChart(
                  qsm.getQuestion().getTitle(),
                  "",
                  "",
                  cateds,
                  PlotOrientation.HORIZONTAL,
                  true,
                  true,
                  true);
          break;
        case CHARTTYPE_BAR_2D_V: // 平面条形图
          chart =
              ChartFactory.createBarChart(
                  qsm.getQuestion().getTitle(),
                  "",
                  "",
                  cateds,
                  PlotOrientation.VERTICAL,
                  true,
                  true,
                  true);
        case CHARTTYPE_BAR_3D_H: // 平面条形图
          chart =
              ChartFactory.createBarChart3D(
                  qsm.getQuestion().getTitle(),
                  "",
                  "",
                  cateds,
                  PlotOrientation.HORIZONTAL,
                  true,
                  true,
                  true);
        case CHARTTYPE_BAR_3D_V: // 平面条形图
          chart =
              ChartFactory.createBarChart3D(
                  qsm.getQuestion().getTitle(),
                  "",
                  "",
                  cateds,
                  PlotOrientation.VERTICAL,
                  true,
                  true,
                  true);
          break;
          //
        case CHARTTYPE_LINE_2D: // 平面条形图
          chart =
              ChartFactory.createLineChart(
                  qsm.getQuestion().getTitle(),
                  "",
                  "",
                  cateds,
                  PlotOrientation.VERTICAL,
                  true,
                  true,
                  true);
          break;
        case CHARTTYPE_LINE_3D: // 平面条形图
          chart =
              ChartFactory.createLineChart3D(
                  qsm.getQuestion().getTitle(),
                  "",
                  "",
                  cateds,
                  PlotOrientation.HORIZONTAL,
                  true,
                  true,
                  true);
          break;
      }

      chart.getTitle().setFont(font);
      chart.getLegend().setItemFont(font);

      // 设置饼图特效
      if (chart.getPlot() instanceof PiePlot) {
        PiePlot pieplot = (PiePlot) chart.getPlot();
        pieplot.setLabelFont(font);
        pieplot.setExplodePercent(0, 0.1);
        pieplot.setStartAngle(-15);
        pieplot.setDirection(Rotation.CLOCKWISE);
        pieplot.setNoDataMessage("No data to display");
        // pieplot.setForegroundAlpha(0.5f);
        // pieplot.setBackgroundImageAlpha(0.3f);
      }
      // 设置非饼图效果
      else {
        chart.getCategoryPlot().getRangeAxis().setLabelFont(font);
        chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font);
        chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
        chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return chart;
  }