Exctracting and showing only Legend

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Alan - Blue
Posts: 5
Joined: Wed Nov 15, 2006 5:03 pm

Exctracting and showing only Legend

Post by Alan - Blue » Tue Mar 13, 2007 4:13 pm

I have got myself into a bit of a pickle, my charts allows users to select as many categories as they wish to the point that the chart image is taken up with just the legend.

So I have used the get legend function and put this into and chart image giving the users the ability to see the chart and the legend seperatly.

The problem I am having is with my second chart all i want to see is the legend I am adding to it. So I really would like to remove the plot is possible. Was thinking something along the lines of setting the size of the legendbox or making the plot transparent.

Any ideas?

Alan - Blue
Posts: 5
Joined: Wed Nov 15, 2006 5:03 pm

Post by Alan - Blue » Tue Mar 13, 2007 5:50 pm

am hiding the rest of the chart now by specifying the hieght of the chart absed on the number of items in the legend.

would be nice to have another way but this does it.

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 Mar 14, 2007 2:36 pm

You could use the LegendTitle's draw() method to draw the legend to any Graphics2D instance. So you could easily create a JPanel subclass that displays just a LegendTitle.
David Gilbert
JFreeChart Project Leader

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

robdocmagic
Posts: 7
Joined: Wed May 17, 2006 12:46 pm

Post by robdocmagic » Thu Mar 29, 2007 1:47 pm

Hi David,

Would you be so kind to write a short code snippet how to draw the legend on in a separate JPanel. I need to do this exact thing as well.

Thanks!

Edited to add: or if anyone else has accomplished this please post a snippet.

-Rob

hookumsnivy
Posts: 13
Joined: Mon Aug 14, 2006 9:14 pm

Post by hookumsnivy » Mon Jul 30, 2007 9:20 pm

There's another way to do it that I am currently working on:

Create a new implementation of the Plot class that does nothing in the draw methods (I didn't include the code).
This will obviously prevent the plot from being drawn.
Creating the Legend Only chart is then very straightforward:

Code: Select all

        JFreeChart legendChart = new JFreeChart("", null, 
                         new HiddenPlot(), 
                         false); //Hidden plot is a plot implementation that does not draw anything.

        LegendTitle legendTitle = new LegendTitle(<CHART_WITH_DATA>.getPlot(), 
                new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0),
                new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 0, 0));
        legendChart.addLegend(legendTitle);
Obviously you can set many more properties, but this is the basic concept I am using.
I decided to take this route instead of the JPanel approach, because I wanted to treat a Legend Only chart just like any other.

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 » Tue Jul 31, 2007 10:44 am

hookumsnivy wrote:I decided to take this route instead of the JPanel approach, because I wanted to treat a Legend Only chart just like any other.
I generally dislike "workarounds", but that one is quite clever.
David Gilbert
JFreeChart Project Leader

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

Locked