H5 Svg 半圆圆环占比图

效果图
在这里插入图片描述
主逻辑

/* 虚线长度 */
stroke-dasharray  
/* 偏移 */
stroke-dashoffset 

代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./assets/global.css">

    <style>
        .circle-progress-bar {
            --width: 300px;
            --stroke-width: 12px;
            --height: calc((var(--width) + var(--stroke-width)) / 2);
            width: var(--width);
            height: var(--height);

            overflow: hidden;
            position: relative;
        }

        .circle-progress-bar .circle-progress-text {
            font-size: 30px;
            position: absolute;
            bottom: 0px;
            width: 100%;
            left: 50%;
            transform: translateX(-50%);
            text-align: center;
        }

        .circle-progress-bar .circle-progress-svg {
            width: var(--width);
            height: var(--width);
            transform: rotate(180deg);
        }

        .circle-progress-bar .circle-progress-circle {
            /* 周长 */
            stroke-dasharray: 628;
            /* 偏移 */
            stroke-dashoffset: 333;
        }
    </style>
</head>

<body>
    <div class="circle-progress-bar">
        <svg class="circle-progress-svg">
            <circle class="circle-progress-circle-back" stroke-linecap="round" cx="150" cy="150" r="144"
                fill="transparent" stroke-width="12" stroke="grey" />
            <circle class="circle-progress-circle" stroke-linecap="round" cx="150" cy="150" r="144" fill="transparent"
                stroke-width="12" stroke="grey" />
        </svg>
        <div class="circle-progress-text">60%</div>
    </div>

    <script type="module">
        let circleProgressBarDom = document.querySelector('.circle-progress-bar')               // 整体的容器
        let circleProgressCircleDom = document.querySelector('.circle-progress-circle')         // 前面弧线DOM
        let circleProgressCircleBackDom = document.querySelector('.circle-progress-circle-back')// 后面弧线DOM
        let circleProgressTextDom = document.querySelector('.circle-progress-text')             // 文字DOM
        let width = 200;                            // 盒子宽度
        let strokeWidth = 20                        // 线宽
        let r = width / 2 - strokeWidth / 2         // 半径
        let perimeter = (r * 2 * Math.PI).toFixed(0)// 周长
        let ratio = .66;                            // 占比 小数
        let backgroundColor = 'rgb(168, 168, 168)'  // 后面弧线的颜色
        let color = 'rgb(255, 181, 54)'             // 前面弧线的颜色

        // 设置宽度、线宽
        circleProgressBarDom.setAttribute('style', `--width: ${width}px; --stroke-width: ${strokeWidth}px`)
        // 设置中心点、半径、线宽、占比、颜色
        circleProgressCircleBackDom.setAttribute('cx', width / 2)
        circleProgressCircleBackDom.setAttribute('cy', width / 2)
        circleProgressCircleBackDom.setAttribute('r', r)
        circleProgressCircleBackDom.setAttribute('stroke-width', strokeWidth)
        circleProgressCircleBackDom.setAttribute('style', `stroke-dasharray: ${perimeter};stroke-dashoffset: ${perimeter / 2 * 0 + perimeter / 2}`)
        circleProgressCircleBackDom.setAttribute('stroke', backgroundColor)
        // 设置中心点、半径、线宽、占比、颜色
        circleProgressCircleDom.setAttribute('cx', width / 2)
        circleProgressCircleDom.setAttribute('cy', width / 2)
        circleProgressCircleDom.setAttribute('r', r)
        circleProgressCircleDom.setAttribute('stroke-width', strokeWidth)
        circleProgressCircleDom.setAttribute('style', `stroke-dasharray: ${perimeter};stroke-dashoffset: ${perimeter / 2 * (1 - ratio) + perimeter / 2}`)
        circleProgressCircleDom.setAttribute('stroke', color)
        // 设置文字
        circleProgressTextDom.textContent = ratio * 100 + '%'

    </script>

</body>

</html>

在线预览
https://linyisonger.github.io/H5.Examples/
源码仓库
https://github.com/linyisonger/H5.Examples.git

相关推荐

  1. OEEL图表——饼状绘制(各国太阳能发电

    2024-07-17 01:00:03       31 阅读
  2. 同比和

    2024-07-17 01:00:03       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 01:00:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 01:00:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 01:00:03       58 阅读
  4. Python语言-面向对象

    2024-07-17 01:00:03       69 阅读

热门阅读

  1. ardupilot 系统时间见解

    2024-07-17 01:00:03       16 阅读
  2. EFFICIENT MODULATION FOR VISION NETWORKS

    2024-07-17 01:00:03       16 阅读
  3. 里氏替换原则

    2024-07-17 01:00:03       22 阅读
  4. 网络安全-网络安全及其防护措施2

    2024-07-17 01:00:03       22 阅读
  5. Git 的基本概念和使用方式

    2024-07-17 01:00:03       25 阅读
  6. 常见的SQL MODE及其解释

    2024-07-17 01:00:03       23 阅读
  7. [C++]类作用域

    2024-07-17 01:00:03       22 阅读
  8. 入门C语言只需一个星期(星期一)

    2024-07-17 01:00:03       27 阅读