📄 vbarchart.jsp
字号:
<!--
Fig. 7.5_04_02: vBarChart.jsp
功能: 基于Java2D的Web图表实例4: 垂直立方图
-->
<%@ 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.*"
%>
<%
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/png");
// 创建一个 500X375 的图像
int width = 500, height = 375;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 创建Java2D对象
Graphics2D g2d = image.createGraphics();
// 填充整个背景
g2d.setPaint(Color.WHITE);
g2d.fillRect(0, 0, width, height);
// 绘制阴影,由灰色渐进圆角矩形组成
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(5, 5, width-5, height-5 , 50, 50);
g2d.fill(bgRR);
// 绘制渐进蓝色圆角矩形背景
GradientPaint blueGP = new GradientPaint(0, 0, new Color(14, 97, 147),
0, height, new Color(240, 243, 247), false);
g2d.setPaint(blueGP);
g2d.fillRoundRect(0, 0, width - 5, height - 5, 50, 50);
// 绘制深蓝色圆角矩形轮廓
BasicStroke bs = new BasicStroke(1.2f);
g2d.setStroke(bs);
g2d.setPaint(new Color(55,71, 105));
g2d.drawRoundRect(0, 0, width -5, height -5, 50, 50);
// 绘制图表绘图区域背景的阴影效果
Rectangle2D.Float drawArea =
new Rectangle2D.Float(63, 48, 400, 300);
g2d.setPaint(Color.GRAY);
g2d.fill(drawArea);
// 填充图表绘图区域背景
g2d.setPaint(Color.WHITE);
drawArea = new Rectangle2D.Float(60,45, 400, 300);
g2d.fill(drawArea);
// 描绘图表绘图区域的轮廓
g2d.setPaint(Color.BLACK);
g2d.draw(drawArea);
// 绘制图表标题
String chartTitle = "计算机编程类图书2004年月销售量统计图";
g2d.setFont(new Font("华文隶书", Font.PLAIN, 25));
int stringLength = g2d.getFontMetrics().stringWidth(chartTitle);
g2d.setColor(Color.WHITE);
g2d.drawString(chartTitle, (width - stringLength) / 2, 25 );
// 创建虚线笔划
float[] dashes = { 3.f };
bs = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
10, dashes, 0);
g2d.setStroke(bs);
String str = "";
stringLength = 0;
g2d.setPaint(Color.BLACK);
g2d.setFont(new Font("宋体", Font.PLAIN, 12));
for (int i = 1; i <= 12; i++)
{
// 绘制垂直方向虚线
g2d.drawLine(60+i * 400/13, 45, 60+i * 400/13, 345);
// 绘制横轴上月份的说明文字
str += i + "月";
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 60+i * 400/13 - stringLength / 2, 360);
// 重置月份说明文字
str = "";
}
g2d.setFont(new Font("Arial", Font.BOLD, 14));
str = "";
int stringHeight = 0;
for (int i = 0; i <= 300; i += 30)
{
// 绘制水平方向虚线
g2d.drawLine(60, 45+i, 460, 45+i);
// 绘制纵轴上销售量的说明文字
str += 100-i / 3;
stringHeight = g2d.getFontMetrics().getAscent();
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 55-stringLength, 45+i + stringHeight / 2);
str = "";
}
String[] bookTitle = { "JAVA", "C#" };
Color[] bookColor = {new Color(230,111, 71) , new Color(107,165,239)};
double[] sales = new double[12];
int[] month = new int[12];
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
g2d.setStroke(new BasicStroke());
Rectangle2D.Double bar ;
for (int i = 0; i < bookTitle.length; i++)
{
// 初始化绘制数据
double bookSales = 0.0;
for (int j = 0; j < sales.length; j++)
{
bookSales = 1 + Math.random() * 295;
sales[j] = 345 - bookSales ;
month[j] = 60+ ((j+1) * 400)/13;
bar = new Rectangle2D.Double(month[j]-6 + i *10,
sales[j]+2, 10, bookSales-2);
// 填充直方图阴影
g2d.setPaint(Color.GRAY);
g2d.fill(bar);
bar = new Rectangle2D.Double(month[j]-10 + i *10,
sales[j], 10, bookSales);
GradientPaint drawGP = new GradientPaint(month[j]-10 + i *10,
(int)sales[j], bookColor[i], month[j]-10 + i *10, 345,
bookColor[i].brighter(), false);
// 填充直方图
g2d.setPaint(drawGP);
g2d.fill(bar);
// 描绘直方图轮廓
g2d.setPaint(Color.BLACK);
g2d.draw(bar);
}
// 绘制图例
g2d.setColor(bookColor[i]);
g2d.fillRect(418, 50+i * 12, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawString(bookTitle[i], 432, 60+i * 12);
}
// 部署图形
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 + -