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

Auto Save

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">
  <div id="chart"></div>
  <h1>SIMPLE BAR CHART</h1>
  
  <!-- (A) HORIZONTAL BAR CHART -->
  <div id="demoA"></div>
  
  <!-- (B) VERTICAL BAR CHART -->
  <div id="demoB"></div>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-bar-chart-pure-css-js/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) SHARED FONT & SIZING */
.hbar, .vbar, .hbar *, .vbar * {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}
 
/* (B) BAR CHART CONTAINER */
/* (B1) SHARED */
.hbar, .vbar {
  display: flex;
  width: 100%;
  border: 1px solid #333;
  margin: 20px 0;
}
 
/* (B2) HORIZONTAL */
.hbar {
  flex-direction: column;
  background: repeating-linear-gradient(
    to right, #f2f2f2, #ddd 2px, #fff 2px, #fff 5%
  );
}
 
/* (B3) VERTICAL */
.vbar {
  align-items: flex-end;
  height: 400px;
  background: repeating-linear-gradient(
    to bottom, #f2f2f2, #ddd 2px, #fff 2px, #fff 10%
  );
}
 
/* (C) EACH INDIVIDUAL BAR */
/* (C1) SHARED */
.hbar div, .vbar div {
  color: #fff;
  padding: 10px;
}
 
/* (C2) HORIZONTAL */
.hbar div { margin: 5px 0; }
 
/* (C3) VERTICAL */
.vbar div { width: 100%; }

/* (X) THE REST DOES NOT MATTER - COSMETICS */
/* PAGE & BODY */
* {
  font-family: arial, 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-1523380360198-653413ad178b?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0NjcyNTcwNw&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  text-align: center;
}

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

/* SVG */
#chart {
  width: 100%; height:120px;
  background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 512 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M332.8 320h38.4c6.4 0 12.8-6.4 12.8-12.8V172.8c0-6.4-6.4-12.8-12.8-12.8h-38.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h38.4c6.4 0 12.8-6.4 12.8-12.8V76.8c0-6.4-6.4-12.8-12.8-12.8h-38.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-288 0h38.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-38.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h38.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-38.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zM496 384H64V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-32c0-8.84-7.16-16-16-16z" /></svg>');
  background-repeat: no-repeat;
  background-position: center;
}

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

JS

              
                var bchart = {
  // (A) HELPER FUNCTION - GENERATE RANDOM DARK COLOR
  // source: https://gist.github.com/Chak10/dc24c61c9bf2f651cb6d290eeef864c1
  rcolor : () => {
    let lum = -0.25, rgb = "#", c, i,
        hex = String("#" + Math.random().toString(16).slice(2, 8).toUpperCase()).replace(/[^0-9a-f]/gi, "");
    if (hex.length < 6) { hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; }
    for (i = 0; i < 3; i++) {
      c = parseInt(hex.substr(i * 2, 2), 16);
      c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
      rgb += ("00" + c).substr(c.length);
    }
    return rgb;
  },
 
  // (B) DRAW BAR CHART
  draw : instance => {
    // (B1) INIT CHART CONTAINER
    let hor = instance.type=="h";
    instance.target.classList.add((hor ? "hbar" : "vbar"));
    instance.target.innerHTML = "";
 
    // (B2) GET LARGEST VALUE FROM DATA
    let largest = 0;
    instance.data.forEach(el => {
      if (el[1] > largest) { largest = el[1]; }
    });
 
    // (B3) DRAW CHART
    let bars = new DocumentFragment(), bar, percent;
    instance.data.forEach(el => {
      // (B3-1) CREATE <DIV> BAR
      bar = document.createElement("div");
      bars.appendChild(bar);
 
      // (B3-2) SET WIDTH/HEIGHT
      percent = Math.ceil((el[1] / largest) * 100) + "%";
      if (hor) { bar.style.width = percent; } else { bar.style.height = percent; }
 
      // (B3-3) BACKGROUND COLOR & TEXT
      bar.style.background = el[2] ? el[2] : bchart.rcolor();
      bar.innerHTML = el[0] + ": " + el[1];
    });
    instance.target.appendChild(bars);
  }
};

// (C) INIT CHART
window.addEventListener("load", () => {
  bchart.draw({
    type : "h",
    target : document.getElementById("demoA"),
    data: [["First", 33], ["Second", 100], ["Third", 70]]
  });
  
  bchart.draw({
    type : "v",
    target : document.getElementById("demoB"),
    data: [
      ["First", 33, "#4e0707"],
      ["Second", 100, "#077419"],
      ["Third", 70, "#124789"]
    ]
  });
});
              
            
!
999px

Console