【问题标题】:Generating URL's for Pie Chart's Section Label using JFree Chart API使用 JFreeChart API 为饼图部分标签生成 URL
【发布时间】:2010-09-14 00:51:11
【问题描述】:

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

提前致谢!

【问题讨论】:

  • 您想点击标签中的 URL 以在浏览器中打开 URL 是否正确?
  • 进一步澄清您的图表是在网页上还是在摆动?

标签: java jsp jfreechart pie-chart


【解决方案1】:

只需在您的 PiePlot 上调用 setLabelGenerator()MessageFormat ArgumentIndex 值对应于系列名称百分比。您可以在标签生成器中引用它们,如下所示:

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

附录:

我正在寻找 URL/超链接。

ChartMouseListener 添加到您的ChartPanel;可以从对应ChartMouseEventChartEntity获取链接。您可以使用java.awt.Desktop 在浏览器中打开网址。

【讨论】:

  • 我想这会给我一个字符串标签。我正在寻找 URL/超链接。
  • @gpmattoo:只需将 URL 设置为与标签相同的值即可。
  • 如果您可以分享您上面所说的 ChartMouseListener 的代码 sn-p,那将有所帮助。请注意,我使用的是 JDK 1.4。
  • @gpmattoo:这里有一个ChartMouseListener 的例子,stackoverflow.com/questions/3309045。看起来 java.awt.Desktop 需要 Java SE 6。
【解决方案2】:

请注意,此答案是针对那些为网页中使用的图表制作网址和地图的人

使用 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" />";
}

【讨论】:

  • 我想这将用于 PIE 图表的 URL 生成器。但是,我正在寻找的是 Pie 标签的 URL,即实现 PieSectionLabelGenerator 接口 jfree.org/jfreechart/api/javadoc/org/jfree/chart/labels/… 的自定义类或类似的东西。
  • @gpmatto 啊抱歉,误读了。让我为那个写一个新的答案。
【解决方案3】:
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.");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2021-08-21
    相关资源
    最近更新 更多