Struts2结合Jfreechart实现数据报表统计(二)

2014-11-24 07:14:44 · 作者: · 浏览: 3
or=set.iterator();itor.hasNext();){ String key=itor.next(); dpd.setValue(key, map.get(key)); } return dpd; } /** * 设置饼状图样式 * @param chart */ private static void setPieStyle(JFreeChart chart){ PiePlot plot = (PiePlot) chart.getPlot(); Font font = new Font("宋体", Font.ITALIC, 12); chart.getTitle().setFont(new Font("宋体", Font.BOLD, 22)); //方块距离饼的距离 只要负值就能把数据放到饼里 plot.setLabelFont(font); //去掉label //plot.setLabelGenerator(null); //在label上显示百分比 //plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}")); //如果百分比要显示一位小数 //plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}",new DecimalFormat("0.0"),new DecimalFormat("0.0%"))); //在label上显示实际数值 //plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}")); //是否显示线 fasle就不显示了 //plot.setLabelLinkMargin(-0.8); //plot.setLabelLinksVisible(false); //有时我们想突出某一个来显示,需要加入下面一句:第一个参数是key,第二个参数是突出显示的大小(可以自己调整一下看看效果就明白了) //plot.setExplodePercent("城管强拆",0.23); //设置相应的数据显示效果 //StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1},{2})", new DecimalFormat("0.0"), new DecimalFormat("0.0")); StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1},{2})", new DecimalFormat("0.0"), new DecimalFormat("0.0%")); plot.setLabelGenerator(standarPieIG); //没有数据的时候所显示的内容 plot.setNoDataMessage("没有相应的数据显示"); chart.getLegend().setItemFont(font); }


如果我们需要创建3D样式的饼状图,只需要修改其样式即可:

\

	/**
	 * 设置饼状图3D样式
	 * @param chart
	 */
	private static void setPie3DStyle(JFreeChart chart){
		//获得3D的水晶饼图对象 
		PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot(); 
		//设置开始角度 
		pieplot3d.setStartAngle(150D); 
		//设置方向为”顺时针方向“ 
		pieplot3d.setDirection(Rotation.CLOCKWISE); 
		//设置透明度,0.5F为半透明,1为不透明,0为全透明 
		pieplot3d.setForegroundAlpha(0.5F); 
		pieplot3d.setNoDataMessage("没有相应的数据显示"); 
		Font font = new Font("宋体", Font.ITALIC, 12);
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 22));
		pieplot3d.setLabelFont(font);
		chart.getLegend().setItemFont(font);
		StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1},{2})", new DecimalFormat("0.0"), new DecimalFormat("0.0%"));
		pieplot3d.setLabelGenerator(standarPieIG); 
	}


2)创建柱状图(显示内容为单个数据):

\

    /**
     * 创建柱状图
     * @param title 标题
     * @param XLabel X轴标签
     * @param YLabel Y轴标签
     * @param map 键值对
     * @return
     */
	public static JFreeChart createBarChart(String title,String XLabel,String YLabel,Map
  
    map){
		DefaultCategoryDataset dataset=createBarDataSet(map);
		//创建一个柱状图(图表标题,X轴显示标签,Y轴显示标签,数据集,图表方向(水平or垂直),是否显示图例[对于简单图应为false],是否生成工具,是否生成url链接)
		JFreeChart chart = ChartFactory.createBarChart3D(title,XLabel, YLabel, dataset, PlotOrientation.VERTICAL, false, false, false);
		setBarStyle(chart);
		return chart;
	}

	/**
	 * 创建柱状图数据
	 * @param list
	 * @return
	 */
	private static DefaultCategoryDataset createBarDataSet(Map
   
     map){ DefaultCategoryDataset dataset = new DefaultCategoryDataset(); Set
    
      set=map.keySet(); for(Iterator
     
       itor=set.iterator();itor.hasNext();){ String key=itor.next(); dataset.addValue(map.get(key), "", key); } return dataset; } /** * 设置柱状图样式 * @param chart */ private static void setBarStyle(JFreeChart chart){ CategoryPlot plot=chart.getCategoryPlot();