Andy1000
Andy1000

Reputation: 1

Difficulty Changing background colour of JFreeChart

Im trying to change the background color to my bar chart and so far nothing seems to be working

Here is my code below:

JFreeChart expbarchart = ChartFactory.createBarChart("Monthly Expenditures", "Expenditure Type", "Amount (£)", barexp, PlotOrientation.VERTICAL, false, true, false);
    ChartPanel expframe = new ChartPanel(expbarchart);
    expframe.setLocation(695, 49);
    expframe.setSize(641,500);
    expframe.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(173, 216, 230), null));
    graphpanel.add(expframe);

I have tried doing .setbackground() and it does not seem to work

Thanks

Upvotes: 0

Views: 2328

Answers (3)

Paul Efford
Paul Efford

Reputation: 265

You have to use JFreeChart.getPlot().setBackgroundPaint(Color.XXXXXX); 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.BLUE);
    ChartPanel chart = new ChartPanel(jfc);
    JFrame frame = new JFrame();
    frame.add(chart);
    frame.pack();
    frame.setVisible(true);
}   

Of course, you can catch the desired color by several different methods regarding your needs, for example:

Color color1 = "anyComponent".getBackgroundColor();

and then apply

jfc.getPlot().setBackgroundPaint(color1);

I hope it helps!

Upvotes: 0

Catalina Island
Catalina Island

Reputation: 7126

BarChartDemo1 shows you how to set the chart background paint:

chart.setBackgroundPaint(new Color(173, 216, 230));

It also shows you how to set a ChartTheme that you can change:

chart

StandardChartTheme theme = new StandardChartTheme("JFree/Shadow", true);
Color color = new Color(173, 216, 230);
theme.setPlotBackgroundPaint(color);
theme.setChartBackgroundPaint(color.brighter());
ChartFactory.setChartTheme(theme);

Upvotes: 3

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

try this and customize as per your need.

        // create the chart...
            JFreeChart chart = ChartFactory.createLineChart(
                "# of Sales by Month",   // chart title
                "Month",                       // domain axis label
                "# of Sales",                   // range axis label
                dataset,                         // data
                PlotOrientation.VERTICAL,        // orientation
                true,                           // include legend
                true,                            // tooltips
                false                            // urls
            );

            if(subTitle != null && !subTitle.isEmpty())
                chart.addSubtitle(new TextTitle(subTitle));
            chart.setBackgroundPaint(Color.BLUE);
    //        Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
    //        chart.setBackgroundPaint(p);

            CategoryPlot plot = (CategoryPlot) chart.getPlot();
            plot.setRangePannable(true);
            plot.setRangeGridlinesVisible(true);
            plot.setBackgroundAlpha(1);
            plot.setBackgroundPaint(Color.BLUE);
    //        Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
    //        plot.setBackgroundPaint(p);


            NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

            ChartUtilities.applyCurrentTheme(chart);

Upvotes: 0

Related Questions