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

📄 line3dchart1.jsp

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JSP
字号:
<!--
	Fig. 7.4_02_01: line3DChart1.jsp
	功能: 基于Java2D的Web图表实例2: 3D线段图1
-->
<%@ page language="java" contentType="image/png;charset=GB2312"
	import="java.awt.*"
	import="javax.imageio.*"
	import="java.awt.geom.*"
	import="java.awt.image.*"
	import="java.io.*"
	import="com.fatcat.webchart.*"
%>
<%!
	// 绘制线段阴影
	public void drawLineShadow(int xPoint[], int yPoint[], int thickness,
		Color color, Graphics2D g2d)
	{
		int shadowX[] = new int[xPoint.length];
		int shadowY[] = new int[yPoint.length];

		for (int i = thickness; i >= 0; i--)
			{	
				if (i == thickness || i == 0)
				{
					g2d.setStroke(new BasicStroke(2.0f));
					g2d.setColor(color.darker());
				}
				else
				{
					g2d.setStroke(new BasicStroke(4.0f));
					g2d.setColor(color);
				}
				
				for (int j = 0; j < xPoint.length; j ++)
				{
					shadowX[j]=xPoint[j] + i;
					shadowY[j]=yPoint[j] - i;				
				}

				g2d.drawPolyline(shadowX, shadowY, shadowX.length); 			
			}
	}
%>

<%
// 清空缓冲区
response.reset();

// 注意这里的MIME类型
response.setContentType("image/png");

// 创建一个 610X400 的图像
int width = 610, height = 400;

BufferedImage image = new BufferedImage(width, height,
  BufferedImage.TYPE_INT_RGB);

// 创建Java2D对象
/**
Graphics g = image.getGraphics();
Graphics2D g2d = (Graphics2D)g;
 */
Graphics2D g2d = image.createGraphics();

// 填充背景
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);

// 绘制新背景
GradientPaint grayGP = new GradientPaint(0, 0, Color.GRAY, width, height, new
  Color(200, 200, 200), false);
g2d.setPaint(grayGP);
g2d.fillRoundRect(20, 20, width - 20, height - 20, 50, 50);

g2d.setPaint(Color.WHITE);
g2d.fillRoundRect(12, 12, width - 20, height - 20, 50, 50);

BasicStroke bs = new BasicStroke(4.0f);
g2d.setStroke(bs);
g2d.setPaint(new Color(53, 76, 112));
g2d.drawRoundRect(12, 12, width - 20, height - 20, 50, 50);

GradientPaint blueGP = new GradientPaint(120, 60, new Color(215, 230, 252), 120,
  300, Color.WHITE, false);
g2d.setPaint(blueGP);
g2d.fillRect(120, 60, 440, 300);

// 绘制图表标题
String chartTitle = "计算机编程类图书2004年月销售量统计图";
g2d.setFont(new Font("方正粗宋简体", Font.PLAIN, 22));
g2d.setColor(Color.BLACK);
g2d.drawString(chartTitle, 140, 40);

// 创建虚线笔划
float[] dashes = { 3.f };
bs = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10,
  dashes, 0);
g2d.setStroke(bs);
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
String str = "2004-";
int stringLength = 0;

for (int i = 1; i <= 12; i++)
{
  // 绘制垂直方向虚线
  g2d.drawLine(80+i * 40, 50, 80+i * 40, 360);

  // 绘制横轴上月份的说明文字
  str += i;
  stringLength = g2d.getFontMetrics().stringWidth(str);
  if (i % 2 == 0)
  {
    g2d.drawString(str, 80+i * 40-stringLength / 2, 387);
  }
  else
  {
    g2d.drawString(str, 80+i * 40-stringLength / 2, 375);
  }
  str = "2004-";
}

str = "";
int stringHeight = 0;

for (int i = 0; i < 300; i += 30)
{
  // 绘制水平方向虚线
  g2d.drawLine(120, 60+i, 570, 60+i);

  // 绘制纵轴上销售量的说明文字
  str += 100-i / 3;
  stringHeight = g2d.getFontMetrics().getAscent();
  stringLength = g2d.getFontMetrics().stringWidth(str);
  g2d.drawString(str, 110-stringLength, 60+i + stringHeight / 2);
  str = "";
}

// 绘制坐标轴
g2d.setStroke(new BasicStroke(3.0f));
g2d.setColor(new Color(53, 76, 112));
g2d.drawLine(120, 50, 120, 360);
g2d.drawLine(120, 360, 570, 360);

// 绘制纵坐标上的标题
g2d.setFont(new Font("黑体", Font.PLAIN, 15));
g2d.drawString("月销售量", 40, 45);

// 调用TriangleServlet类,绘制坐标轴上的箭头
TriangleServlet ts = new TriangleServlet();
ts.setFillColor(new Color(53, 76, 112));
ts.setBaseLine(10);
ts.setAlpha(60);
ts.drawTrigangle(570, 360, 2, 2, g2d); // 绘制横坐标轴上的箭头
ts.drawTrigangle(120, 50, 1, 2, g2d); // 绘制纵坐标轴上的箭头

String[]bookTitle = { "JAVA", "C#" };
Color[]bookColor = { Color.RED, Color.ORANGE };
int[] sales = new int[12];
int[] month = new int[12];

g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
for (int i = 0; i < bookTitle.length; i++)
{
  // 初始化绘制数据
  int bookSales = 0;
  for (int j = 0; j < sales.length; j++)
  {
    bookSales = 45+(int)(Math.random() * 50);
    sales[j] = 360-bookSales * 3;
    month[j] = 120+j * 40;
  }

  // 重新设置笔划
  g2d.setStroke(new BasicStroke(5.0f));

  // 设定阴影的厚度
  int thickness = 15;

  // 绘制阴影
  drawLineShadow(month, sales, thickness, bookColor[i], g2d);

  // 绘制月销售量折线
  g2d.drawPolyline(month, sales, sales.length);

  // 绘制图例
  g2d.setColor(bookColor[i]);
  g2d.fillRect(30, 140+i * 20, 10, 10);
  g2d.setColor(Color.BLACK);
  g2d.drawString(bookTitle[i], 45, 150+i * 20);
}

// 部署图形
g2d.dispose();

// 利用ImageIO类的write方法对图像进行编码
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
%>

<%
/**************************************************************************
 * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓).  *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors of this code have used their                   *
 * best efforts in preparing the code. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these codes. The authors *
 * shall not be liable in any event for incidental or consequential       *
 * damages in connection with, or arising out of, the furnishing,         *
 * performance, or use of these programs.                                 *
 **************************************************************************/
%>

⌨️ 快捷键说明

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