legend as separate image

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
maspark
Posts: 1
Joined: Thu Nov 15, 2007 5:22 pm

legend as separate image

Post by maspark » Thu Nov 15, 2007 5:39 pm

Very pleased with my first use of JFreeChart, I have very quickly developed some on demand graphs deployed via a provider servlet.

I am dynamically generating an XYLineChart but I ran into a problem when I had many data series (say > 100), in that the legend swamped the graph.

I could adjust the size of the image using:

ChartUtilities.writeChartAsPNG(out, chart, width, height);

but it didn't seem easy to work out how big the graph was as well as the
legend.

I tried the following solution 1) generate the chart without a legend, write out a PNG 2) call the same code again, extract just the legend (from the chart) and write out a new PNG. I looked for examples on this forum, but don't really have enough experience with this type of graphics to know what I am doing wrong.

The code to do step 2) is as follows but I just get a black (or white) rectangle - no legend - anyone else done this?

Thanks Mark

Code: Select all

 response.setContentType("image/png");
                                               
                LegendTitle legend=new LegendTitle(chart.getPlot());
                
                int someHeight = (int) legend.getHeight();
                int someWidth  = (int)legend.getWidth();
                
                BufferedImage image = new BufferedImage(someWidth, someHeight, BufferedImage.SCALE_DEFAULT);
                Graphics2D g2 = image.createGraphics();
                Rectangle2D chartArea = new Rectangle2D.Double(1, 1, someWidth, someHeight);
                legend.draw(g2,chartArea,null);
                ChartUtilities.writeBufferedImageAsPNG(out, image); 

pjbell
Posts: 1
Joined: Fri Dec 28, 2007 12:57 pm

Legend too big on StackedBarChart3D graph

Post by pjbell » Fri Dec 28, 2007 1:01 pm

I'm also having a similiar problem using StackedBarChart3D. I need to have a legend but it swamps the graph making it hard to view.

Ideally i would like to extract the legend and use it on another dialog to popup on demand for the user.

Is this possible?

Thanks

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

Re: legend as separate image

Post by BigDong » Fri May 31, 2013 2:07 pm

I am also interested by a solution.

Stimpy
Posts: 2
Joined: Thu Jun 20, 2013 8:55 am
antibot: No, of course not.

Re: legend as separate image

Post by Stimpy » Thu Jun 20, 2013 9:14 am

I am also interested in a solution. I need the code, that generated 'Price Volume Chart Demo' this one leaves out several months in the legend, but their values seem to be drawn in the chart. How to skip labels that are drawn next to the x-axis ? I also have a simmilar chart, with 2 Values axises, and my x-axis is also time. It works all fine as long as the values for each day do not overlapp. But unfortunatley the do, when i want to draw more than 60 datasets. I dont want to skip datasets, i just want to hide every second label?
:?:

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: legend as separate image

Post by John Matthews » Thu Jun 20, 2013 5:47 pm

One possible approach is cited here.

Locked