使用JSP怎么在网页中绘图?

想加入一个动态折线图。就是根据数据库中的数据进行统计然后作图。需要什么控件请指出。... 想加入一个动态折线图。就是根据数据库中的数据进行统计然后作图。需要什么控件请指出。 展开
 我来答
sgby0212
推荐于2016-12-06 · TA获得超过634个赞
知道小有建树答主
回答量:428
采纳率:0%
帮助的人:195万
展开全部
简单的可以用java.awt.geom java.awt.image 这两个包
// 清空缓冲区
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));
g2d.setColor(bookColor[i]);

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

// 绘制图例
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();
CodeMan
推荐于2016-07-03 · 知道合伙人软件行家
CodeMan
知道合伙人软件行家
采纳数:193 获赞数:505
php开发 html mysql javascript vb

向TA提问 私信TA
展开全部
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));
g2d.setColor(bookColor[i]);

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

// 绘制图例
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();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
DoubleJankin
2007-09-13 · 超过14用户采纳过TA的回答
知道答主
回答量:69
采纳率:0%
帮助的人:0
展开全部
可以使用Jfreechart.jar组件
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式