2016-04-03 79 views
0

我试着给背景颜色改变我的条形图,至今似乎没有什么是工作难度更改背景颜色

这里是我下面的代码:

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(),它似乎并没有工作

感谢

回答

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

为什么'ChartFactory.createLineChart()'? –

+0

为什么不''chart.setBackgroundPaint()'? – trashgod

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

您必须使用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); 

我希望它有帮助!