Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 581
enjal Avatar asked Aug 24 '14 07:08

enjal


2 Answers

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.

like image 186
Aubin Avatar answered Nov 05 '22 20:11

Aubin


You have to use JFreeChart.getPlot().setBackgroundPaint(Color.WHITE); like this:

public static void main(String[] args) {
    DefaultPieDataset pieDataset = new DefaultPieDataset(); 
    pieDataset.setValue("LoggedIn" +": "+ 5, 10);
    pieDataset.setValue("LoggedOut" +": "+ 8, 17);
    JFreeChart jfc = ChartFactory.createPieChart("title", pieDataset, false, false, false );
    jfc.getPlot().setBackgroundPaint(Color.WHITE);
    ChartPanel chart = new ChartPanel(jfc);
    JFrame frame = new JFrame();
    frame.add(chart);
    frame.pack();
    frame.setVisible(true);
}   

I hope it helps!

like image 39
Paul Efford Avatar answered Nov 05 '22 21:11

Paul Efford