A discussion forum for JFreeChart (a 2D chart library for the Java platform).
-
eladmena
- Posts: 9
- Joined: Tue Sep 08, 2009 12:19 pm
- antibot: No, of course not.
Post
by eladmena » Tue Apr 06, 2010 11:20 am
Hey,
In case using a legend being placed either in the top or bottom of the chart (RectangleEdge is either top or bottom) and there are too many items in the legend, then all legend items are shown and the legend takes all the chart area, which result in the graph not being visible.
see attached image of current look left and desired look right:
I have tried setting the legend height using the setHeight() method, with no luck.
Also BlockContainers gave no solution for this.
Can you assist?
Thanks
-
paradoxoff
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Post
by paradoxoff » Wed Apr 07, 2010 6:49 pm
As you have noted, setting the width/height of a Legend has no effect. For every drawing cycle, the bounds of a title will be recalculated. The bounds are calculated based on various properties, most of which can be changed:
1. A smaller font will lead to a smaller legend. See LegendTitle.setItemFont(Font);
2. A shorter legend text will lead to a shorter legend. Since your legend texts are very long (IMHO too long to be really helpful), I recommend to change that.
2a) By default, the legend labels will represent the series keys of the dataset, so you could try to change the series keys.
2b) You can change the way how the series keys are transformed into the legend labels. This transformations is performed in the getLegendItem(int datasetIndex, int series) method of the renderer. An XYLineAndShapeRenderer uses the return value of getLegendItemLabelGenerator().generateLabel(dataset, series). The LegendItemLabelGenerator is by default a StandardXYSeriesLabelGenerator which simply returns the series keys in its generateLabel(XYDataset dataset, int seriesIndex) method. You can create your own implementation and change that behaviour. In order to still see the full-length legend labels, you could use tooltips for the legend items. In this case you would have to set a real LegendItemToolTipGenerator as this might by null according to the documentation of AbstractXYItemRenderer and is in fact not initialized.
-
Jim McGlaughlin
- Posts: 26
- Joined: Tue Jul 25, 2006 9:16 pm
- Location: Newburyport, MA USA
-
Contact:
Post
by Jim McGlaughlin » Sat Apr 10, 2010 2:07 pm
Another technique would be to put the legends in a separate panel.
Maintain an array of colors and assign colors to the series which are in int using myRenderer.setSeriesPaint( series, Paint ).
Your separate panel can have an imageIcon as part of the legend that is a small rectangle with the same color as the series line.
Use ActionEvent to communicate between the chart's panel and the legend panel.
-
eladmena
- Posts: 9
- Joined: Tue Sep 08, 2009 12:19 pm
- antibot: No, of course not.
Post
by eladmena » Wed Apr 28, 2010 10:09 am
Sorry for the late reply, I didn't get an email of the responses.
paradoxoff:
I wouldn't want to change the names, since in some cases it is not necessary, if only I could have known the legend is taking more than half of the area I could have done something like this.
Jim McGlaughlin:
I'm not sure I understand how to what you're suggesting, do you have an example for a reference?
Thanks
-
gengar
- Posts: 26
- Joined: Thu Mar 18, 2010 12:28 am
- antibot: No, of course not.
Post
by gengar » Wed Apr 28, 2010 7:41 pm
He was suggesting that you create your own set of colors for your chart. Then, assign one of these colors to each of the series of the plot. Since you know the color you assigned, you can create your own legend with a JPanel for example.
-
Jim McGlaughlin
- Posts: 26
- Joined: Tue Jul 25, 2006 9:16 pm
- Location: Newburyport, MA USA
-
Contact:
Post
by Jim McGlaughlin » Thu Apr 29, 2010 7:28 pm
This is an image of something I've done.
The 'imageicons' to the left of the devices is the same color as the series paint.
A mouse rollover of the devices makes the line bold in the graph.
A right click on the series line in the graph at the point tooltips would come up make the device highlighted on the left.

-
BigDong
- Posts: 7
- Joined: Tue Jul 24, 2012 4:46 pm
- antibot: No, of course not.
Post
by BigDong » Fri May 31, 2013 1:58 pm
Hi, I am encoutroung the exact same probleme.
I've tried to implement with Jim McGlaughlin but failed, could you provide us a short sample of code as demo please ?
-
remiohead
- Posts: 201
- Joined: Fri Oct 02, 2009 3:53 pm
- antibot: No, of course not.
Post
by remiohead » Fri May 31, 2013 3:21 pm
What did you try and where did it fail specifically? Please post your code as a SSCCE (
http://sscce.org/) and I'll be happy to look over it.
-
BigDong
- Posts: 7
- Joined: Tue Jul 24, 2012 4:46 pm
- antibot: No, of course not.
Post
by BigDong » Fri May 31, 2013 4:26 pm
Actually I didn't wrote any code, I looked over my project to see where I could do the edit provided by Jim McGlaughlin.
It seems that his legend isn't generated by jfreechart.
Do you have any suggestion ?
(Unfortunatly, I am not allowed to past any code since I'm not the owner of this application)
-
remiohead
- Posts: 201
- Joined: Fri Oct 02, 2009 3:53 pm
- antibot: No, of course not.
Post
by remiohead » Fri May 31, 2013 7:58 pm
Create a LegendPanel that extends JPanel. LegendPanel will require a LegendTitle and in the paintComponent() method it will call the draw method of LegendTitle. The LegendItemSource for your LegendTitle will be the plot, unless you want to customise it.
Note, I was not asking for the code to your app. I was asking for a self contained compilable sample. That's not the same thing.