报告趋势线

发布于 2024-08-25 04:22:51 字数 284 浏览 7 评论 0原文

除了显示数据点之外,如何在 JasperReports 中创建一条遵循数据趋势的线?以下是之前和之后的照片:

之前

之后

时间序列报告似乎没有任何此类选项来绘制橙色线。 (橙色线应该是平滑且细的,但这是总体思路。)

有什么想法如何使用 iReport 3.7.1 制作这样的报告吗?

How would you create a line in JasperReports that follows the trend for the data, in addition to showing the data points? Here are before and after shots:

Before

After

The Time Series report does not appear to have any such option to draw the orange line. (The orange line should be smooth, and thinner, but that's the general idea.)

Any ideas how to craft such a report with iReport 3.7.1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-09-01 04:22:51

一种解决方案需要以下项目:

  • BezierLineCustomizer 使线条弯曲。
  • RunningAverageIncrementer 用于计算基于变量的运行平均值。
  • 使用 RunningAverageIncremeter 的 iReport 变量。

BezierLineCustomizer 类

public class BezierLineCustomizer
  implements JRChartCustomizer {
  public BezierLineCustomizer() {
  }

  public void customize( JFreeChart jFreeChart, JRChart jrChart ) {
    XYPlot xyPlot = ( XYPlot )jFreeChart.getPlot();

    XYSplineRenderer splineRenderer = new XYSplineRenderer();

    // Make the spline line thick and orange.
    //
    splineRenderer.setSeriesShapesVisible( 0, false );
    splineRenderer.setSeriesShapesVisible( 1, false );
    splineRenderer.setSeriesLinesVisible( 1, false );
    splineRenderer.setSeriesStroke(
        0, new BasicStroke(
            4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, null, 0.0f
        )
    );

    splineRenderer.setSeriesPaint( 0, new Color( 255, 140, 0 ) );
    splineRenderer.setSeriesVisibleInLegend( 1, Boolean.FALSE );

    // Duplicate the data into a new dataset to control its line independently.
    //
    xyPlot.setDataset( 1, xyPlot.getDataset(0) );

    XYItemRenderer defaultRenderer = new XYLineAndShapeRenderer();
    defaultRenderer.setSeriesVisible( 0, Boolean.FALSE );
    defaultRenderer.setSeriesVisibleInLegend( 0, Boolean.FALSE );

    xyPlot.setRenderer( 1, defaultRenderer );
    xyPlot.setRenderer( 0, splineRenderer );
  }
}

RunningAverageIncrementer 类

public class RunningAverageIncrementer
  implements JRIncrementer {
  /** Default number of tallies. */
  private static final int DEFAULT_TALLIES = 128;

  /** Number of tallies within the sliding window. */
  private static final int DEFAULT_SLIDING_WINDOW_SIZE = 30;

  /** Stores a sliding window of values. */
  private List<Double> values = new ArrayList<Double>( DEFAULT_TALLIES );

  /**
   * Instantiated by the RunningAverageIncrementerFactory class.
   */
  public RunningAverageIncrementer() {
  }

  /**
   * Calculates the average of previously known values.
   * @return The average of the list of values returned by getValues().
   */
  private double calculateAverage() {
    double result = 0.0;
    List<Double> values = getValues();

    for( Double d: getValues() ) {
      result += d.doubleValue();
    }

    return result / values.size();
  }

  /**
   * Called each time a new value to be averaged is received.
   * @param value The new value to include for the average.
   */
  private void recordValue( Double value ) {
    List<Double> values = getValues();

    // Throw out 
    //
    if( values.size() > getSlidingWindowSize() ) {
      values.remove( 0 );
    }

    this.values.add( value );
  }

  private List<Double> getValues() {
    return values;
  }

  private int getIterations() {
    return getValues().size();
  }

  /**
   * Returns the newly incremented value, which is calculated by averaging
   * the previous value from the previous call to this method.
   * 
   * @param jrFillVariable Unused.
   * @param tally New data point to average.
   * @param abstractValueProvider Unused.
   * @return The newly incremented value.
   */
  public Object increment( JRFillVariable jrFillVariable, Object tally, 
                           AbstractValueProvider abstractValueProvider ) {
    double value = ((Number)tally).doubleValue();

    recordValue( value );

    double previousAverage = calculateAverage();
    double newAverage = 
      ( ( value - previousAverage ) / ( getIterations() + 1 ) ) + previousAverage;

    return new BigDecimal( newAverage );
  }

  protected int getSlidingWindowSize() {
    return DEFAULT_SLIDING_WINDOW_SIZE;
  }
}

iReport 变量

创建一个使用 RunningAverageIncrementerFactory 类的变量(留给读者练习)。将其变量表达式设置为绘制的值。将其初始值表达式设置为零。

Spline

设置 TimeSeries 图表的 Customizer Class 属性以使用 BezierLineCustomizer 类。

结果

经过这些修改,运行平均值清晰可见:

One solution requires the following items:

  • BezierLineCustomizer to make the lines curved.
  • RunningAverageIncrementer to calculate a running average based on a variable.
  • iReport variable that uses the RunningAverageIncremeter.

BezierLineCustomizer Class

public class BezierLineCustomizer
  implements JRChartCustomizer {
  public BezierLineCustomizer() {
  }

  public void customize( JFreeChart jFreeChart, JRChart jrChart ) {
    XYPlot xyPlot = ( XYPlot )jFreeChart.getPlot();

    XYSplineRenderer splineRenderer = new XYSplineRenderer();

    // Make the spline line thick and orange.
    //
    splineRenderer.setSeriesShapesVisible( 0, false );
    splineRenderer.setSeriesShapesVisible( 1, false );
    splineRenderer.setSeriesLinesVisible( 1, false );
    splineRenderer.setSeriesStroke(
        0, new BasicStroke(
            4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, null, 0.0f
        )
    );

    splineRenderer.setSeriesPaint( 0, new Color( 255, 140, 0 ) );
    splineRenderer.setSeriesVisibleInLegend( 1, Boolean.FALSE );

    // Duplicate the data into a new dataset to control its line independently.
    //
    xyPlot.setDataset( 1, xyPlot.getDataset(0) );

    XYItemRenderer defaultRenderer = new XYLineAndShapeRenderer();
    defaultRenderer.setSeriesVisible( 0, Boolean.FALSE );
    defaultRenderer.setSeriesVisibleInLegend( 0, Boolean.FALSE );

    xyPlot.setRenderer( 1, defaultRenderer );
    xyPlot.setRenderer( 0, splineRenderer );
  }
}

RunningAverageIncrementer Class

public class RunningAverageIncrementer
  implements JRIncrementer {
  /** Default number of tallies. */
  private static final int DEFAULT_TALLIES = 128;

  /** Number of tallies within the sliding window. */
  private static final int DEFAULT_SLIDING_WINDOW_SIZE = 30;

  /** Stores a sliding window of values. */
  private List<Double> values = new ArrayList<Double>( DEFAULT_TALLIES );

  /**
   * Instantiated by the RunningAverageIncrementerFactory class.
   */
  public RunningAverageIncrementer() {
  }

  /**
   * Calculates the average of previously known values.
   * @return The average of the list of values returned by getValues().
   */
  private double calculateAverage() {
    double result = 0.0;
    List<Double> values = getValues();

    for( Double d: getValues() ) {
      result += d.doubleValue();
    }

    return result / values.size();
  }

  /**
   * Called each time a new value to be averaged is received.
   * @param value The new value to include for the average.
   */
  private void recordValue( Double value ) {
    List<Double> values = getValues();

    // Throw out 
    //
    if( values.size() > getSlidingWindowSize() ) {
      values.remove( 0 );
    }

    this.values.add( value );
  }

  private List<Double> getValues() {
    return values;
  }

  private int getIterations() {
    return getValues().size();
  }

  /**
   * Returns the newly incremented value, which is calculated by averaging
   * the previous value from the previous call to this method.
   * 
   * @param jrFillVariable Unused.
   * @param tally New data point to average.
   * @param abstractValueProvider Unused.
   * @return The newly incremented value.
   */
  public Object increment( JRFillVariable jrFillVariable, Object tally, 
                           AbstractValueProvider abstractValueProvider ) {
    double value = ((Number)tally).doubleValue();

    recordValue( value );

    double previousAverage = calculateAverage();
    double newAverage = 
      ( ( value - previousAverage ) / ( getIterations() + 1 ) ) + previousAverage;

    return new BigDecimal( newAverage );
  }

  protected int getSlidingWindowSize() {
    return DEFAULT_SLIDING_WINDOW_SIZE;
  }
}

iReport Variable

Create a variable that uses the RunningAverageIncrementerFactory class (exercise left to the reader). Set its variable expression to the plotted value. Set its initial value expression to zero.

Spline

Set the Customizer Class property of the TimeSeries chart to use the BezierLineCustomizer class.

Result

After these modifications, the running average is clearly visible:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文