【问题标题】:JFreeChart - Ring Plot Simple Label PositioningJFreeChart - 环形图简单标签定位
【发布时间】:2016-05-23 13:54:02
【问题描述】:

我在处理 JFreeChart RingPlot 时遇到了一些麻烦。我已经设法将标签放在我的图表中,但我无法随意更改它们的位置。我现在在哪里;

我需要将实验室移到更靠近图表边缘的位置,以便降低剖面深度并获得更好的环外观。到目前为止,我尝试使用 setSimpleLabelOffset 和 setLabelGap 方法,但效果不佳。

这是我的代码;

    DefaultPieDataset dataset = new DefaultPieDataset();

    dataset.setValue("Critical", new Integer(5));
    dataset.setValue("Important", new Integer(20));
    dataset.setValue("Moderate", new Integer(19));
    dataset.setValue("Low", new Integer(5));


    JFreeChart chart = ChartFactory.createRingChart("", dataset, false, true, false);

    RingPlot pie = (RingPlot) chart.getPlot();

    pie.setBackgroundPaint(Color.WHITE);
    pie.setOutlineVisible(false);
    pie.setShadowPaint(null);

    pie.setSimpleLabels(true);
    pie.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
    //pie.setSimpleLabelOffset(new RectangleInsets(1, 1, 1, 1));
    //pie.setLabelGap(0.05);
    //pie.setLabelPadding(new RectangleInsets(100, 5, 10, 5));
    pie.setLabelBackgroundPaint(null);
    pie.setLabelOutlinePaint(null);
    pie.setLabelShadowPaint(null);


    pie.setSectionDepth(0.50);
    pie.setSectionOutlinesVisible(false);
    pie.setSeparatorsVisible(false);

    pie.setIgnoreZeroValues(true);

知道如何实现这一目标吗? 提前致谢。

编辑:感谢@trashgod 的回复,但我猜我的环境有问题。我复制并粘贴了您在上面提供的整个代码,我得到的是:

【问题讨论】:

    标签: java jfreechart pie-chart


    【解决方案1】:

    PiePlot 标签的默认 RectangleInsets 插入 相对于情节的pieArea

    this.simpleLabelOffset = new RectangleInsets(UnitType.RELATIVE, 0.18, 0.18, 0.18, 0.18);
    

    下面的示例将插图切成两半并相应地更改截面深度:

     pie.setSimpleLabelOffset(new RectangleInsets(UnitType.RELATIVE, 0.09, 0.09, 0.09, 0.09));
     pie.setSectionDepth(0.33);
    

    jfreechart-1.0.19.jar and jcommon-1.0.23.jar、Java 1.8.0_92、Mac OS X 10.11.5、Windows 10 测试:

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
    import org.jfree.chart.plot.RingPlot;
    import org.jfree.data.general.DefaultPieDataset;
    import org.jfree.ui.RectangleInsets;
    import org.jfree.util.UnitType;
    
    /**
     * @see http://stackoverflow.com/a/37414338/230513
     */
    public class Test {
    
        private void display() {
            JFrame f = new JFrame("Test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            DefaultPieDataset dataset = new DefaultPieDataset();
            dataset.setValue("Critical", new Integer(5));
            dataset.setValue("Important", new Integer(20));
            dataset.setValue("Moderate", new Integer(19));
            dataset.setValue("Low", new Integer(5));
            JFreeChart chart = ChartFactory.createRingChart("", dataset, false, true, false);
            RingPlot pie = (RingPlot) chart.getPlot();
            pie.setBackgroundPaint(Color.WHITE);
            pie.setOutlineVisible(false);
            pie.setShadowPaint(null);
            pie.setSimpleLabels(true);
            pie.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
            pie.setSimpleLabelOffset(new RectangleInsets(
                UnitType.RELATIVE, 0.09, 0.09, 0.09, 0.09));
            pie.setLabelBackgroundPaint(null);
            pie.setLabelOutlinePaint(null);
            pie.setLabelShadowPaint(null);
            pie.setSectionDepth(0.33);
            pie.setSectionOutlinesVisible(false);
            pie.setSeparatorsVisible(false);
            pie.setIgnoreZeroValues(true);
            f.add(new ChartPanel(chart){
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(400, 400);
                }
            });
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Test()::display);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多