参考自:http://www.360doc.com/content/06/0330/15/7147_88686.shtml

一个使用CategoryDataset画分类柱图及分类折线图的例子:

CategoryChart的代码如下所示:

public class CategoryChart {

	public JFreeChart chart;
	private String title;
	private String categoryAxisLabel;
	private String valueAxisLabel;
	/**
	 * 分类关键字,如:"分类1","分类2"
	 */
	private String[] rowKeys;
	/**
	 * 某种分类类型中的值,如分类1中的"column1","column2"
	 */
	private String[] columnKeys;
	/**
	 * 每种分类对应的的填充数据
	 */
	private double[][] datas;
	private CategoryDataset dataset;

	/**
	 * 柱图
	 */
	public void createBarChart3D() {
		createCategoryDataset();
		chart = ChartFactory.createBarChart3D(title, // 图表标题
				categoryAxisLabel, // 目录轴的显示标签
				valueAxisLabel, // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向:水平、垂直
				true, // 是否显示图例(对于简单的柱状图必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
				);
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(new Color(238, 244, 255));// 设置图表的颜色
		chart.setBackgroundPaint(Color.WHITE);
		setChartFont("");

		ValueAxis rangeAxis = plot.getRangeAxis();
		// 设置最高的一个 Item 与图片顶端的距离
		rangeAxis.setUpperMargin(0.15);
		// 设置最低的一个 Item 与图片底端的距离
		rangeAxis.setLowerMargin(0.15);
		plot.setRangeAxis(rangeAxis);

		BarRenderer3D renderer = new BarRenderer3D();
		// 设置分类同一个columnKey的平行柱之间距离为0
		renderer.setItemMargin(0);
		// 显示每个柱的数值,并修改该数值的字体属性
		renderer
				.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelFont(new Font("楷体", Font.PLAIN, 12));
		renderer.setItemLabelsVisible(true);
		plot.setRenderer(renderer);
	}

	/**
	 * 折线图
	 */
	public void createLineChart() {
		createCategoryDataset();
		chart = ChartFactory.createLineChart(title, // 图表标题
				categoryAxisLabel, // 目录轴的显示标签
				valueAxisLabel, // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向:水平、垂直
				true, // 是否显示图例(对于简单的柱状图必须是false)
				false, // 是否生成工具
				false // 是否生成URL链接
				);
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(new Color(238, 244, 255));// 设置图表的颜色
		chart.setBackgroundPaint(Color.WHITE);
		setChartFont("");
	}

	/**
	 * 创建分类数据集
	 */
	private void createCategoryDataset(){
		//当没有数据时,创建一个默认的分类数据集用来画图
		if(rowKeys == null || columnKeys == null || datas == null){
			dataset = new DefaultCategoryDataset();
		}else{
			// 创建分类数据集
			dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys,
					datas);
		}
	}
	
	/**
	 * 图标乱码解决
	 */
	private void setChartFont(String charttype) {
		Font title = new Font("楷体", Font.PLAIN, 15);
		Font f = new Font("楷体", Font.PLAIN, 12);
		// title(标题),
		TextTitle textTitle = chart.getTitle();
		textTitle.setFont(title);

		// 获得图表区域对象
		if ("PieChart".equals(charttype)) {
			PiePlot plot = (PiePlot) chart.getPlot();
			plot.setLabelFont(f);
		} else if ("XYChart".equals(charttype)) {
			XYPlot plot = (XYPlot) chart.getPlot();
			ValueAxis valAxis = plot.getDomainAxis();
			// 设置X轴坐标上的文字
			valAxis.setTickLabelFont(f);
			// 设置X轴的标题文字
			valAxis.setLabelFont(f);
			ValueAxis numberaxis = plot.getRangeAxis();
			// 设置Y轴坐标上的文字
			numberaxis.setTickLabelFont(f);
			// 设置Y轴的标题文字
			numberaxis.setLabelFont(f);
		} else {
			CategoryPlot plot = chart.getCategoryPlot();
			CategoryAxis domainAxis = plot.getDomainAxis();
			// 设置X轴坐标上的文字
			domainAxis.setTickLabelFont(f);
			// 设置X轴的标题文字
			domainAxis.setLabelFont(f);

			ValueAxis numberaxis = plot.getRangeAxis();
			// 设置Y轴坐标上的文字
			numberaxis.setTickLabelFont(f);
			// 设置Y轴的标题文字
			numberaxis.setLabelFont(f);
		}
		// legend(图释)
		chart.getLegend().setItemFont(f);

	}
//setter和getter方法
//构造函数
}

 测试程序如下所示:

public class CategoryChartTest extends CategoryChart {

	private void createCategoryChart(String chartType){
		if("LineCategoryChart".equalsIgnoreCase(chartType)){
			createLineChart();
		}else if("BarCategoryChart".equalsIgnoreCase(chartType)){
			createBarChart3D();
		}
	}
	
	public void createTwoCategoryChart(String chartType){
		this.setTitle("全年的风速和风能频率分布图");
		this.setCategoryAxisLabel("风速(m/s)");
		this.setValueAxisLabel("频率/%");
		this.setRowKeys(new String[]{"风速","风能"});
		this.setColumnKeys(new String[]{"<0.5","1","2","3","4","5",">5"});
		double[][] datas = new double[][]{{22.3,12.2,10.0,3.2,14.0,21.3,5.6},{4.3,36.5,23.5,21.0,6.3,9.8,15.7}};
		this.setDatas(datas);
		createCategoryChart(chartType);
		
	}
	
	public void createThreeCategoryChart(String chartType){
		this.setTitle("水果销量统计图");
		this.setCategoryAxisLabel("水果");
		this.setValueAxisLabel("销量");
		double[][] datas = new double[][] {{672, 766, 223, 540, 126}, {325, 521, 210, 340, 106}, {332, 256, 523, 240, 526}};
		String[] rowKeys = {"苹果","梨子","葡萄"};
		String[] columnKeys = {"北京","上海","广州","成都","深圳"};
		this.setRowKeys(rowKeys);
		this.setColumnKeys(columnKeys);
		this.setDatas(datas);
		createCategoryChart(chartType);
	}
	
	public static void main(String[] args) {
		CategoryChartTest test = new CategoryChartTest();
		//test.createTwoCategoryChart("BarCategoryChart");  // 1
		//test.createTwoCategoryChart("LineCategoryChart");
		//test.createThreeCategoryChart("BarCategoryChart");
		test.createThreeCategoryChart("LineCategoryChart"); //4
		JFreeChart chart = test.getChart();
		ChartFrame frame = new ChartFrame("BarChart Tesing!", chart);
		frame.pack();
		frame.setVisible(true);
	}
}

下面列出分别调用代码1、代码4时所画的图形:

调用代码1所画图形如下所示:


JFreeChart的分类柱图及分类折线图
 调用代码4时所画的图形如下所示:


JFreeChart的分类柱图及分类折线图

 

相关文章: