[align=left][size=large]初学JFreeChart,和大家交流。[/size][/align]
public class BarChartDemo {
/**
* @param args
*/
public static void main(String[] args) {
CategoryDataset dataset = getDataSet();
JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", //图标题
"水果", //目录轴的显示标签
"产量", //数值轴的显示标签
dataset,
PlotOrientation.VERTICAL,//图表方向:水平,垂直
true, //是否显示图例
false, //是否生成工具
false);//是否生成URL链接
//设置图标题的字体
Font font = new Font("黑体",Font.HANGING_BASELINE,20);
TextTitle title = new TextTitle("水果产量图");
// BarRenderer3D render = new BarRenderer3D();
// render.setBaseItemLabelFont(new Font("黑体",Font.BOLD,12));//12号黑体加粗
//设置图例中的字体
LegendTitle legend = chart.getLegend();
legend.setItemFont(new Font("宋体",Font.BOLD,16));
//横轴
CategoryPlot plot = (CategoryPlot) chart.getPlot();
// CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
// plot.setRenderer(render);//使用我们设计的效果
CategoryAxis cateaxis = plot.getDomainAxis();
cateaxis.setLabelFont(new Font("粗体",Font.BOLD,16));
//设置横轴的标签
cateaxis.setTickLabelFont(new Font("粗体",Font.BOLD,16));
//纵轴
NumberAxis numaxis = (NumberAxis)plot.getRangeAxis();
numaxis.setLabelFont(new Font("宋体",Font.BOLD,16));
chart.setTitle(title);
FileOutputStream fos_jpg = null ;
try {
fos_jpg = new FileOutputStream("D:\\fruit.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 500, 400,null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}finally{
try {
fos_jpg.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static CategoryDataset getDataSet(){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "苹果");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(300, "南京", "香蕉");
dataset.addValue(400, "广州", "葡萄");
dataset.addValue(500, "海南", "橘子");
return dataset ;
}
private static CategoryDataset getDataSet2() {
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;
}
}
public class BarChartDemo {
/**
* @param args
*/
public static void main(String[] args) {
CategoryDataset dataset = getDataSet();
JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", //图标题
"水果", //目录轴的显示标签
"产量", //数值轴的显示标签
dataset,
PlotOrientation.VERTICAL,//图表方向:水平,垂直
true, //是否显示图例
false, //是否生成工具
false);//是否生成URL链接
//设置图标题的字体
Font font = new Font("黑体",Font.HANGING_BASELINE,20);
TextTitle title = new TextTitle("水果产量图");
// BarRenderer3D render = new BarRenderer3D();
// render.setBaseItemLabelFont(new Font("黑体",Font.BOLD,12));//12号黑体加粗
//设置图例中的字体
LegendTitle legend = chart.getLegend();
legend.setItemFont(new Font("宋体",Font.BOLD,16));
//横轴
CategoryPlot plot = (CategoryPlot) chart.getPlot();
// CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
// plot.setRenderer(render);//使用我们设计的效果
CategoryAxis cateaxis = plot.getDomainAxis();
cateaxis.setLabelFont(new Font("粗体",Font.BOLD,16));
//设置横轴的标签
cateaxis.setTickLabelFont(new Font("粗体",Font.BOLD,16));
//纵轴
NumberAxis numaxis = (NumberAxis)plot.getRangeAxis();
numaxis.setLabelFont(new Font("宋体",Font.BOLD,16));
chart.setTitle(title);
FileOutputStream fos_jpg = null ;
try {
fos_jpg = new FileOutputStream("D:\\fruit.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 500, 400,null);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}finally{
try {
fos_jpg.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static CategoryDataset getDataSet(){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "苹果");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(300, "南京", "香蕉");
dataset.addValue(400, "广州", "葡萄");
dataset.addValue(500, "海南", "橘子");
return dataset ;
}
private static CategoryDataset getDataSet2() {
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;
}
}