Jump to content
We've recently updated our Privacy Statement, available here ×

Problem of chart rendering quality in XLS, PPTX,... exports + solution


phgaudin

Recommended Posts

Hi all,

By default the quality of the rendered charts is too bad to be usable when a JasperReport is exported in XLS, PPT,...

I found a possible solution in www.jasperforge.org/plugins/espforum/view.php but the problem with this solution is that all charts are rendered as bitmap images, even when exported to PDF.

I needed a solution that allows the chart to be redered in :

  • a "vector" format (ie DRAW) when exported to PDF
  • a good quality bitmap when exported to XLS, PPT,...

The solution I found (inspired by the one of rckrll106) is to enhance the getImageData() of the net.sf.jasperreports.engine.JRAbstractSvgRenderer class :


 public byte[] getImageData() throws JRException {
    Dimension2D dimension = getDimension();
    if (dimension != null) {
      int imageDPI = JRProperties.getIntegerProperty("net.sf.jasperreports.svgrenderer.chart.dpi", 72);
      double scale = ((double) imageDPI) / 72d;      byte imageType = getImageType();
      BufferedImage bi =
          new BufferedImage(
              (int) (scale * dimension.getWidth()),
              (int) (scale * dimension.getHeight()),
              // avoid creating JPEG images with transparency that would result
              // in invalid image files for some viewers (browsers)
              (imageType == JRRenderable.IMAGE_TYPE_GIF || imageType == JRRenderable.IMAGE_TYPE_PNG)
                  ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB
          );      Graphics2D g = createGraphics(bi);
      g.scale(scale, scale);
      Color backcolor = getBackcolor();
      if (backcolor != null) {
        g.setColor(backcolor);
        g.fillRect(0, 0, (int) (scale * dimension.getWidth()), (int) (scale * dimension.getHeight()));
      }
      render(g, new Rectangle((int) dimension.getWidth(), (int) dimension.getHeight()));
      g.dispose();      return JRImageLoader.loadImageDataFromAWTImage(bi, getImageType());
    }
    return null;
  }


You can then change the quality of the rendered charts by adding - in the jasperreports.properties - the net.sf.jasperreports.svgrenderer.chart.dpi property and setting it to (for example) 200.

What do you think about this solution ?

Is there any chance to integrate it in one of the next releases of JasperReports ?

Regards,

Phil



Post Edited by phgaudin at 05/25/2011 05:58
Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi,

 

While we are indeed aware of the chart quality in XLS, PPT and the other formats in which we cannot draw vectorial graphics, I don't agree with you that the quality is bad also in PDF.

If you take a look at the charts in the /demo/samples/charts samples shipped with the JR project distro, you can see the charts are rendered vectorial in PDF. I have attached a copy of one of the PDF here for your convenience. You can zoom into the chart as much as you want and you'll not notice any degradation of quality.

If in your case, the charts don't have quality in PDF, then you should attach the JRXML here so that we see how the charts are created.

 

As for the suggestion about configuring resolution using a custom JR property, we'll probably work on that for an upcoming release.

 

Thank you,

Teodor

 

Link to comment
Share on other sites

Hi Teodor,

First of all, thanks for your reply /tools/fckeditor/editor/images/smiley/msn/wink_smile.gif !

I do not complain at all about the quality of the charts in PDF ! They are great when rendered in vectorial.

My only issue is about charts in XLS, PPT,... which are rendered as bitmaps.

The solution I found in the forum message I refered had a drawback : with that solution all charts are rendered as bitmaps, even when exported in PDF

The solution I suggested allows, from the same JasperPrint, to export the JasperPrint :

  • to PDF as vectorial (with optimal quality),
  • to XLS, PPT,... as bitmap with the possibility to change the "resolution" of the generated bitmaps (by changing a property)

... so that you get best of both worlds !

I think that the code provided in the first message of this thread could be used as a - hopefully - good starting point...

Kind regards,

Phil

Link to comment
Share on other sites

Hi,

A new JR configuration property named "net.sf.jasperreports.image.dpi" is now available globally and can be used to specify the DPI resolution for images, when rasterized during report export.

By default, the property has the value 72.

This has been committed to SVN and will be part of the next release.

Thank you,
Teodo

 

Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...