版本1:jfreechart-1.0.9
生成圖檔代碼如下:
Java代碼

- ChartUtilities.writeChartAsJPEG(out, chart, 500, 400);
得到的圖檔顯示如下,出現不正常的紅色背景
jfreechart-1.0.9生成圖檔各主要代碼如下:
JFreeChart類
Java代碼

- public BufferedImage createBufferedImage(int width, int height, ChartRenderingInfo info)
- {
- return createBufferedImage(width, height, 2, info);
- }
- public BufferedImage createBufferedImage(int width, int height, int imageType, ChartRenderingInfo info)
- {
- BufferedImage image = new BufferedImage(width, height, imageType);
- Graphics2D g2 = image.createGraphics();
- draw(g2, new Rectangle2D.Double(0.0D, 0.0D, width, height), null, info);
- g2.dispose();
- return image;
- }
如上所述,建立BufferedImage的時候,使用的iamgeType是2,也就是BufferedImage.TYPE_INT_ARGB格式。
解決方法一:手動設定imageType為1(即BufferedImage.TYPE_INT_RGB),如下代碼:
Java代碼

- BufferedImage bi = chart.createBufferedImage(500, 400, 1, null);
- ImageIO.write(bi, "jpeg", out);
解決方法二:更新jfreechart到1.0.10,該版本中JFreeChart中的createBufferedImage代碼如下:
Java代碼

- public static void writeChartAsJPEG(OutputStream out, float quality, JFreeChart chart, int width, int height, ChartRenderingInfo info)
- throws IOException
- {
- if (chart == null) {
- throw new IllegalArgumentException("Null 'chart' argument.");
- }
- BufferedImage image = chart.createBufferedImage(width, height, 1, info);
- EncoderUtil.writeBufferedImage(image, "jpeg", out, quality);
- }
1.0.10版本在調用JFreeChart的createBufferedImage預設使用了imageType為BufferedImage.TYPE_INT_RGB
調整後生成的圖檔如下: