JfreeChart修复饼图半径

时间:2014-11-19 04:21:48

标签: java jfreechart

我正在使用jFreeChart并使用图例绘制饼图。 馅饼半径根据图例而有所不同,所以我想修正饼图的大小(绘图区域)

RingPlot plot = new RingPlot(dataset);
StringBuffer chartFileName = new StringBuffer(Integer.toString(generatedCharts)).append(Long.toString(System.currentTimeMillis())).append(".png");

JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(new GradientPaint(new Point(0, 0), new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));

TextTitle t = chart.getTitle();
t.setHorizontalAlignment(org.jfree.ui.HorizontalAlignment.LEFT);
t.setPaint(new Color(240, 240, 240));
t.setFont(new Font("Arial", Font.BOLD, 26));

plot.setBackgroundPaint(null);
plot.setOutlineVisible(false);
plot.setLabelGenerator(null);
plot.setSectionDepth(0.35);
plot.setSectionOutlinesVisible(false);
plot.setSimpleLabels(true);
plot.setShadowPaint(null);
plot.setOuterSeparatorExtension(0);
plot.setInnerSeparatorExtension(0);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}",new DecimalFormat("#,##0"), new DecimalFormat("0.000%")));
plot.setLabelBackgroundPaint(null);
plot.setLabelOutlinePaint(null);

Font font=new Font("",0,16);
plot.setLabelFont(font);

chart.getLegend().setFrame(BlockBorder.NONE);
chart.getLegend().setPosition(RectangleEdge.BOTTOM); 
chart.setBackgroundPaint(java.awt.Color.white);
chart.setPadding(new RectangleInsets(4, 8, 2, 2));

1 个答案:

答案 0 :(得分:3)

我能够将您的代码添加到ApplicationFrame,我得到了这个:

enter image description here

我确实有一些意见:

  1. 使用工厂而不是直接调用构造函数。例如,您的“饼图”实际上是一个“环形图”。有方便的方法来创建不同的类型或图表。例如:JFreeChart chart = ChartFactory.createRingChart(...);
  2. 您可以使用以下部分或全部内容调整饼图的大小:setInteriorGap()setLabelGap()setLabelLinkMargin()和/或setMaximumLabelWidth()
  3. 如果您只想更改图表的半径,请将以下代码添加到您的代码中:

    public static void setPieRadius(JFreeChart chart, double radius)
    {
        if (chart != null)
        {
            Plot plot = chart.getPlot();
            if (plot instanceof PiePlot)
            {
                PiePlot piePlot = (PiePlot) plot;
                double ig = 1.0 - radius;
                if (ig > PiePlot.MAX_INTERIOR_GAP)
                {
                    ig = PiePlot.MAX_INTERIOR_GAP;
                }
                piePlot.setInteriorGap(ig);
            }
        }
    }
    
  4. 我仍然不确定你的问题是什么。我建议您获取更多JFreeChart饼图示例,并在JFreeChart论坛中发布 SPECIFIC 问题描述(或者如果您愿意,也可以在此处)。