Search code examples
javaswingjfreechart

Changing background colour of jFreeChart


I am trying to change the background color of jfreechart. It is displaying in grey color and I want a white background. I have tried

chart.setBackgroundPaint(Color.WHITE); 

However it does not show me the white background.
I have the following code that displays the the plot

chart = ChartFactory.createXYLineChart("Line Chart","Year","Temperature", dataset);
ChartPanel chartPanel = new ChartPanel(chart, false);
graph1.setLayout(new BorderLayout());
graph1.add(chartPanel, BorderLayout.EAST);
graph1.add(chartPanel);
SwingUtilities.updateComponentTreeUI(this);
graph1.updateUI();
System.out.println("Database created successfully...");

How should I set a white background?


Solution

  • ChartPanel inherit method javax.swing.JComponent.setBackground(java.awt.Color)

    chartPanel.setBackground( Color.RED );
    

    Or try:

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

    See documentation of JFreeChart.getPlot() and Plot.setBackgroundPaint()

    See this post on SO or this one too.