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

📄 chart.java

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

import java.util.*;

import javax.servlet.http.*;

import java.awt.*;

import com.sun.image.codec.jpeg.*;
import com.trulytech.mantis.result.*;
import com.trulytech.mantis.system.*;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

/**
 * <p>Title: Chart</p>
 * <p>Description: 作图类 [动态生成GIF图形]</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author WangXian
 * @version 1.2
 */

public class Chart {
  static final private String CONTENT_TYPE = "image/png";
//宽度
  protected int Width = 320;
//类型 0-饼状图 1-拄状图 2-线型图 3-字符串
  private int Type = 0;
//背景颜色
  private Color BKColor = Color.white;
//字体颜色
  private Color FontColor = Color.black;
//记录集
  private DBResult Result = null;
//Http Response
  private Object response = null;
//标题在Result中的字段数
  private int Label = 0;
//数值在Result中的字段数
  private int Value = 1;
//图形填充色
  private Color fill = Color.blue;
//显示字符
  private String Str = "";
//显示字体
  private Font Strfont = null;

  private static Random randGen = new Random();

  /**
   * 构造函数
   * @param response HttpServletResponse或者String,如果是String,则保存为文件,必须是GIF图形
   * @param Width int 宽度
   * @param BKColor Color 背景色,如果为null,则产生随机背景色
   * @param FontColor Color 字体颜色,如果为null,则产生随机颜色
   * @param Type int type=3为显示字符图片 0显示pie
   */
  public Chart(Object response, int Width, Color BKColor,
               Color FontColor, int Type) {
    this.response = response;
    this.Width = Width;
    this.BKColor = BKColor;
    this.FontColor = FontColor;
    this.Type = Type;

  }

  /**
   * 设置填充色
   * @param fill 填充色
   */
  public void setFill(Color fill) {
    this.fill = fill;
  }

  public void setStrfont(Font Strfont) {
    this.Strfont = Strfont;
  }

  /**
   * 设置显示字符
   * @param Str 显示字符
   */
  public void setStr(String Str) {
    this.Str = Str;
  }

  /**
   * 设置结果集
   * @param Result 结果集
   */
  public void setResult(DBResult Result) {
    this.Result = Result;
  }

  /**
   * 设置图形宽度
   * @param Width 宽度
   */
  public void setWidth(int Width) {
    this.Width = Width;
  }

  /**
   * 设置图形背景色
   * @param BKColor 背景色
   */
  public void setBKColor(Color BKColor) {
    this.BKColor = BKColor;
  }

  /**
   * 设置字体颜色
   * @param FontColor 字体颜色
   */
  public void setFontColor(Color FontColor) {
    this.FontColor = FontColor;
  }

  /**
   * 设置图形类型
   * @param Type 类型
   */
  public void setType(int Type) {
    this.Type = Type;
  }

  /**
   * 设置标题
   * @param Label 标题在Result中的字段数
   */
  public void setLabel(int Label) {
    this.Label = Label;
  }

  /**
   * 设置数值
   * @param Value 数值在Result中的字段数
   */
  public void setValue(int Value) {
    this.Value = Value;
  }

  /**
   * 生成随机颜色
   * @param fc int
   * @param bc int
   * @return Color
   */
  private Color getRandColor(int fc, int bc) {
    if (fc > 255)
      fc = 255;
    if (bc > 255)
      bc = 255;

    int r = fc + randGen.nextInt(bc - fc);
    int g = fc + randGen.nextInt(bc - fc);
    int b = fc + randGen.nextInt(bc - fc);
    return new Color(r, g, b);
  }

  /**
   * 刷新背景色
   * @param g Graphics
   */
  private void refreshBgColor(Graphics g) {
    g.setColor(Color.white);
    g.fillRect(0, 0, this.Width, 20);

    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 100; i++) {
      int x = randGen.nextInt(50);
      int y = randGen.nextInt(20);
      int xl = randGen.nextInt(this.Width - 30);
      int yl = randGen.nextInt(12);
      g.drawLine(x, y, x + xl, y + yl);
    }

  }

  /**
   * 执行doChart操作
   * @return String
   * @throws Exception
   */
  public String drawChart() throws Exception {
//写日志
    logWriter.Info("开始创建图形 (" + this.getClass().getName() + ")");
    OutputStream out = null;
    if (response instanceof HttpServletResponse) {

      ( (HttpServletResponse) response).setContentType(CONTENT_TYPE);
      ( (HttpServletResponse) response).setHeader("Content-Disposition",
                                                  " filename=image.png");

      out = ( (HttpServletResponse) response).getOutputStream();

    }
    else {
      out = new BufferedOutputStream(new
                                     FileOutputStream( (String) response));
    }
    try {

//计算图形高度
      int Height = new Float(Width / 1.3).intValue();
      if (Type == 3)
        Height = 20;
      BufferedImage img = new BufferedImage(Width,
                                            Height,
                                            BufferedImage.TYPE_INT_RGB);
      Graphics2D g = img.createGraphics();

      if (BKColor != null) {
        g.setColor(BKColor);
        g.fillRect(0, 0, Width, Height);
      }
      if (Type == 0) /*绘制饼状图*/ {
        PieChart chart = new PieChart();
        chart.drawChart(g, Result, Label, Value, Width, Height, fill);
        g.dispose();
      }
      else if (Type == 3) { //字符串
        if (BKColor == null)
          refreshBgColor(g);
        if (FontColor == null)
          g.setColor(getRandColor(20, 130));
        else
          g.setColor(FontColor);
        if (Strfont != null)
          g.setFont(Strfont);

        g.drawString(Str, 6,
                     16);
        g.dispose();
      }

      ImageIO.write(img, "png", out);

      out.flush();
      return "";
    }
    finally {
      out.close();
    }

  }

  public Font getStrfont() {
    return Strfont;
  }
}

/**
 * <p>Title: PieChart</p>
 * <p>Description: 绘制饼状图形</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author WangXian
 * @version 1.0
 */

class PieChart {

  /**
   * 绘制饼状图形
   * @param g 图形
   * @param Result 结果集
   * @param Label 标题字段
   * @param Value 值字段
   * @param Width 宽度
   * @param Height 高度
   * @param fill 填充颜色
   * @throws Exception
   */
  public void drawChart(Graphics g, DBResult Result, int Label, int Value,
                        int Width, int Height, Color fill) throws Exception {
    int arc = 0;
    int newArc = 0;
    double sumValue = 0;
    int i = 0;
    int scale = Height / 60;
    //设置字体
    Font f = new Font("Serif", Font.PLAIN, Height / 24);
    g.setFont(f);
    if (g == null || Result == null || Label == Value)
      return;
//计算总值
    int nSize=Result.ResultBuffer.size();

    for (i = 0; i < nSize; i++) {
      ArrayList rec = (ArrayList) Result.ResultBuffer.get(i);
      sumValue += Double.valueOf( ( (DBColumn) (rec.get(Value))).Value).
          doubleValue();
    }

    for (i = 0; i < nSize; i++) {
      ArrayList rec = (ArrayList) Result.ResultBuffer.get(i);
      newArc = new Double( (Double.valueOf( ( (DBColumn) (rec.get(Value))).
                                           Value).doubleValue() * 360) /
                          (sumValue)).intValue();
      g.setColor(fill);
      //绘制图例
      g.fillRect(new Float(Width * 0.8).intValue(),
                 (Height - 4 * scale - i * 3 * scale), 2 * scale, 2 * scale);
      g.drawString( ( (DBColumn) (rec.get(Label))).Value,
                   new Float(Width * 0.8).intValue() + 3 * scale,
                   (Height - 2 * scale - i * 3 * scale));
      //绘制图形
      if (i == (Result.ResultBuffer.size() - 1))
        g.fillArc(2 * scale, 4 * scale, new Float(Width * 0.75).intValue() - 1,
                  new Float(Height * 0.9).intValue() - 1, arc, 360 - arc);
      else
        g.fillArc(2 * scale, 4 * scale, new Float(Width * 0.75).intValue() - 1,
                  new Float(Height * 0.9).intValue() - 1, arc, newArc);
      arc += newArc;
      fill = rotateColor(fill);
      fill = rotateBrightness(fill);
    }
    g.setColor(Color.black);
    g.drawOval(2 * scale, 4 * scale, new Float(Width * 0.75).intValue() - 1,
               new Float(Height * 0.9).intValue() - 1);

    //绘制图形边框
    arc = 0;
    for (i = 0; i < nSize; i++) {
      ArrayList rec = (ArrayList) Result.ResultBuffer.get(i);
      newArc = new Double( (Double.valueOf( ( (DBColumn) (rec.get(Value))).
                                           Value).doubleValue() * 360) /
                          (sumValue)).intValue();
      g.setColor(Color.black);
      g.drawLine( ( (new Float(Width * 0.75).intValue() - 1)) / 2 + 2 * scale,
                 ( (new Float(Height * 0.9).intValue() - 1)) / 2 + 4 * scale,
                 new Float( (new Double( (Width * 0.75) - 1).doubleValue() / 2 +
                             (new
                              Double( (Width * 0.75 - 1) *
                                     (Math.cos( (arc * Math.PI) / 180) / 2))).
                             doubleValue())).intValue() + 2 * scale,
                 new Float( (new Double( (Height * 0.9) - 1).doubleValue() / 2 -
                             (new
                              Double( (Height * 0.9 - 1) *
                                     (Math.sin( (arc * Math.PI) / 180) / 2))).
                             doubleValue())).intValue() + 4 * scale);
      arc += newArc;

    }

  }

  /**
   * 转换颜色
   * @param c 颜色
   * @return Color
   * @throws Exception
   */
  private Color rotateColor(Color c) throws Exception {
    float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
    hsb[0] += 0.085;
    if (hsb[0] > 1.0)
      hsb[0] -= 1.0;

    return new Color(Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]));
  }

  /**
   * 转换饱和度
   * @param c 颜色
   * @return Color
   * @throws Exception
   */
  private Color rotateBrightness(Color c) throws Exception {
    float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
    hsb[2] -= 0.095;
    if (hsb[2] > 1.0)
      hsb[2] -= 1.0;
    return new Color(Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]));
  }

}

⌨️ 快捷键说明

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