难度改变JFreeChart的背景颜色

问题描述 投票:0回答:3

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

这是我的代码如下:

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(),它似乎不起作用

谢谢

java swing jfreechart
3个回答
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);

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

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

我希望它有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.