Animate Chart Bars from Left to Right

MCM13

I want to animate a bar chart, but I would like for the bars to animate from left to right. As of right now, they all go at the same time.

How can I achieve that?

$(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

iota

You can adjust the time based on the index so the rightermost bars take longer to animate.

$("#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 wait for the previous animation to finish, the delay can be calculated using the 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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Animate Chart Bars from Left to Right

From Dev

Animate div from left to right

From Dev

Animate width from right to left

From Dev

Animate SVG from left to right

From Dev

Animate width from right to left

From Dev

Animate div from left to right

From Dev

Animate div from right to left and then from left to right again

From Dev

How to animate a background image from left to right and from right to left

From Dev

How to change the direction to Right to Left bars in a bar type chart?

From Dev

jQuery animate to left or right from variable

From Dev

iOS: Animate color change from left to right

From Dev

Animate width toggle from right to left in jQuery

From Dev

Animate SVG fill from left to right

From Dev

JQuery animate div from right to left with smoothness

From Dev

How to animate a LAYOUT from right to left?

From Dev

Animate border left to right

From Dev

How to start bars from right to left using amchart?

From Dev

How to animate 2 css layouts from left to right or from right to left with jquery?

From Dev

UIView doesn't animate horizontally from right to left

From Dev

jQuery/CSS animate div width from left to right

From Dev

How to animate full screen stripes moving from left to right

From Dev

Animate image like it's moving from right to left

From Dev

Animate absolute object from right: 0 to left: 0 in Chrome

From Dev

animate a button from left to right in a parent with text align center

From Dev

how to animate image & change the direction from right to left

From Dev

Animate images from right to left like slide show

From Dev

How can I animate from left to right the searchbar in a searchdisplaycontroller?

From Dev

UIView doesn't animate horizontally from right to left

From Dev

How to animate UIButton frame from left to right and reverse

Related Related

  1. 1

    Animate Chart Bars from Left to Right

  2. 2

    Animate div from left to right

  3. 3

    Animate width from right to left

  4. 4

    Animate SVG from left to right

  5. 5

    Animate width from right to left

  6. 6

    Animate div from left to right

  7. 7

    Animate div from right to left and then from left to right again

  8. 8

    How to animate a background image from left to right and from right to left

  9. 9

    How to change the direction to Right to Left bars in a bar type chart?

  10. 10

    jQuery animate to left or right from variable

  11. 11

    iOS: Animate color change from left to right

  12. 12

    Animate width toggle from right to left in jQuery

  13. 13

    Animate SVG fill from left to right

  14. 14

    JQuery animate div from right to left with smoothness

  15. 15

    How to animate a LAYOUT from right to left?

  16. 16

    Animate border left to right

  17. 17

    How to start bars from right to left using amchart?

  18. 18

    How to animate 2 css layouts from left to right or from right to left with jquery?

  19. 19

    UIView doesn't animate horizontally from right to left

  20. 20

    jQuery/CSS animate div width from left to right

  21. 21

    How to animate full screen stripes moving from left to right

  22. 22

    Animate image like it's moving from right to left

  23. 23

    Animate absolute object from right: 0 to left: 0 in Chrome

  24. 24

    animate a button from left to right in a parent with text align center

  25. 25

    how to animate image & change the direction from right to left

  26. 26

    Animate images from right to left like slide show

  27. 27

    How can I animate from left to right the searchbar in a searchdisplaycontroller?

  28. 28

    UIView doesn't animate horizontally from right to left

  29. 29

    How to animate UIButton frame from left to right and reverse

HotTag

Archive