如何使用JFree Chart package生成餅圖的標籤的URL。我們可以擴展PieSectionLabelGenerator,但我需要示例來說明如何。 請指教!使用JFree Chart API爲餅圖的部分標籤生成URL
在此先感謝!
如何使用JFree Chart package生成餅圖的標籤的URL。我們可以擴展PieSectionLabelGenerator,但我需要示例來說明如何。 請指教!使用JFree Chart API爲餅圖的部分標籤生成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.");
}
}
只需在PiePlot
上調用setLabelGenerator()
即可。該MessageFormat
ArgumentIndex值對應於系列名稱,值和百分比。您可以參考它們在你的標籤生成器,如下圖所示:
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {1} {2}"));
附錄:
我要尋找一個URL /超鏈接。
將ChartMouseListener
加入您的ChartPanel
;您可以從相應的ChartMouseEvent
的ChartEntity
獲取鏈接。您可以使用java.awt.Desktop
在瀏覽器中打開該URL。
我想象會給我一個字符串標籤。我正在尋找一個URL /超鏈接。 – 2010-09-14 17:04:14
@gpmattoo:只需將URL設置爲與標籤相同的值即可。 – trashgod 2010-09-15 02:06:57
如果您可以共享上述的ChartMouseListener的代碼片段,那將有所幫助。請注意,我正在使用JDK 1.4。 – 2010-09-15 17:31:40
注意這個答案是針對在網頁中使用的圖表的製作網址和地圖
對於通過使用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" />";
}
我會想象這將是用於PIE圖表的URL生成器。然而,我正在尋找的是餡餅標籤的URL,即自定義類實現PieSectionLabelGenerator接口http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/labels/PieSectionLabelGenerator.html或類似的東西。 – 2010-09-15 00:30:03
@gpmatto對不起,誤讀。讓我爲這個寫一個新的答案。 – Adam 2010-09-15 14:54:41
它是正確的,你想點擊的URL的標籤,以便在瀏覽器中打開URL? – trashgod 2010-09-14 18:30:47
是的,這就對了。 – 2010-09-15 00:10:24
爲了進一步闡明您的網頁或擺動圖表? – Adam 2010-09-15 15:06:56