JFreeChart - Ring Plot Simple Label Positioning

ε祈祈猫儿з 提交于 2019-12-11 02:07:23

问题


I'm having some trouble while working on JFreeChart RingPlot. I've managed to put labels inside my chart, yet I can't change their positions as I want. Here where am I right now;

I need to move the labes closer to the edges of the chart so that I can lower the section depth and have a better ring look. So far, I tried to play with setSimpleLabelOffset and setLabelGap methods but didn't work well.

Here is my code;

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

Any idea how may I achieve this? Thanks in advance.

Edit: Thanks for the response @trashgod, but something is wrong with my environment iI guess. I copied and pasted the whole code you presented above and what I get is this:


回答1:


The default RectangleInsets for PiePlot labels are inset relative to the plot's pieArea:

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

The example below cuts the insets in half and changes the section depth accordingly:

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

As tested with 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);
    }
}


来源:https://stackoverflow.com/questions/37392925/jfreechart-ring-plot-simple-label-positioning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!