[英]Generating URL's for Pie Chart's Section Label using JFree Chart API
How to generate URL's for labels for Pie Charts using JFree Chart package.We can extend the PieSectionLabelGenerator but i would need examples to show how. 如何使用JFree Chart包为饼图的标签生成URL。我们可以扩展PieSectionLabelGenerator,但是我需要一些示例来展示如何。 Please advice! 请指教!
Thanks in Advance! 提前致谢!
Just invoke setLabelGenerator()
on your PiePlot
. 只需在PiePlot
上调用setLabelGenerator()
PiePlot
。 The MessageFormat
ArgumentIndex values correspond to the series name , value and percentage . MessageFormat
ArgumentIndex值对应于系列名称 , 值和百分比 。 You can reference them in your label generator, as shown below: 您可以在标签生成器中引用它们,如下所示:
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {1} {2}"));
Addendum: 附录:
I am looking for a URL/Hyperlink. 我正在寻找一个URL /超链接。
Add a ChartMouseListener
to your ChartPanel
; 将ChartMouseListener
添加到您的ChartPanel
; you can obtain the link from the ChartEntity
of the corresponding ChartMouseEvent
. 您可以从相应的ChartMouseEvent
的ChartEntity
获得链接。 You can use java.awt.Desktop
to open the URL in a browser. 您可以使用java.awt.Desktop
在浏览器中打开URL。
Note this answer is targeted for those making urls and maps for charts used in web pages 请注意,此答案适用于为网页中使用的图表制作网址和地图的用户
For Making the Pie Segments Themselves URLs by using an HTML Map: I would advise that you actually extend the StandardPieURLGenerator
. 通过使用HTML映射使饼段自身成为URL:我建议您实际上扩展StandardPieURLGenerator
。 Then you only need to do two things: 然后,您只需要做两件事:
Add The Data 添加数据
Either through constructor arguments or setters, make a way to add the data into fields within your class. 通过构造函数参数或设置器,可以找到一种将数据添加到类中的字段中的方法。
Override generateURL 覆盖generateURL
generateURL will be called when the JFreeChart is wanting the generator to make a URL. 当JFreeChart希望生成器生成URL时,将调用generateURL。 If you are wanting to add parameters then I would do something like this: 如果您想添加参数,则可以执行以下操作:
public String generateURL(PieDataset dataset, Comparable key, int pieIndex)
{
return super.generateURL(dataset, key, pieIndex) + "&" + yourParameters;
}
To Add URLs in the Label 在标签中添加URL
Extend the StandardPieSectionLabelGenerator
and override generateAttributedSectionLabel
instead for the same steps above. 扩展StandardPieSectionLabelGenerator
并改写generateAttributedSectionLabel
以执行上述相同步骤。 Your function will now look more like this: 您的函数现在看起来像这样:
public String generateAttributedSectionLabel(PieDataset dataset, Comparable key)
{
return super.generateAttributedSectionLabel(dataset, key) + "<a href="YOUR_URL_HERE" />";
}
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.");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.