源代码:
package com.sun.test; import java.io.FileOutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; public class BarChartDemo { public static void main(String[] args) { CategoryDataset dateset = getDateSet2(); JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", "水果", "产量", dateset, PlotOrientation.VERTICAL, true, false, false); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream("D:\\fruit.jpg"); ChartUtilities.writeChartAsJPEG(fos_jpg,0.5f, chart, 400, 300); } catch (Exception e) { e.printStackTrace(); } finally { try { fos_jpg.close(); } catch (Exception e) { e.printStackTrace(); } } } private static CategoryDataset getDateSet2() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(100, "上海", "苹果"); dataset.addValue(100, "广州", "苹果"); dataset.addValue(200, "北京", "梨子"); dataset.addValue(200, "上海", "梨子"); dataset.addValue(200, "广州", "梨子"); dataset.addValue(300, "北京", "葡萄"); dataset.addValue(300, "上海", "葡萄"); dataset.addValue(300, "广州", "葡萄"); dataset.addValue(400, "北京", "香蕉"); dataset.addValue(400, "上海", "香蕉"); dataset.addValue(400, "广州", "香蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(500, "广州", "荔枝"); return dataset; } }
症状:
导出图片后一堆方框,乱码,分析后认为没有设置字符集
解决办法:
并不需要设置字符集,只要设置字体也可以,用FONT类