Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 378|回复: 0

[jsp]JSP 画java 2D饼图

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-9-30 16:54:11 | 显示全部楼层 |阅读模式
    <%@ page language="java" contentType="image/jpeg;charset=GBK"
    import="java.awt.*, java.awt.image.*, javax.imageio.ImageIO"%>  <%!// 绘制饼图的说明 [code]  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() + 50;
            position.height = arc2d.getHeight() + 50;
            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(252, 197,
        113), width / 2, height / 2, new Color(255, 255, 169), 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.BLACK);
        g2d.setFont(new Font("宋体", Font.PLAIN, 30));
        //int stringLength = g2d.getFontMetrics().stringWidth(chartTitle);
        //g2d.drawString(chartTitle, (width - stringLength) / 2, 30);
        // 定义圆弧
        Arc2D.Double arc2d = new Arc2D.Double();
        double startAngle = 30.0, arcAngle = 0.0;
        double rectWidth = 295.0, rectHeight = 245.0;
        Point2D.Double p2d = new Point2D.Double((width - rectWidth) / 2, 70.0);
        int arcType = Arc2D.PIE;
        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;
        // 定义厚度
        int thickness = 25;
        for (int i = 0; i < bookSales.length; i++) {
            bookSales = 1 + Math.random() * 99;
            totalSales += bookSales;
        }
        // 绘制起始角度不大于90度的圆弧
        startAngle = 30;
        for (int i = 0; i < bookSales.length; i++) {
            arcAngle = bookSales;
            if (startAngle >= 90) {
                break;
            }
            for (int j = thickness; j > 0; j--) {
                g2d.setColor(color.darker());
                arc2d = new Arc2D.Double(p2d.x, p2d.y + j, rectWidth,
                rectHeight, startAngle, arcAngle, arcType);
                // 填充圆弧
                g2d.fill(arc2d);
            }
            // 更新圆弧的起始角度
            startAngle += arcAngle;
        }
        // 绘制起始角度不小于270度的圆弧
        startAngle = 390;
        for (int i = bookSales.length - 1; i > 0; i--) {
            arcAngle = bookSales;
            if (startAngle <= 270) {
                break;
            }
            for (int j = thickness; j > 0; j--) {
                g2d.setColor(color.darker());
                arc2d = new Arc2D.Double(p2d.x, p2d.y + j, rectWidth,
                rectHeight, startAngle, -arcAngle, arcType);
                // 填充圆弧
                g2d.fill(arc2d);
            }
            // 更新圆弧的起始角度
            startAngle -= arcAngle;
        }
        // 绘制起始角度在90度到270度之间的圆弧
        startAngle = 30;
        for (int i = 0; i < bookSales.length; i++) {
            arcAngle = bookSales;
            if (startAngle > 90 && startAngle < 270) {
                for (int j = thickness; j > 0; j--) {
                    g2d.setColor(color.darker());
                    arc2d = new Arc2D.Double(p2d.x, p2d.y + j, rectWidth,
                    rectHeight, startAngle + arcAngle, -arcAngle,
                    arcType);
                    // 填充圆弧
                    g2d.fill(arc2d);
                }
            }
            // 更新圆弧的起始角度
            startAngle += arcAngle;
        }
        // 绘制最上面一层圆弧
        startAngle = 30;
        for (int i = 0; i < bookSales.length; i++) {
            arcAngle = bookSales;//item.getPercent() * 3.6;
            g2d.setColor(color);
            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;
            g2d.setFont(new Font("宋体", Font.PLAIN, 12));
        }
        // 部署图形
        g2d.dispose();
        // 利用ImageIO类的write方法对图像进行编码
        ServletOutputStream sos = response.getOutputStream();
        ImageIO.write(image, "PNG", sos);
        sos.close();
       
        out.clear();
    out = pageContext.pushBody();
    %>       
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-20 16:57 , Processed in 0.410713 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表