Ejemplo n.º 1
0
 public static byte[] getChartData(
     PointTimeSeriesCollection pointTimeSeriesCollection,
     int width,
     int height,
     long from,
     long to) {
   return getChartData(
       pointTimeSeriesCollection,
       pointTimeSeriesCollection.hasMultiplePoints(),
       width,
       height,
       from,
       to);
 }
Ejemplo n.º 2
0
 public static void writeChart(
     PointTimeSeriesCollection pointTimeSeriesCollection,
     OutputStream out,
     int width,
     int height,
     long from,
     long to)
     throws IOException {
   writeChart(
       pointTimeSeriesCollection,
       pointTimeSeriesCollection.hasMultiplePoints(),
       out,
       width,
       height,
       from,
       to);
 }
Ejemplo n.º 3
0
  public static void writeChart(
      PointTimeSeriesCollection pointTimeSeriesCollection,
      boolean showLegend,
      OutputStream out,
      int width,
      int height,
      long from,
      long to)
      throws IOException {
    // 创建主题样式
    StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
    // 设置标题字体
    standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 18));
    // 设置图例的字体
    standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 14));
    // 设置轴向的字体
    standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 14));
    // 应用主题样式
    ChartFactory.setChartTheme(standardChartTheme);

    JFreeChart chart =
        ChartFactory.createTimeSeriesChart(null, null, null, null, showLegend, false, false);
    chart.setBackgroundPaint(
        SystemSettingsDao.getColour(SystemSettingsDao.CHART_BACKGROUND_COLOUR));

    XYPlot plot = chart.getXYPlot();
    ((DateAxis) plot.getDomainAxis()).setTimeZone(pointTimeSeriesCollection.getTimeZone());
    plot.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.PLOT_BACKGROUND_COLOUR));
    Color gridlines = SystemSettingsDao.getColour(SystemSettingsDao.PLOT_GRIDLINE_COLOUR);
    plot.setDomainGridlinePaint(gridlines);
    plot.setRangeGridlinePaint(gridlines);
    ((NumberAxis) plot.getRangeAxis()).setAutoRangeStickyZero(false);

    double numericMin = 0;
    double numericMax = 1;

    int numericSeriesCount = pointTimeSeriesCollection.getNumericSeriesCount();
    if (pointTimeSeriesCollection.hasNumericData()) {
      for (int i = 0; i < numericSeriesCount; i++) {
        NumericTimeSeries nts = pointTimeSeriesCollection.getNumericTimeSeries(i);
        AbstractXYItemRenderer renderer;
        if (nts.getPlotType() == DataPointVO.PlotTypes.STEP) renderer = new XYStepRenderer();
        else if (nts.getPlotType() == DataPointVO.PlotTypes.LINE)
          renderer = new XYLineAndShapeRenderer(true, false);
        else {
          XYSplineRenderer spline = new XYSplineRenderer();
          spline.setBaseShapesVisible(false);
          renderer = spline;
        }

        if (nts.getPaint() != null) renderer.setSeriesPaint(0, nts.getPaint(), false);
        if (nts.getStroke() != null) renderer.setSeriesStroke(0, nts.getStroke(), false);

        plot.setDataset(i, new TimeSeriesCollection(nts.getTimeSeries()));
        plot.setRenderer(i, renderer);
      }

      numericMin = plot.getRangeAxis().getLowerBound();
      numericMax = plot.getRangeAxis().getUpperBound();

      if (!pointTimeSeriesCollection.hasMultiplePoints()) {
        // If this chart displays a single point, check if there should be a range description.
        TimeSeries timeSeries = pointTimeSeriesCollection.getNumericTimeSeries(0).getTimeSeries();
        String desc = timeSeries.getRangeDescription();
        if (!StringUtils.isBlank(desc)) {
          // Replace any HTML entities with Java equivalents
          desc = StripEntities.stripHTMLEntities(desc, ' ');
          plot.getRangeAxis().setLabel(desc);
        }
      }
    } else plot.getRangeAxis().setVisible(false);

    if (pointTimeSeriesCollection.getRangeMarkers() != null) {
      boolean rangeAdjusted = false;
      for (Marker marker : pointTimeSeriesCollection.getRangeMarkers()) {
        plot.addRangeMarker(marker);
        if (marker instanceof ValueMarker) {
          ValueMarker vm = (ValueMarker) marker;
          if (numericMin > vm.getValue()) {
            numericMin = vm.getValue();
            rangeAdjusted = true;
          }
          if (numericMax < vm.getValue()) {
            numericMax = vm.getValue();
            rangeAdjusted = true;
          }
        }
      }

      if (rangeAdjusted) {
        double adj = (numericMax - numericMin);
        plot.getRangeAxis().setLowerBound(numericMin - adj * plot.getRangeAxis().getLowerMargin());
        plot.getRangeAxis().setUpperBound(numericMax + adj * plot.getRangeAxis().getUpperMargin());
      }
    }

    int discreteValueCount = pointTimeSeriesCollection.getDiscreteValueCount();
    double interval = (numericMax - numericMin) / (discreteValueCount + 1);
    int intervalIndex = 1;

    if (pointTimeSeriesCollection.hasDiscreteData()) {
      for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
        DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
        XYStepRenderer renderer = new XYStepRenderer();

        TimeSeries ts = new TimeSeries(dts.getName(), null, null);
        for (IValueTime vt : dts.getValueTimes())
          addMillisecond(
              ts,
              vt.getTime(),
              numericMin + (interval * (dts.getValueIndex(vt.getValue()) + intervalIndex)));

        if (dts.getPaint() != null) renderer.setSeriesPaint(0, dts.getPaint(), false);
        if (dts.getStroke() != null) renderer.setSeriesStroke(0, dts.getStroke(), false);

        plot.setDataset(
            numericSeriesCount + i,
            new TimeSeriesCollection(ts, pointTimeSeriesCollection.getTimeZone()));
        plot.setRenderer(numericSeriesCount + i, renderer);

        intervalIndex += dts.getDiscreteValueCount();
      }
    }

    if (from > 0) plot.getDomainAxis().setLowerBound(from);
    if (to > 0) plot.getDomainAxis().setUpperBound(to);

    if (pointTimeSeriesCollection.hasDiscreteData()) {
      // Add the value annotations.
      double annoX = plot.getDomainAxis().getLowerBound();
      intervalIndex = 1;
      for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
        DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);

        for (int j = 0; j < dts.getDiscreteValueCount(); j++) {
          XYTextAnnotation anno =
              new XYTextAnnotation(
                  " " + dts.getValueText(j), annoX, numericMin + (interval * (j + intervalIndex)));
          if (!pointTimeSeriesCollection.hasNumericData()
              && intervalIndex + j == discreteValueCount)
            // This prevents the top label from getting cut off
            anno.setTextAnchor(TextAnchor.TOP_LEFT);
          else anno.setTextAnchor(TextAnchor.BOTTOM_LEFT);
          anno.setPaint(
              ((AbstractRenderer) plot.getRenderer(numericSeriesCount + i)).lookupSeriesPaint(0));
          plot.addAnnotation(anno);
        }

        intervalIndex += dts.getDiscreteValueCount();
      }
    }

    // Return the image.
    ChartUtilities.writeChartAsPNG(out, chart, width, height);
  }