`
太阳神喻
  • 浏览: 105227 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

JFreeChart柱状图

阅读更多

 	/**
	 * 柱状图
	 * @param dataset
	 * @param title 
	 * @param xLabel 
	 * @param yLabel 
	 */
	private JFreeChart setBarChart(CategoryDataset dataset, String title, String xLabel, String yLabel) {
		JFreeChart chart = ChartFactory.createBarChart3D(title, xLabel, yLabel, dataset,  PlotOrientation.VERTICAL,  true,  false,  false);
		chart.getTitle().setFont(titleFont);// 设置标题字体
		chart.setBackgroundPaint(bgColor); // 设置背景色 
		CategoryPlot plot = chart.getCategoryPlot();
		// 设置图表的纵轴和横轴
		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setLowerMargin(0.1);// 设置距离图片左端距离此时为10%
		domainAxis.setUpperMargin(0.1);// 设置距离图片右端距离此时为百分之10
		domainAxis.setCategoryLabelPositionOffset(10);// 图表横轴与标签的距离(10像素)
		domainAxis.setCategoryMargin(0.2);// 横轴标签之间的距离20%
		domainAxis.setMaximumCategoryLabelLines(2);
		domainAxis.setLabelFont(font);
		domainAxis.setTickLabelFont(font);
		// 设定柱子的属性
		ValueAxis rangeAxis = plot.getRangeAxis();
		rangeAxis.setUpperMargin(0.1);// 设置最高的一个柱与图片顶端的距离(最高柱的10%)
		rangeAxis.setLabelFont(font);
		rangeAxis.setTickLabelFont(font);
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//Y轴显示整数
		rangeAxis.setAutoRangeMinimumSize(1);   //最小跨度
		rangeAxis.setLowerBound(0);   //最小值
		rangeAxis.setAutoRange(false);   //不自动分配Y轴数据
		// 设置图表的颜色
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setSeriesPaint(0, Color.MAGENTA);// 第一柱子的颜色
		renderer.setSeriesOutlinePaint(0, Color.WHITE);// 边框颜色
		renderer.setSeriesPaint(1, Color.BLUE);// 第二柱子的颜色
		renderer.setSeriesOutlinePaint(1, Color.WHITE);// 边框颜色
		renderer.setSeriesPaint(2, Color.RED);// 第三柱子的颜色
		renderer.setSeriesOutlinePaint(2, Color.WHITE);// 边框颜色
		renderer.setItemMargin(0.1);// 组内柱子间隔为组宽的10%
		renderer.setBaseLegendTextFont(font);
		//显示每个柱的数值,并修改该数值的字体属性
        renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setItemLabelFont(new Font("黑体",Font.BOLD,12));//12号黑体加粗
        renderer.setItemLabelPaint(Color.YELLOW);//字体颜色
        renderer.setItemLabelsVisible(true);
        plot.setRenderer(renderer);//使用我们设计的效果

		plot.setRenderer(renderer);// 使用我们设计的效果
		plot.setForegroundAlpha(0.6f);// 设置柱的透明度
		// 设置纵横坐标的显示位置
		plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
		plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
		
		plot.setNoDataMessage("无对应的数据,请重新查询。");
		plot.setNoDataMessageFont(titleFont);//字体的大小
	    plot.setNoDataMessagePaint(Color.RED);//字体颜色

	    return chart;
	}

 

		//模拟数据
		Random r = new Random();
		List groupList = new ArrayList();
		for (int i = 0; i < 50; i++) {
			Object[] objs = new Object[4];
			objs[0] = "第"+(r.nextInt(5)+1)+"模块";
			objs[1] = r.nextInt(50)+20;
			objs[2] = r.nextInt(30)+10;
			objs[3] = r.nextInt(30)+10;
			groupList.add(objs);
		}
//		String hql = "select s.modulename,count(s.id),count(distinct s.userip),count(distinct s.userid) from Statistic s group by s.modulename";
//		List groupList = //查数据库
		DefaultCategoryDataset dpd = new DefaultCategoryDataset();
		if (groupList != null && groupList.size() != 0) {
			for (Object object : groupList) {
				Object[] objs = (Object[]) object;
				dpd.setValue((Number) objs[1], "点击数", objs[0].toString());
				dpd.setValue((Number) objs[2], "IP数", objs[0].toString());
				dpd.setValue((Number) objs[3], "用户数", objs[0].toString());
			}
		}
		
		JFreeChart chart =jt.setBarChart(dpd,"图片标题","x轴说明","y轴说明");

 

使用方法和上一篇的饼图一样.

  • 大小: 27.8 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics