如何使用 JFree Chart 包为饼图的标签生成 URL。我们可以扩展 PieSectionLabelGenerator,但我需要示例来说明如何操作。请指教!
提前致谢!
如何使用 JFree Chart 包为饼图的标签生成 URL。我们可以扩展 PieSectionLabelGenerator,但我需要示例来说明如何操作。请指教!
提前致谢!
只需调用setLabelGenerator()
你的PiePlot
. MessageFormat
ArgumentIndex值对应于系列名称、值和百分比。您可以在标签生成器中引用它们,如下所示:
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {1} {2}"));
附录:
我正在寻找一个 URL/超链接。
添加一个ChartMouseListener
到你的ChartPanel
; 您可以从ChartEntity
相应的ChartMouseEvent
. 您可以使用java.awt.Desktop
在浏览器中打开 URL。
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.");
}
}
请注意,此答案针对那些为网页中使用的图表制作网址和地图的人
使用 HTML 地图制作饼图分割自己的 URL:
我建议您实际扩展StandardPieURLGenerator
. 那么你只需要做两件事:
添加数据
无论是通过构造函数参数还是设置器,都可以将数据添加到类中的字段中。
覆盖 generateURL
当 JFreeChart 希望生成器生成 URL 时,将调用 generateURL。如果您想添加参数,那么我会这样做:
public String generateURL(PieDataset dataset, Comparable key, int pieIndex)
{
return super.generateURL(dataset, key, pieIndex) + "&" + yourParameters;
}
在标签中添加 URL
为上述相同步骤扩展StandardPieSectionLabelGenerator
并覆盖。generateAttributedSectionLabel
您的函数现在看起来更像这样:
public String generateAttributedSectionLabel(PieDataset dataset, Comparable key)
{
return super.generateAttributedSectionLabel(dataset, key) + "<a href="YOUR_URL_HERE" />";
}