determining legend height

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
alfonso

determining legend height

Post by alfonso » Mon Sep 19, 2005 2:39 pm

I am making a timechart from previously unknown data. Sometimes there are 2 series, sometimes there are 20. Is it somehow possible to get the legend height? I got 0.0 anytime. I want to keep the same chart dimensions, independent form the legend height. it should be like that:

chart_height = DEFINED_CHART_HEIGHT + LEGEND_HEIGHT

but how to determine legend height?

ChartUtilities.saveChartAsPNG(..., chart, chart_width, chart_height).

Can anyone help?

ps. I am using jFreeChart 1.0rc

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 » Mon Sep 19, 2005 4:25 pm

It is generally not very easy to do this, because JFreeChart was designed to draw everything within the space allocated to it. You might be able to hack something using the arrange() method in the LegendTitle class (which returns the legend size, for given constraints). Once you know the legend size, you could increase the height of the chart by that amount. In theory that should work...I don't know what problems you'll run into in practice.
David Gilbert
JFreeChart Project Leader

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

alfonso

Post by alfonso » Tue Sep 20, 2005 9:36 am

Thank you very much for quick reply.

Code: Select all

Size2D size = chart.getLegend().arrange((Graphics2D) bfi.getGraphics(),
                     new RectangleConstraint(chartDimX, chartDimY));
No problems encountered.

Code: Select all

size.height
gives exact height of the legend, so I managed to solve my problem.

Best Regards,
alfonso

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 Sep 20, 2005 10:37 am

Great! I'm glad it worked.
David Gilbert
JFreeChart Project Leader

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

BigDong
Posts: 7
Joined: Tue Jul 24, 2012 4:46 pm
antibot: No, of course not.

Re: determining legend height

Post by BigDong » Fri May 31, 2013 5:02 pm

Hey alfonso I am also interested by your code

What's bfi in : (Graphics2D) bfi.getGraphics() ?

How did you cast/convert your JFreeChart img to a graphics2d ?

Locked