JFreeChart生成柱状图的类

根据公司数据分析的需求, 花了很长时间研究了JFreeChart生成柱状图的类,如下是一个通用生成柱状图的类。如有问题,可以留言或QQ: devid_pitoushi@Live.cn  

package com.mycompany.helper.common.service;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;

/**
 * 统计图表类(采用JFreeChart控件)
 */
public class StatChart {
 
 /**
  * 得到生成的图表的路径
  * @throws IOException
  * data: 用于显示的数据; rowKeys: (Y轴)图例;
  * columnKeys: (X轴); totalTitle: 总标题;
  * xTitle: x轴标题; yTitle: y轴标题; direction: 图显示的方向
  * width: 宽; height: 高
  */
 public static String getChartURL(HttpServletRequest request, HttpSession session,
   double[][] data, String[] rowKeys, String[] columnKeys,
   String totalTitle, String xTitle, String yTitle, String direction,
   int width, int height)
   throws IOException {
  //数据集
  CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
    rowKeys, columnKeys, data);
  
  //图表的工厂
  JFreeChart chart = null;
  if(direction == "HORIZONTAL") {
   
   //chart = ChartFactory.createBarChart(totalTitle, xTitle,
     //yTitle, dataset, PlotOrientation.HORIZONTAL, true, true, false);
   chart = ChartFactory.createBarChart3D(totalTitle, xTitle,
     yTitle, dataset, PlotOrientation.HORIZONTAL, true, true, false);
  } else {
   chart = ChartFactory.createBarChart3D(totalTitle, xTitle,
     yTitle, dataset, PlotOrientation.VERTICAL, true, true, true);
   
  }
  chart.setBackgroundPaint(Color.white);
  
  //设置属性
  setAttribute(chart);
  //得到文件名
  String filename = ServletUtilities.saveChartAsPNG(chart, width, height,
    null, session);
  String graphURL = request.getContextPath()
    + "/DisplayChart?filename=" + filename;
  return graphURL;
 }
 
 /**
  * 设置字体
  */
 public static void setAttribute(JFreeChart chart) {
  /*------------设置文字操作--------------*/
  CategoryPlot plot = chart.getCategoryPlot();//获得图表区域对象
  CategoryAxis domainAxis = plot.getDomainAxis();
  domainAxis.setVisible(true);
  plot.setDomainAxis(domainAxis);
  ValueAxis rAxis = plot.getRangeAxis();

  //设置标题的文字
  TextTitle textTitle = chart.getTitle();
  textTitle.setFont(new Font("黑体", Font.PLAIN, 14));
  //设置X轴坐标上的文字
  domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
  //设置X轴的标题文字
  domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
  //设置Y轴坐标上的文字
  rAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
  //设置Y轴的标题文字
  rAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));


  //CategoryPlot plot = chart.getCategoryPlot();
  //设置网格背景颜色
  plot.setBackgroundPaint(Color.white);
  //设置网格竖线颜色
  plot.setDomainGridlinePaint(Color.decode("#799AE1"));
  //设置网格横线颜色
  plot.setRangeGridlinePaint(Color.decode("#799AE1"));
  plot.setBackgroundAlpha(0.9f);
  //显示每个柱的数值,并修改该数值的字体属性
  BarRenderer3D renderer = new BarRenderer3D();
  renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
  renderer.setBaseItemLabelsVisible(true);
  renderer.setBaseFillPaint(Color.decode("#799AE1"));
  renderer.setMaximumBarWidth(0.03);     //设置柱状图的width
  
  //renderer.setItemLabelPaint(Color.decode("#799AE1"));
  //默认的数字显示在柱子中,通过如下两句可调整数字的显示
  //注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
  renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
    ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
  renderer.setItemLabelAnchorOffset(10D);
  //设置每个地区所包含的平行柱的之间距离
  //renderer.setItemMargin(0.3);
  plot.setRenderer(renderer);
  //设置地区、销量的显示位置
  //将下方的“”放到上方
  plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
  //将默认放在左边的“”放到右方
  plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
  plot.setWeight(100);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值