Difficulty Changing background colour of JFreeChart

戏子无情 提交于 2019-12-25 06:28:08

问题


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


回答1:


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:

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



回答2:


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);



回答3:


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!



来源:https://stackoverflow.com/questions/36389172/difficulty-changing-background-colour-of-jfreechart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!