难度改变JFreeChart的背景颜色

时间:2016-04-03 18:10:22

标签: java swing jfreechart

我试图将背景颜色更改为条形图,到目前为止似乎没有任何工作

以下是我的代码:

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

我尝试过.setbackground()但似乎无法正常工作

由于

3 个答案:

答案 0 :(得分:2)

BarChartDemo1向您展示如何设置图表背景颜料:

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

它还会向您展示如何设置可以更改的ChartTheme

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

答案 1 :(得分:0)

  

尝试此操作并根据需要进行自定义。

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

答案 2 :(得分:0)

你必须像这样使用JFreeChart.getPlot().setBackgroundPaint(Color.XXXXXX);

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

当然,您可以通过几种不同的方法来捕捉所需的颜色,例如:

Color color1 = "anyComponent".getBackgroundColor();

然后申请

jfc.getPlot().setBackgroundPaint(color1);

我希望它有所帮助!