Trying to create custom Legend with Values from dataset

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
aram535
Posts: 25
Joined: Thu Apr 24, 2008 11:59 pm

Trying to create custom Legend with Values from dataset

Post by aram535 » Tue Jun 30, 2009 2:01 pm

Hi,

I'm trying to add a legend to this chart:

Image

Which would make it look like this excel graph:

Image

Any pointers would be greatly appreciated.

Aram

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Re: Trying to create custom Legend with Values from dataset

Post by RichardWest » Tue Jun 30, 2009 8:11 pm

This feature is not available in JFreeChart (I inquired about it a couple years ago). You could use a JTable to display the table below the frame. It is fairly easy to write a TableModel that queries the dataset and populates the table. The 'legend' is easily accomplished via a custom TableCellRenderer which queries the legend information from the plot.

If you want to be more fancy, you will need to customize the renderer logic to actually draw the table on the plot.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

aram535
Posts: 25
Joined: Thu Apr 24, 2008 11:59 pm

Re: Trying to create custom Legend with Values from dataset

Post by aram535 » Wed Jul 01, 2009 10:57 am

I'll have a look at JTable. Thank you.

aram535
Posts: 25
Joined: Thu Apr 24, 2008 11:59 pm

Re: Trying to create custom Legend with Values from dataset

Post by aram535 » Wed Jul 01, 2009 12:30 pm

Ok so now I have a JTable with the data, but I can't figure out how to render it into the chart. Anyone have any snippets they can share?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Trying to create custom Legend with Values from dataset

Post by paradoxoff » Thu Jul 02, 2009 8:02 am

aram535 wrote:Ok so now I have a JTable with the data, but I can't figure out how to render it into the chart. Anyone have any snippets they can share?
No code snippet, but some ideas...
1. Just place the JTable in the same container as the ChartPanel. But then, the JTable will not be part of the JFreeChart.
2. Write a wrapper around a JTable (or more general around a JComponent) that turns the JComponent into a Title. Inside the draw(Graphics2D titleG2, Rectangle2D titleRect) method of the Title, you can query the preferredSize of the JComponent, place it into the titleRect, and then call JComponent.paintComponent(titleG2). Simply place the JComponentTitle where it belongs.
3. You could try to use a CompositeTitle with a GridArrangement and add the LegendItemBlockContainers (which are the building blocks of a "normal" LegendTitle) and LabelBlocks to the GridArrangement. In this way, you could eliminate the JTable (well, finally at least a link to a code snippet.The missing methods of GridArrangement have been implemented in the meantime).
Finally, in order to keep the size of the data area constant, regardless of whether this (rather bulky) title is shown or not, you can try this patch.

aram535
Posts: 25
Joined: Thu Apr 24, 2008 11:59 pm

Re: Trying to create custom Legend with Values from dataset

Post by aram535 » Thu Jul 02, 2009 3:03 pm

Thank you. The wrapper idea works sadly the composition and the visual of the data is just not very pretty. I'll have to read more into JTable and JComponent to see what parameters exist in making them more visually appealing.

Thanks again.

willi.firulais
Posts: 15
Joined: Fri Jul 28, 2006 4:31 pm

Re: Trying to create custom Legend with Values from dataset

Post by willi.firulais » Tue Sep 15, 2009 7:49 pm

Hallo,
aram535 wrote:Ok so now I have a JTable with the data, but I can't figure out how to render it into the chart. Anyone have any snippets they can share?
I have a similar problem. Did you you succeed? If you have some code snippets to share, it would be great.

Thx, for your tips in this thread.

aram535
Posts: 25
Joined: Thu Apr 24, 2008 11:59 pm

Re: Trying to create custom Legend with Values from dataset

Post by aram535 » Tue Sep 15, 2009 7:54 pm

It sort of did, but I ended up having to pull it back out. it simply isn't dynamic enough if the data is not a known quantity.

Locked