How to disable scaling in JFreeChart?

十年热恋 提交于 2019-12-12 13:02:00

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2904863/how-to-disable-scaling-in-jfreechart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!