"Bring Your Chart to Life: Give Those Bars a Left-to-

I am looking to add animation to a bar chart, with the bars animating from left to right. Currently, they all animate simultaneously.

Is there a way to achieve this effect?

$(function() {
  $("#bars li .bar").each(function() {
    var percentage = $(this).data('percentage');

    $(this).animate({
      'height': percentage + '%'
    }, 1000);
  });
});
@import "lesshat";
@import url(https://fonts.googleapis.com/css?family=Roboto:400+700);
body {
  background: #30303A;
  font-family: Roboto;
}

#chart {
  width: 650px;
  height: 300px;
  margin: 30px auto 0;
  display: block;
}

#chart #numbers {
  width: 50px;
  height: 100%;
  margin: 0;
  padding: 0;
  display: inline-block;
  float: left;
}

#chart #numbers li {
  text-align: right;
  padding-right: 1em;
  list-style: none;
  height: 29px;
  border-bottom: 1px solid #444;
  position: relative;
  bottom: 30px;
}

#chart #numbers li:last-child {
  height: 30px;
}

#chart #numbers li span {
  color: #eee;
  position: absolute;
  bottom: 0;
  right: 10px;
}

#chart #bars {
  display: inline-block;
  background: rgba(0, 0, 0, 0.2);
  width: 600px;
  height: 300px;
  padding: 0;
  margin: 0;
  box-shadow: 0 0 0 1px #444;
}

#chart #bars li {
  display: table-cell;
  width: 100px;
  height: 300px;
  margin: 0;
  text-align: center;
  position: relative;
}

#chart #bars li .bar {
  display: block;
  width: 70px;
  margin-left: 15px;
  background: #49E;
  position: absolute;
  bottom: 0;
}

#chart #bars li .bar:hover {
  background: #5AE;
  cursor: pointer;
}

#chart #bars li .bar:hover:before {
  color: white;
  content: attr(data-percentage) '%';
  position: relative;
  bottom: 20px;
}

#chart #bars li span {
  color: #eee;
  width: 100%;
  position: absolute;
  bottom: -2em;
  left: 0;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="chart">
  <ul id="bars">
    <li>
      <div data-percentage="56" class="bar"></div><span>Option 1</span></li>
    <li>
      <div data-percentage="33" class="bar"></div><span>Option 2</span></li>
    <li>
      <div data-percentage="54" class="bar"></div><span>Option 3</span></li>
    <li>
      <div data-percentage="94" class="bar"></div><span>Option 4</span></li>
    <li>
      <div data-percentage="23" class="bar"></div><span>Option 5</span>
    </li>
  </ul>
</div>

View on JSFiddle

Answer №1

You have the option to customize the duration of the animation for each bar based on their index, allowing you to control how long each one takes to complete.

$("#bars li .bar").each( function( index ) {
    var percentage = $(this).data('percentage');
    console.log(percentage)
    $(this).animate({
      'height' : percentage + '%'
    }, 1000 + index * 500);
});

$(function() {
  $("#bars li .bar").each( function( index ) {
    var percentage = $(this).data('percentage');
    $(this).animate({
      'height' : percentage + '%'
    }, 1000 + index * 500);
  });
});
@import "lesshat";
 @import url(https://fonts.googleapis.com/css?family=Roboto:400+700);
 body {
     background: #30303A;
     font-family: Roboto;
}
 #chart {
     width: 650px;
     height: 300px;
     margin: 30px auto 0;
     display: block;
}
 #chart #numbers {
     width: 50px;
     height: 100%;
     margin: 0;
     padding: 0;
     display: inline-block;
     float: left;
}
 #chart #numbers li {
     text-align: right;
     padding-right: 1em;
     list-style: none;
     height: 29px;
     border-bottom: 1px solid #444;
     position: relative;
     bottom: 30px;
}
 #chart #numbers li:last-child {
     height: 30px;
}
 #chart #numbers li span {
     color: #eee;
     position: absolute;
     bottom: 0;
     right: 10px;
}
 #chart #bars {
     display: inline-block;
     background: rgba(0,0,0,0.2);
     width: 600px;
     height: 300px;
     padding: 0;
     margin: 0;
     box-shadow: 0 0 0 1px #444;
}
 #chart #bars li {
     display: table-cell;
     width: 100px;
     height: 300px;
     margin: 0;
     text-align: center;
     position: relative;
}
 #chart #bars li .bar {
     display: block;
     width: 70px;
     margin-left: 15px;
     background: #49E;
     position: absolute;
     bottom: 0;
}
 #chart #bars li .bar:hover {
     background: #5AE;
     cursor: pointer;
}
 #chart #bars li .bar:hover:before {
     color: white;
     content: attr(data-percentage) '%';
     position: relative;
     bottom: 20px;
}
 #chart #bars li span {
     color: #eee;
     width: 100%;
     position: absolute;
     bottom: -2em;
     left: 0;
     text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="chart">
  <ul id="bars">
    <li><div data-percentage="56" class="bar"></div><span>Option 1</span></li>
    <li><div data-percentage="33" class="bar"></div><span>Option 2</span></li>
    <li><div data-percentage="54" class="bar"></div><span>Option 3</span></li>
    <li><div data-percentage="94" class="bar"></div><span>Option 4</span></li>
    <li><div data-percentage="23" class="bar"></div><span>Option 5</span>
    </li>
  </ul>
</div>

To ensure seamless animations, you can calculate the delay for each bar based on its index.

$("#bars li .bar").each( function( index ) {
    var percentage = $(this).data('percentage');
    console.log(percentage)
    $(this).delay(index * 1000).animate({
      'height' : percentage + '%'
    }, 1000);
});

$(function() {
  $("#bars li .bar").each( function( index ) {
    var percentage = $(this).data('percentage');
    $(this).delay(index * 1000).animate({
      'height' : percentage + '%'
    }, 1000);
  });
});
@import "lesshat";
 @import url(https://fonts.googleapis.com/css?family=Roboto:400+700);
 body {
     background: #30303A;
     font-family: Roboto;
}
 #chart {
     width: 650px;
     height: 300px;
     margin: 30px auto 0;
     display: block;
}
 #chart #numbers {
     width: 50px;
     height: 100%;
     margin: 0;
     padding: 0;
     display: inline-block;
     float: left;
}
 #chart #numbers li {
     text-align: right;
     padding-right: 1em;
     list-style: none;
     height: 29px;
     border-bottom: 1px solid #444;
     position: relative;
     bottom: 30px;
}
 #chart #numbers li:last-child {
     height: 30px;
}
 #chart #numbers li span {
     color: #eee;
     position: absolute;
     bottom: 0;
     right: 10px;
}
 #chart #bars {
     display: inline-block;
     background: rgba(0,0,0,0.2);
     width: 600px;
     height: 300px;
     padding: 0;
     margin: 0;
     box-shadow: 0 0 0 1px #444;
}
 #chart #bars li {
     display: table-cell;
     width: 100px;
     height: 300px;
     margin: 0;
     text-align: center;
     position: relative;
}
 #chart #bars li .bar {
     display: block;
     width: 70px;
     margin-left: 15px;
     background: #49E;
     position: absolute;
     bottom: 0;
}
 #chart #bars li .bar:hover {
     background: #5AE;
     cursor: pointer;
}
 #chart #bars li .bar:hover:before {
     color: white;
     content: attr(data-percentage) '%';
     position: relative;
     bottom: 20px;
}
 #chart #bars li span {
     color: #eee;
     width: 100%;
     position: absolute;
     bottom: -2em;
     left: 0;
     text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="chart">  
  <ul id="bars">
    <li><div data-percentage="56" class="bar"></div><span>Option 1</span></li>
    <li><div data-percentage="33" class="bar"></div><span>Option 2</span></li>
    <li><div data-percentage="54" class="bar"></div><span>Option 3</span></li>
    <li><div data-percentage="94" class="bar"></div><span>Option 4</span></li>
    <li><div data-percentage="23" class="bar"></div><span>Option 5</span>
    </li>
  </ul>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I use jQuery UI to slide a div, while also smoothly moving the adjacent div to take its place?

Wishing you an amazing New Year! I am looking to create a smooth sliding effect for a div when a button is clicked. I want the adjacent div to slide alongside it seamlessly, without any clunky motions or delays. Currently, the adjacent div only moves afte ...

Verifying the size of the unordered list and deleting a list item

I'm attempting to remove the last <li> element from a <ul> element only if it goes beyond a certain length. To achieve this, I have implemented the following code: var selector = "#ulelement" if($(selector).children().length > threshol ...

An issue occurred while trying to resolve static symbol values

We are encountering a build error in our Angular 2 application, specifically when using "ng build --prod", as opposed to when using "ng build" or "ng serve". The error message that appears is: ERROR in Error encountered resolving symbol values statically. ...

When using React.js, clicking on an answer option will change the color of all options, not just the one that was clicked

I'm currently working on my quiz page where all answer options change color when clicked. However, I only want the selected answer option to be colored based on its correctness. The data is being retrieved from a data.json array. export default functi ...

How to align content within a FormControlLabel using Material UI

My goal is to line up the label and radio button inside a FormControlLabel component so that the label occupies the same width regardless of its content. The current appearance can be seen below: https://i.stack.imgur.com/GhXed.png Below is the code I am ...

Ajax request in Rails not receiving a response from the controller

After troubleshooting a simple GET request to the controller action, I confirmed that the request is being made successfully and there are no issues with the controller executing the action. However, I am not receiving any response data. $.ajax({ url: ...

Struggling to transfer information from JavaScript to Python Flask?

I am currently working on a basic configuration where I need to send a string to my Flask application through Ajax, but unfortunately, I am encountering Error code 400: Bad request. Here is the JavaScript code snippet: headers = { 'Content-type&a ...

Utilize Mongoose's post method to generate a fresh collection without any data

Libraries in use: Express, Mongoose, Express-Restify-Mongoose Challenge: I am currently exploring the process of creating a POST request that involves providing the schema within the req.body. My goal is to seamlessly generate a new collection if it doesn ...

SVGs appear at the forefront of all other elements

is where this issue is arising.... specifically in the date selection feature on the left side of the options panel. When using both Google Charts and Material Icons with the date picker component from https://github.com/nickeljew/react-month-picker, we a ...

Some examples of CSS styling that makes the parent element shorter than the child element

Currently, I am engaged in a project aimed at enhancing my understanding of Spring MVC practices. As part of this endeavor, I have embarked on the development of a simplified version of Twitter. Essentially, users can log in, share brief updates, and view ...

Error: Unable to Locate the jqueryFileTree Connector Script

I am currently working on a project to build a webpage that showcases a file tree. The jqueryFileTree plugin from the following site has been selected for this purpose: My plan is to eventually connect this tree to a server. But for now, my focus is on ge ...

Tips for structuring these components using HTML/CSS to work around DOMPDF's inability to handle floats

The concept I am trying to convey is illustrated in the image below. My goal is to display a user's car picture with the name underneath. Each image/name pair should be contained within a DIV so that as the user adds more cars, they will automaticall ...

Stop text from wrapping around the amazing fonts image

I am facing an issue with displaying an image using awesomefont character with text next to it. The problem is that I want the text to be right-justified beside the image instead of appearing below it. Despite trying different alignment codes and container ...

Transferring Variables to the Following Page Without Using a Form or Changing the URL

I have a question about passing a variable along with a link to exampleA.php without using URL extensions or form submitting. I've done some research and found that most people suggest using Ajax, but I want to handle the variable once I'm on the ...

Updating View in Angular 2 ngClass Issue

I'm encountering some challenges with updating my view using ngClass despite the back end updating correctly. Below is my controller: @Component({ selector: 'show-hide-table', template: ' <table> <thead> ...

Traverse JSON object as a string

I'm new to working with JavaScript. I have a JSON string that was created using the Google Gson API, and I'm passing this string to a JavaScript function. The JSON string looks like this: '{"validationCode":"V10291","caseNumber":"2010CF1010 ...

Using a Fake Timer to test the setTimeout function for a class method is not possible

One way to test setTimeout with a normal function is by using the following code: function doAsync() { setTimeout(doAsync, 1000) } jest.useFakeTimers() test("setTimeout with function", async () => { doAsync() jest.advanceTimersByTime(2 ...

Load Bootstrap tab activation

I'm having trouble getting the active class to work on my tabs. I've tried using body onload click trigger, showing tabs by ID, and many other methods, but nothing seems to be working. I have implemented hashed URLs to allow for individual tab li ...

Why did the bootstrap installation in my project fail?

Previously, everything on my website was functioning correctly. However, upon launching it now, I noticed that the carousel, buttons, and other elements are broken. It seems like there might be an issue with the Bootstrap CDN as the buttons and sliders are ...

Is it possible to alter the hyperlink (href) dynamically according to the H2 content?

I am currently assisting a major affiliate website in dynamically replacing their links. They have expressed a desire to utilize the content of the H2 tags that follow their images with the class name wp-block-image. This is an example of the code: < ...