package com.point.jfsc.boss.web.report.jfreechart;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
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.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
/**
* 该类用于演示最简单的柱状图生成
* @author Winter Lau
*/
public class BarChartDemo {
public static void main(String[] args) {
//得到数据,比如从数据库查询
CategoryDataset dataset = getDataSet2();
//构造chart
JFreeChart chart = ChartFactory.createBarChart3D(
"水果产量图", // 图表标题
"水果", // 目录轴的显示标签
"产量", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、
true, // 是否显示图例(对于简单的柱状图必须
false, // 是否生成工具
false // 是否生成URL链接
);
//处理chart中文显示问题
processChart(chart);
//chart输出图片
writeChart2Image(chart);
//chart 以swing形式输出
ChartFrame pieFrame = new ChartFrame("公司组织家", chart);
pieFrame.pack();
pieFrame.setVisible(true);
}
/**
* 获取一个演示用的组合数据集对象
* @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;
}
/**
* 解决图表汉字显示问题
* @param chart
*/
private static void processChart(JFreeChart chart ){
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
ValueAxis rAxis = plot.getRangeAxis();
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
chart.getLegend().setItemFont(new Font("宋体",Font.PLAIN,12));
// renderer.setItemLabelGenerator(new LabelGenerator(0.0));
// renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));
// renderer.setItemLabelsVisible(true);
}
/**
* 输出图片
* @param chart
*/
private static void writeChart2Image(JFreeChart chart){
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("D:\\fruit.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,1,chart,400,300,null);
}catch(Exception e){
e.printStackTrace();
} finally {
try {
fos_jpg.close();
} catch (Exception e) {}
}
}
}