Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

              
                <div class="widget-wrap">
  <h1>SIMPLE JS PIECHART</h1>
  <!-- (A) DEMO -->
  <div id="demo"></div>

  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-javascript-pie-chart/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) PIE + DONUT CHART */
.pie, .hole { border-radius: 50%; }
.pie { width: 300px; height: 300px; }
.hole { width: 150px; height: 150px; background: #fff; }
.donut {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* (B) LEGEND */
.legend {
  display: grid;
  grid-template-columns: 50px auto;
  margin-top: 30px;
}
.legend div { padding: 10px; }

/* (X) DOES NOT MATTER */
* {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1608830597395-6738a70ff2bd?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2Nzg5NDc4NDk&ixlib=rb-4.0.3&q=80);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

/* WIDGET */
.widget-wrap {
  min-width: 600px;
  padding: 30px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.8);
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 50px;
}
#code-boxx a {
  display: inline-block;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                function pie (instance) {
  // (A) RANDOM COLOR HELPER
  // credit : https://stackoverflow.com/questions/1484506/random-color-generator
  var rnd = () => {
    let letters = "0123456789ABCDEF", color = "#";
    for (let i=0; i<6; i++) { color += letters[Math.floor(Math.random() * 16)]; }
    return color;
  };

  // (B) TOTAL VALUE + RANDOM COLORS IF REQUIRED
  var total = 0;
  for (let i of instance.data) {
    total += i[1];
    if (i[2] == undefined) { i[2] = rnd(); }
  }

  // (C) CREATE HTML
  var chart = document.createElement("div"),
      legend = document.createElement("div");
  chart.className = "pie";
  legend.className = "legend";
  instance.target.appendChild(chart);
  instance.target.appendChild(legend);
  if (instance.donut) {
    var donut = document.createElement("div");
    donut.className = "hole";
    chart.classList.add("donut")
    chart.appendChild(donut);
  }

  // (D) CREATE SEGMENTS
  var css = "background: conic-gradient(",
      from = 0, to = 0, slice;
  for (let i of instance.data) {
    // (D1) PIE SLICE
    slice = Math.floor((i[1] / total) * 360);
    to += slice;
    css += `${i[2]} ${from}deg ${to}deg,`;
    from = to;

    // (D2) LEGEND
    legend.innerHTML += `<div style="background:${i[2]}"></div>
    <div>${i[0]}</div>`;
  }
  chart.style.cssText = css.slice(0, -1) + ")";
}

window.onload = () => {
  pie({
    target : document.getElementById("demo"), // required, target container
    donut : false, // optional, "convert" to donut chart
    data : [ // required, ["name", quantity, color]
      ["First", 33, "#ff0000"],
      ["Second", 21, "green"],
      ["Third", 14]
    ]
  });
};
              
            
!
999px

Console