CSS Vertical Stacked Bar Chart

CSS Vertical Stacked Bar Chart
Code Snippet:Stacked Bar Charts
Author: Adrian Kirsten
Published: March 3, 2024
Last Updated: March 4, 2024
Downloads: 476
License: MIT
Edit Code online: View on CodePen
Read More

This code creates a vertical stacked bar chart using CSS and jQuery. It visually represents data percentages in bars. The JavaScript part adjusts the bar heights based on the data provided, enabling easy data visualization. This chart style is helpful for showcasing proportional data in a visually appealing manner without using traditional chart libraries.

It’s handy for showcasing progress, comparisons, or any data with percentages in a visually engaging way. This method offers a simple, lightweight solution without relying on extensive chart libraries, making it easy to implement and customize.

How to Create CSS Vertical Stacked Bar Chart

1. First of all, load the Google Fonts and jQuery by adding the following CDN links into the head tag of your webpage.

<link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
<script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

2. Within the body section, create a container element to hold the chart. Use nested div or section elements to structure the bars. Assign classes to elements for styling and manipulation purposes.

<div class="container">
  <h1>Stacked Bar Charts</h1>
  <p>A nice and simple stacked bar chart with some CSS &amp; some Jquery.</p>
  
  <!-- BEGIN CIRCLE CODE -->
  <section class="section-stacked-bars">

<section class="report report-usage-daily" id="reveal-usage">
	<ul class="vertical-bars">
		<li class="bar">
			<span class="usage" data-usage="20">
				<span class="usage-value">
					<small>20%</small>
				</span>
				<small class="legend">80%</small>
			</span>
		</li>
		<li class="bar">
			<span class="usage" data-usage="40">
				<span class="usage-value">
					<small>40%</small>
				</span>
				<small class="legend">60%</small>
			</span>
		</li>
		<li class="bar">
			<span class="usage" data-usage="60">
				<span class="usage-value">
					<small>60%</small>
				</span>
				<small class="legend">40%</small>
			</span>
		</li>
		<li class="bar">
			<span class="usage" data-usage="80">
				<span class="usage-value">
					<small>80%</small>
				</span>
				<small class="legend">40%</small>
			</span>
		</li>
		<li class="bar">
			<span class="usage" data-usage="100">
				<span class="usage-value">
					<small>100%</small>
				</span>
				<small class="legend">0%</small>
			</span>
		</li>
		<li class="bar">
			<span class="usage" data-usage="60">
				<span class="usage-value">
					<small>60%</small>
				</span>
				<small class="legend">40%</small>
			</span>
		</li>
		<li class="bar">
			<span class="usage" data-usage="40">
				<span class="usage-value">
					<small>40%</small>
				</span>
				<small class="legend">60%</small>
			</span>
		</li>
		<li class="bar">
			<span class="usage" data-usage="0">
				<span class="usage-value">
					<small>0%</small>
				</span>
				<small class="legend">100%</small>
			</span>
		</li>
	</ul>
</section>
						
					</section>
  <!-- END CIRCLE CODE -->
  
</div>

3. Define the CSS styles for the chart elements. Customize the colors, dimensions, positions, and typography based on your design preferences. Pay attention to classes like .bar, .usage, and .usage-value for styling the bars.

.vertical-bars .bar .usage {
  display: block;
  background: #e67e22;
  height: 250px;
  width: 40px;
  position: relative;
  font-size: small;
}
.vertical-bars .bar .usage small {
  position: absolute;
  top: 0;
  right: -35px;
}
.vertical-bars .bar .usage-value {
  background: #d35400;
  display: block;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 80%;
}
.vertical-bars .bar .usage-value small {
  background: #2c3e50;
  z-index: 5;
}

body {
  background: #2c3e50;
}

.container {
  width: 940px;
  margin: 0 auto;
  color: #ecf0f1;
  font-family: "Open Sans", sans-serif;
  text-align: center;
}

h1 {
  text-transform: uppercase;
}

p {
  margin-bottom: 48px;
}

a {
  color: #ecf0f1;
}

ul {
  margin: 0 0 0 -10px;
  padding: 0;
}
ul li {
  display: inline-block;
  margin-left: 10px;
  width: 10%;
  text-align: left;
}

4. Finally, add the following jQuery script to manipulate the chart elements dynamically. Target the data attributes within the HTML elements and adjust the height of the bars accordingly. Utilize $(document).ready() to ensure the DOM is fully loaded before executing the script.

$(document).ready(function() {
	var graph = $('.usage');
	$.each(graph, function() {
	
		var usage = $(this).attr('data-usage');
		$(this).children('.usage-value').css({
			'height': + usage +'%',
		});
		
	});
});

That’s all! hopefully, you have successfully created a Vertical Stacked Bar Chart using jQuery and CSS. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X