<%@ page language="java" contentType="image/png;charset=GB2312"
import="java.awt.*"
import="javax.imageio.*"
import="java.awt.geom.*"
import="java.awt.image.*"
import="java.text.*"
%>
<%!
// 绘制饼图的说明
public void drawTips(String tips, Color color, Arc2D.Double arc2d, Graphics2D g2d)
{
Arc2D.Double position = arc2d;
position.setAngleExtent(arc2d.getAngleExtent()/2);
position.x = arc2d.x - 15;
position.y = arc2d.y - 15;
position.width = arc2d.getWidth() + 30 ;
position.height = arc2d.getHeight() + 30;
Point2D.Double startPoint =
(Point2D.Double)position.getStartPoint();
Point2D.Double endPoint = (Point2D.Double)position.getEndPoint();
g2d.setPaint(color);
int stringLength = g2d.getFontMetrics().stringWidth(tips);
if (endPoint.x <= arc2d.getCenterX())
g2d.drawString(tips, (float)endPoint.x - stringLength, (float)endPoint.y);
else
g2d.drawString(tips, (float)endPoint.x , (float)endPoint.y );
}
%>
<%
// 清空缓冲区
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(25, 67, 117),
width/2, height/2, new Color(12, 104, 151), true);
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);
// 绘制图表标题
String chartTitle = "编程类图书销售量统计饼图";
g2d.setColor(Color.ORANGE);
g2d.setFont(new Font("汉鼎繁淡古", Font.PLAIN, 30));
int stringLength = g2d.getFontMetrics().stringWidth(chartTitle);
g2d.drawString(chartTitle, (width - stringLength) / 2, 25 );
// 定义圆弧
Arc2D.Double arc2d = new Arc2D.Double();
double startAngle = 30.0, arcAngle = 0.0;
double rectWidth = 320.0, rectHeight = 280.0;
Point2D.Double p2d = new Point2D.Double((width - rectWidth)/2, 70.0);
int arcType = Arc2D.PIE;
// 声明绘制数据
String bookTitle[] = {"Python", "JAVA", "C#", "Perl", "PHP"};
Color color[] = new Color[5];
color[0] = new Color(99,99,0);
color[1] = new Color(255,169,66);
color[2] = new Color(33,255, 66);
color[3] = new Color(33,0,255);
color[4] = new Color(255,0,66);
double bookSales[] = new double[5];
double totalSales = 0.0 ;
double proportion = 0.0;
for (int i=0; i< bookSales.length; i++)
{
bookSales[i] = 1 + Math.random() * 99;
totalSales += bookSales[i];
}
for(int i = 0 ; i < bookTitle.length; i++)
{
arcAngle = bookSales[i]*360 / totalSales;
proportion = bookSales[i]/totalSales * 100 ;
g2d.setColor( color[i] );
arc2d = new Arc2D.Double(p2d.x, p2d.y, rectWidth,
rectHeight, startAngle, arcAngle, arcType);
// 填充圆弧
g2d.fill( arc2d) ;
// 描绘圆弧轮廓
g2d.setStroke(new BasicStroke(1.2f));
g2d.setPaint(Color.GRAY);
g2d.draw(arc2d);
// 更新圆弧的起始角度
startAngle += arcAngle;
// 格式化浮点数的输出格式
DecimalFormat twoDigits = new DecimalFormat("0.00");
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
// 绘制说明文字
drawTips(bookTitle[i] + " "+twoDigits.format(proportion) + "%",
Color.WHITE, arc2d, g2d);
}
// 部署图形
g2d.dispose();
// 利用ImageIO类的write方法对图像进行编码
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
%>
分享到:
相关推荐
JS 完美画饼图,JS 完美画饼图,JS 完美画饼图
数据分析,画饼图的jupyter notebook代码
asp实现画饼图功能,很有用哦,asp实现画饼图功能,很有用哦
【ASP.NET】按比例画饼图 一个很简单的实例
画饼图 使用html5加javascript技术生成饼图 简单容易理解
C#354-画饼图组件源代码
根据省份位置,在Echarts 地图上画饼图, 可点击放大单个饼图
javascript raphael 画饼图,很方便 很简单 很容易懂!不用更改代码,直接写相应的html格式就好了!全自动生成饼图
一个画饼图的小程序。是立体的效果的,,也可以平面图。。
android achat画饼图,柱图,抽出了chatenginer的柱图和饼图并重写
用CSS绘制饼图,针对移动端上的展示,建议在谷歌浏览器上打开效果更佳,上传了两个html文件,pie.html是css静态画饼图,pieAuto.html是用我自己写的插件可以根据传入的参数动态绘制饼图
MFC饼图类,不支持VC6,如需要修改,请找作者。
LabVIEW中实现饼图显示,提供比较好的人机界面
纯js画饼状图,只需要创建表格,使用非常简单
该DEMO用于绘制饼图,圆弧。根据百分比可以以不同颜色分割饼图。
自定义控件饼状图,android版本,github上的源代码,共享给大家
听说你不会用python画饼图?赶紧进来看一下刚出炉的教程
画饼图组件
iOS 画饼图、折线图、柱状图
此程序主要是用C语言编写的,用TC运行更加方便。可以实现饼图数据自行输入~