Beispiel #1
0
 private static boolean isAsciiAlphanumeric(String str) {
   if (StringUtils.isEmpty(str)) {
     return false;
   }
   int sz = str.length();
   for (int i = 0; i < sz; i++) {
     if (CharUtils.isAsciiAlphanumeric(str.charAt(i)) == false) {
       return false;
     }
   }
   return true;
 }
  protected static Queue<String> queueLinesForType(
      EDIMessage ediMessage,
      EDISegment segment,
      Queue<String> lookAhead,
      SegmentIterator SegmentIterator) {
    Queue<String> segments = new LinkedList<String>();

    while (SegmentIterator.hasNext()) {
      String line = SegmentIterator.next();
      StringTokenizer tokenizer =
          new StringTokenizer(line, CharUtils.toString(ediMessage.elementDelimiter()));
      String token = tokenizer.nextToken();

      if (StringUtils.equals(segment.tag(), token)) {
        segments.add(line);
      } else {
        lookAhead.add(line);
        break;
      }
    }

    return segments;
  }
  protected static FieldMatch advanceToMatch(
      EDIMessage ediMessage, Iterator<Field> fieldIterator, String line) {
    // advance the reader, read the line.
    StringTokenizer tokenizer =
        new StringTokenizer(line, CharUtils.toString(ediMessage.elementDelimiter()));

    // first token is always the tag.
    String ediSegmentTag = tokenizer.nextToken();

    while (fieldIterator.hasNext()) {
      Field field = fieldIterator.next();
      if (matchesSegment(field, ediSegmentTag)) {
        LOG.debug(
            "SegmentMatch[" + ediSegmentTag + " -> " + field.getType().getName() + "] :: " + line);
        return new FieldMatch(field, line);
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug("Field: " + field + " does not match: " + line);
      }
    }

    return null;
  }
  /* (non-Javadoc)
   * @see com.lily.dap.service.report2.impl.chart.ChartStrategy#outputChart(java.lang.String, int, int, java.util.Map, java.util.List, java.io.OutputStream)
   */
  public void outputChart(
      String type,
      int height,
      int width,
      Map<String, String> paramMap,
      Map<String, Object> variableMap,
      List<Map<String, Object>> dataList,
      OutputStream os)
      throws IOException {
    String nameField = ChartUtils.evaluateParam(paramMap.get("namefield"), variableMap);
    String valueField = ChartUtils.evaluateParam(paramMap.get("valuefield"), variableMap);
    DefaultPieDataset dataset = createDataset(dataList, nameField, valueField);

    String title = ChartUtils.evaluateParam(paramMap.get("title"), variableMap);

    boolean legend =
        paramMap.get("legend") == null
            ? true
            : "true"
                .equals(
                    ChartUtils.evaluateParam(paramMap.get("legend"), variableMap).toLowerCase());

    JFreeChart chart;
    if (ChartConstants.TYPE_PIE3D.equals(type))
      chart =
          ChartFactory.createPieChart3D(
              title, // 图表标题
              dataset, // 数据集
              legend, // 是否显示图例
              false, // 是否生成工具
              false // 是否生成URL链接
              );
    else
      chart =
          ChartFactory.createPieChart(
              title, // 图表标题
              dataset, // 数据集
              legend, // 是否显示图例
              false, // 是否生成工具
              false // 是否生成URL链接
              );

    // 设置标题字体
    if (chart.getTitle() != null) chart.getTitle().setFont(titleFont);

    // 设置图例字体
    if (chart.getLegend() != null) chart.getLegend().setItemFont(legendFont);

    PiePlot pie = (PiePlot) chart.getPlot();

    //		if (pie instanceof PiePlot3D) {
    //			PiePlot3D pie3d = (PiePlot3D)pie;
    //		}

    // 设置前景透明度
    if (paramMap.get("foregroundAlpha") != null) {
      float f =
          Float.parseFloat(ChartUtils.evaluateParam(paramMap.get("foregroundAlpha"), variableMap));

      pie.setForegroundAlpha(f);
    }

    // 设置标签的最大宽度
    if (paramMap.get("maximumlabelwidth") != null) {
      float f =
          Float.parseFloat(
              ChartUtils.evaluateParam(paramMap.get("maximumlabelwidth"), variableMap));

      pie.setMaximumLabelWidth(f);
    }

    // 设置标饼上标签的标签字体
    pie.setLabelFont(labelFont);

    String labelFormat = paramMap.get("labelformat");
    if (labelFormat != null) {
      /*
       * 因为labelformat参数可能包含{0}、{1}、{2}这样的字符串,所以要通过检查'{'后面跟的字符是不是数字,来判断labelformat参数是不是表达式
       */
      int index = labelFormat.indexOf('{');

      if (index >= 0) {
        char ch = labelFormat.charAt(index + 1);

        if (!CharUtils.isAsciiNumeric(ch))
          labelFormat = ChartUtils.evaluateParam(labelFormat, variableMap);
      }

      // 设置饼上标签的显示格式,其中,0:数据名称,1:数据值,2:数据占整个百分比
      StandardPieSectionLabelGenerator generator =
          new StandardPieSectionLabelGenerator(labelFormat);
      pie.setLabelGenerator(generator);
    }

    String legendformat = paramMap.get("legendformat");
    if (legendformat != null) {
      /*
       * 因为legendformat参数可能包含{0}、{1}、{2}这样的字符串,所以要通过检查'{'后面跟的字符是不是数字,来判断legendformat参数是不是表达式
       */
      int index = legendformat.indexOf('{');

      if (index >= 0) {
        char ch = legendformat.charAt(index + 1);

        if (!CharUtils.isAsciiNumeric(ch))
          legendformat = ChartUtils.evaluateParam(legendformat, variableMap);
      }

      // 设置饼上标签的显示格式,其中,0:数据名称,1:数据值,2:数据占整个百分比
      StandardPieSectionLabelGenerator generator =
          new StandardPieSectionLabelGenerator(legendformat);
      pie.setLegendLabelGenerator(generator);
    }

    if (paramMap.get("color") != null) {
      int index = 0;

      String[] colGroups = ChartUtils.evaluateParam(paramMap.get("color"), variableMap).split(",");
      for (String colVal : colGroups) {
        Color color = ChartUtils.createColor(colVal.trim());

        pie.setSectionPaint(index++, color);
      }
    }

    // 设置饼图是圆形还是椭圆形
    if (paramMap.get("circular") != null) {
      boolean isCircular =
          "true"
              .equals(
                  ChartUtils.evaluateParam(paramMap.get("circular"), variableMap).toLowerCase());

      pie.setCircular(isCircular);
    }

    ChartUtilities.writeChartAsJPEG(os, 1, chart, width, height, null);
  }