JFreeChart解决中文乱码问题(柱型)

package com.asiainfo.JfreeChartTestv1;

import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.AbstractRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class JfreeChartTest {

/**
* @param args
*/
public static void main(String[] args) {
CategoryDataset dataset = getCategoryDataset2();
JFreeChart jFChart = ChartFactory.createBarChart("水果产量图",
"水果种类", "水果产量(吨)", dataset,
PlotOrientation.VERTICAL, true, false, false);
TextTitle tt = jFChart.getTitle();
Font ft = new Font("SimSun", 12, 12);
tt.setFont(ft);
LegendTitle leTitle = jFChart.getLegend();
leTitle.setItemFont(ft);
Axis axis = jFChart.getCategoryPlot().getDomainAxis();
Axis axis2 = jFChart.getCategoryPlot().getRangeAxis();
axis2.setLabelFont(ft);
axis.setLabelFont(ft);
axis.setTickLabelFont(ft);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("d:\\fruit.jpg");
try {
ChartUtilities.writeChartAsJPEG(fos, 1f, jFChart, 400, 300);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static CategoryDataset getCategoryDataset2() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(300,"苹果","苹果");
dataset.addValue(200, "梨", "梨");
dataset.addValue(400,"桃","桃");
return dataset;
}

}

你可能感兴趣的:(java,jfreechart)