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.