【问题标题】:JfreeChart fix the Pie Chart RadiusJfreeChart 修复饼图半径
【发布时间】:2014-11-19 04:21:48
【问题描述】:

我正在使用 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));

【问题讨论】:

  • 开SO账户的时候,你有没有费心看一下规则。如果没有与您的问题相关的代码,那么任何人都应该如何帮助您。另外,您是否费心查看JFreeChart Forums 的答案?他们在那里也回答了很多这样的问题。
  • 更好地解释您的问题。 “我想修复饼图(绘图区域)的大小”是什么意思?这到底是什么意思?
  • 另外,dataset 和 generatedCharts 定义不正确。添加完整代码。

标签: java jfreechart


【解决方案1】:

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

我确实有一些观察:

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

我仍然不确定你的问题是什么。我建议您获取更多JFreeChart 饼图示例,并在 JFreeChart 论坛(如果您愿意,也可以在此处)发布SPECIFIC问题描述。

【讨论】:

  • 我创建了自己的数据集,因为您没有提供数据集。
  • @Ammi 如果此答案满足您的问题,请将其标记为。
  • 嘿,谢谢你的回答,我的问题是在创建图表时我们必须指定图表的宽度和高度,如果我在数据集中对特定值的名称太长,那么在这种情况下,图例是显示名称占用了太多空间,因为图表的大小越来越小,简而言之,绘图区域根据图例而变化(用于值的键)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多