Skip to content

howto combined bar line plot

Erich Seifert edited this page Sep 21, 2017 · 2 revisions

How can I combine a bar plot with a line plot?

Often it can be desirable to overlay a bar plot with a line plot. The BarPlot is derived from XYPlot, so bar plots are in fact regular x-y plots which use a special renderer (a BarRenderer) to display their data.

We can get a combined bar and line plot by adding a second data series and styling it using a DefaultLineRenderer2D:

DataSeries barSeries = new DataSeries(data, 0, 1);
DataSeries lineSeries = new DataSeries(data, 0, 1);

BarPlot plot = new BarPlot(barSeries, lineSeries);

// Change the color of all bars
PointRenderer barRenderer = plot.getPointRenderers(barSeries).get(0);
barRenderer.setColor(Color.LIGHT_GRAY);

// Display the second data series as a line plot
LineRenderer lineRenderer = new DefaultLineRenderer2D();
lineRenderer.setColor(Color.RED);
plot.setLineRenderers(lineSeries, lineRenderer);
// The default point renderer (BarRenderer) needs to be deactivated (or changed)
plot.setPointRenderers(lineSeries, null);