Skip to content

Instantly share code, notes, and snippets.

@jkeck
Created November 14, 2015 06:22
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkeck/eddbac03eb746fe4e98b to your computer and use it in GitHub Desktop.
Save jkeck/eddbac03eb746fe4e98b to your computer and use it in GitHub Desktop.
Simple SVG Pie Chart
svg {
width: 100px;
border-radius: 50%;
background:yellowgreen;
transform: rotate(-90deg);
}
circle {
fill: none;
stroke-width: 32;
r: 16;
cx: 16;
cy: 16;
}
circle.first {
stroke: deeppink;
}
circle.second {
stroke: #fb3;
}
circle.third {
stroke: black;
}
<svg viewBox="0 0 32 32">
<circle class='first' stroke-dasharray="75 100"></circle>
<circle class='second' stroke-dasharray="50 100"></circle>
<circle class='third' stroke-dasharray="24 100"></circle>
</svg>
@jpravetz
Copy link

jpravetz commented Sep 17, 2017

Thanks for this. Nice and simple for what I was looking to do

@udayratan
Copy link

How to dynamically create for percentages (40,35,25)

@hoanghuynh
Copy link

@udayratan you can replace stroke-dasharray with the percentage values that you need.

@am35a
Copy link

am35a commented Jan 22, 2021

it doesn't work for FF

@Kymage2010
Copy link

Kymage2010 commented Mar 25, 2021

it doesn't work for FF

It perfect work in FF! Only add PX in rule.

<div class="svg_wrap">
         <svg viewBox="0 0 32 32">
                  <circle class='svg_background'></circle>
                  <circle class='svg_calc' stroke-dasharray="50 100"></circle>
          </svg>
          <div class="svg_value">50</div>
  </div>

and CSS
`

/*pie charts*/

.svg_wrap {
    display: flex;
    align-items: center;
    justify-content: center;
}

.svg_wrap svg {
    width: 100px;
    border-radius: 50%;
    transform: rotate(-90deg);
}

.svg_wrap svg circle {
    fill: none;
    r: 16px;
    cx: 16px;
    cy: 16px;
    stroke-width: 8;
}

.svg_wrap svg circle.svg_background {
    stroke: rgb(225, 225, 225);
}

.svg_wrap svg circle.svg_calc {
    stroke: #296fca;
}

.svg_wrap .svg_value {
    position: absolute;
    font-weight: 500;
}

.svg_wrap .svg_value:after {
    content: "%";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment