package com.ice.jfreechart.utils;
import java.awt.Font;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
/**
*
*
* 项目名称:jfreeChart study
* 类名称:CreateChart
* 类描述:
* 创建人:xuhanbing
* 创建时间:2012-3-12 下午01:47:00
* 修改人:xuhanbing
* 修改时间:2012-3-12 下午01:47:00
* 修改备注:
* @version 1.0
*
*/
public class CreateChart {
/**
* 生成饼状图
* @param title
* @param pieDataset
* @return
*/
public static JFreeChart creatChartPie(String title,DefaultPieDataset pieDataset){
//创建pieChart对象
JFreeChart chart = ChartFactory.createPieChart3D(
title,//饼状标题
pieDataset,//数据
true,//说明
true,//提示
false//地址
);
//设置字体
setPieFont(chart);
return chart;
}
/**
* 设置饼状图字体
* @param chart
*/
public static void setPieFont(JFreeChart chart){
chart.getTitle().setFont(new Font("黑体",Font.ITALIC,18));//设置标题
PiePlot piePlot = (PiePlot) chart.getPlot();//获取绘图对象
piePlot.setLabelFont(new Font("宋体",Font.ITALIC,12));//设置标签字体
chart.getLegend().setItemFont(new Font("宋体",Font.ITALIC,12));//说明
}
/**
* 生成柱状图
* @param title
* @param categoryAxisLabel
* @param valueAxisLabel
* @param categoryDataset
* @return
*/
public static JFreeChart creatChartBar(String title,String categoryAxisLabel,String valueAxisLabel,DefaultCategoryDataset categoryDataset){
//创建pieChart对象
JFreeChart chart = ChartFactory.createBarChart3D(
title,//柱状图标题
categoryAxisLabel,//目录轴
valueAxisLabel,//数值轴
categoryDataset,//数据
PlotOrientation.VERTICAL,//显示方向
true,//说明
true,//提示
false//地址
);
//设置字体
setBarFont(chart);
return chart;
}
/**
* 设置柱状图字体
* @param chart
*/
public static void setBarFont(JFreeChart chart){
chart.getTitle().setFont(new Font("黑体",Font.ITALIC,18));//设置标题
chart.getLegend().setItemFont(new Font("宋体",Font.ITALIC,12));//说明
CategoryPlot plot = chart.getCategoryPlot();//获取绘图对象
CategoryAxis3D categoryAxis3D = (CategoryAxis3D) plot.getDomainAxis();//获取横轴
categoryAxis3D.setTickLabelFont(new Font("宋体",Font.ITALIC,12));//标记标签
categoryAxis3D.setLabelFont(new Font("宋体",Font.ITALIC,12));//横轴标签
NumberAxis3D numberAxis3D = (NumberAxis3D) plot.getRangeAxis();//获取纵轴
numberAxis3D.setLabelFont(new Font("宋体",Font.ITALIC,12));//纵轴标签
}
/**
* 生成折线图
* @param title
* @param categoryAxisLabel
* @param valueAxisLabel
* @param dataset
* @return
*/
public static JFreeChart creatChartLine(String title,String categoryAxisLabel,String valueAxisLabel,DefaultCategoryDataset dataset){
//创建pieChart对象
JFreeChart chart = ChartFactory.createLineChart3D(
title, //折线图标题
categoryAxisLabel, //目录轴
valueAxisLabel, //数值轴
dataset, //数据
PlotOrientation.VERTICAL, //显示方向
true, //说明
true, //提示
false//地址
);
//设置字体
setLineFont(chart);
return chart;
}
/**
* 设置折线图字体
* @param chart
*/
public static void setLineFont(JFreeChart chart){
chart.getTitle().setFont(new Font("黑体",Font.ITALIC,18));//设置标题
chart.getLegend().setItemFont(new Font("宋体",Font.ITALIC,12));//说明
CategoryPlot plot = chart.getCategoryPlot();//获取绘图对象
CategoryAxis3D categoryAxis3D = (CategoryAxis3D) plot.getDomainAxis();//获取横轴
categoryAxis3D.setTickLabelFont(new Font("宋体",Font.ITALIC,12));//标记标签
categoryAxis3D.setLabelFont(new Font("宋体",Font.ITALIC,12));//横轴标签
NumberAxis3D numberAxis3D = (NumberAxis3D) plot.getRangeAxis();//获取纵轴
numberAxis3D.setLabelFont(new Font("宋体",Font.ITALIC,12));//纵轴标签
}
}