【问题标题】:Difficulty Changing background colour of JFreeChart更改 JFreeChart 的背景颜色的难度
【发布时间】:2016-04-03 18:10:22
【问题描述】:

我正在尝试将 背景颜色 更改为我的条形图,但到目前为止似乎没有任何效果

下面是我的代码:

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


    【解决方案1】:

    BarChartDemo1 教你如何设置图表背景:

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

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

    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】:

      试试这个并根据您的需要进行自定义。

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

      【讨论】:

      • 为什么是ChartFactory.createLineChart()
      • 为什么不chart.setBackgroundPaint()
      【解决方案3】:

      你必须像这样使用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);
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 2014-10-17
        • 1970-01-01
        • 1970-01-01
        • 2021-04-01
        • 2017-02-13
        • 2013-08-08
        相关资源
        最近更新 更多