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

📄 vbarchart3dbean.java

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//	Fig. 9.2_04_01: VBarChart3DBean.java
//	功能: 生成3D垂直直方图的JavaBean

package chart;

import java.util.*;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.imageio.*;
import com.fatcat.webchart.*;

public class VBarChart3DBean extends HttpServlet
{
  HttpServletResponse response;

  static int width = 500, height = 400;
  static int plotWidth = 400, plotHeight = 300;

  private String domainAxisLabel = "";
  private String rangeAxisLabel = "";
  private String chartTitle = "";

  String series[] =
  {
    "Python", "JAVA", "C#"
  };
  String category[] =
  {
    "2005年1月", "2005年2月", "2005年3月"
  };

  Color paintColor[] =
  {
    new Color(255, 0, 66), new Color(255, 169, 66), new Color(33, 255, 66),
    new Color(33, 0, 255), new Color(99, 99, 0), new Color(255, 187, 151)
  };

  private Graphics2D g2d = null;
  private BufferedImage image = null;

  int thickness = 12;
  int alpha = 45;

  private float alphaComposite = 1.0f;

  public VBarChart3DBean()
  {
    // 默认构造器
  }

  public VBarChart3DBean(HttpServletResponse response)
  {
    this.response = response;
  }

  // 设置横轴标题
  public void setDomainAxisLabel(String domainAxisLabel)
  {
    this.domainAxisLabel = domainAxisLabel;
  }

  // 设置透明度
  public void setAlphaComposite(float alphaComposite)
  {
    this.alphaComposite = alphaComposite;
  }

  // 设置纵轴标题
  public void setRangeAxisLabel(String rangeAxisLabel)
  {
    this.rangeAxisLabel = rangeAxisLabel;
  }

  // 设置图表标题
  public void setChartTitle(String chartTitle)
  {
    this.chartTitle = chartTitle;
  }

  // 定义绘制斜线的方法
  public void drawSlantLine(double startPtX, double startPtY, double thickness,
    double alpha, Graphics2D g2d)
  {
    Point2D.Double newStartPoint =
      getPoint2DPoint(startPtX, startPtY, thickness, alpha);
    Point2D.Double newEndPoint = new Point2D.Double(startPtX, startPtY);
    Line2D.Double slantLine = new Line2D.Double(newStartPoint, newEndPoint);
    g2d.draw(slantLine);
  }

  // 定义绘制垂直直线的方法
  public void drawVerticalLine(double startPtX, double startPtY,
    double thickness, double alpha, double length, Graphics2D g2d)
  {
    Point2D.Double newStartPoint =
      getPoint2DPoint(startPtX, startPtY, thickness, alpha);
    Point2D.Double newEndPoint =
      new Point2D.Double(newStartPoint.getX(), newStartPoint.getY() - length);
    Line2D.Double verticalLine = new Line2D.Double(newStartPoint, newEndPoint);
    g2d.draw(verticalLine);
  }

  // 定义绘制水平直线的方法
  public void drawHorizontalLine(double startPtX, double startPtY,
    double thickness, double alpha, double length, Graphics2D g2d)
  {
    Point2D.Double newStartPoint =
      getPoint2DPoint(startPtX, startPtY, thickness, alpha);
    Point2D.Double newEndPoint =
      new Point2D.Double(newStartPoint.getX() + length, newStartPoint.getY());
    Line2D.Double horizontalLine =
      new Line2D.Double(newStartPoint, newEndPoint);
    g2d.draw(horizontalLine);
  }

  // 定义获取水平偏移量的方法
  public double getOffsetX(double thickness, double alpha)
  {
    return thickness * Math.cos(alpha * Math.PI / 180);
  }

  // 定义获取垂直偏移量的方法
  public double getOffsetY(double thickness, double alpha)
  {
    return thickness * Math.sin(alpha * Math.PI / 180);
  }

  // 定义创建新的Point2D.Double对象的方法
  public Point2D.Double getPoint2DPoint(double startPtX, double startPtY,
    double thickness, double alpha)
  {
    double newStartPtX = startPtX + getOffsetX(thickness, alpha);
    double newStartPtY = startPtY - getOffsetY(thickness, alpha);
    Point2D.Double point = new Point2D.Double(newStartPtX, newStartPtY);
    return point;
  }

  public void initialize()
  {
    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    g2d = image.createGraphics();
    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, width, height);
  }

  // 绘制整个图表的背景
  public void drawBackground()
  {
    // 阴影和图表之间的偏移量
    int shadowOffset = 5;

    g2d.setColor(Color.white);
    g2d.fillRect(0, 0, width, height);

    // 打开反锯齿功能,用于平滑处理相关背景
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);

    // 绘制阴影,由灰色渐进圆角矩形组成
    GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY, width, height,
      new Color(218, 214, 212), false);
    g2d.setPaint(grayGP);
    RoundRectangle2D.Float bgRR = new RoundRectangle2D.Float(shadowOffset,
      shadowOffset, width - shadowOffset, height - shadowOffset, 50, 50);
    g2d.fill(bgRR);

    // 绘制圆角矩形背景
    GradientPaint chartGP = new GradientPaint(0, 0, new Color(33, 66, 111),
      0, height, new Color(45, 86, 177), false);

    g2d.setPaint(chartGP);
    g2d.fillRoundRect(0, 0, width - shadowOffset, height - shadowOffset, 50, 50);

    // 绘制深蓝色圆角矩形轮廓
    BasicStroke bs = new BasicStroke(2.0f);
    g2d.setStroke(bs);
    g2d.setPaint(new Color(55, 71, 105));
    g2d.drawRoundRect(0, 0, width - shadowOffset, height - shadowOffset, 50, 50);
    g2d.setStroke(new BasicStroke());

    // 重新关闭反锯齿功能
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_OFF);
  }

  // 绘制图表标题
  public void drawTitle()
  {
    g2d.setFont(new Font("华文隶书", Font.PLAIN, 18));
    int titleLength = g2d.getFontMetrics().stringWidth(chartTitle);

    g2d.setColor(new Color(245, 213, 164));
    g2d.drawString(chartTitle, (width - titleLength) / 2, 15);
  }

  // 绘制图表主体部分
  public void drawChart()
  {
    // 获得当前的AffineTransform对象
    AffineTransform old = g2d.getTransform();
    g2d.translate(80, 30);


    // 定义图表绘制区域的右下方基准点坐标
    int basePointX = 0, basePointY = 300;

    // 定义图表绘制区左上角坐标
    int topLeftPointX = basePointX, topLeftPointY = 0;

    // 填充图表绘图区域背景
    GradientPaint plotGP = new GradientPaint(120, 60, new Color(255, 255, 150),
      120, 300, Color.WHITE, false);
    g2d.setPaint(plotGP);

    Rectangle2D.Double drawArea = new Rectangle2D.Double(topLeftPointX,
      topLeftPointY, plotWidth, plotHeight);
    drawArea.setRect(topLeftPointX + thickness * Math.cos(alpha * Math.PI / 180),
      topLeftPointY - thickness * Math.sin(alpha * Math.PI / 180),
      plotWidth, plotHeight);
    g2d.fill(drawArea);

    // 创建DrawParallelogram类的实例
    DrawParallelogram parallelogram = new DrawParallelogram();

    // 绘制基准点右边水平方向的平行四边形
    parallelogram.setFillColor(new Color(252, 198, 112));
    parallelogram.setBasePoint(basePointX, basePointY);
    parallelogram.setWidth(plotWidth);
    parallelogram.setHeight(0);
    parallelogram.setThickness(thickness);
    parallelogram.setAngle(alpha);
    parallelogram.drawParallelogram(2, g2d);
    parallelogram.setOutlineColor(Color.BLACK);
    parallelogram.drawParallelogram(1, g2d);

    // 绘制基准点上方垂直方向的平行四边形
    parallelogram.setPRightFillColor(new Color(252, 198, 112));
    parallelogram.setWidth(0);
    parallelogram.setHeight(plotHeight);
    parallelogram.setThickness(thickness);
    parallelogram.setAngle(alpha);
    parallelogram.drawParallelogram(2, g2d);
    parallelogram.setOutlineColor(Color.BLACK);
    parallelogram.drawParallelogram(1, g2d);



    // 创建虚线笔划
    float[]dashes =
    {
      3.f
    };
    BasicStroke bs = new BasicStroke(1.0f, BasicStroke.CAP_ROUND,
      BasicStroke.JOIN_ROUND, 10, dashes, 0);
    g2d.setStroke(bs);
    g2d.setFont(new Font("宋体", Font.PLAIN, 12));

⌨️ 快捷键说明

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