jfreechart生成条形图,扇图

基本原理很简单,利用jfreechart生成对应图形的图片,然后在页面上显示出来。这种方式相对于flash组件形式最大的优点就是可以在所有平台上都正常显示。

前提:导入需要的2个jar文件,jcommon-版本号.jar,jfreechart-版本号.jar。

listCharts为自己需要显示数据的集合name为列名,count为列值。例如
Map<String, Object> map = null;
   
while (rs.next()) {
    
map = new HashMap<String, Object>();
    
map.put("name", rs.getString(1));
    
map.put("count", rs.getString(2));
    
list.add(map);
   }
///////////////正式代码:

if(chartsType.equals("Column3D")){
   DefaultCategoryDataset dataset=new DefaultCategoryDataset();
   for (int i = 0; i < listCharts.size(); i++) {
      Map<String, Object> rsMap = listCharts.get(i);
      String key = (String)rsMap.get("name");
      int value = Integer.parseInt((String) rsMap.get("count"));
      dataset.setValue(value,key,key);
				}
      JFreeChart chart=ChartFactory.createBarChart("hi", "","报警数(条)", dataset, PlotOrientation.VERTICAL, true, true, false); //创建一个JFreeChart
      if(alarmType.equals("alarmTypeAll")){
      chart.setTitle(new TextTitle("报警类型--报警信息统计图",new Font("宋体",Font.BOLD+Font.ITALIC,20)));//可以重新设置标题,替换“hi”标题
			 	}
      else{
	chart.setTitle(new TextTitle(""+alarmType+"--报警信息统计图",new Font("宋体",Font.BOLD+Font.ITALIC,20)));//可以重新设置标题,替换“hi”标题
			 	}
	 CategoryPlot plot = chart.getCategoryPlot();
	 CategoryAxis categoryAxis=plot.getDomainAxis();//获得横坐标
	categoryAxis.setLabelFont(new Font("微软雅黑",Font.BOLD,12));//设置横坐标字体
	 plot.setBackgroundPaint(Color.white); // 柱状图的背景
	plot.setRangeGridlinesVisible(true); // 横虚线是否可见
	 plot.setForegroundAlpha(0.65f); // 前北京透明度设置
	plot.setRangeGridlinePaint(Color.gray); // 设置横虚线的颜色
	//可以控制柱图的位置,但是不能把显示的数据置顶。
	LayeredBarRenderer renderer = new LayeredBarRenderer();
	//不可以控制柱图的位置,可以把显示的数据置顶。
	//BarRenderer renderer = new BarRenderer();
	renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示每个柱的数值 
	renderer.setBaseItemLabelsVisible(true); 
	renderer.setMaximumBarWidth(100); // 设置柱状的宽度
	renderer.setBase(0.01);
	renderer.setDrawBarOutline(true); // 设置边框是否可见
	renderer.setBaseOutlinePaint(Color.blue); // 设置边框的颜色
	renderer.setSeriesPaint(0, Color.blue); // 第一种柱状的颜色
	renderer.setSeriesPaint(1, Color.orange); // 第二种柱状的颜色
	renderer.setSeriesPaint(2, Color.gray); // 第三种柱状的颜色
	renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
	//偏移量
	renderer.setItemLabelAnchorOffset(-50.0f);
	// 这里设置柱状图 数值
	 renderer.setItemMargin(0.05D);
		     // 文字的设置
	CategoryAxis domainAxis = plot.getDomainAxis();
	//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
	 domainAxis.setVisible(true);
	plot.setDomainAxis(domainAxis);
	ValueAxis rAxis = plot.getRangeAxis();

	 // 设置标题的文字
	TextTitle textTitle = chart.getTitle();
	textTitle.setFont(new Font("宋体", Font.PLAIN, 12));
	// 设置X轴坐标上的文字
	domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 10));
		        
		        // 设置X轴的标题文字
	domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
		        // 设置Y轴坐标上的文字
	rAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 10));
		        // 设置Y轴的标题文字
	rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
		        
		        
	rAxis.setUpperMargin(0.1);//设置高度,能完全显示数据。

	domainAxis.setUpperMargin(0.1);// 左右两边靠边框
	domainAxis.setLowerMargin(0.1);
		        
	//domainAxis.setCategoryLabelPositionOffset(4);

	plot.setNoDataMessage("no data");
	plot.setNoDataMessagePaint(Color.blue);
		        
	//LayeredBarRenderer lbr = new LayeredBarRenderer();//(BarRenderer)类:
	// 给图添加呈现器
	renderer.setSeriesBarWidth(1, 0.5);//控制位置
	plot.setRenderer(renderer);
	int filename =(int)(Math.random()*10000);//产生0-1000的整数随机数  
	OutputStream os = new FileOutputStream("company.jpeg");//图片是文件格式的,故要用到FileOutputStream用来输出。
	ChartUtilities.saveChartAsJPEG(new File("C:\\apache-tomcat-6.0.35\\webapps\\Crt\\jfreepic\\"+filename+".jpeg"), chart, 1000, 400); 
	//使用一个面向application的工具类,将chart转换成JPEG格式的图片。第3个参数是宽度,第4个参数是高度。
	os.close();//关闭输出流
	//使用一个面向application的工具类,将chart转换成JPEG格式的图片。第3个参数是宽度,第4个参数是高度。
	JSONArray jArray = new JSONArray();
	JSONObject jObject = new JSONObject();
	jObject.put("fimename",filename);
	jArray.add(jObject);
	System.out.print(jArray.toString());
	PrintWriter out;
	try {
		out = response.getWriter();
		out.write(jArray.toString());
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	else if(chartsType.equals("Pie3D")){//扇形图
		DefaultPieDataset dpd=new DefaultPieDataset(); //建立一个默认的饼图
			 for (int i = 0; i < listCharts.size(); i++) {
					Map<String, Object> rsMap = listCharts.get(i);
					String key = (String)rsMap.get("name");
					int value = Integer.parseInt((String) rsMap.get("count"));
					dpd.setValue(key,value);  //输入数据
				}
		JFreeChart chart = null;
		if(alarmType.equals("alarmTypeAll")){
			chart=ChartFactory.createPieChart("报警类型--报警信息统计图",dpd,true,true,false); 
		}
		else{
			chart=ChartFactory.createPieChart(""+alarmType+"--报警信息统计图",dpd,true,true,false); 
		}
			
		//chart.setTitle(new TextTitle("报警信息统计图",new Font("微软雅黑",Font.BOLD,12)));
		LegendTitle legend=chart.getLegend(0);//设置Legend
	         legend.setItemFont(new Font("宋体",Font.BOLD,14));
	         PiePlot plot=(PiePlot) chart.getPlot();//设置Plot
	         plot.setLabelFont(new Font("隶书",Font.BOLD,16));
	         plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
	         OutputStream os = new FileOutputStream("company.jpeg");//图片是文件格式的,故要用到FileOutputStream用来输出。
	         int filename =(int)(Math.random()*10000);//产生0-1000的整数随机数  
	         ChartUtilities.saveChartAsJPEG(new File("C:\\apache-tomcat-6.0.35\\webapps\\Crt\\jfreepic\\"+filename+".jpeg"), chart, 1000,400); 
	         //使用一个面向application的工具类,将chart转换成JPEG格式的图片。第3个参数是宽度,第4个参数是高度。
	         os.close();//关闭输出流
}

猜你喜欢

转载自775265430.iteye.com/blog/2212243