2016-04-03 5 views
0

Im пытается изменить цвет фона к моей гистограмме и до сих пор ничего не кажется, работаетСложность Изменение цвета фона 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() и это не похоже на работу

Благодаря

ответ

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

Надеюсь, это поможет!