CSS3 simple donut chart

前端 未结 5 1742
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 20:17

What I\'m trying to do is create a simple donut chart. I\'m currently using CSS(3) only but I don\'t know if it\'s possible without javascript.

What I have so far:

5条回答
  •  Happy的楠姐
    2020-12-13 21:21

    SVG for the win!

    .item {
        position: relative;
        float: left;
    }
    
    .item h2 {
        text-align:center;
        position: absolute;
        line-height: 125px;
        width: 100%;
    }
    
    svg {
       -webkit-transform: rotate(-90deg);
        transform: rotate(-90deg);
    }
    
    .circle_animation {
      stroke-dasharray: 440; /* this value is the pixel circumference of the circle */
      stroke-dashoffset: 440;
    }
    
    .html .circle_animation {
        -webkit-animation: html 1s ease-out forwards;
        animation: html 1s ease-out forwards;
    }
    
    .css .circle_animation {
        -webkit-animation: css 1s ease-out forwards;
        animation: css 1s ease-out forwards;
    }
    
    @-webkit-keyframes html {
      to {
        stroke-dashoffset: 80; /* 50% would be 220 (half the initial value specified above) */
      }
    }
    
    @keyframes html {
      to {
        stroke-dashoffset: 80;
      }
    }
    
    @-webkit-keyframes css {
      to {
        stroke-dashoffset: 160;
      }
    }
    
    @keyframes css {
      to {
        stroke-dashoffset: 160;
      }
    }

    HTML

    Layer 1

    CSS

    Layer 1

    JSFiddle version


    Here is a version with background circles as requested in the comments:

    .item {
        position: relative;
        float: left;
    }
    
    .item h2 {
        text-align:center;
        position: absolute;
        line-height: 125px;
        width: 100%;
    }
    
    svg {
       -webkit-transform: rotate(-90deg);
        transform: rotate(-90deg);
    }
    
    .circle_animation {
      stroke-dasharray: 440; /* this value is the pixel circumference of the circle */
      stroke-dashoffset: 440;
    }
    
    .html .circle_animation {
        -webkit-animation: html 1s ease-out forwards;
        animation: html 1s ease-out forwards;
    }
    
    .css .circle_animation {
        -webkit-animation: css 1s ease-out forwards;
        animation: css 1s ease-out forwards;
    }
    
    @-webkit-keyframes html {
      to {
        stroke-dashoffset: 80; /* 50% would be 220 (half the initial value specified above) */
      }
    }
    
    @keyframes html {
      to {
        stroke-dashoffset: 80;
      }
    }
    
    @-webkit-keyframes css {
      to {
        stroke-dashoffset: 160;
      }
    }
    
    @keyframes css {
      to {
        stroke-dashoffset: 160;
      }
    }

    HTML

    Layer 1

    CSS

    Layer 1


    How does it work?

    stroke-dasharray is used to define the 'pattern' a dashed line uses (MDN). By providing a single value you create a pattern with a dash of 440px and a space of 440px. (440px is roughly the circumference of the circle).

    stroke-dashoffset effectively moves the starting point of the dash pattern (MDN).

    A dash-offset of 220 (half of the stroke-dasharray) would produce a half-circle. 110 = quarter circle etc.

提交回复
热议问题