Alex Arnon
Alex Arnon

Reputation: 113

How to disable scaling in JFreeChart?

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.:

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 http://img14.imageshack.us/img14/3625/76676732.jpg

Upvotes: 3

Views: 3012

Answers (1)

trashgod
trashgod

Reputation: 205875

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

Upvotes: 1

Related Questions