JSFiddle - React, Tailwind, and code Playground
by Pradeep Kumar
HTML
<div class="card">
<div class="donut-chart critical">
<div class="slice one"></div>
<div class="slice two"></div>
<div class="chart-center">
<span></span>
</div>
</div>
</div>
<div class="card">
<div class="donut-chart moderate">
<div class="slice one"></div>
<div class="slice two"></div>
<div class="chart-center">
<span></span>
</div>
</div>
</div>
<div class="card">
<div class="donut-chart success">
<div class="slice one"></div>
<div class="slice two"></div>
<div class="chart-center">
<span></span>
</div>
</div>
</div>
SCSS
body{
background-color:#efefef;
}
.card {
float: left;
padding: 20px;
margin: 0 20px 0 0;
}
.donut-chart {
position: relative;
border-radius: 50%;
overflow: hidden;
.slice {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.chart-center {
position: absolute;
border-radius: 50%;
span {
display: block;
text-align: center;
}
}
}
@mixin donut-chart($name, $perc, $size, $width, $base, $center, $color, $textColor: $color, $textSize: 40px) {
$color2: $color;
$base2: $base;
$deg: ($perc/100*360)+deg;
$deg1: 90deg;
$deg2: $deg;
// If percentage is less than 50%
@if $perc < 50 {
$base: $color;
$color: $base2;
$color2: $base2;
$deg1: ($perc/100*360+90)+deg;
$deg2: 0deg;
}
.donut-chart {
&#{$name} {
width: $size;
height: $size;
background: $base;
.slice {
&.one {
clip: rect(0 $size $size/2 0);
-webkit-transform: rotate($deg1);
transform: rotate($deg1);
background: $color;
}
&.two {
clip: rect(0 $size/2 $size 0);
-webkit-transform: rotate($deg2);
transform: rotate($deg2);
background: $color2;
}
}
.chart-center {
top: $width;
left: $width;
width: $size - ($width * 2);
height: $size - ($width * 2);
background: $center;
span {
font-size: $textSize;
line-height: $size - ($width * 2);
color: $textColor;
&:after {
content: '#{$perc}%';
}
}
}
}
}
}
@include donut-chart('.critical', 0, 200px, 25px, #f00, #fff, #f00);
@include donut-chart('.moderate', 50, 200px, 25px, #fff, #fff, orange);
@include donut-chart('.success', 100, 200px, 25px, #0f0, #fff, #0f0);