ChartsUtil jfreeChart 帮助类

package com.viewlinecn.tvsysmanager.test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;

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.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;

/**
 *
 * @author <a href="mailto:linzheyan@viewlinecn.com">林哲炎</a> created in
 *         2008-12-26
 *
 * web图表:柱状图、饼图和曲线图辅助类
 */
public class ChartsUtil {
 private static final Font CHART_FONT = new Font("黑体", 12, 12);

 /**
  * 2D饼图
  *
  * @param title
  *            图形的标题
  * @param dataset
  *            数据集对象
  * @param legend
  *            显示标题
  * @param tooltips
  *            启用热键
  * @param urls
  *            启用超键接
  * @return
  */
 public static JFreeChart get2DPieChart(String title,
   DefaultPieDataset dataset, boolean legend, boolean tooltips,
   boolean urls) {
  JFreeChart chart = ChartFactory.createPieChart(title, dataset, legend,
    tooltips, urls);
  PiePlot plot = (PiePlot) chart.getPlot();
  String unitSytle = "{0}={1}({2})";

  plot.setNoDataMessage("无对应的数据,请重新查询。");
  plot.setNoDataMessagePaint(Color.red);
  // 引出标签显示样式
  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,
    NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

  // 图例显示样式
  plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
    unitSytle, NumberFormat.getNumberInstance(), new DecimalFormat(
      "0.00%")));
  return chart;
 }

 /**
  * 3D饼图
  *
  * @param title
  * @param dataset
  * @return
  */
 public static JFreeChart get3DPieChart(String title,
   DefaultPieDataset dataset, boolean legend, boolean tooltips,
   boolean urls) {
  JFreeChart chart = ChartFactory.createPieChart3D(title, dataset,
    legend, tooltips, urls);
  PiePlot plot = (PiePlot) chart.getPlot();
  String unitSytle = "{0}={1}({2})";

  plot.setNoDataMessage("无对应的数据,请重新查询。");
  plot.setNoDataMessagePaint(Color.red);

  // 指定 section 轮廓线的厚度(OutlinePaint不能为null)
  plot.setOutlineStroke(new BasicStroke(0));

  // 设置第一个 section 的开始位置,默认是12点钟方向
  plot.setStartAngle(90);

  plot.setToolTipGenerator(new StandardPieToolTipGenerator(unitSytle,
    NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

  // 指定图片的透明度 0.5F为半透明,1为不透明,0为全透明
  plot.setForegroundAlpha(1);

  // 引出标签显示样式
  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,
    NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

  // 图例显示样式
  plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
    unitSytle, NumberFormat.getNumberInstance(), new DecimalFormat(
      "0.00%")));
  return chart;
 }

 /**
  * 垂直柱状图(比较型)
  *
  * @param title
  *            图形的标题
  * @param xAxis
  *            x轴标题
  * @param yAxis
  *            y轴标题
  * @param dataset
  *            数据集对象
  * @param legend
  *            显示标题
  * @param tooltips
  *            启用热键
  * @param urls
  *            启用超键接
  * @return
  */
 public static JFreeChart get3DBarChart(String title, String xAxis,
   String yAxis, CategoryDataset dataset, boolean legend,
   boolean tooltips, boolean urls) {
  JFreeChart chart = ChartFactory.createBarChart3D(title, xAxis, yAxis,
    dataset, PlotOrientation.VERTICAL, legend, tooltips, urls);

  CategoryPlot plot = chart.getCategoryPlot();

  // 设置网格背景颜色
  plot.setBackgroundPaint(Color.white);
  // 设置网格竖线颜色
  plot.setDomainGridlinePaint(Color.pink);
  // 设置网格横线颜色
  plot.setRangeGridlinePaint(Color.pink);
  // 显示每个柱的数值,并修改该数值的字体属性
  BarRenderer3D renderer = new BarRenderer3D();
  renderer
    .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
  renderer.setBaseItemLabelsVisible(true);
  // 默认的数字显示在柱子中,通过如下两句可调整数字的显示
  // 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
  renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
    ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
  renderer.setItemLabelAnchorOffset(10D);
  // 设置每个地区所包含的平行柱的之间距离
  // renderer.setItemMargin(0.3);
  plot.setRenderer(renderer);
  // 设置地区、销量的显示位置

  // 将x轴显示在上方
  plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

  // 将y轴显示在右方
  plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
  
  
  CategoryAxis domainAxis = plot.getDomainAxis(); //获取X 轴
      
  //下面设置解决中文乱码问题
  domainAxis.setLabelFont(CHART_FONT); //设置X轴的标题文字
  domainAxis.setTickLabelFont(new Font("黑体", Font.BOLD, 12));// 设置X轴坐标上的文字
  final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();  //获取y轴
  rangeAxis.setLabelFont(CHART_FONT);  //设置y轴的标题文字
  chart.getTitle().setFont(CHART_FONT); //设置标题文字
  chart.getLegend().setItemFont(CHART_FONT); //设置底部中文乱码
  
  return chart;
 }

 /**
  * 曲线图
  *
  * @param title
  *            图形的标题
  * @param xAxis
  *            x轴标题
  * @param yAxis
  *            y轴标题
  * @param dataset
  *            数据集对象
  * @param legend
  *            显示标题
  * @param tooltips
  *            启用热键
  * @param urls
  *            启用超键接
  * @param showItem
  *            是否显示各数据点及其数值
  * @return
  */
 public static JFreeChart getLineChart(String title, String xAxis,
   String yAxis, TimeSeriesCollection dataset, boolean legend,
   boolean tooltips, boolean urls, boolean showItem) {
  JFreeChart chart = ChartFactory.createTimeSeriesChart(title, xAxis,
    yAxis, dataset, legend, tooltips, urls);
  XYPlot plot = (XYPlot) chart.getPlot();
  // 设置网格背景颜色
  plot.setBackgroundPaint(Color.white);
  // 设置网格竖线颜色
  plot.setDomainGridlinePaint(Color.pink);
  // 设置网格横线颜色
  plot.setRangeGridlinePaint(Color.pink);
  // 设置曲线图与xy轴的距离
  plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 0D));
  NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
  plot.getDomainAxis().setLowerMargin(0.05);
  plot.getDomainAxis().setUpperMargin(0.05);
  numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  if (showItem) {
   XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) plot
     .getRenderer();
   // 设置曲线是否显示数据点
   xylineandshaperenderer.setBaseShapesVisible(true);
   // 设置曲线显示各数据点的值
   XYItemRenderer xyitem = plot.getRenderer();
   xyitem.setBaseItemLabelsVisible(true);
   xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(
     ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
   xyitem
     .setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
   xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 10));
   plot.setRenderer(xyitem);
  }
  return chart;
 }

 /**
  * 曲线图(非时序图)
  *
  * @param title
  *            图形的标题
  * @param xAxis
  *            x轴标题
  * @param yAxis
  *            y轴标题
  * @param dataset
  *            数据集对象
  * @param plotor
  * @param legend
  *            显示标题
  * @param tooltips
  *            启用热键
  * @param urls
  *            启用超键接
  * @param showItem
  *            是否显示各数据点及其数值
  * @return
  */
 public static JFreeChart getUnequallyLineChart(String title, String xAxis,
   String yAxis, DefaultCategoryDataset dataset,
   PlotOrientation plotor, boolean legend, boolean tooltips,
   boolean urls, boolean showItem) {
  JFreeChart chart = ChartFactory.createLineChart(title, xAxis, yAxis,
    dataset, plotor, tooltips, urls, showItem);

  CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
  // 设置网格背景颜色
  plot.setBackgroundPaint(Color.white);
  // 设置网格竖线颜色
  plot.setDomainGridlinesVisible(true);
  plot.setDomainGridlinePaint(Color.pink);
  // 设置网格横线颜色
  plot.setRangeGridlinePaint(Color.pink);
  // 设置曲线图与xy轴的距离
  plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
  if (showItem) {
   LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) plot
     .getRenderer();

   // 设置曲线是否显示数据点
   lineandshaperenderer.setShapesVisible(true);
   lineandshaperenderer
     .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
   lineandshaperenderer.setBaseItemLabelsVisible(true);
   // 设置曲线显示各数据点的值
   CategoryItemRenderer xyitem = plot.getRenderer();
   xyitem.setBaseItemLabelsVisible(true);
   xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(
     ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
   xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 10));
   plot.setRenderer(xyitem);
  }
  return chart;
 }
}

 

 

例子:

Structs2 中的Action的一个方法
 public String showCompareLine() throws Exception{
  //访问量统计时间线
  TimeSeries timeSeries2006 = new TimeSeries("2006年度", Month.class);
  TimeSeries timeSeries2007 = new TimeSeries("2007年度", Month.class);
  //时间曲线数据集合
  TimeSeriesCollection lineDataset = new TimeSeriesCollection();
  
  //构造数据集合
  timeSeries2006.add(new Month(1, 2007), 7200);
  timeSeries2006.add(new Month(2, 2007), 7000);
  timeSeries2006.add(new Month(3, 2007), 4200);
  timeSeries2006.add(new Month(4, 2007), 8200);
  timeSeries2006.add(new Month(5, 2007), 7300);
  timeSeries2006.add(new Month(6, 2007), 8200);
  timeSeries2006.add(new Month(7, 2007), 9200);
  timeSeries2006.add(new Month(8, 2007), 7300);
  timeSeries2006.add(new Month(9, 2007), 9400);
  timeSeries2006.add(new Month(10, 2007), 7500);
  timeSeries2006.add(new Month(11, 2007), 6600);
  timeSeries2006.add(new Month(12, 2007), 3500);
  
  timeSeries2007.add(new Month(1, 2007), 10200);
  timeSeries2007.add(new Month(2, 2007), 9000);
  timeSeries2007.add(new Month(3, 2007), 6200);
  timeSeries2007.add(new Month(4, 2007), 8200);
  timeSeries2007.add(new Month(5, 2007), 8200);
  timeSeries2007.add(new Month(6, 2007), 11200);
  timeSeries2007.add(new Month(7, 2007), 13200);
  timeSeries2007.add(new Month(8, 2007), 8300);
  timeSeries2007.add(new Month(9, 2007), 10400);
  timeSeries2007.add(new Month(10, 2007), 12500);
  timeSeries2007.add(new Month(11, 2007), 10600);
  timeSeries2007.add(new Month(12, 2007), 10500);
  
  lineDataset.addSeries(timeSeries2006);
  lineDataset.addSeries(timeSeries2007);
  
  chart = ChartsUtil.getLineChart("访问量统计时间线", "月份", "访问量", lineDataset, true, true, true, false);
  
  //设置子标题
  TextTitle subtitle = new TextTitle("2006/2007年度访问量对比", new Font("黑体", Font.BOLD, 12));
  chart.addSubtitle(subtitle);
  //设置主标题
  chart.setTitle(new TextTitle("阿蜜果blog访问量统计", new Font("隶书", Font.ITALIC, 15)));
  chart.setAntiAlias(true);
  
  return SUCCESS;
  
 }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值