JFreeChart生成柱状图的类

来源:互联网 发布:linux监视局域网流量 编辑:程序博客网 时间:2024/04/29 21:26

根据公司数据分析的需求, 花了很长时间研究了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);
 }
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 3岁儿童口吃怎么办 流产了怀不上宝宝怎么办 产妇脸上长斑怎么办 宝贝肚子涨气怎么办 七个月宝宝便秘怎么办 宝宝功能性便秘怎么办 苹果x手机卡了怎么办 宝宝不吃水果蔬菜怎么办 小宝宝不拉粑粑怎么办 小宝宝个子不高怎么办 孩子不开口说话怎么办 孩子吹空调发烧怎么办 孩子上学不想上怎么办 孩子上学想留级怎么办 孩子口算太慢怎么办 孩子浮躁没耐心怎么办 如果孩子打妈妈怎么办 孩子英语成绩不好怎么办 小孩说话发音不准怎么办 小孩gk发音不准怎么办 大学无英语基础怎么办 孩子应用题理解不透怎么办 嫉妒一个人好痛苦怎么办 爱嫉妒别人怎么办心理 总有人嫉妒我怎么办 从小嫉妒闺蜜怎么办 怕闺蜜比我好怎么办 孩子不会做应用题怎么办 当你嫉妒别人怎么办 单位社保发票丢失怎么办 单位医保发票丢失怎么办 图书明细没有给怎么办 拼音拼不到一起怎么办 孩子不会拼拼音怎么办 小孩子不会拼拼音怎么办 小孩拼音发音不准怎么办 孩子声调不会标怎么办 孩子拼音发音不准怎么办 孩子学拼音不会怎么办? 孩子不会拼生字怎么办? 对数字不敏感怎么办