2013-02-26 109 views
0

這是稍微修改的org.jfree.chart.demo.BarChartDemo1代碼:如何去除圖表上的白色邊框?

public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 

       CategoryDataset dataset = createDataset(); 

       JFreeChart chart = createChart(dataset); 

       //chart.setBorderVisible(false); // no effect 
       //chart.setPadding(new RectangleInsets(0, 0, 0, 0)); // no effect 

       ChartPanel chartPanel = new ChartPanel(chart); 
       chartPanel.setFillZoomRectangle(true); 
       chartPanel.setMouseWheelEnabled(true); 
       //chartPanel.setPreferredSize(new Dimension(500, 270)); 
       chartPanel.setBounds(100,100,640,480); 

       JFrame frame = new JFrame(); 
       frame.setLayout(null); 
       //frame.setContentPane(chartPanel); 
       frame.add(chartPanel); 
       frame.pack(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 

      } 
     }); 
    } 

它吸引

enter image description here

是否可以刪除圖表周圍的白色邊框?有些嘗試是在代碼中,這沒有任何作用。

+1

嘗試使用佈局管理器而不是設置邊界 – MadProgrammer 2013-02-26 20:07:05

+0

這是爲了本地化效果。 – 2013-02-26 22:33:46

回答

2

鑑於您已經消除了域和範圍軸,還有一個您未考慮的填充源。你缺少這樣的:

chart.getPlot().setInsets(new RectangleInsets(){ 
     public void trim(Rectangle2D area) {}; 
    }); 

你在貼出的例子看空的空間是由於Plot插圖在您發佈的代碼操縱JFreeChart。解決方案代碼中匿名子類的原因是在原始實現中消除1像素「暈」。

編輯:

我撥弄了一下週圍越來越注意到你可能會或可能不會需要這個除了插圖修復。我沒有在這方面做太深入的探討,但是通過分類CategoryPlot似乎對這個專業案例至少起到了訣竅。

private class WrappedCategoryPlot extends CategoryPlot 
{ 
    @Override 
    protected AxisSpace calculateAxisSpace(Graphics2D g2, Rectangle2D plotArea) 
    { 
    return new AxisSpace(); 
    } 
} 
+0

仍然有小的邊框... – 2013-02-26 22:32:24

+0

我編輯了我的答案了一下。與原始答案一起使用時,第二個更改是否有幫助? – Creakazoid 2013-02-26 22:39:42

+0

對不起:( – 2013-02-26 23:15:17