⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chartmanagement.java

📁 java版源代码,里面包含很多源代码,大家可以看看.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.trulytech.mantis.util;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;

import java.awt.*;
import java.awt.image.*;

import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;
import org.jfree.data.general.*;
import org.jfree.data.xy.*;
import com.trulytech.mantis.result.*;
import org.jfree.chart.title.TextTitle;

/**
 * <p>Title: ChartManagement</p>
 * <p>Description:后台产生图形(JFreeChart) </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author Wang Xian
 * @version 1.2
 * PIE
 * RING
 * BAR
 * LINE
 * STACKBAR
 * AREA
 * STACKAREA
 * XYLINE
 * XYAREA
 * 输出为PNG类型
 */
public class ChartManagement {

  static final private String CONTENT_TYPE = "image/png";

  //Http Response
  private HttpServletResponse response = null;
  //标识图表的类型
  final public static String PIE = "pie";
  final public static String RING = "ring";
  final public static String BAR = "bar";
  final public static String LINE = "line";
  final public static String STACKBAR = "stackbar";
  final public static String AREA = "area";
  final public static String STACKAREA = "stackbararea";
  final public static String XYLINE = "xyline";
  final public static String XYAREA = "xyarea";

  final public static int RESPONSE = 0;
  final public static int FILE = 1;
  final public static int BUFFERIMAGE = 2;

  //是否是3D的图表
  private boolean is3d = true;
  //是否是alpha的图表
  private boolean isalpha = true;
  //输出图的高度和宽度
  private int width = 400;
  private int height = 300;
  //数据源
  private DBResult result = null;

  //标识图表的类型,输入方式和输出方式
  private String chart_type = "";

  //图表的各种标识
  private String title = "图表标题";
  private String categoryTag = "目录轴的显示标签";
  private String valueTag = "数值轴的显示标签";
  private String[] Legend = null;
  private Color backGroundColor = null;
  //存储目标文件名
  private String descFile = "";
  //子标题
  private String subTitle = "";

  /**
   *
   * @param chart_type String
   * @param response Object HttpServletResponse或者String,如果是String,则保存为文件,必须是GIF图形
   * @param Result DBResult
   * @param is3d boolean
   * @param Legend 图例
   */
  public ChartManagement(String chart_type, HttpServletResponse response,
                         DBResult Result, boolean is3d, String[] Legend) {
    this.chart_type = chart_type;
    this.result = Result;
    this.response = response;
    this.is3d = is3d;
    this.Legend = Legend;

    if (Legend == null) {
      this.Legend = new String[this.result.ColumnCount - 1];
      for (int i = 0; i < this.result.ColumnCount - 1; i++)
        this.Legend[i] = "项目" + new Integer(i);
    }
    if (Legend.length < this.result.ColumnCount - 1) {
      this.Legend = new String[this.result.ColumnCount - 1];
      for (int i = 0; i < this.result.ColumnCount - 1; i++)
        this.Legend[i] = "项目" + new Integer(i);

    }
  }

  /**
   *
   * @param chart_type String
   * @param response Object HttpServletResponse或者String,如果是String,则保存为文件,必须是GIF图形
   * @param Result DBResult
   * @param is3d boolean
   */
  public ChartManagement(String chart_type, HttpServletResponse response,
                         DBResult Result, boolean is3d) {
    this.chart_type = chart_type;
    this.result = Result;
    this.response = response;
    this.is3d = is3d;
    this.Legend = new String[this.result.ColumnCount - 1];
    for (int i = 0; i < this.result.ColumnCount - 1; i++)
      this.Legend[i] = "项目" + new Integer(i);

  }

  /**
   * 生成Web图形
   * @return String
   * @throws Exception
   */
  public String generateChart() throws Exception {
    outputChart(this.getInputData(), ChartManagement.RESPONSE);
    return "";

  }

  /**
   * 生成BufferImage
   * @return BufferedImage
   * @throws Exception
   */
  public BufferedImage generateChartImage() throws Exception {
    return outputChart(this.getInputData(), ChartManagement.BUFFERIMAGE);

  }

  /**
   * 生成文件
   * @return BufferedImage
   * @throws Exception
   */
  public BufferedImage generateChartFile() throws Exception {
    return outputChart(this.getInputData(), ChartManagement.FILE);

  }

  /**
   * 组织数据源
   * @return Dataset 数据源
   * @throws Exception
   */
  private Dataset getInputData() throws Exception {
    Dataset dataset = null;

    if (result != null) {
      //生成PieDataset
      if (chart_type.equalsIgnoreCase(ChartManagement.PIE) ||
          chart_type.equalsIgnoreCase(ChartManagement.RING)) {
        dataset = new DefaultPieDataset();
        if (result.ColumnCount == 2) {
          //循环赋值
          ArrayList al = null;
          int nSize = result.ResultBuffer.size();
          for (int i = 0; i < nSize; i++) {
            al = (ArrayList) result.ResultBuffer.get(i);
            String categoryTag = null;
            double value = 0d;

            value = new Double( ( (DBColumn) al.get(1)).Value).doubleValue();
            categoryTag = ( (DBColumn) al.get(0)).Value;

            ( (DefaultPieDataset) dataset).setValue(categoryTag, value);
          }
        }
        else {
          throw new Exception("得到的数据的列数不对,应该是两列");
        }
      }
      //生成XYDataSet
      else if (chart_type.equalsIgnoreCase(ChartManagement.XYLINE) ||
               chart_type.equalsIgnoreCase(ChartManagement.XYAREA)) {
        if (result.ColumnCount > 1) {
          //分别代表x和y值
          if (result.ColumnCount == 2) {
            XYSeries xyseries = new XYSeries("First");
            ArrayList al = null;
            XYSeriesCollection xyseriescollection = new XYSeriesCollection();
            int nSize = result.ResultBuffer.size();
            for (int i = 0; i < nSize; i++) {
              al = (ArrayList) result.ResultBuffer.get(i);

              xyseries.add(new Double( ( (DBColumn) al.get(0)).Value).
                           doubleValue(),
                           new Double( ( (DBColumn) al.get(1)).Value).
                           doubleValue());

            }
            xyseriescollection.addSeries(xyseries);
            dataset = xyseriescollection;
          }
          //仅仅去前3个值, 第一个为类型,后两个分别是x和y值
          else if (result.ColumnCount > 2) {
            ArrayList al = null;
            HashMap map = new HashMap();
            XYSeriesCollection xyseriescollection = new XYSeriesCollection();
            int nSize = result.ResultBuffer.size();
            for (int i = 0; i < nSize; i++) {
              al = (ArrayList) result.ResultBuffer.get(i);
              if (map.get( ( (DBColumn) al.get(0)).Value) == null) {
                XYSeries xyseries = new XYSeries( ( (DBColumn) al.get(0)).Value);
                xyseries.add(new Double( ( (DBColumn) al.get(1)).Value).
                             doubleValue(),
                             new Double( ( (DBColumn) al.get(2)).Value).
                             doubleValue());
                map.put( ( (DBColumn) al.get(0)).Value, xyseries);
              }
              else {
                XYSeries xyseries = (XYSeries) map.get( ( (DBColumn) al.get(0)).
                    Value);
                xyseries.add(new Double( ( (DBColumn) al.get(1)).Value).
                             doubleValue(),
                             new Double( ( (DBColumn) al.get(2)).Value).
                             doubleValue());
                map.put( ( (DBColumn) al.get(0)).Value, xyseries);

              }

            }
            this.Legend = new String[map.size()];
            Iterator it = map.keySet().iterator();
            int i = 0;
            while (it.hasNext()) {
              Object obj = it.next();
              Legend[i] = (String) obj;
              xyseriescollection.addSeries( (XYSeries) map.get(obj));
              i++;
            }

            map.clear();
            map = null;
            dataset = xyseriescollection;

          }

        }
        else {
          throw new Exception("得到的数据的列数错误");
        }

      }
      //生成CategoryDataSet
      else if (chart_type.equalsIgnoreCase(ChartManagement.BAR) ||
               chart_type.equalsIgnoreCase(ChartManagement.LINE) ||
               chart_type.equalsIgnoreCase(ChartManagement.STACKBAR) ||
               chart_type.equalsIgnoreCase(ChartManagement.AREA) ||
               chart_type.equalsIgnoreCase(ChartManagement.STACKAREA)) {
        dataset = new DefaultCategoryDataset();

        if (result.ColumnCount > 1) {
          //循环赋值
          ArrayList al = null;
          int nSize = result.ResultBuffer.size();
          for (int i = 0; i < nSize; i++) {
            al = (ArrayList) result.ResultBuffer.get(i);
            String categoryTag = null;
            double value = 0d;

            //判断是复杂的数据集还是简单的数据集
            if (result.ColumnCount == 2) {
              value = new Double( ( (DBColumn) al.get(1)).Value).doubleValue();
              categoryTag = ( (DBColumn) al.get(0)).Value;
              ( (DefaultCategoryDataset) dataset).addValue(value,
                  Legend[0],
                  categoryTag);
            }
            //////////
            //例如: name, age, income
            //      a,20,2000
            //      b,35 3000
            //则有两中颜色的类别 : age和income
            /////////////
            else if (result.ColumnCount > 2) {
              for (int m = 1; m < result.ColumnCount; m++) {
                value = new Double( ( (DBColumn) al.get(m)).Value).doubleValue();
                categoryTag = ( (DBColumn) al.get(0)).Value;
                ( (DefaultCategoryDataset) dataset).addValue(value,
                    Legend[m - 1],
                    categoryTag);

              }
            }
          }
        }
        else {
          throw new Exception("得到的数据的列数错误");
        }
      }
      else {
        dataset = getExtInputData();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -