2017-10-06 84 views
1

我正在使用Jfreechart創建甜甜圈圖表。我想刪除分隔線(ie.line在綠色部分和白色部分之間) - 此行超出了圖表部分。我需要限制這只是爲了圖表寬度。如何刪除jfree圖表中的分隔線 - 甜甜圈圖表

下面是實際的和預期的圖表: enter image description here

在此添加的圖像可以清晰地看到圖表餅(標記爲藍色圓圈)外推廣的分節符線。我需要擺脫那條延長線,分節線不應超出實際的餡餅區域。

任何人都可以幫助我實現這些目標。

下面是我正在使用的代碼:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.geom.Rectangle2D; 
import javax.swing.JPanel; 

import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.plot.PiePlotState; 
import org.jfree.chart.plot.RingPlot; 
import org.jfree.data.general.DefaultPieDataset; 
import org.jfree.data.general.PieDataset; 
import org.jfree.text.TextUtilities; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RectangleInsets; 
import org.jfree.ui.RefineryUtilities; 
import org.jfree.ui.TextAnchor; 
public class DonutChart extends ApplicationFrame { 

    private static final long serialVersionUID = 1L; 

    static class CustomRingPlot extends RingPlot { 

     private Font centerTextFont;  
     private Color centerTextColor; 

     public CustomRingPlot(PieDataset dataset) { 
      super(dataset); 
      this.centerTextFont = new Font(Font.SANS_SERIF, Font.BOLD, 24); 
      this.centerTextColor = Color.BLACK; 
     } 

     @Override 
     protected void drawItem(Graphics2D g2, int section, 
       Rectangle2D dataArea, PiePlotState state, int currentPass) { 
      super.drawItem(g2, section, dataArea, state, currentPass); 
      if (currentPass == 1 && section == 0) { 
       Number n = this.getDataset().getValue(section); 
       g2.setFont(this.centerTextFont); 
       g2.setPaint(this.centerTextColor); 
       TextUtilities.drawAlignedString(n.toString(), g2, 
         (float) dataArea.getCenterX(), 
         (float) dataArea.getCenterY(), 
         TextAnchor.CENTER); 
      } 
     } 
    } 

    public DonutChart(String title) { 
     super(title); 
     setContentPane(createDemoPanel()); 
    } 

    private static PieDataset createDataset() { 
     DefaultPieDataset dataset = new DefaultPieDataset(); 
     dataset.setValue("A", 71); 
     dataset.setValue("B", 29); 
     return dataset; 
    } 

    private static JFreeChart createChart(PieDataset dataset) { 
     CustomRingPlot plot = new CustomRingPlot(dataset); 
     JFreeChart chart = new JFreeChart("", 
       JFreeChart.DEFAULT_TITLE_FONT, plot, false); 
     plot.setBackgroundPaint(null); 
     plot.setOutlineVisible(false); 
     plot.setLabelGenerator(null); 
     plot.setSectionPaint("A", Color.RED); 
     plot.setSectionPaint("B", Color.WHITE); 
     plot.setSectionDepth(0.5); 
     plot.setSectionOutlinesVisible(true); 
     plot.setSectionOutlinePaint(Color.RED); 
     plot.setShadowPaint(null); 
     return chart; 
    } 

    public static JPanel createDemoPanel() { 
     JFreeChart chart = createChart(createDataset()); 
     chart.setPadding(new RectangleInsets(4, 8, 2, 2)); 
     ChartPanel panel = new ChartPanel(chart); 
     panel.setMouseWheelEnabled(true); 
     panel.setPreferredSize(new Dimension(600, 300)); 
     return panel; 
    } 

    public static void main(String[] args) { 
     RingChartDemo1 demo = new RingChartDemo1("JFreeChart: Ring Chart Demo 1"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 
    } 
} 
+0

您的例子並不編譯,你setSectionOutlinePaint的'調用()'不推薦使用;請相應地編輯你的問題。此外,恢復版權並引用[原創](http://www.jfree.org/jfreechart/samples.html)。 – trashgod

回答

3

所觀察到的線是所謂的「分離器」的行。 RingPlot有幾種方法來控制它們;其中,請參閱setSeparatorsVisible(boolean)

通過在您的方法createChart()中調用plot.setSeparatorsVisible(false),您可以隱藏這些行。

image