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);