Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section class="row">
  <div class="column">
    <div class="donut-chart" data-donut-chart="1"></div>
  </div>
  <div class="column">
    <div class="donut-chart" data-donut-chart="2"></div>
  </div>
</section>

              
            
!

CSS

              
                /**
  * Demo scaffolding
  * --------------------------------------------------
  */
*,
*:before,
*:after
{
    -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
            box-sizing: border-box;
}

body
{
    position: relative;

    margin: 0;

    background: #434a54;

    text-rendering: optimizeSpeed;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.row
{
    display: block;

    width: 300px;
    margin: 50px auto;

    -webkit-animation: fadeIn .4s ease-in .4s both;
            animation: fadeIn .4s ease-in .4s both;
}
.row:before,
.row:after
{
    display: table;

    content: ' ';
}
.row:after
{
    clear: both;
}
@media only screen and (min-width: 600px)
{
    .row
    {
        width: 600px;
    }
}

.column
{
    float: left;

    width: 300px;
    padding: 25px;
}

@-webkit-keyframes fadeIn
{
    0%
    {
        opacity: 0;
    }

    100%
    {
        opacity: 1;
    }
}

@keyframes fadeIn
{
    0%
    {
        opacity: 0;
    }

    100%
    {
        opacity: 1;
    }
}
/**
  * Donut Chart
  * --------------------------------------------------
  */
.donut-chart
{
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;

    position: relative;

    overflow: hidden;

    width: 250px;
    height: 250px;

    border-radius: 50%;
    background-color: #f5f7fa;
    -webkit-box-shadow: 0 0 0 6px rgba(0, 0, 0, .2);
            box-shadow: 0 0 0 6px rgba(0, 0, 0, .2);
}

.outer-circle,
.inner-circle
{
    position: absolute;

    border-radius: 50%;
}

.outer-circle
{
    top: 6%;
    right: 6%;
    bottom: 6%;
    left: 6%;

    background-color: #fff;
    -webkit-box-shadow: inset 0 0 0 6px rgba(0, 0, 0, .2);
            box-shadow: inset 0 0 0 6px rgba(0, 0, 0, .2);
}

.inner-circle
{
    top: 22%;
    right: 22%;
    bottom: 22%;
    left: 22%;

    background-color: #f5f7fa;
    -webkit-box-shadow: 0 0 0 6px rgba(0, 0, 0, .2);
            box-shadow: 0 0 0 6px rgba(0, 0, 0, .2);
}

.inner-circle-label,
.inner-circle-value
{
    position: absolute;
    top: 50%;
    left: 0;

    display: block;

    width: 100%;

    text-align: center;
}

.inner-circle-label
{
    font-size: 18px;
    line-height: 1;

    margin-top: -30px;

    color: #aab2bd;
}

.inner-circle-value
{
    font-size: 42px;
    font-weight: bold;
    line-height: 1;

    margin-top: -12px;

    color: #656d78;
}

.wedge-container,
.wedge,
.wedge-extension,
.wedge-label
{
    position: absolute;
    top: 0;
    left: 0;

    width: 100%;
    height: 100%;

    -webkit-transition: all .4s linear;
            transition: all .4s linear;

    border-radius: 50%;
}

.wedge,
.wedge-extension
{
    -webkit-box-shadow: inset 0 0 0 6px rgba(0, 0, 0, .2);
            box-shadow: inset 0 0 0 6px rgba(0, 0, 0, .2);
}

.wedge-value
{
    font-size: 14px;
    font-weight: bold;
    line-height: 20px;

    position: absolute;
    top: 5%;
    left: 50%;

    display: block;

    width: 30px;
    margin-left: -15px;

    text-align: center;
}

.wedge-extension
{
    z-index: 100;
}

.wedge
{
    z-index: 200;
}

.wedge-label
{
    z-index: 300;
}

              
            
!

JS

              
                /**
  * Donut Chart
  * --------------------------------------------------
  */

;(function(window, document, undefined) {

  'use strict';

  var DonutChart = DonutChart || {

    /**
      * Initialize Chart
      */

    init: function(options) {

      this.settings(options);
      this.createChartStructre();
      this.setChartMeta();
      this.build();

    }, // init()

    /**
      * Update Chart
      */

    update: function(options) {

      this.settings(options);
      this.setChartMeta();
      this.build();

    }, // update()

    /**
      * Chart Settings
      */

    settings: function(options) {

      this.config = {
        container: options.container ? options.container : this.config.container,
        data: options.data ? options.data : this.config.data,
        label: options.label ? this.label : 'Total',
        offset: options.offset ? options.offset : 0
      };

    }, // settings()

    /**
      * Build chart
      */

    build: function() {

      var container = this.config.container.querySelector('.outer-circle');
      var wedges = container.querySelectorAll('[data-wedge-id]');
      var wedgesIdArray = [];

      for (var i = 0; i < wedges.length; i++) {
        wedgesIdArray.push(wedges[i].dataset.wedgeId);
      }

      for (var j = 0; j < this.config.data.wedges.length; j++) {
        if (wedgesIdArray.indexOf(this.config.data.wedges[j].id) == -1) {
          var wedge = this.createWedge(this.config.data.wedges[j]);
          container.appendChild(wedge);
        }
        this.setWedge(this.config.data.wedges[j]);
      }

    }, // createWedges()

    /**
      * Create chart structure
      */

    createChartStructre: function() {

      var outer = document.createElement('div');
      var inner = document.createElement('div');
      var label = document.createElement('span');
      var value = document.createElement('span');

      outer.className = 'outer-circle';
      inner.className = 'inner-circle';
      label.className = 'inner-circle-label';
      value.className = 'inner-circle-value';

      this.config.container.appendChild(outer);
      this.config.container.appendChild(inner);
      inner.appendChild(label);
      inner.appendChild(value);

    }, // createChartStructre()

    /**
      * Set chart meta
      */

    setChartMeta: function() {

      var label = this.config.container.querySelector('.inner-circle-label');
      var value = this.config.container.querySelector('.inner-circle-value');

      label.innerHTML = this.config.label;
      value.innerHTML = this.config.data.total;

    }, // setChartMeta()

    /**
      * Create wedge
      */

    createWedge: function(data) {

      var container = document.createElement('div');
      var wedge = document.createElement('div');
      var extension = document.createElement('div');
      var label = document.createElement('div');
      var value = document.createElement('span');

      container.setAttribute('data-wedge-id', data.id);

      container.className = 'wedge-container';
      wedge.className = 'wedge';
      extension.className = 'wedge-extension';
      label.className = 'wedge-label';
      value.className = 'wedge-value';

      container.appendChild(wedge);
      container.appendChild(extension);
      container.appendChild(label);
      label.appendChild(value);

      return container;

    }, // createWedge()

    /**
      * Set wedge
      */

    setWedge: function(data) {

      var container = this.config.container.querySelector('[data-wedge-id="' + data.id + '"]');
      var wedge = container.querySelector('.wedge');
      var extension = container.querySelector('.wedge-extension');
      var label = container.querySelector('.wedge-label');
      var value = container.querySelector('.wedge-value');

      var wedgeDegrees = (360 * data.value) / this.config.data.total;
      var labelDegrees = wedgeDegrees / 2;
      var w = container.offsetWidth;

      container.style.transform = 'rotate(' + this.config.offset + 'deg)';
      container.style.webkitTransform = 'rotate(' + this.config.offset + 'deg)';
      container.style.clip = wedgeDegrees > 180 ? 'auto' : 'rect(0, ' + w + 'px, ' + w +'px, ' + (w / 2) + 'px)';

      wedge.style.transform = 'rotate(' + wedgeDegrees + 'deg)';
      wedge.style.webkitTransform = 'rotate(' + wedgeDegrees + 'deg)';
      wedge.style.backgroundColor = this.color(data.color, 5);
      wedge.style.clip = 'rect(0, ' + (w / 2) + 'px, ' + w +'px, 0)';

      if (wedgeDegrees > 180) {
        extension.style.opacity = 1;
        extension.style.transform = 'rotate(' + 180 + 'deg)';
        extension.style.webkitTransform = 'rotate(' + 180 + 'deg)';
        extension.style.backgroundColor = this.color(data.color, 5);
        extension.style.clip = 'rect(0, ' + (w / 2) + 'px, ' + w +'px, 0)';
      } else {
        extension.style.opacity = 0;
      }

      label.style.transform = 'rotate(' + labelDegrees + 'deg)';
      label.style.webkitTransform = 'rotate(' + labelDegrees + 'deg)';
      label.style.color = this.color(data.color, -30);

      value.innerHTML = data.value;

      this.config.offset += wedgeDegrees;

    }, // setWedge()

    /**
      * Color Utility
      */

    color: function( color, percent ) {

      var num = parseInt(color.slice(1), 16);
      var amt = Math.round(2.55 * percent);
      var R = (num >> 16) + amt;
      var B = (num >> 8 & 0x00FF) + amt;
      var G = (num & 0x0000FF) + amt;

      return "#" + (0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000 + (B < 255 ? B < 1 ? 0 : B : 255) * 0x100 + (G < 255 ? G < 1 ? 0 : G : 255)).toString(16).slice(1);

    } // color()

  }; // DonutChart

  window.DonutChart = DonutChart;

})(window, document);


/**
  * Demo
  * --------------------------------------------------
  */

// Object.create() polyfill
if (typeof Object.create !== 'function') {
  Object.create = function(obj) {
    function F() {}
    F.prototype = obj;
    return new F();
  };
}

// Select containers
var chartContainer1 = document.querySelector('[data-donut-chart="1"]');
var chartContainer2 = document.querySelector('[data-donut-chart="2"]');

// Data
var chartData1a = {
  total: 64,
  wedges: [
    { id: 'a', color: '#4FC1E9', value: 10 },
    { id: 'b', color: '#A0D468', value: 16 },
    { id: 'c', color: '#ED5565', value: 24 },
    { id: 'd', color: '#AC92EC', value: 14 }
  ]
};

var chartData1b = {
  total: 96,
  wedges: [
    { id: 'a', color: '#4FC1E9', value: 26 },
    { id: 'b', color: '#A0D468', value: 20 },
    { id: 'c', color: '#ED5565', value: 18 },
    { id: 'd', color: '#AC92EC', value: 32 }
  ]
};

var chartData2a = {
  total: 200,
  wedges: [
    { id: 'a', color: '#5D9CEC', value: 45 },
    { id: 'b', color: '#48CFAD', value: 25 },
    { id: 'c', color: '#FFCE54', value: 30 },
    { id: 'd', color: '#FC6E51', value: 100 }
  ]
};

var chartData2b = {
  total: 220,
  wedges: [
    { id: 'a', color: '#5D9CEC', value: 65 },
    { id: 'b', color: '#48CFAD', value: 40 },
    { id: 'c', color: '#FFCE54', value: 60 },
    { id: 'd', color: '#FC6E51', value: 55 }
  ]
};

// Create new chart objects
var Chart1 = Object.create(DonutChart);
var Chart2 = Object.create(DonutChart);

Chart1.init({
  container: chartContainer1,
  data: chartData1a
});

Chart2.init({
  container: chartContainer2,
  data: chartData2a
});

setTimeout(function() {
  setInterval(function() {

    Chart1.update({
      data: chartData1a
    });

    Chart2.update({
      data: chartData2a
    });

  }, 4000);
}, 2000);

setInterval(function() {

  Chart1.update({
    data: chartData1b
  });

  Chart2.update({
    data: chartData2b
  });

}, 4000);

              
            
!
999px

Console