簡體   English   中英

JfreeChart修復餅圖半徑

[英]JfreeChart fix the Pie Chart Radius

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

我能夠將您的代碼添加到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論壇中發布特定的問題描述(如果需要,也可以在此處)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM