How to generate a URL for a pie chart section label
How to generate a URL for a pie chart section label
Is there any way to generate a URL for a pie chart section label? I looked through the documentation and the code and it does not seem like this is possible? I also noticed that the pie chart legend code ignores URL generation, so even a legend item URL doesn't help.
The nature of the data being charted will cause the items of greatest interest to also be the smallest pie sections - not ideal but that is the way I have to do it. Because of that, they will also be the hardest thing for a user to attempt to target with their mouse. In those situations, the pie section label would provide the most convenient target to carry a URL.
I was hoping there might be something in 1.0.3 to address this need but I have not found anything in the latest source.
Any suggestions would be greatly appreciated.
The nature of the data being charted will cause the items of greatest interest to also be the smallest pie sections - not ideal but that is the way I have to do it. Because of that, they will also be the hardest thing for a user to attempt to target with their mouse. In those situations, the pie section label would provide the most convenient target to carry a URL.
I was hoping there might be something in 1.0.3 to address this need but I have not found anything in the latest source.
Any suggestions would be greatly appreciated.
I had read that as well, but when I looked at the source for pie charts in 1.0.3 it still looks like the legend item has it's value hardcoded to null or an empty string -- can't remember which.angel wrote:I've just read in the news section for 1.0.3 release:But this is for the legend, not labels.fixed URL generation for legend items
Ive also been looking at trying to add urls for the pie legend in an html map these last couple of hours, and while the news for 1.0.3 says URL generation is fixed for legend items it doesnt seem to be the case for pie charts at least.
As for the actual labels, these don't appear to have any form of ChartEntity at all in the RenderingInfo. The LegendEntityItems are there, but they don't have any kind of reference to the section they are for. The url and tooltip text in the LegendItemEntity instances is always null.
I noticed however that their order in the EntityCollection does seem to match the order of the sections in the DefaultPieDataset, and based on that I just now banged up this method that will go and add in the urlText for the legends. You call it after drawing the chart and before extracting the information from the RenderingInfo to create html image maps with. Won't solve the url for labels issue, but legends should do equally well for making the really small sections linkable as desired by the OP.
In my case I deal with the streaming of the image to the browser myself so I have full control over the process. If your using something like cewolf Im not sure where you would have the opportunity to call this method. That you will need to figure out yourself.
Adding tooltips is left as an exercise for the reader ;-)
As for the actual labels, these don't appear to have any form of ChartEntity at all in the RenderingInfo. The LegendEntityItems are there, but they don't have any kind of reference to the section they are for. The url and tooltip text in the LegendItemEntity instances is always null.
I noticed however that their order in the EntityCollection does seem to match the order of the sections in the DefaultPieDataset, and based on that I just now banged up this method that will go and add in the urlText for the legends. You call it after drawing the chart and before extracting the information from the RenderingInfo to create html image maps with. Won't solve the url for labels issue, but legends should do equally well for making the really small sections linkable as desired by the OP.
In my case I deal with the streaming of the image to the browser myself so I have full control over the process. If your using something like cewolf Im not sure where you would have the opportunity to call this method. That you will need to figure out yourself.
Code: Select all
/**
* Adds url text to LegendItemEntities in a RenderingInfo object.
* Note: Only supports single pie charts, and uses a hardcoded
* value of 0 for the pieIndex passed to the url generator.
* This method relies on the itemIndex in the PieDataset matching the order
* in which the associated LegendItemEntity instances are found in the charts
* RenderingInfo EntityCollection created when the chart is drawn.
* @param plot The PiePlot from the chart that generated the RenderingInfo
* @param info The RenderingInfo whose legends are to have url text added
*/
public static void addUrlsToLegendInfo(PiePlot plot, ChartRenderingInfo info)
{
PieURLGenerator urlGenerator = plot.getURLGenerator();
if(urlGenerator != null)
{
PieDataset dataset = plot.getDataset();
EntityCollection entities = info.getEntityCollection();
int legendIndex = 0;
Iterator i = entities.iterator();
while(i.hasNext())
{
ChartEntity entity = (ChartEntity)i.next();
if(entity instanceof LegendItemEntity)
{
Comparable key = dataset.getKey(legendIndex); //RELIES ON COINCIDENCE OF ORDER
String urlText = urlGenerator.generateURL(dataset, key, 0);
((LegendItemEntity)entity).setURLText(urlText);
legendIndex++;
}
}
}
}
Thanks for the reply. I have been struggling with this for a long time as well. I tried a few different approaches and they all led to deadends.
1. Thought about just setting the correct URL text on the actual legend item, but LegendItem does not have a setter for URL text.
2. Thought about overriding getLegendItems for PiePlot to reconstruct the LegendItemCollection with correctly loaded URL text, but the LegendItems in the collection do not have the necessary series and dataset index values set.
3. Thought about overriding getLegendItems from PiePlot, and completely re-implementing to generate the correct URL text, but I discovered that the URL is just eventually ignored because LegendTitle.createLegendItemBlock fails to capture the URL when it creates the LabelBlock.
In 1.0.3, this last issue has been corrected and LegendTitle now correctly captures URL text when creating the LabelBlock, but completely reimplementing getLegendItems seems like an extreme measure to just get URL text.

1. Thought about just setting the correct URL text on the actual legend item, but LegendItem does not have a setter for URL text.
2. Thought about overriding getLegendItems for PiePlot to reconstruct the LegendItemCollection with correctly loaded URL text, but the LegendItems in the collection do not have the necessary series and dataset index values set.
3. Thought about overriding getLegendItems from PiePlot, and completely re-implementing to generate the correct URL text, but I discovered that the URL is just eventually ignored because LegendTitle.createLegendItemBlock fails to capture the URL when it creates the LabelBlock.
In 1.0.3, this last issue has been corrected and LegendTitle now correctly captures URL text when creating the LabelBlock, but completely reimplementing getLegendItems seems like an extreme measure to just get URL text.

-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
I'll take a look at this...
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
I've committed a change to CVS that enables the URLs for legend items. I might be able to do the same for the section labels, but I'll have to look through the code some more...
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


Thank you for the quick turnaround! I pulled the changes from CVS and everything is working well for pie chart legend URL generation. That should provide a useful workaround when 1.0.4 is released. Obviously, I hope you get a chance to look into section label url generation a little more. That would provide the ultimate solution to my problem. 
Many thanks....

Many thanks....