JFreeChart生成柱形图

效果图

先获取一个组合数据集对象

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//double value, Comparable rowKey,Comparable columnKey
dataset.addValue(value,rowKey, Comparable columnKey);

然后创建一个JFreeChart

	/**
	 * @param dataset 数据集
	 * @param title 图表标题
	 * @param time 目录轴的显示标签
	 */
	public File exportExcel(DefaultCategoryDataset dataset,String title,String time){
		// 设置数据区域
		JFreeChart chart = ChartFactory.createBarChart(title // 图表标题
                    , time // 目录轴的显示标签
                    , "" // 数值轴的显示标签
                    , dataset // 数据集
                    , PlotOrientation.VERTICAL // 图表方向:HORIZONTAL 水平、VERTICAL 垂直
                    , true // 是否显示图例
                    , true // 是否生成工具
                    , true );// 是否生成URL链接
		iSetBarChart(chart);//设置柱状图的样式
		CategoryPlot cp = chart.getCategoryPlot();
		// 背景色设置
		cp.setBackgroundPaint(ChartColor.WHITE);
		cp.setRangeGridlinePaint(ChartColor.GRAY);
		//将JFreeChart对象输出到文件,Servlet输出流等
		return saveAsFile(chart, 
                    "mail/Echart/" +title+ ".png" //图片名以及路径
                    ,  1200, 400);//长宽
	}

设置柱形图样式

       /**
	 * 设置柱状图的样式
	 * @param chart
	 */
	public void iSetBarChart(JFreeChart chart) {
		CategoryPlot categoryplot = chart.getCategoryPlot();// 图本身
		ValueAxis rangeAxis = categoryplot.getRangeAxis();
		CategoryAxis domainAxis = categoryplot.getDomainAxis();
		// 设置Y轴的提示文字样式
		rangeAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		// 设置Y轴刻度线的长度
		rangeAxis.setTickMarkInsideLength(10f);
		rangeAxis.setLowerMargin(0.1);//数据轴下(左)边距 
		rangeAxis.setUpperMargin(0.1);//数据轴上(右)边距 
		// rangeAxis.setTickMarkOutsideLength(10f);
		// 设置X轴下的标签文字
		domainAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		// 设置X轴上提示文字样式
		domainAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));
		domainAxis.setUpperMargin(0.05);//分类轴上(右)边距 
		domainAxis.setLowerMargin(0.005);//分类轴下(左)边距 
		NumberAxis vn = (NumberAxis) categoryplot.getRangeAxis();

		// 设置Y轴的数字为百分比样式显示
		DecimalFormat df = new DecimalFormat("0.0%");
		vn.setNumberFormatOverride(df);
		// 使柱状图反过来显示
		/* vn.setInverted(true);
		 vn.setVerticalTickLabels(true);*/

		// 自定义柱状图中柱子的样式
		BarRenderer brender = new BarRenderer();
		brender.setSeriesPaint(0, Color.decode("#009B4C")); // 给series1 Bar	 绿色  正常
		brender.setSeriesPaint(1, Color.decode("#E62129")); // 给series2 Bar 红色 异常
		brender.setSeriesPaint(2, Color.decode("#8B8B8C")); // 给series4 Bar	 灰色  离线
		brender.setSeriesPaint(3, Color.decode("#F08619")); // 给series3 Bar 橙色  待机
		brender.setSeriesPaint(4, Color.decode("#E0CB03")); // 给series5 Bar 休息
		brender.setSeriesPaint(5, Color.decode("#00BF00")); // 给series6 Bar
		// 设置柱状图的顶端显示数字
		brender.setIncludeBaseInRange(true);
		brender.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		brender.setDefaultItemLabelsVisible(true);
		// 设置柱子为平面图不是立体的
		brender.setBarPainter(new StandardBarPainter());
		// 设置柱状图之间的距离0.1代表10%;
		brender.setItemMargin(0.1);
		// 设置柱子的阴影,false代表没有阴影
		brender.setShadowVisible(false);
		brender.setItemLabelAnchorOffset(0);//设置距离右边距距离
		/*如果数值没有显示空间,设置显示格式   */
		ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition(
				ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT,0);
		brender.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
		brender.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
		// 设置图的背景为白色
		categoryplot.setBackgroundPaint(Color.WHITE);
		// 设置背景虚线的颜色
		categoryplot.setRangeGridlinePaint(Color.decode("#B6A2DE"));
		// 去掉柱状图的背景边框,使边框不可见
		categoryplot.setOutlineVisible(false);
		// 设置标题的字体样式
		chart.getTitle().setFont(new Font("微软雅黑", Font.PLAIN, 20));
		// 设置图表下方图例上的字体样式
		chart.getLegend().setItemFont(new Font("微软雅黑", Font.PLAIN, 12));

		categoryplot.setRenderer(brender);
	}

将JFreeChart对象输出到文件

	// 保存为文件
	public File saveAsFile(JFreeChart chart, String outputPath,int weight, int height) {
		FileOutputStream out = null;
		File outFile =null;
		try {
			outFile = new File(outputPath);
			if (!outFile.getParentFile().exists()) {
				outFile.getParentFile().mkdirs();
			}
			out = new FileOutputStream(outputPath);
			// 保存为PNG
			ChartUtils.writeChartAsPNG(out, chart, weight, height);
			// 保存为JPEG 注意与上面的命名对应
			// ChartUtilities.writeChartAsJPEG(out, chart, weight, height);
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					// do nothing
				}
			}
		}
		return outFile;
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值