コード例 #1
0
  public static IntervalXYDataset createDatasetBar() throws Exception {
    IntervalXYDataset dataset1 = null;
    Object[][] chart = null;
    Object[][] chart_qu = null;
    TCarRecordDao tdao = new TCarRecordDao();
    TimeSeries s1, s2;
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    try {
      if (type == 0) {
        // 从DAO层获得数据,以天为统计单位,数据线以天为单位
        chart = tdao.getCarRecodebyTimearea_day(timefrom, timeto, 1);
        chart_qu = tdao.getCarRecodebyTimearea_day(timefrom, timeto, 0);
        s1 = new TimeSeries("入场柱形", Day.class);
        s2 = new TimeSeries("出场柱形", Day.class);
      } else {

        // 传进DAO层的数据是年月,数据线以月的单位
        Date date = java.sql.Date.valueOf(timefrom);
        Date date_t = java.sql.Date.valueOf(timeto);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String newdate_from = sdf.format(date);
        String timefrom_temp = newdate_from.substring(0, 7);
        String newdate_to = sdf.format(date_t);
        String timeto_temp = newdate_to.substring(0, 7);
        chart = tdao.getCarRecodebyTimearea_month(timefrom_temp, timeto_temp, 1);
        chart_qu = tdao.getCarRecodebyTimearea_month(timefrom_temp, timeto_temp, 0);
        s1 = new TimeSeries("入场柱形", Month.class);
        s2 = new TimeSeries("出场柱形", Month.class);
      }

      s1.clear();
      Date d, min_r = null, min_c = null, max_r = null, max_c = null;
      double h;
      if (chart != null && chart.length > 0) { // 数据为空的处理
        System.out.println("ff");
        min_r = (Date) chart[0][1];
        max_r = (Date) chart[0][1];
        for (int i = 0; i < chart.length; i++) {
          d = (Date) chart[i][1];
          if (d.before(min_r)) min_r = d;
          if (d.after(max_r)) max_r = d;
          h = new Double(chart[i][0].toString());
          // 添加数据,0是以天为单位,1是以月为单位
          if (type == 0) s1.add(new Day(d.getDate(), (d.getMonth() + 1), (d.getYear() + 1900)), h);
          else s1.add(new Month((d.getMonth() + 1), (d.getYear() + 1900)), h);
        }
        dataset.addSeries(s1);
      } else s1 = null;

      s2.clear();

      if (chart_qu != null && chart_qu.length > 0) {
        min_c = (Date) chart_qu[0][1];
        max_c = (Date) chart_qu[0][1];
        for (int i = 0; i < chart_qu.length; i++) {
          d = (Date) chart_qu[i][1];
          if (d.before(min_c)) min_c = d;
          if (d.after(max_c)) max_c = d;
          h = new Double(chart_qu[i][0].toString());
          if (type == 0) s2.add(new Day(d.getDate(), (d.getMonth() + 1), (d.getYear() + 1900)), h);
          else s2.add(new Month((d.getMonth() + 1), (d.getYear() + 1900)), h);
        }
        dataset.addSeries(s2);
      } else s2 = null;

      // 取得有数据的时间段的开始和结束
      if (min_c != null && min_r != null) min = min_c.before(min_r) ? min_c : min_r;
      else if (min_c != null) min = min_c;
      else if (min_r != null) min = min_r;
      else min = null;

      if (max_c != null && max_r != null) max = max_c.before(max_r) ? max_r : max_c;
      else if (max_c != null) max = max_c;
      else if (max_r != null) max = max_r;
      else max = null;

      int seriesCount = dataset.getSeriesCount(); // 一共有多少个序列,目前为一个
      for (int i = 0; i < seriesCount; i++) {
        int itemCount = dataset.getItemCount(i); // 每一个序列有多少个数据项
        for (int j = 0; j < itemCount; j++) {
          if (highValue_Y < dataset.getYValue(i, j)) { // 取第i个序列中的第j个数据项的最大值
            highValue_Y = (int) dataset.getYValue(i, j);
          }
          if (minValue_Y > dataset.getYValue(i, j)) { // 取第i个序列中的第j个数据项的最小值
            minValue_Y = (int) dataset.getYValue(i, j);
          }
        }
      }

      dataset1 = dataset;
      return dataset1;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return dataset1;
    }
  }
コード例 #2
0
  @Override
  public Document getXmlContent() {

    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    setXslProperty(
        "baseUrl", requestContext.getContextPath()); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty(
        "fullyQualifiedServerUrl",
        PentahoSystem.getApplicationContext()
            .getFullyQualifiedServerURL()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String mapName = "chart" + AbstractChartComponent.chartCount++; // $NON-NLS-1$
    Document chartDefinition =
        jcrHelper.getSolutionDocument(definitionPath, RepositoryFilePermission.READ);

    if (chartDefinition == null) {
      Element errorElement = result.addElement("error"); // $NON-NLS-1$
      errorElement
          .addElement("title")
          .setText(
              Messages.getInstance()
                  .getString(
                      "ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
                                                                                     // //$NON-NLS-2$
      String message =
          Messages.getInstance()
              .getString(
                  "CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", definitionPath); // $NON-NLS-1$
      errorElement.addElement("message").setText(message); // $NON-NLS-1$
      error(message);
      return result;
    }
    // create a pie definition from the XML definition
    dataDefinition = createChart(chartDefinition);

    if (dataDefinition == null) {
      Element errorElement = result.addElement("error"); // $NON-NLS-1$
      errorElement
          .addElement("title")
          .setText(
              Messages.getInstance()
                  .getString(
                      "ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
                                                                                     // //$NON-NLS-2$
      String message =
          Messages.getInstance()
              .getString(
                  "CHARTS.ERROR_0002_CHART_DATA_MISSING",
                  actionPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      errorElement.addElement("message").setText(message); // $NON-NLS-1$
      // System .out.println( result.asXML() );
      return result;
    }

    // create an image for the dial using the JFreeChart engine
    PrintWriter printWriter = new PrintWriter(new StringWriter());
    // we'll dispay the title in HTML so that the dial image does not have
    // to
    // accommodate it
    String chartTitle = ""; // $NON-NLS-1$
    try {
      if (width == -1) {
        width =
            Integer.parseInt(
                chartDefinition.selectSingleNode("/chart/width").getText()); // $NON-NLS-1$
      }
      if (height == -1) {
        height =
            Integer.parseInt(
                chartDefinition.selectSingleNode("/chart/height").getText()); // $NON-NLS-1$
      }
    } catch (Exception e) {
      // go with the default
    }
    if (chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME)
        != null) { //$NON-NLS-1$
      urlTemplate =
          chartDefinition
              .selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME)
              .getText(); //$NON-NLS-1$
    }

    if (chartDefinition.selectSingleNode("/chart/paramName") != null) { // $NON-NLS-1$
      paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); // $NON-NLS-1$
    }

    Element root = result.addElement("charts"); // $NON-NLS-1$
    TimeSeriesCollection chartDataDefinition = (TimeSeriesCollection) dataDefinition;
    if (chartDataDefinition.getSeriesCount() > 0) {
      // create temporary file names
      String[] tempFileInfo = createTempFile();
      String fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX];
      String filePathWithoutExtension =
          tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX];

      ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
      JFreeChartEngine.saveChart(
          chartDataDefinition,
          chartTitle,
          "",
          filePathWithoutExtension,
          width,
          height,
          JFreeChartEngine.OUTPUT_PNG,
          printWriter,
          info,
          this); //$NON-NLS-1$
      applyOuterURLTemplateParam();
      populateInfo(info);
      Element chartElement = root.addElement("chart"); // $NON-NLS-1$
      chartElement.addElement("mapName").setText(mapName); // $NON-NLS-1$
      chartElement.addElement("width").setText(Integer.toString(width)); // $NON-NLS-1$
      chartElement.addElement("height").setText(Integer.toString(height)); // $NON-NLS-1$
      for (int row = 0; row < chartDataDefinition.getSeriesCount(); row++) {
        for (int column = 0; column < chartDataDefinition.getItemCount(row); column++) {
          Number value = chartDataDefinition.getY(row, column);
          Comparable rowKey = chartDataDefinition.getSeriesKey(row);
          RegularTimePeriod columnKey = chartDataDefinition.getSeries(row).getTimePeriod(column);
          Element valueElement = chartElement.addElement("value2D"); // $NON-NLS-1$
          valueElement.addElement("value").setText(value.toString()); // $NON-NLS-1$
          valueElement.addElement("row-key").setText(rowKey.toString()); // $NON-NLS-1$
          valueElement.addElement("column-key").setText(columnKey.toString()); // $NON-NLS-1$
        }
      }
      String mapString = ImageMapUtilities.getImageMap(mapName, info);
      chartElement.addElement("imageMap").setText(mapString); // $NON-NLS-1$
      chartElement.addElement("image").setText(fileName); // $NON-NLS-1$
    }
    return result;
  }