2016-05-23 141 views
2

我在JFreeChart RingPlot上工作時遇到了一些麻煩。我已經設法將標籤放入圖表中,但我無法按自己的意願更改其位置。我現在在哪裏;JFreeChart - 環形圖簡單標籤定位

enter image description here

我需要靠攏的LABES到圖表的邊緣,使我可以降低部分深度,並且具有更好的環的外觀。到目前爲止,我嘗試使用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的迴應,但是我的環境出了問題。我複製並粘貼你上面提出和我所得到的是這樣的整個代碼:

image

回答

0

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

image

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); 
    } 
} 
+0

什麼版本'jfreechart'和'jcommon'您使用的? – trashgod