Save Chart with small size.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Alphonse87
Posts: 9
Joined: Thu May 08, 2008 3:18 pm

Save Chart with small size.

Post by Alphonse87 » Tue Jun 10, 2008 9:56 am

Hi,

I want to create PDF with my chart. I use IText and I already did this for PDF with "normal" size ( higher than the size of my chart ) : I used JFreeChart.draw(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D area) and it works very well.

I have a problem when the size of the Rectangle2D is smaller than the size of the chart : the whole chart isn't drawn. I find another way to do this : I create a BufferedImage with JFreeChart.createBufferedImage(int width, int height) with bigger size and then I draw and resize this BufferedImage to get the right size with Graphics.drawImage(Image img, int x, int y, int width, int height, ImageObserver observer). The problem is that the quality of the PDF is not really satisfactory and really less good than with the first method.

I searched on my side but I couldn't find a better solution, so I'm looking for help if anyone has an idea ...

Thanks !

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

Re: Save Chart with small size.

Post by paradoxoff » Wed Jun 11, 2008 12:23 pm

Alphonse87 wrote:... "normal" size ( higher than the size of my chart ) : I used JFreeChart.draw(java.awt.Graphics2D g2, java.awt.geom.Rectangle2D area) and it works very well.

I have a problem when the size of the Rectangle2D is smaller than the size of the chart : the whole chart isn't drawn.
A JFreeChart doesn´t have a size, it tries its best to draw isetlf into any area that is provided in the draw-method.
Can you post a full code example?

Alphonse87
Posts: 9
Joined: Thu May 08, 2008 3:18 pm

Post by Alphonse87 » Wed Jun 11, 2008 4:56 pm

Well, the problem is that with images like 600*600, the legend and the title are really big and the chart is to small so I tried this :

Code: Select all

 final PdfTemplate tp = cb.createTemplate(widthChart, heightChart);
final Graphics2D g2 = tp.createGraphics(widthChart, heightChart, mapper);
            if ((widthChart > 700) && (heightChart > 700)) {
                final Rectangle2D r2D = new Rectangle2D.Double(0, 0, widthChart, heightChart);
                jFreeChart.draw(g2, r2D);
            } else {
                final int widthBufferedImage = 700;
                final int heightBufferedImage = (int) (widthBufferedImage * (heightChart / widthChart));

                final BufferedImage image = jFreeChart.createBufferedImage(widthBufferedImage,
                                          heightBufferedImage);

                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2.drawImage(image, 0, 0, (int) widthChart, (int) heightChart, null);
            }
            g2.dispose();
In this case, with jFreeChart.draw(g2, r2D), the quality is really good and I haven't any problem, but with g2.drawImage(image, 0, 0, (int) widthChart, (int) heightChart, null);, the quality is not really good. I tried with bigger widthBufferedImage and HeightBufferedImage, but I have mostly an OutOfMemory exception.

Locked