Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a pie chart in CSS

Tags:

css

How can I create a pie chart with CSS like the one below?

enter image description here

like image 827
Rohit Azad Malik Avatar asked Apr 05 '12 12:04

Rohit Azad Malik


People also ask

How do I make a pie chart in CSS?

The CSS Setting for the Pie Chart We use aspect-ratio: 1 to make sure the element remains square. We can also use height: var(--w) but it's always good to learn and use a new CSS property. You may wonder why I am using a variable to define the width instead of simply setting width: 150px .


2 Answers

I find this the easiest CSS-only solution. Somewhat simplified below.

    .pieContainer {        height: 150px;        position: relative;      }            .pieBackground {        position: absolute;        width: 150px;        height: 150px;        border-radius: 100%;        box-shadow: 0px 0px 8px rgba(0,0,0,0.5);      }             .pie {        transition: all 1s;        position: absolute;        width: 150px;        height: 150px;        border-radius: 100%;        clip: rect(0px, 75px, 150px, 0px);      }            .hold {        position: absolute;        width: 150px;        height: 150px;        border-radius: 100%;        clip: rect(0px, 150px, 150px, 75px);      }            #pieSlice1 .pie {        background-color: #1b458b;        transform:rotate(30deg);      }            #pieSlice2 {        transform: rotate(30deg);      }            #pieSlice2 .pie {        background-color: #0a0;        transform: rotate(60deg);      }            #pieSlice3 {        transform: rotate(90deg);      }            #pieSlice3 .pie {        background-color: #f80;        transform: rotate(120deg);      }            #pieSlice4 {        transform: rotate(210deg);      }            #pieSlice4 .pie {        background-color: #08f;        transform: rotate(10deg);      }            #pieSlice5 {        transform: rotate(220deg);      }            #pieSlice5 .pie {        background-color: #a04;        transform: rotate(70deg);      }            #pieSlice6 {        transform: rotate(290deg);      }            #pieSlice6 .pie {        background-color: #ffd700;        transform: rotate(70deg);      }            .innerCircle {        position: absolute;        width: 120px;        height: 120px;        background-color: #444;        border-radius: 100%;        top: 15px;        left: 15px;         box-shadow: 0px 0px 8px rgba(0,0,0,0.5) inset;        color: white;      }      .innerCircle .content {        position: absolute;        display: block;        width: 120px;        top: 30px;        left: 0;        text-align: center;        font-size: 14px;      }
    <div class="pieContainer">        <div class="pieBackground"></div>        <div id="pieSlice1" class="hold"><div class="pie"></div></div>        <div id="pieSlice2" class="hold"><div class="pie"></div></div>        <div id="pieSlice3" class="hold"><div class="pie"></div></div>        <div id="pieSlice4" class="hold"><div class="pie"></div></div>        <div id="pieSlice5" class="hold"><div class="pie"></div></div>        <div id="pieSlice6" class="hold"><div class="pie"></div></div>        <div class="innerCircle"><div class="content"><b>Data</b><br>from 16<sup>th</sup> April, 2014</div></div>      </div>
like image 178
MastaBaba Avatar answered Oct 12 '22 21:10

MastaBaba


I saw some people opting for Google Developers Tool, its very tough and it also uses JS and you only want CSS. So here is the most easy way, Pure CSS, made by using background gradient.

.pie {    width: 400px;    height: 400px;    background-image: conic-gradient(orange 64%, blue 17%, black);    border-radius: 50%  }
<div class="pie"></div>
like image 41
Aaditya Pandey Avatar answered Oct 12 '22 22:10

Aaditya Pandey