码迷,mamicode.com
首页 > 其他好文 > 详细

JFreeChart的简单使用

时间:2015-05-23 23:54:28      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

实例1:简单的饼图

public class Test {	
	public static void main(String[] args) {	
		//建立默认的饼图
		DefaultPieDataset ds=new DefaultPieDataset();
		ds.setValue("苹果",6000);
		ds.setValue("三星", 9000);
		ds.setValue("诺基亚",3200);
		ds.setValue("其他", 9000);
		
		//参数:1->标题 2-> 数据集 3->是否显示legend(在图的下方显示颜色块标注) 4->是否显示提示 5->图中是否存在url
		JFreeChart chart=ChartFactory.createPieChart("全球手机厂商出货量", ds, true, true, false);
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 30));
		chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 20));
		//PiePlot是图中饼图的上一级区域
		PiePlot plot=(PiePlot) chart.getPlot();
		plot.setLabelFont(new Font("宋体", Font.PLAIN, 20));
		//下面的标题是Frame的标题
		ChartFrame chartFrame=new ChartFrame("全球手机厂商出货量", chart);
		chartFrame.pack();
		chartFrame.setVisible(true);
	}
}

效果图:

技术分享

实例2:柱状图

public class Test2 {	
	public static void main(String[] args) {	
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.setValue(6000, "苹果","苹果");
		dataset.setValue(9000, "三星", "三星");
		dataset.setValue(3200, "诺基亚", "诺基亚");
		dataset.setValue(9000, "其他", "其他");
		
		JFreeChart chart=ChartFactory.createBarChart("全球手机厂商出货量", "手机厂商", "出货量(单位:万台)",dataset,PlotOrientation.VERTICAL,true,true,false);
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 30));
		chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 20));
		//CategoryPlot是图中饼图的上一级区域
		CategoryPlot plot=chart.getCategoryPlot();
		//设置纵坐标-->前者是外围标识(出货量) 后者是坐标标识(手机厂商)
		plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 20));
		plot.getRangeAxis().setTickLabelFont(new Font("宋体", Font.BOLD, 20));
		//设置横坐标
		plot.getDomainAxis().setLabelFont(new Font("宋体", Font.BOLD, 20));
		plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.BOLD, 20));
		
		ChartFrame chartFrame=new ChartFrame("全球手机厂商出货量", chart);
		chartFrame.pack();
		chartFrame.setVisible(true);
	}
}
效果图

技术分享

实例3:和Struts2的整合

1.导入strut2和JFreeChart的整合插件jar包

2.Action中为:public JFreeChart getChart(){}

3.Struts.xml文件中增加配置:

<action name="ChartOutputAction" class="chartOutputAction">
			<result name="success" type="chart">
				<param name="height">400</param>
				<param name="width">600</param>
			</result>
</action>

4.继承包

<package name="default" namespace="/" extends="struts-default,jfreechart-default">

JFreeChart的简单使用

标签:

原文地址:http://www.cnblogs.com/liuruowang/p/4525073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!