JFreechart 的一些用法 例子

public abstract class AbstractStatistic implements Statistic {

 /**
  * 创建统计图形
  *
  * @param columnKeys
  *            关键字数组
  * @param data
  *            数据数组
  * @param title
  *            标题
  * @param session
  * @param rowKeys
  *            行关键字
  * @param showType
  * @return
  */

 public String createStatisticImg(String[] columnKeys, double[][] data,
   String title, HttpSession session, String[] rowKeys, String showType) {
  try {

   JFreeChart chart = null;
   if(showType.equals("1")){
      chart = createBarChart3D(data,columnKeys,rowKeys,title);
   }
   if(showType.equals("2")){
      chart = createLineChart(data,columnKeys,rowKeys,title);
   }
   if(showType.equals("3")){
       chart = createAreaChart(data,columnKeys,rowKeys,title);
   }
   if(showType.equals("4")){
    chart = createLineChart3D(data, columnKeys, rowKeys, title);
   }
   String filename = ServletUtilities.saveChartAsPNG(chart, 800, 450,
     null, session);
   return filename;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return "";
 }
 
 public JFreeChart createBarChart3D(double[][] data, String[] columnKeys, String[] rowKeys,String title){
  
  
  CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
    rowKeys, columnKeys, data);
  
  JFreeChart chart = ChartFactory.createBarChart3D(title,// "分布与对比图"
    "", null, dataset, PlotOrientation.VERTICAL, true, false,
    false);

  chart.setBackgroundPaint(Color.WHITE);// 设置整张图片背景色
  CategoryPlot plot = chart.getCategoryPlot();
  plot.setBackgroundPaint(Color.gray);// 设置图形区域背景色
  // 设置是否显示垂直网格线
  plot.setDomainGridlinesVisible(true);
  // 设置是否显示水平网格线
  plot.setRangeGridlinesVisible(true);
  CategoryAxis domainAxis = plot.getDomainAxis();
  /*------设置X轴标题的倾斜程度----*/
  domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
  domainAxis.setMaximumCategoryLabelLines(20);
  
  domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));

  plot.setDomainAxis(domainAxis);

  ValueAxis rangeAxis = plot.getRangeAxis();
  // 设置最高的一个 Item 与图片顶端的距离
  rangeAxis.setUpperMargin(0.15);
  // 设置最低的一个 Item 与图片底端的距离
  rangeAxis.setLowerMargin(0.15);
  plot.setRangeAxis(rangeAxis);
  BarRenderer3D renderer = new BarRenderer3D();
  renderer.setBaseOutlinePaint(Color.BLACK);
  // 设置 Wall 的颜色<BR>
  renderer.setWallPaint(Color.gray);
  // 设置每种柱的颜色
  renderer.setSeriesPaint(0, new Color(255, 0, 0));
  renderer.setSeriesPaint(1, new Color(0, 0, 255));
  renderer.setSeriesPaint(2, new Color(0, 255, 0));

  // 置每种柱的 Outline 颜色
  renderer.setSeriesOutlinePaint(0, Color.BLACK);
  renderer.setSeriesOutlinePaint(1, Color.BLACK);
  renderer.setSeriesOutlinePaint(2, Color.BLACK);
  // 设置每个地区所包含的平行柱的之间距离
  renderer.setItemMargin(0.1);

  // 显示每个柱的数值,并修改该数值的字体属性
  // renderer.setSeriesItemLabelGenerator(0,new
  // StandardCategoryItemLabelGenerator("##%",java.text.NumberFormat.getPercentInstance()));
  renderer
    .setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
  renderer.setItemLabelFont(new Font("黑体", Font.PLAIN, 12));
  renderer.setItemLabelsVisible(true);
  // 设置标签显示位置,不加这句将显示在柱体内
  renderer.setPositiveItemLabelPosition(new ItemLabelPosition(
    ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));

  plot.setRenderer(renderer);
  // 设置柱的透明度<BR>
  plot.setForegroundAlpha(0.6f);

  // 设置地区、销量的显示位置<BR>
  plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
  plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
  
  return chart;
  
 }
 
 
 public JFreeChart createLineChart(double[][] data, String[] columnKeys, String[] rowKeys,String title){
  
  CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
    rowKeys, columnKeys, data);
  
  JFreeChart chart = ChartFactory.createLineChart(title,// "区域分布与对比图"
    "", null, dataset, PlotOrientation.VERTICAL, true, false,
    false);
  CategoryPlot plot = chart.getCategoryPlot();
  CategoryAxis domainAxis = plot.getDomainAxis();
  /*------设置X轴标题的倾斜程度----*/
  domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
  domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));
  return chart;
 }
 
 public JFreeChart createLineChart3D(double[][] data, String[] columnKeys, String[] rowKeys,String title){
  
  CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
    rowKeys, columnKeys, data);
   
  JFreeChart chart = ChartFactory.createLineChart3D(title,// "区域分布与对比图"
    "", null, dataset, PlotOrientation.VERTICAL, true, false,
    false);
  CategoryPlot plot = chart.getCategoryPlot();
  CategoryAxis domainAxis = plot.getDomainAxis();
  /*------设置X轴标题的倾斜程度----*/
  domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
  domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));
  return chart;
 }
 
 public JFreeChart createAreaChart(double[][] data, String[] columnKeys, String[] rowKeys,String title){
  CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
    rowKeys, columnKeys, data);
        JFreeChart chart = ChartFactory.createAreaChart(title, "统计时间", "统计数量", dataset, PlotOrientation.VERTICAL, true, true, false);
        chart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot)chart.getPlot();
        categoryplot.setForegroundAlpha(0.5F);
        categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setDomainGridlinesVisible(true);
        categoryplot.setDomainGridlinePaint(Color.white);
        categoryplot.setRangeGridlinesVisible(true);
        categoryplot.setRangeGridlinePaint(Color.white);
        CategoryAxis categoryaxis = categoryplot.getDomainAxis();
        categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
        categoryaxis.setLowerMargin(0.0D);
        categoryaxis.setUpperMargin(0.0D);
        categoryaxis.addCategoryLabelToolTip("Type 1", "The first type.");
        categoryaxis.addCategoryLabelToolTip("Type 2", "The second type.");
        categoryaxis.addCategoryLabelToolTip("Type 3", "The third type.");
        NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        numberaxis.setLabelAngle(0.0D);
  return chart;
 }
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值