private static JFreeChart createChart(CategoryDataset categorydataset) {
   JFreeChart jfreechart =
       ChartFactory.createStackedBarChart3D(
           "Stacked Bar Chart 3D Demo 2",
           "Category",
           "Value",
           categorydataset,
           PlotOrientation.VERTICAL,
           true,
           true,
           false);
   CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
   NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
   numberaxis.setNumberFormatOverride(new DecimalFormat("0.0%"));
   StackedBarRenderer3D stackedbarrenderer3d = (StackedBarRenderer3D) categoryplot.getRenderer();
   stackedbarrenderer3d.setRenderAsPercentages(true);
   stackedbarrenderer3d.setDrawBarOutline(false);
   stackedbarrenderer3d.setBaseItemLabelGenerator(
       new StandardCategoryItemLabelGenerator(
           "{3}", NumberFormat.getIntegerInstance(), new DecimalFormat("0.0%")));
   stackedbarrenderer3d.setBaseItemLabelsVisible(true);
   stackedbarrenderer3d.setBasePositiveItemLabelPosition(
       new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
   stackedbarrenderer3d.setBaseNegativeItemLabelPosition(
       new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
   return jfreechart;
 }
示例#2
0
  public static String getSimplePieChart(Map dataSource, String objectName, HttpSession session)
      throws Throwable {
    DefaultPieDataset dataset = new DefaultPieDataset();

    Element chartObject =
        XMLHandler.getElementByAttribute(getChartObjectList(), "name", objectName);

    String title = chartObject.getAttributeValue("title");

    int width = Integer.parseInt(chartObject.getAttributeValue("width"));
    int height = Integer.parseInt(chartObject.getAttributeValue("height"));

    Element LabelKeys = chartObject.getChild("Labels");
    Element ValueKeys = chartObject.getChild("Values");

    String valueKey = ValueKeys.getText();
    String valueType = ValueKeys.getAttributeValue("type");
    List labelKeys = LabelKeys.getChildren("Label");
    String labelKey = LabelKeys.getText();

    if (valueType.equalsIgnoreCase("number")) {
      for (int i = 0; i < dataSource.size(); i++) {
        Map rec = (Map) dataSource.get("ROW" + i);
        Number value = (Number) rec.get(valueKey);
        String label;
        if (labelKeys.isEmpty()) {
          label = DataFilter.show(rec, labelKey);
        } else {
          label = ((Element) labelKeys.get(i)).getText();
        }
        dataset.setValue(label, value);
      }
    } else {
      for (int i = 0; i < dataSource.size(); i++) {
        Map rec = (Map) dataSource.get("ROW" + i);
        double value = (Double) rec.get(valueKey);
        String label;
        if (labelKeys.isEmpty()) {
          label = DataFilter.show(rec, labelKey);
        } else {
          label = ((Element) labelKeys.get(i)).getText();
        }
        dataset.setValue(label, value);
      }
    }

    JFreeChart chart =
        ChartFactory.createPieChart3D(
            title,
            dataset,
            chartObject.getAttribute("showLegend").getBooleanValue(),
            chartObject.getAttribute("showToolTips").getBooleanValue(),
            chartObject.getAttribute("urls").getBooleanValue());

    PiePlot3D pie3dplot = (PiePlot3D) chart.getPlot();

    float alpha = 0.7F;
    if (chartObject.getAttribute("alpha") != null) {
      alpha = chartObject.getAttribute("alpha").getFloatValue();
    }
    pie3dplot.setForegroundAlpha(alpha);

    return ServletUtilities.saveChartAsPNG(chart, width, height, null, session);
  }
示例#3
0
  public static String getSimpleBarChart(Map dataSource, String objectName, HttpSession session)
      throws Throwable {
    Element chartObject =
        XMLHandler.getElementByAttribute(getChartObjectList(), "name", objectName);

    List invokeFields = chartObject.getChild("InvokeFields").getChildren("Field");
    double[][] data = new double[invokeFields.size()][dataSource.size()];
    String[] rowKeys = new String[invokeFields.size()];
    String[] columnKeys = new String[dataSource.size()];
    String columnLabel = chartObject.getChildText("ColumnLabel");

    for (int i = 0; i < dataSource.size(); i++) {
      Map rec = (Map) dataSource.get("ROW" + i);
      columnKeys[i] = DataFilter.show(rec, columnLabel);
      for (int j = 0; j < invokeFields.size(); j++) {
        data[j][i] =
            Double.parseDouble(
                rec.get(((Element) invokeFields.get(j)).getAttributeValue("name")).toString());
      }
    }
    for (int i = 0; i < invokeFields.size(); i++) {
      rowKeys[i] = ((Element) invokeFields.get(i)).getAttributeValue("label");
    }

    CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

    PlotOrientation plotOrientation =
        chartObject.getAttributeValue("plotOrientation").equalsIgnoreCase("VERTICAL")
            ? PlotOrientation.VERTICAL
            : PlotOrientation.HORIZONTAL;
    JFreeChart chart =
        ChartFactory.createBarChart3D(
            chartObject.getAttributeValue("title"),
            chartObject.getAttributeValue("categoryAxisLabel"),
            chartObject.getAttributeValue("valueAxisLabel"),
            dataset,
            plotOrientation,
            chartObject.getAttribute("showLegend").getBooleanValue(),
            chartObject.getAttribute("showToolTips").getBooleanValue(),
            chartObject.getAttribute("urls").getBooleanValue());

    CategoryPlot C3dplot = (CategoryPlot) chart.getPlot();
    if (chartObject.getAttribute("alpha") != null) {
      C3dplot.setForegroundAlpha(chartObject.getAttribute("alpha").getFloatValue());
    }

    BarRenderer3D barrenderer = (BarRenderer3D) C3dplot.getRenderer();
    barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator());
    barrenderer.setItemLabelsVisible(true);
    barrenderer.setPositiveItemLabelPosition(
        new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BASELINE_CENTER));

    int width, height;
    if (chartObject.getAttributeValue("width").equalsIgnoreCase("auto")) {
      width = (50 * dataSource.size()) * invokeFields.size() + 100;
    } else {
      width = Integer.parseInt(chartObject.getAttributeValue("width"));
    }
    if (chartObject.getAttributeValue("height").equalsIgnoreCase("auto")) {
      height = (50 * dataSource.size()) * invokeFields.size() + 100;
    } else {
      height = Integer.parseInt(chartObject.getAttributeValue("height"));
    }

    return ServletUtilities.saveChartAsPNG(chart, width, height, session);
  }