package com.icss.jfreechart.reports;
import java.awt.Color;
import java.awt.Font;
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.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
/**
* 该类用于最简单的柱状图生成
* @author Administrator
*
*/
public class TestJfreeChart {
public static void main(String[] args){
CategoryDataset dataset = getDataSet2();
JFreeChart chart = ChartFactory.createBarChart3D(
"水果产量图",//图表标题
"水果",//目录轴的显示标签
"产量",//数值轴的显示标签
dataset,//数据集
PlotOrientation.VERTICAL,//图表方向:水平、垂直
true,//是否显示图例(对于简单的柱状图必须是false)
false,//是否生成工具
false);//是否生成URL链表
Font titleFont = new Font("黑体", Font.BOLD, 20);
TextTitle textTitle = chart.getTitle();
textTitle.setFont(titleFont);// 为标题设置上字体
// Font plotFont = new Font("宋体", Font.PLAIN, 16);
// PiePlot plot = (PiePlot) chart.getPlot();
// plot.setLabelFont(plotFont); // 为饼图元素设置上字体
// Font plotFont = new Font("宋体", Font.PLAIN, 16);
// PiePlot plot = (PiePlot) chart.getPlot();
// plot.setLabelFont(plotFont); // 为饼图元素设置上字体
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis domainAxisTemp = plot.getDomainAxis();
domainAxisTemp.setLabelFont(new Font("黑体", Font.CENTER_BASELINE, 24));
//设置X轴各项标题的字体
domainAxisTemp.setTickLabelFont(new Font("黑体", Font.ROMAN_BASELINE, 12));
domainAxisTemp.setTickLabelPaint(Color.BLUE);
//Y轴字体
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(new Font("黑体", Font.ROMAN_BASELINE, 12));
Font LegendFont = new Font("楷体", Font.PLAIN, 18);
LegendTitle legend = chart.getLegend(0);
legend.setItemFont(LegendFont);// 为图例说明设置字体
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\aa\\jfreechart.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg, 0.7f, chart, 400, 300,null);
// ChartUtilities.writeChartAsJPEG(fos_jpg, 100, chart, 400, 300,null);//
/**
* 第二参数加上就不能生成图片,第二参数的范围:
* ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,400,300,null);
* 问题出在第二个参数.它的值范围是0.0f to 1.0f
*/
} catch (Exception e) {
try {
fos_jpg.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/**
* 获取一个演示用的简单数据集对象
*/
private static CategoryDataset getDataSet(){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "", "苹果");
dataset.addValue(200, "", "梨子");
dataset.addValue(300, "", "葡萄");
dataset.addValue(400, "", "香蕉");
dataset.addValue(500, "", "荔枝");
return dataset;
}
/**
* 获取一个演示用的组合数据对象
* @return
*/
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;
}
}