2010-09-14 44 views
1

如何使用JFree Chart package生成饼图的标签的URL。我们可以扩展PieSectionLabelGenerator,但我需要示例来说明如何。 请指教!使用JFree Chart API为饼图的部分标签生成URL

在此先感谢!

+0

它是正确的,你想点击的URL的标签,以便在浏览器中打开URL? – trashgod 2010-09-14 18:30:47

+0

是的,这就对了。 – 2010-09-15 00:10:24

+0

为了进一步阐明您的网页或摆动图表? – Adam 2010-09-15 15:06:56

回答

0
static class CustomLegendGenerator 
     implements PieSectionLabelGenerator { 

    public String generateSectionLabel(final PieDataset dataset, final Comparable key) { 
     String temp = null; 
     if (dataset != null) { 
      temp = key.toString(); 
      if (key.toString().equalsIgnoreCase("abc")) { 
       temp = temp + " (abc String)"; 
      } 
      if (key.toString().equalsIgnoreCase("xyz")) { 
       temp = temp + " (xyz description)"; 
      } 
      if (key.toString().equalsIgnoreCase("klm")) { 
       temp = temp + " (Klm description)"; 
      } 
     } 
     return temp; 
    } 

    public AttributedString generateAttributedSectionLabel(PieDataset pd, Comparable cmprbl) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 
} 
1

只需在PiePlot上调用setLabelGenerator()即可。该MessageFormatArgumentIndex值对应于系列名称百分比。您可以参考它们在你的标签生成器,如下图所示:

PiePlot plot = (PiePlot) chart.getPlot(); 
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {1} {2}")); 

附录:

我要寻找一个URL /超链接。

ChartMouseListener加入您的ChartPanel;您可以从相应的ChartMouseEventChartEntity获取链接。您可以使用java.awt.Desktop在浏览器中打开该URL。

+0

我想象会给我一个字符串标签。我正在寻找一个URL /超链接。 – 2010-09-14 17:04:14

+0

@gpmattoo:只需将URL设置为与标签相同的值即可。 – trashgod 2010-09-15 02:06:57

+0

如果您可以共享上述的ChartMouseListener的代码片段,那将有所帮助。请注意,我正在使用JDK 1.4。 – 2010-09-15 17:31:40

0

注意这个答案是针对在网页中使用的图表的制作网址和地图

对于通过使用HTML地图制作的馅饼段本身的网址: 我会建议你实际上延长StandardPieURLGenerator。然后,你只需要做两件事情:

添加数据

无论是通过构造函数参数或制定者,做一个办法你的类中添加数据到字段。

覆盖generateURL

generateURL将在JFreeChart的是想发生器,使URL来调用。如果你想添加参数,那么我会做这样的事情:

public String generateURL(PieDataset dataset, Comparable key, int pieIndex) 
{ 
    return super.generateURL(dataset, key, pieIndex) + "&" + yourParameters; 
} 

要在标签添加网址

延长StandardPieSectionLabelGenerator并覆盖generateAttributedSectionLabel而不是为上述相同的步骤。你的功能现在看起来更像是这样的:

public String generateAttributedSectionLabel(PieDataset dataset, Comparable key) 
{ 
    return super.generateAttributedSectionLabel(dataset, key) + "<a href="YOUR_URL_HERE" />"; 
} 
+0

我会想象这将是用于PIE图表的URL生成器。然而,我正在寻找的是馅饼标签的URL,即自定义类实现PieSectionLabelGenerator接口http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/labels/PieSectionLabelGenerator.html或类似的东西。 – 2010-09-15 00:30:03

+0

@gpmatto对不起,误读。让我为这个写一个新的答案。 – Adam 2010-09-15 14:54:41