Circle Graph

By Kevin Ball

F6
#
How do I use This?

A SCSS based circle graph

HTML
<div class="circle-graph" data-circle-graph data-percent="50">
  <div class="circle-graph-progress">
    <div class="circle-graph-progress-fill"></div>
  </div>
  <div class="circle-graph-percents">
    <div class="circle-graph-percents-wrapper">
      <span class="circle-graph-percents-number"></span>
      <span class="circle-graph-percents-units">of 100</span>
    </div>
  </div>
</div>

@mixin circle-graph-circle($size) {
  content: "";
  position: absolute;
  border-radius: 50%;
  left: calc(50% - #{$size/2});
  top: calc(50% - #{$size/2});
  width: $size;
  height: $size;
}

$circle-graph-size: rem-calc(180);
$circle-graph-percent-number-size: rem-calc(24);
$circle-graph-percent-unit-size: rem-calc(16);
$circle-graph-background-color: $light-gray;
$circle-graph-empty-color: $dark-gray;
$circle-graph-fill-color: $primary-color;

.circle-graph {
  width: $circle-graph-size;
  height: $circle-graph-size;
  border-radius: 50%;
  background-color: $circle-graph-empty-color;
  position: relative;

  &.gt-50 {
    background-color: $circle-graph-fill-color;
  }
}

.circle-graph-progress {
  @include circle-graph-circle($circle-graph-size);
  clip: rect(0, $circle-graph-size, $circle-graph-size, #{$circle-graph-size/2});

  .circle-graph-progress-fill {
    @include circle-graph-circle($circle-graph-size);
    clip: rect(0, #{$circle-graph-size/2}, $circle-graph-size, 0);
    background: $circle-graph-fill-color;
    transform: rotate(60deg);
  }

  .gt-50 & {
    clip: rect(0, #{$circle-graph-size/2}, $circle-graph-size, 0);

    .circle-graph-progress-fill {
      clip: rect(0, $circle-graph-size, $circle-graph-size, #{$circle-graph-size/2});
      background: $circle-graph-empty-color;
    }
  }
}
.circle-graph-percents {
  @include circle-graph-circle(#{$circle-graph-size/1.45});
  background: $circle-graph-background-color;
  text-align: center;
  display: table;
  z-index: 4;

  .circle-graph-percents-number {
    display: block;
    font-size: $circle-graph-percent-number-size;
    font-weight: bold;
    color: $circle-graph-fill-color;
  }

  .circle-graph-percents-units {
    display: block;
    font-size: $circle-graph-percent-unit-size;
    font-weight: bold;
  }
}
.circle-graph-percents-wrapper {
  display: table-cell;
  vertical-align: middle;
  line-height: 0;

  span {
    line-height: 1;
  }
}


.circle-graph {
  width: 11.25rem;
  height: 11.25rem;
  border-radius: 50%;
  background-color: #8a8a8a;
  position: relative;
}

.circle-graph.gt-50 {
  background-color: #1779ba;
}

.circle-graph-progress {
  content: "";
  position: absolute;
  border-radius: 50%;
  left: calc(50% - 5.625rem);
  top: calc(50% - 5.625rem);
  width: 11.25rem;
  height: 11.25rem;
  clip: rect(0, 11.25rem, 11.25rem, 5.625rem);
}

.circle-graph-progress .circle-graph-progress-fill {
  content: "";
  position: absolute;
  border-radius: 50%;
  left: calc(50% - 5.625rem);
  top: calc(50% - 5.625rem);
  width: 11.25rem;
  height: 11.25rem;
  clip: rect(0, 5.625rem, 11.25rem, 0);
  background: #1779ba;
  -webkit-transform: rotate(60deg);
      -ms-transform: rotate(60deg);
          transform: rotate(60deg);
}

.gt-50 .circle-graph-progress {
  clip: rect(0, 5.625rem, 11.25rem, 0);
}

.gt-50 .circle-graph-progress .circle-graph-progress-fill {
  clip: rect(0, 11.25rem, 11.25rem, 5.625rem);
  background: #8a8a8a;
}

.circle-graph-percents {
  content: "";
  position: absolute;
  border-radius: 50%;
  left: calc(50% - 7.75862rem/2);
  top: calc(50% - 7.75862rem/2);
  width: 7.75862rem;
  height: 7.75862rem;
  background: #e6e6e6;
  text-align: center;
  display: table;
  z-index: 4;
}

.circle-graph-percents .circle-graph-percents-number {
  display: block;
  font-size: 1.5rem;
  font-weight: bold;
  color: #1779ba;
}

.circle-graph-percents .circle-graph-percents-units {
  display: block;
  font-size: 1rem;
  font-weight: bold;
}

.circle-graph-percents-wrapper {
  display: table-cell;
  vertical-align: middle;
  line-height: 0;
}

.circle-graph-percents-wrapper span {
  line-height: 1;
}

JS
$("[data-circle-graph]").each(function() {
  var $graph = $(this),
      percent = parseInt($graph.data('percent'), 10),
      deg = 360*percent/100;
  if(percent > 50) {
    $graph.addClass('gt-50');
  }
  $graph.find('.circle-graph-progress-fill').css('transform','rotate('+ deg +'deg)');
  $graph.find('.circle-graph-percents-number').html(percent+'%');
});