有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何删除图表周围的白色边框?

这是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) 个答案

  1. # 1 楼答案

    既然您已经消除了域轴和范围轴,那么还有一个未考虑的填充源。您缺少以下内容:

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