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?
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With