简体   繁体   English

如何删除jfree图表 - 圆环图中的分隔线

[英]How to remove separator line in jfree chart- donut chart

I am creating Donut chart using Jfreechart. 我正在使用Jfreechart创建甜甜圈图表。 I want to remove separator line (ie.line between green section and white section)- This line is exceeding the chart part. 我想删除分隔线(即绿色部分和白色部分之间的线) - 这条线超出了图表部分。 i need to restrict that to just to chart width. 我需要将其限制为图表宽度。

Below is the actual and expected chart: 以下是实际和预期的图表: 在此输入图像描述

In this added image you can clearly see the extented section break line outside the chart pie.(marked in blue circle). 在这个添加的图像中,您可以清楚地看到图表饼外的扩展部分断开线(以蓝色圆圈标记)。 i need to get rid of that extended line , the section break line should not go beyond the actual pie area. 我需要摆脱那条延长线,断面线不应超出实际的馅饼面积。

Any one help me to achieve these. 任何人都帮助我实现这些目标。

Below is the code i'm using: 以下是我正在使用的代码:

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

The observed lines are the so-called "separator" lines. 观察到的线是所谓的“分隔线”。 RingPlot has several methods to control them; RingPlot有几种控制它们的方法; amongst them, see setSeparatorsVisible(boolean) . 其中,请参阅setSeparatorsVisible(boolean)

By calling plot.setSeparatorsVisible(false) in your method createChart() , you can hide those lines. 通过在方法createChart()调用plot.setSeparatorsVisible(false) ,可以隐藏这些行。

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM