Handling Legend

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Narita
Posts: 4
Joined: Fri Sep 05, 2008 1:44 pm

Handling Legend

Post by Narita » Tue Sep 09, 2008 2:13 pm

Can we decide the legends before hand of plotting the graph?

For a huge dataset, Legends do not fit in correct proportion of the view. I have seen similar questions in the forum. I have got some hint of using LegendTitle class with custom Arrangement instances..... which I basically did not understand.

Can someone help me on this topic?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Wed Sep 10, 2008 8:43 am

What do you mean by "decide" the legends? JFreeChart will allocate space to the legend (and titles and axes) BEFORE using the remaining space to plot the data. If your legend is using too much space in the chart, it could be a hint that either you are plotting the chart in too small a space, or your dataset has too much information to be interpreted by mere mortals.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Narita
Posts: 4
Joined: Fri Sep 05, 2008 1:44 pm

Post by Narita » Wed Sep 10, 2008 10:05 am

Yes the plot size is less and all problem is due to this reason.

Can you provide me with an example of a plot using LegendTitle? I want to know if I can solve my problem using LegendTitle.

agscontact
Posts: 13
Joined: Mon Dec 01, 2008 4:31 am

any solutions

Post by agscontact » Thu Dec 18, 2008 9:42 am

I dont know why jfreechart ppl are avoiding this......and why they release this bulshit half implemented release...i am getting unimplemented exception from most of the arrangement for legend...i am not able to put fix size for legend

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: any solutions

Post by david.gilbert » Thu Dec 18, 2008 9:51 am

agscontact wrote:i am not able to put fix size for legend
By design, the legend grows to accommodate its contents. That allows the datasets in the chart to be updated dynamically, and when the legend contents change the chart layout updates too. It works pretty well for a very wide range of use cases.

Now I can understand that there are cases where you might want the legend size to remain fixed. Unfortunately, that's going to require you to do some coding, because those cases are catered for in "bullshit half implemented" JFreeChart.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

agscontact
Posts: 13
Joined: Mon Dec 01, 2008 4:31 am

give some hint

Post by agscontact » Fri Dec 19, 2008 2:59 am

good that you realize it...Can you help those people who are already using jfreechart in products installed in customer location....giving some hint for the approach how to proceed to make the legend of fixed size.......c'mmon gilbert you are the project lead...u should be able to ressolve it.....

agscontact
Posts: 13
Joined: Mon Dec 01, 2008 4:31 am

Post by agscontact » Fri Dec 19, 2008 3:59 am

I have seen many queries on fixing the width of legend in forum.....We can have %width for legend ....legend.setWidthFactor(30)...it will keep 30% width of the chart reserved for legend.....i hope gilbert can suggest some approach...

titsworth
Posts: 3
Joined: Fri Dec 19, 2008 3:24 am

Post by titsworth » Fri Dec 19, 2008 6:27 am

So here's a solution:

In LegendTitle.java, add you desired parameters for how you want to control the legend, e.g. Percent width, max width, max height, fixed width and height, and all the corresponding getters/setters.

In LegenTitle.java's arrange method, after

Code: Select all

RectangleConstraint c = toContentConstraint(constraint);

Add something like:

Code: Select all

newConstraints = new RectangleConstraint(
   c.getWidth(), 
   new Range(
      c.getWidthRange().getLowerBound() +    
      c.getWidthRange().getLength()*(1-getWidthFactor())/2,     
      c.getWidthRange().getUpperBound() - 
      c.getWidthRange().getLength()*(1-getWidthFactor())/2), 
   c.getWidthConstraintType(),
   c.getHeight(), 
   new Range(
      c.getHeightRange().getLowerBound() + 
      c.getHeightRange().getLength()*(1-getHeightFactor())/2,  
      c.getHeightRange().getUpperBound() - 
      c.getHeightRange().getLength()*(1-getHeightFactor())/2), 
   c.getHeightConstraintType()
);
Size2D size = container.arrange(g2, newConstraints );
...
You can change this to allow for fixed ranges as well. You'll run into a problem, though. If your legend items won't fit in the space given, the following will happen:

1. If the natural arrangement is wider than your available space, it will lock down the width to the max available width.
2. It will then arrange the legend given that fixed width, but using a variable height.
3. If the height of the legend exceed your max available height, it will try to lock down the width of the legend to the max available width and the height to the max available height.
4. Turns out, the ability to fix the height of the legend (when the width is already fixed) is not implemented in FlowArrangement.java, so the legend's height explodes.

The question now is: what is the best way to implement this? Drop off items that won't fit, shrink all items' fonts and markers, something else? I believe you could choose which to do, but I haven't thought into that implementation, but it doesn't seem like it would be too hard.

Given all of this information: I am a JFreeChart newbie and have no idea what I'm doing yet, so this is not a clean and most likely incorrect solution. But I'm just diving in.

And @david.gilbert, thanks for the free "bullshit half implemented" software.

titsworth
Posts: 3
Joined: Fri Dec 19, 2008 3:24 am

Post by titsworth » Fri Dec 19, 2008 6:39 am

Also just realized you can switch to the Grid Arrangement, but you would have to calculate or enter the the desired number of rows/columns, but all of the items overlap and looks like crap.

Once again, it seems like you might need to think about presenting or visualizing the data in a different way. If you have 100+ series, instead of showing the legend, provide a dialog or panel with a Jlist (or whatever) of the data. I'm sure there is a way to pull the line/marker info and render it in the jlist to make it just as useful as the legend would be.

Locked