Sunday, September 26, 2010

JFreeChart in Firefox

   If you work with Pentaho via Firefox you probably upset with a quality of charts. Flash charts look just great. I'm talking here about JFree charts. This issue affects Firefox only! IE works a bit better in this case.

  JFreeChart
   JFreeChart have several advantages over Flash Charts. JFreeChart is a java library that produces images in various formats: gif, jpeg, png. As a result these charts can be incorporated into PDF, Excel and RTF reports. Second advantage comes from Apple - Adobe Flash war. Flash charts will not work on iPhone and iPad. So there are some reasons to use JFreeChart.

  Blurred Charts
   The case when charts in Pentaho are blurred is common enough. I designed a report with a chart in it with the latest Pentaho Report Designer 3.6.1, placed it on a dashboard, where it produced in HTML format, and added pdf export button. When I open my dashboard in Firefox the chart looks blurred. This is mainly concern to any text that is on the chart. Ok, you can understand these charts, but this is not a thing your customer will like. On the other hand charts exported to PDF looks very neat. Also when opened with Internet Explorer charts looks nice.

  How to fix it?
   The problem here is in the way Firefox scaling images. Chart images are usually in high quality, and their size can be twice larger then the place for a chart on html page. So Firefox forced to downscale these images. And here, for the blame of Firefox, it can't do that nice. Firefox just don't have a good image scaling algorithm. You can check a bug, which wasn't fixed at the time of this writing.
But the good news is that Firefox have a "bad" images scaling algorithm that can correctly downscale our chart! I'll explain it:
<img> HTML element have a property image-rendering. In Firefox this property can be set to any of 5 values: auto, inherit, optimizeSpeed, optimizeQuality, -moz-crisp-edges. Currently only two rendering algorithms are used. Bilinear resampling for values: auto and optimizeQuality. Nearest neighbor resampling for optimizeSpeed and -moz-crisp-edges values. And here is a trick: Nearest neighbor resampling works better to images with sharp edges.
So all you need is just add following style for all chart images:
<style type="text/css">
img {
    image-rendering: -moz-crisp-edges
}
</style>
This is how looks blurred image(you will see blurring in Firefox only):



And here is how looks sharp image(for me it's looks even better then in IE):


Sharp image can be rendered with problems if you changed zoom in a browser.

  Why pentaho can't fix it?
   Ok, I don't know. May be it consider this bug as not important. But it is certainly a disadvantage, which may scare some potential customers. JasperReports don't have this issue, however I don't sure which way they overcome it.


4 comments:

  1. Maybe this bug isn't a priority because they will start focusing on using a better charting solution like protovis...?

    ReplyDelete
  2. Protovis is a javascript library, and there is no straight way to generate images for PDF reports using it. It can be used in bundle with JFreeChart, using ChartBeans for example, but it not easy to make them work togather. It's much cheaper to fix the bug then do all these integrations.

    ReplyDelete
  3. The image scaling is a problem between the various size systems. Raster images are pixel based, while text is point based. Both units have no fixed relationship (see CSS3-Units standard for details) and the ratio between the two of them differs between systems and browsers.

    We resort to rendering all images under the assumption that the display is working at 96dpi and specify the image as point unit (so that they scale with the font sizes). Browser who then actually use a 96dpi rendering work fine. Browser who choose a arbitrary mapping are less fine, but as a server application we cannot guess what the user eventually will use to view and how the user's local system is configured.

    Note: Windows uses a 96dpi by default for its displays, Linux uses 100dpi. You can change the DPI value for the images via the report-configuration.

    I have not heard of that workaround yet - I cant fix anything that is not reported as bug or feature request against Pentaho Reporting's JIRA system. Adding a style entry for each image is easy and could have been added at any time.

    I do not have time monitoring the Firefox bug database. If something annoys you in the way we handle stuff, file a JIRA case.

    Regards,
    Thomas Morgner,
    Reporting bugger ;)

    ReplyDelete
  4. Hi Thomas,

    I'm glad to see you at my blog. I like reading yours and highly appreciate your work on Pentaho reporting.

    Actually I've reported a JIRA issue, it's here:
    http://jira.pentaho.com/browse/PRD-2839.
    However I didn't insist on fixing it because I don't sure if it will work fine on all platforms and browsers.

    ReplyDelete