jfreechart-立体统计图


        最近信用办项目要将有关统计的平面柱状图改为立体柱状图,用了jfreechart。简单例子,分享一下。


1,首先,引入IText-2.1.5.jar jcommon-1.0.17.jar jfreechart-1.0.14.jar

2,代码

柱状图:

<span style="font-family:KaiTi_GB2312;font-size:18px;">import java.awt.Font;
import java.io.File;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class AppBar {
	
	public static void main(String[] args) throws Exception {
		DefaultCategoryDataset ds = new DefaultCategoryDataset();
		ds.addValue(2000, "IBM", "一季度");
		ds.addValue(2300, "ORACLE", "一季度");
		ds.addValue(2800, "JBOSS", "一季度");
		ds.addValue(3300, "用友", "一季度");
		
		ds.addValue(4800, "IBM", "二季度");
		ds.addValue(4300, "ORACLE", "二季度");
		ds.addValue(3200, "JBOSS", "二季度");
		ds.addValue(1800, "用友", "二季度");
		
		ds.addValue(1500, "IBM", "三季度");
		ds.addValue(2600, "ORACLE", "三季度");
		ds.addValue(3900, "JBOSS", "三季度");
		ds.addValue(2100, "用友", "三季度");
		
		String title = "前三季度各大公司JEE AS销量统计" ;
		JFreeChart chart = ChartFactory.createBarChart3D(title, "季度", "销量(单位:万台)", ds, PlotOrientation.VERTICAL, true, false, false);
		
		//中文
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 25));//大标题
		
		//提示条
		chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
		
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		//域轴字体
		plot.getDomainAxis().setLabelFont(new Font("宋体", Font.BOLD, 18));
		plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 15));//小标签字体
		
		//range
		plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));
		
		plot.setForegroundAlpha(0.6f);
		
		ChartUtilities.saveChartAsJPEG(new File("f:\\bar.jpg"), chart, 800, 500);
	}
}
</span>

运行结果:



饼图:

<span style="font-family:KaiTi_GB2312;font-size:18px;">import java.awt.Font;
import java.io.File;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class App {
	
	public static void main(String[] args) throws Exception {
		//
		String title = "各大公司JEE AS市场占有率统计" ;
		DefaultPieDataset ds = new DefaultPieDataset();
		ds.setValue("IBM", 2000);
		ds.setValue("ORACLE", 3500);
		ds.setValue("JBOSS", 1570);
		ds.setValue("用友", 4400);
		JFreeChart chart = ChartFactory.createPieChart3D(title, ds, true, false, false);
		
		//中文
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 25));//标题字体
		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 18));
		
		//绘图区
		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setLabelFont(new Font("宋体", Font.PLAIN, 15));
		
		//背景
		//chart.setBackgroundImage(ImageIO.read(new File("f:/sunset.jpg")));//图表区背景
		//plot.setBackgroundImage(ImageIO.read(new File("f:/water.jpg")));
		
		//设置分裂效果  二维支持 ,三维无此效果
		plot.setExplodePercent("IBM", 0.1f);
		plot.setExplodePercent("JBOSS", 0.2f);
		
		//设置前景色透明度
		plot.setForegroundAlpha(0.7f);
		
		//设置标签生成器
		//{0}:公司名称
		//{1}:销量
		//{2}:百分比
		//{3}:总量
		//{4}:
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}/{3}-{2})"));
		ChartUtilities.saveChartAsJPEG(new File("f:\\pie.jpg"), chart, 800, 500);
	}
}
</span>


折线图:

<span style="font-family:KaiTi_GB2312;font-size:18px;">import java.awt.Font;
import java.io.File;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class AppLine {
	
	public static void main(String[] args) throws Exception {
		DefaultCategoryDataset ds = new DefaultCategoryDataset();
		ds.addValue(2000, "IBM", "一季度");
		ds.addValue(2300, "ORACLE", "一季度");
		ds.addValue(2800, "JBOSS", "一季度");
		ds.addValue(3300, "用友", "一季度");
		
		ds.addValue(4800, "IBM", "二季度");
		ds.addValue(4300, "ORACLE", "二季度");
		ds.addValue(3200, "JBOSS", "二季度");
		ds.addValue(1800, "用友", "二季度");
		
		ds.addValue(1500, "IBM", "三季度");
		ds.addValue(2600, "ORACLE", "三季度");
		ds.addValue(3900, "JBOSS", "三季度");
		ds.addValue(2100, "用友", "三季度");
		
		String title = "前三季度各大公司JEE AS销量统计" ;
		JFreeChart chart = ChartFactory.createLineChart(title, "季度", "销量(单位:万台)", ds, PlotOrientation.VERTICAL, true, false, false);
		
		//中文
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 25));//大标题
		
		//提示条
		chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));
		
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		//域轴字体
		plot.getDomainAxis().setLabelFont(new Font("宋体", Font.BOLD, 18));
		plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 15));//小标签字体
		
		//range
		plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));
		
		plot.setForegroundAlpha(0.6f);
		
		ChartUtilities.saveChartAsJPEG(new File("f:\\line.jpg"), chart, 800, 500);
	}
}</span>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值