Bar Chart in HTML using JavaScript / jQuery and CSS

A powerful yet lightweight and simple JavaScript charts library to create CSS powered bar chart in HTML. The “bar-chart.js” also works as a jQuery plugin to quickly and easily create beautiful charts. You just need to define your chart data in plugin configuration, then it will create div and CSS based bars dynamically to represents your data. The plugin comes with multiple options to draw basic to advanced bar charts.

Furthermore, you can fully customize the plugin with additional CSS (and with configuration options) .

Plugin Overview

Plugin: Bar Chart
Author: Fabio Papa
Licence: MIT Licence
Published: September 17, 2019
Repository: Fork on GitHub
Dependencies: jQuery 1.3.1 or Latest version, Modernizr JS and Normalize CSS
File Type: zip archive (HTML, CSS & JavaScript )
Package Size: 13.3 KB

How to Create Bar Chart in HTML using JavaScript

1. To create bar charts, we need to getting started with bar-chart.js and with its dependencies. So, load Modernizr JS,jQuery and Normalize CSS by adding the following CDN links into your HTML page.

<!-- Modernizer JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.js"></script>
 
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<!-- Normalize CSS -->	
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css" />

2. Now, also include the Bar Chart plugin’s JavaScript and CSS file (from downloaded project directory).

<!-- Bar Chart CSS -->
<link rel="stylesheet" href="css/bar-chart.css">

<!-- Bar Chart JS -->
<script src="js/plugins.js"></script>
<script src="js/bar-chart.js"></script>

3. After loading all assets, now its time to draw your bar chart. But before this, define a place (a div element) in which your chart will be created dynamically.

<div class="example">
    <p class="description">
      A standard bar chart with no options passed in. As basic as they get!
    </p>
    <div id="basic"></div>
</div>

3. Define all necessary options as follow to draw your bar chart and initialize the plugin.

drawBarChart([ 1, 2, 3, 4, 5 ], {
  title: "Dunder Mifflin—Quabity Assuance Over Time",
  height: "500px",
  width: "800px",
  barSpacing: "5%",
  barColor: "#ace",
  showTicks: true,
  yAxisName: "Blunders (per month)"
}, $("#basic"));