Search code examples
jfreechart

JFreeChart background color setting is not respected


In JFreeChart I used to generate the chart with CategoryDataset and with createLineChart. This looked like this:

enter image description here

Due to some axis changes I had to switch to XYLineAndShapeRenderer, and now the chart looks like this: enter image description here

Please notice the gray area around the chart. I want to change it to transparent or at least white just like it was on the old one above.

I tried doing it by:

chart.getPlot().setBackgroundPaint(Color.WHITE);

But it does not work.

How can I change that background to white, and change the plot's background to gray (just like it was on the old one?)

Thanks!

UPDATE

I can update the plot's background, but not the ChartPanel's.

Here is the diagram which has these options:

lineChart.getPlot().setBackgroundPaint(Color.red);
chartPanel.setBackground(Color.yellow);

enter image description here

Here I would like to have the yellow as the background of the chartPanel.


Solution

  • It was a user error. The gray area around the plot was not the ChartPanel but the JFreeChart itself. The chart and plot backgrounds can be set separately, for example:

    lineChart.setBackgroundPaint(Color.pink);
    plot.setBackgroundPaint(Color.cyan.darker());
    

    A complete example for testing is seen here.

    background colors