DefaultLineChartReducer implements LineChartReducer { @Override public List<SeriesData<Long>> reduceData(@NotNull List<SeriesData<Long>> dataList, @NotNull LineConfig config) { if (config.getDataBucketInterval() > 0) { return dataList; } List<SeriesData<Long>> reduced = new ArrayList<>(); for (SeriesData<Long> data: dataList) { while (reduced.size() >= 2) { SeriesData<Long> preLast = reduced.get(reduced.size() - 2); SeriesData<Long> last = reduced.get(reduced.size() - 1); if (preLast.value.equals(last.value) && (config.isStepped() || last.value.equals(data.value))) { reduced.remove(reduced.size() - 1); } else { break; } } reduced.add(data); } return reduced; }  @Override List<SeriesData<Long>> reduceData(@NotNull List<SeriesData<Long>> dataList, @NotNull LineConfig config); @NotNull @Override Path2D reducePath(@NotNull Path2D path, @NotNull LineConfig config);  }
@Test public void reduceData() { List<SeriesData<Long>> data = new ImmutableList.Builder<SeriesData<Long>>() .add(new SeriesData<>(0, 10L)) .add(new SeriesData<>(1, 10L)) .add(new SeriesData<>(2, 13L)) .add(new SeriesData<>(3, 13L)) .add(new SeriesData<>(4, 13L)) .add(new SeriesData<>(5, 13L)) .add(new SeriesData<>(6, 5L)) .add(new SeriesData<>(7, 5L)).build(); List<SeriesData<Long>> expected = new ImmutableList.Builder<SeriesData<Long>>() .add(new SeriesData<>(0, 10L)) .add(new SeriesData<>(1, 10L)) .add(new SeriesData<>(2, 13L)) .add(new SeriesData<>(5, 13L)) .add(new SeriesData<>(6, 5L)) .add(new SeriesData<>(7, 5L)).build(); List<SeriesData<Long>> result = myReducer.reduceData(data, myConfig); assertSeriesEquals(expected, result); }
@Test public void reduceDataForSteppedLine() { List<SeriesData<Long>> data = new ImmutableList.Builder<SeriesData<Long>>() .add(new SeriesData<>(0, 10L)) .add(new SeriesData<>(1, 10L)) .add(new SeriesData<>(2, 13L)) .add(new SeriesData<>(3, 13L)) .add(new SeriesData<>(4, 13L)) .add(new SeriesData<>(5, 13L)) .add(new SeriesData<>(6, 5L)) .add(new SeriesData<>(7, 5L)).build(); List<SeriesData<Long>> expected = new ImmutableList.Builder<SeriesData<Long>>() .add(new SeriesData<>(0, 10L)) .add(new SeriesData<>(2, 13L)) .add(new SeriesData<>(6, 5L)) .add(new SeriesData<>(7, 5L)).build(); myConfig.setStepped(true); List<SeriesData<Long>> result = myReducer.reduceData(data, myConfig); assertSeriesEquals(expected, result); }