如何在jfree图表中删除分隔线-甜甜圈图表 [英] How to remove separator line in jfree chart- donut chart

查看:231
本文介绍了如何在jfree图表中删除分隔线-甜甜圈图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jfreechart创建甜甜圈图.我要删除分隔线(即,绿色部分和白色部分之间的线)-这条线超出了图表部分.我需要将其限制为仅图表宽度.

以下是实际图表和预期图表:

在此添加的图像中,您可以清楚地看到统计图饼图外面的扩展的断面折线(以蓝色圆圈标记).我需要摆脱该延长线,该分节线不应超出实际的饼图面积.

任何人都可以帮助我实现这些目标.

以下是我正在使用的代码:

  import java.awt.Color;导入java.awt.Dimension;导入java.awt.Font;导入java.awt.Graphics2D;导入java.awt.geom.Rectangle2D;导入javax.swing.JPanel;导入org.jfree.chart.ChartPanel;导入org.jfree.chart.JFreeChart;导入org.jfree.chart.plot.PiePlotState;导入org.jfree.chart.plot.RingPlot;导入org.jfree.data.general.DefaultPieDataset;导入org.jfree.data.general.PieDataset;导入org.jfree.text.TextUtilities;导入org.jfree.ui.ApplicationFrame;导入org.jfree.ui.RectangleInsets;导入org.jfree.ui.RefineryUtilities;导入org.jfree.ui.TextAnchor;公共类DonutChart扩展了ApplicationFrame {私有静态最终长serialVersionUID = 1L;静态类CustomRingPlot扩展了RingPlot {私有Font centerTextFont;私人颜色centerTextColor;公共CustomRingPlot(PieDataset数据集){超级(数据集);this.centerTextFont =新字体(Font.SANS_SERIF,Font.BOLD,24);this.centerTextColor = Color.BLACK;}@Override受保护的void drawItem(Graphics2D g2,int部分,Rectangle2D dataArea,PiePlotState状态,int currentPass){super.drawItem(g2,section,dataArea,state,currentPass);if(currentPass == 1&& section == 0){数字n = this.getDataset().getValue(section);g2.setFont(this.centerTextFont);g2.setPaint(this.centerTextColor);TextUtilities.drawAlignedString(n.toString(),g2,(浮动)dataArea.getCenterX(),(浮动)dataArea.getCenterY(),TextAnchor.CENTER);}}}公共DonutChart(字符串标题){超级(标题);setContentPane(createDemoPanel());}私有静态PieDataset createDataset(){DefaultPieDataset数据集=新的DefaultPieDataset();dataset.setValue("A",71);dataset.setValue("B",29);返回数据集;}私人静态JFreeChart createChart(PieDataset数据集){CustomRingPlot plot = new CustomRingPlot(dataset);JFreeChart图表=新的JFreeChart(",JFreeChart.DEFAULT_TITLE_FONT,剧情,假);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);退货图表}公共静态JPanel createDemoPanel(){JFreeChart图表= createChart(createDataset());chart.setPadding(new RectangleInsets(4,8,2,2));ChartPanel面板=新的ChartPanel(图表);panel.setMouseWheelEnabled(true);panel.setPreferredSize(new Dimension(600,300));返回面板;}公共静态void main(String [] args){RingChartDemo1演示=新的RingChartDemo1("JFreeChart:环形图表演示1");demo.pack();RefineryUtilities.centerFrameOnScreen(demo);demo.setVisible(true);}} 

解决方案

观察到的行就是所谓的分隔符"行.

I am creating Donut chart using 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; amongst them, see setSeparatorsVisible(boolean).

By calling plot.setSeparatorsVisible(false) in your method createChart(), you can hide those lines.

这篇关于如何在jfree图表中删除分隔线-甜甜圈图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆