Java Code Examples for org.jfree.data.time.TimeSeriesCollection#getSeries()

The following examples show how to use org.jfree.data.time.TimeSeriesCollection#getSeries() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeSeriesCollectionTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests the remove series method.
 */
public void testRemoveSeries() {

    TimeSeriesCollection c1 = new TimeSeriesCollection();

    TimeSeries s1 = new TimeSeries("Series 1");
    TimeSeries s2 = new TimeSeries("Series 2");
    TimeSeries s3 = new TimeSeries("Series 3");
    TimeSeries s4 = new TimeSeries("Series 4");

    c1.addSeries(s1);
    c1.addSeries(s2);
    c1.addSeries(s3);
    c1.addSeries(s4);

    c1.removeSeries(s3);

    TimeSeries s = c1.getSeries(2);
    boolean b1 = s.equals(s4);
    assertTrue(b1);

}
 
Example 2
Source File: TimeSeriesCollectionTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests the remove series method.
 */
public void testRemoveSeries() {

    TimeSeriesCollection c1 = new TimeSeriesCollection();

    TimeSeries s1 = new TimeSeries("Series 1");
    TimeSeries s2 = new TimeSeries("Series 2");
    TimeSeries s3 = new TimeSeries("Series 3");
    TimeSeries s4 = new TimeSeries("Series 4");

    c1.addSeries(s1);
    c1.addSeries(s2);
    c1.addSeries(s3);
    c1.addSeries(s4);

    c1.removeSeries(s3);

    TimeSeries s = c1.getSeries(2);
    boolean b1 = s.equals(s4);
    assertTrue(b1);

}
 
Example 3
Source File: TimeSeriesChartHyperlinkProvider.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public JRPrintHyperlink getEntityHyperlink(ChartEntity entity)
{
	JRPrintHyperlink printHyperlink = null;
	if (hasHyperlinks() && entity instanceof XYItemEntity)
	{
		XYItemEntity itemEntity = (XYItemEntity) entity;
		TimeSeriesCollection dataset = (TimeSeriesCollection) itemEntity.getDataset();
		TimeSeries series = dataset.getSeries(itemEntity.getSeriesIndex());
		Map<RegularTimePeriod, JRPrintHyperlink> serieHyperlinks = itemHyperlinks.get(series.getKey());
		if (serieHyperlinks != null)
		{
			RegularTimePeriod timePeriod = series.getTimePeriod(itemEntity.getItem());
			printHyperlink = serieHyperlinks.get(timePeriod);
		}
	}
	return printHyperlink;
}
 
Example 4
Source File: TimeSeriesBuilderCallbackTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddPointToSeries()
{
    TimeSeriesHolder timeSeriesHolder = new TimeSeriesHolder();

    SeriesDefinition seriesDefinition = mock(SeriesDefinition.class);
    when(seriesDefinition.getSeriesLegend()).thenReturn(SERIES_LEGEND);

    timeSeriesHolder.beginSeries(seriesDefinition);

    timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[0], _values[0]));
    timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[1], _values[1]));
    timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[2], _values[2]));

    timeSeriesHolder.endSeries(seriesDefinition);

    TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) timeSeriesHolder.getPopulatedDataset();

    TimeSeries actualTimeSeries = timeSeriesCollection.getSeries(SERIES_LEGEND);
    for(int i = 0; i < NUMBER_OF_DATA_POINTS; i++)
    {
        TimeSeriesDataItem dataItem0 = actualTimeSeries.getDataItem(i);
        assertEquals(_dates[i].getTime(), dataItem0.getPeriod().getMiddleMillisecond());
        assertEquals(_values[i], dataItem0.getValue());
    }
}
 
Example 5
Source File: CollectorFunctionUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static TimeSeries queryExistingValueFromDataSet( final TimeSeriesCollection dataset,
                                                        final String seriesName ) {
  try {
    return dataset.getSeries( seriesName );
  } catch ( Exception ignored ) {
    // dataset.getValue throws exceptions if the keys dont match ..
  }
  return null;
}
 
Example 6
Source File: XYChartExpression.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected ExtTimeTableXYDataset convertToTable( final XYDataset xyDataset ) {
  final TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) xyDataset;
  final ExtTimeTableXYDataset tableXYDataset = new ExtTimeTableXYDataset();
  final int count = timeSeriesCollection.getSeriesCount();
  for ( int i = 0; i < count; i++ ) {
    final Comparable key = timeSeriesCollection.getSeriesKey( i );
    final TimeSeries timeSeries = timeSeriesCollection.getSeries( i );
    final int itemCount = timeSeries.getItemCount();
    for ( int ic = 0; ic < itemCount; ic++ ) {
      final TimeSeriesDataItem seriesDataItem = timeSeries.getDataItem( ic );
      tableXYDataset.add( seriesDataItem.getPeriod(), seriesDataItem.getValue(), key, false );
    }
  }
  return tableXYDataset;
}
 
Example 7
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 8
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 9
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 10
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 11
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 12
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 13
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 14
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 15
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 16
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 17
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}
 
Example 18
Source File: ComplexChartPlotter.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
@Override
void doPlot(Report report, OutputStream out) throws IOException {
    List<String> labels = report.getLabels();
    List<List<Report.Field>> records = report.getRecords();

    String dateLabel = labels.get(dateIndex);
    List<String> valueLabels = labels
        .stream()
        .filter(l -> !l.equals(dateLabel))
        .collect(Collectors.toList());

    TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
    for (String valueLabel : valueLabels) {
        seriesCollection.addSeries(new TimeSeries(valueLabel));
    }

    for (List<Report.Field> record : records) {
        Report.Field dateField = record.get(dateIndex);
        Day day = Day.parseDay(dateField.getAsString());
        List<Number> values = record.stream()
            .filter(f -> !f.equals(dateField))
            .map(Report.Field::getAsNumber)
            .collect(Collectors.toList());

        for (int j = 0; j < values.size(); j++) {
            Number value = values.get(j);
            TimeSeries series = seriesCollection.getSeries(j);
            series.add(day, value);
        }
    }

    JFreeChart chart;
    if (chartType == ChartType.LINE) {
        chart = createTimeSeriesChart(
            reportType.getDisplayName(),
            dateLabel,
            null,
            seriesCollection
        );
    } else if (chartType == ChartType.BAR) {
        chart = createXYBarChart(
            reportType.getDisplayName(),
            dateLabel,
            true,
            null,
            seriesCollection
        );
    } else {
        throw new IllegalStateException(String.format("Chart type %s not handled.", chartType));
    }
    writeChartAsPNG(out, chart, PNG_WIDTH, PNG_HEIGHT);
}