1

This is slightly modified code of 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);

            }
        });
    }

it draws

enter image description here

Is it possible to remove that white border around a chart? Some tries are in code, which had no effect.

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

1 Answers1

3

Seeing as you have already eliminated the domain and range axes, there remains one more source of padding that you have not accounted for. You are missing this:

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

The empty space you see in your posted example is due to Plot insets where your posted code is manipulating the JFreeChart. The reason for the anonymous subclass in the solution code is to eliminate a 1-pixel "halo" in the original implementation.

EDIT:

I fiddled around a bit more and noticed you may or may not need this in addition to the inset fix. I haven't poked around too deeply in this, but passing in a sub-classed CategoryPlot appears to do the trick for this specialized case at the very least.

private class WrappedCategoryPlot extends CategoryPlot
{
  @Override
  protected AxisSpace calculateAxisSpace( Graphics2D g2, Rectangle2D plotArea )
  {
     return new AxisSpace();
  }
}
Creakazoid
  • 331
  • 1
  • 4