yglodt
yglodt

Reputation: 14551

JFreeChart interpolate values

I have a JFreeChart TimeSeriesChart where some of the values may contain nulls, because they only exist if they change above a certain threshold.

Chart

Is there a way to tell it to ignore values which are null, and connect (interpolate) in between?

The code is here:

final TimeSeries tseries = new TimeSeries("Temperature");
final TimeSeries hseries = new TimeSeries("Humidity");
final TimeSeries pseries = new TimeSeries("Pressure");

for (final Map<String, Object> value : list) {
    final Date datetime = (Date) value.get("datetime");
    final BigDecimal temperature = (BigDecimal) value.get("temperature");
    final BigDecimal humidity = (BigDecimal) value.get("humidity");
    final BigDecimal pressure = (BigDecimal) value.get("pressure");
    tseries.add(new Millisecond(datetime), temperature);
    hseries.add(new Millisecond(datetime), humidity);
    pseries.add(new Millisecond(datetime), pressure);
}

final JFreeChart timeChart = ChartFactory.createTimeSeriesChart(sensor.getName(), null, null, null, false, false, false);

timeChart.getXYPlot().setRangeAxis(new NumberAxis("Temperature (°C)"));
timeChart.getXYPlot().getRangeAxis().setRange(-10, 30);
((NumberAxis) timeChart.getXYPlot().getRangeAxis()).setTickUnit(new NumberTickUnit(5));

timeChart.getXYPlot().setDataset(0, new TimeSeriesCollection(tseries));
timeChart.getXYPlot().setDataset(1, new TimeSeriesCollection(hseries));
timeChart.getXYPlot().setDataset(2, new TimeSeriesCollection(pseries));

timeChart.getXYPlot().setRenderer(0, new StandardXYItemRenderer());
timeChart.getXYPlot().setRenderer(1, new StandardXYItemRenderer());
timeChart.getXYPlot().setRenderer(2, new StandardXYItemRenderer());

final NumberAxis humAxis = new NumberAxis("Humidity (%)");
humAxis.setRange(20, 80);
humAxis.setTickUnit(new NumberTickUnit(5));
timeChart.getXYPlot().setRangeAxis(1, humAxis);
timeChart.getXYPlot().mapDatasetToRangeAxis(1, 1);

final NumberAxis pressAxis = new NumberAxis("Pressure (hPa)");
pressAxis.setRange(970, 1020);
pressAxis.setTickUnit(new NumberTickUnit(10));
timeChart.getXYPlot().setRangeAxis(2, pressAxis);
timeChart.getXYPlot().mapDatasetToRangeAxis(2, 2);

final DateAxis axis = (DateAxis) timeChart.getXYPlot().getDomainAxis();
scheduledReportService.setChartScale(scheduledReport, axis);

ChartUtilities.writeChartAsPNG(baos, timeChart, Chart.WIDTH, Chart.HEIGHT);

Upvotes: 0

Views: 430

Answers (1)

trashgod
trashgod

Reputation: 205785

Because ChartFactory.createTimeSeriesChart() instantiates XYLineAndShapeRenderer, try setDrawSeriesLineAsPath():

XYPlot plot = timeChart.getXYPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setDrawSeriesLineAsPath(true);

Upvotes: 1

Related Questions