用jfreechart画柱状图

用jfreechart画柱状图的程序,图片命名为chart.jpg,保存在了webroot的images文件夹下
 
                // 创建一个柱状图
		  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		  // 装载数据
		  dataset.setValue(6, "Profit", "Jane");
		  dataset.setValue(3, "Profit2", "Jane");
		  dataset.setValue(7, "Profit", "Tom");
		  dataset.setValue(6, "Profit2", "Tom");
		  dataset.setValue(8, "Profit", "Jill");
		  dataset.setValue(9, "Profit2", "Jill");
		  dataset.setValue(5, "Profit", "John");
		  dataset.setValue(8, "Profit2", "John");
		  dataset.setValue(12, "Profit", "Fred");
		  dataset.setValue(11, "Profit2", "Fred");  
		  
		  // 产生柱状图
		  // JFreeChart chart = ChartFactory.createXYLineChart("标题","x轴标志","y轴标志","设置数据","设置图形显示方向",是否显示图形,是否进行提示,是否配置报表存放地址);
		  // 图形显示方向:
		  // (1)HORIZONTAL:横向显示图形
		  // (2)VERTICAL:纵向显示图形
		  // 3D柱状图
		  JFreeChart chart = ChartFactory.createBarChart3D("销售统计图","Salesman", "Profit", dataset, PlotOrientation.VERTICAL,true, true, false);
		  
		  // 设置总的背景颜色
		  chart.setBackgroundPaint(ChartColor.yellow);
		  // 设置标题颜色
		  chart.getTitle().setPaint(ChartColor.blue);
		  // 获得图表对象
		  CategoryPlot p = chart.getCategoryPlot();
		  // 设置图的背景颜色
		  p.setBackgroundPaint(ChartColor.black);
		  // 设置表格线颜色
		  p.setRangeGridlinePaint(ChartColor.red);
		  try{   
		//   // 创建图形显示面板
		//   ChartFrame cf = new ChartFrame("柱状图",chart);
		//   cf.pack();
		//   // 设置图片大小
		//   cf.setSize(500,300);
		//   // 设置图形可见
		//   cf.setVisible(true);
		   
		    // 保存图片到指定文件夹
	              ServletContext con=this.getServletContext();
		      String basePath = con.getRealPath("/");
	             //System.out.println(basePath+"images\\chart.jpg");
		    ChartUtilities.saveChartAsJPEG(new File(basePath+"images\\chart.jpg"), chart, 500, 300);
		  } catch (Exception e){
		   System.err.println("Problem occurred creating chart.");
		  }
		
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值