views:

376

answers:

1

Hi All,

We're using JFreeChart to build an engine to display graphs. This is a web service that runs on Tomcat + Java 1.5.0, and renders charts to PNGs and JPEGs (using ChartUtilities.writeChartAs{PNG,JPEG}() ).

We've run into a problem where JFreeChart seems to scale everything inside the Plot area, but only by a few pixels. The result is that the graph looks inconsistent, e.g.:

  • Minor ticks are sometimes stretched horizontally, so that they seem to be two pixels wide instead of one.
  • We use a small image in the top-right of the plot area as a watermark. This is stretched by one pixel horizontally and vertically somewhere near (but not exactly) its middle.
  • Background grid lines seem to appear on sub-pixel boundaries. I have not found a way to create an accurately dotted grid line.

We have tried both 1.0.9 and 1.0.13, with exactly the same results (except for the minor ticks, which were not available in the older version). Also, rendering the image to a Frame instead of JPEG/PNG produced an identical result.

Help is greatly appreciated, in advance :)

EDIT: An SSCCE:

@Test
public void testScaling1() throws InterruptedException {

    // Load Image:
    Component dummy = new Component() {};
    MediaTracker tracker = new MediaTracker(dummy);
    Image img = Toolkit.getDefaultToolkit().getImage("C:\\My\Image.gif");
    tracker.addImage(img, 0);
    tracker.waitForAll();

    // Build Data set and base chart.
    TimeSeriesCollection dataset = new TimeSeriesCollection();

    TimeSeries ts = new TimeSeries("Sample");
    ts.add(new Second(0, 0, 0, 1, 1, 1900), 1.0);
    ts.add(new Second(1, 0, 0, 1, 1, 1900), 3.0);
    ts.add(new Second(2, 0, 0, 1, 1, 1900), 4.0);
    ts.add(new Second(3, 0, 0, 1, 1, 1900), 2.0);

    dataset.addSeries(ts);

    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "blabla",
            null,
            null,
            dataset,
            true,
            true,
            false
    );

    // Add BG image in top-right corner.
    XYPlot xy = chart.getXYPlot();
    xy.setBackgroundAlpha(0.0F);
    xy.setBackgroundImage(img);
    xy.setBackgroundImageAlignment(Align.NORTH_WEST);
    xy.setBackgroundImageAlpha(1.0F);

    paintChart(chart);

}

Use an image with small-font text, or a grid. This will show the scaling effect on the background image.

Edit 2: We've resorted to subclassing or proxying Renderers and drawing the label in text in their drawItem() (or similar) methods. This works well. However, minor ticks are now a problem - they also seem to be getting scaled. E.g.: See the 9th and 15th ticks.

look at the bottom

+1  A: 

I am unable to reproduce the effect you describe using either saveChartAsJPEG() or writeChartAsPNG() with version 1.0.13, Java 1.5, Mac OS X, in code like this:

try {
    ChartUtilities.writeChartAsPNG(new FileOutputStream(
        new File("test.png")), chart, 600, 400);
} catch (IOException ex) {
    ex.printStackTrace();
}

Does the screen exhibit the same artifacts? What happens when you change the WIDTH and HEIGHT parameters or omit the watermark? Are you using special fonts with unusual metrics? Have you tried a different platform?

You can run TimeSeriesChartDemo1 as follows:

java -cp jfreechart-1.0.13.jar:jcommon-1.0.16.jar org.jfree.chart.demo.TimeSeriesChartDemo1

Mac OS 10.5.8, Java 1.5.0_24, JFreeChart 1.0.13, TimeSeriesDemo1, using saveChartAsPNG(), ImageIO.read() and setBackgroundImage(). setBackgroundImageAlignment(Align.NORTH_WEST) is a little funky, though.

alt text

trashgod
1. The problem repeats at all widths and heights.2. The watermark's (non)presence does not change anything - I still see scaling artifacts.3. Not specifying fonts does not change the situation.4. I've run this on several Windows and Linux machines.I shall try to put up a minimal piece of code to demonstrate this.
Alex Arnon
Interesting. It appear OK on Ubuntu, as shown above.
trashgod
trashgod: Are you speaking of TimeSeriesDemo1, or my example above (testScaling1())?
Alex Arnon
`TimeSeriesDemo1` on Ubuntu, pictured above using `saveChartAsPNG()`.
trashgod
Image updated. I any of this helpful?
trashgod
trashgod: Thank you for the effort, but unfortunately this is not all that helpful, since the plot area does not contain any of the elements that undergo scaling like I said above.We have since resolved our problems by using text labels.
Alex Arnon