JFreeChart插值

时间:2017-04-25 20:48:18

标签: java jfreechart

我有一个JFreeChart TimeSeriesChart,其中一些值可能包含空值,因为它们仅在超过某个阈值时才存在。

Chart

有没有办法告诉它忽略null的值,并在它们之间连接(插值)?

代码在这里:

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);

1 个答案:

答案 0 :(得分:1)

由于ChartFactory.createTimeSeriesChart()实例化XYLineAndShapeRenderer,请尝试setDrawSeriesLineAsPath()

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