Bar graph data remains unchanged after each iteration of the loop

I've been attempting to implement a for loop in a JavaScript bar graph that resets the bar height to 0 when a button is clicked, but it doesn't seem to be working as expected. I've tried checking the console in Google Developer tools for errors, but nothing is showing up. The function responsible for this behavior is called "reset." Maybe another set of eyes could help identify what I might be overlooking. Any assistance is greatly appreciated.

function createBarChart(data) {
  // Start with the container.
  var chart = document.createElement("div");

  // The container must have position: relative.
  chart.style.position = "relative";

  // The chart's height is the value of its largest
  // data item plus a little margin.
  var height = 0;
  for (var i = 0; i < data.length; i += 1) {
    height = Math.max(height, data[i].value);
  }
  chart.style.height = (height + 10) + "px";

  // Give the chart a bottom border.
  chart.style.borderBottomStyle = "solid";
  chart.style.borderBottomWidth = "1px";

  // Iterate through the data.
  var barPosition = 0;

  // We have a preset bar width for the purposes of this
  // example.  A full-blown chart module would make this
  // customizable.
  var barWidth = 48.30;
  for (i = 0; i < data.length; i += 1) {
    // Basic column setup.
    var dataItem = data[i];
    var bar = document.createElement("div");
    bar.style.className = "bars";
    bar.style.position = "absolute";
    bar.style.left = barPosition + "px";
    bar.style.width = barWidth + "px";
    bar.style.backgroundColor = dataItem.color;
    bar.style.height = dataItem.value + "px";
    bar.style.borderStyle = "ridge";
    bar.style.borderColor = dataItem.color;

    // Visual flair with CSS Level 3 (for maximum compatibility
    // we set multiple possible properties to the same value).
    // Hardcoded values here just for illustration; a
    // full module would allow major customizability.
    bar.style.MozBoxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
    bar.style.WebkitBoxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
    bar.style.boxShadow = "rgba(128, 128, 128, 0.75) 0px 7px 12px";
    bar.style.MozBorderRadiusTopleft = "8px";
    bar.style.WebkitBorderTopLeftRadius = "8px";
    bar.style.borderTopLeftRadius = "8px";
    bar.style.MozBorderRadiusTopright = "8px";
    bar.style.WebkitBorderTopRightRadius = "8px";
    bar.style.borderTopRightRadius = "8px";
    bar.style.backgroundImage =
      "-moz-linear-gradient(" + dataItem.color + ", black)";
    bar.style.backgroundImage =
      "-webkit-gradient(linear, 0% 0%, 0% 100%," +
      "color-stop(0, " + dataItem.color + "), color-stop(1, black))";
    bar.style.backgroundImage =
      "linear-gradient(" + dataItem.color + ", black)";

    // Recall that positioning properties are treated *relative*
    // to the corresponding sides of the containing element.
    bar.style.bottom = "-1px";
    chart.appendChild(bar);

    // Move to the next bar.  We provide an entire bar's
    // width as space between columns.
    barPosition += (barWidth * 2);
  }

  return chart;
};

function reset() {
  'use strict';
  var bars = document.getElementsByClassName("bars");
  for (var i = 0; i < bars.length; i++) {
    bars[i].style.height = "0px";
  }
}

window.onload = function() {
  var colors = [{
      color: "red",
      value: 40
    },
    {
      color: "blue",
      value: 10
    },
    {
      color: "green",
      value: 100
    },
    {
      color: "black",
      value: 65
    },
    {
      color: "yellow",
      value: 75
    },
    {
      color: "purple",
      value: 120
    },
    {
      color: "grey",
      value: 121
    },
    {
      color: "orange",
      value: 175
    },
    {
      color: "olive",
      value: 220
    },
    {
      color: "maroon",
      value: 275
    },
    {
      color: "brown",
      value: 300
    },
    {
      color: "teal",
      value: 15
    }
  ];

  var chart = createBarChart(colors);
  document.querySelector("#wrapper").appendChild(chart); // keeps chart inside wrapper div
};
<div id="wrapper"></div>
<button onclick="reset()">Reset</button>

Answer №1

There appears to be a typo in your input. The character O was used instead of the number 0

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

The inline feature of the Bootstrap input group addon is not being utilized

Check out my code snippet here: http://www.bootply.com/iR1SvOyEGH <form> <div class="form-group"> <label for="inputEmail">Company Name</label> <input type="text" class="form-control"> <span class="input-gro ...

Solution to ensure fixed header with correct z-index placement

Everything is functioning correctly, except for the issue that arises when scrolling down to view thumbnails. The left bar covers the thumbnail section, making it impossible to select items. I suspect that the z-index of a div is causing this problem. I ha ...

How can the parent div's height be determined by the child div when using position: absolute?

I am struggling with setting the height of a parent div that contains a nested child div with absolute positioning. The child div has a fixed height of 652px, but I cannot seem to get the parent div to match this height. I have tried adjusting the clear an ...

Adjust the path-clip to properly fill the SVG

Is there a way to adjust the clip-path registration so that the line fills correctly along its path instead of top to bottom? Refer to the screenshots for an example. You can view the entire SVG and see how the animation works on codepen, where it is contr ...

Updating the load context variable in Django template after making changes via an AJAX call: a step-by-step guide

Here is the code snippet for displaying table items in my customer_items.html page: <table id="tblData" style="width: 100% !important;" class="display table table-bordered table-striped table-condensed"> <thead> <tr> ...

What could be causing my directive to not display my scope?

I am currently developing a directive that includes a custom controller, and I am testing the scope functionality. However, I am facing an issue where the ng-show directive is not functioning as expected when trying to test if the directive has a scope fro ...

Creating an AJAX data form in a JSP scenario

I want to correctly set up the data parameter for the ajax call. <script type="text/javascript"> $(document).ready(function() { $('#call').click(function () { $.ajax({ type: "post", ...

Stop the page from scrolling

I'm trying to find a way to disable page scrolling, so I used this style for the body: overflow: hidden; Surprisingly, it worked perfectly on desktop but had no effect on mobile devices. After checking out this resource, it seems that creating a ch ...

Illustration: Fixing a CSS Issue

After implementing Technique #8 from the Nine Techniques for CSS Image Replacement, I am not getting the desired results. Instead of correctly positioned images, the output is not what I anticipated. Here is a link to see for yourself: I made a modificati ...

Problem with spacing in a Bootstrap 5 column of size medium-5

I'm currently working on a template for my website and facing an issue with removing the margin displayed on the right side of my div using flex. I've tried setting the margin to 0 on one of the columns and also experimented with changing the col ...

The function to focus on this.$refs[("p" + index)] element is not available

I need help transforming a div into an input box when clicked, allowing me to edit the post inside a loop. Here is the button found on the post: <a @click="setFocusEdit(index)" v-if="isAuthor(post)" href="#" >Edit Me</a> And here is the spec ...

Having trouble with CSS basics? Seeing properties change across multiple classes?

My CSS skills are still in the beginner stage and I am currently struggling to troubleshoot an issue with my code. The HTML: <div id="loginForm"> <span class="dottedLink"><a href="resetlogin">Recover login details</a></span ...

Stop the execution of the setTimeout function when the webpage loads in JavaScript

In my postgres database, I have a table named students for storing student information. The structure of the table is as follows: id first_name last_name time_remind 1 pers1 pers1_name 2022-07-30 21:30 2 pers2 pers2_name 2022-07-30 20:38 Current ...

Using JavaScript: How to utilize Array.reduce within a function that also accepts arguments

let foo = 0; let bar = 0; const arr1 = [1, 2, 3, 4, 5]; const arr2 = [6, 7, 8, 9, 10]; function calculateSum(arr) { return arr.reduce((accum, val) => accum + val, 0); } foo = calculateSum(arr1); // Expect foo to equal 15 bar = calculateSum(arr2); ...

creating a Vue app using Node results in an HTML page with no content due to syntax errors

After creating a VueJs page using the CLI, I wanted to share it with others who might not have Vue CLI or Node installed. Just like opening .html files in a browser, I tried to open the index.html file after building it. However, when I opened the file, a ...

Angular 5: There was an issue with the property not defined for lowercase conversion

I keep encountering this error whenever I attempt to filter a column of a table. The data is retrieved from a FireStore cloud collection and the 'auteur' field is defined in each document. Here is how my component looks: import { Component, OnI ...

The input field will be in a read-only state if it contains a value from the database. However, it will be editable if the value

Hello everyone, I am a newcomer to this community and would greatly appreciate your help. I am encountering an issue with the following lines of code: <input type="text" class="form-control" id="fines" name="fines&quo ...

Having trouble getting the ReactJS AXIOS post function to work properly?

Error in console: [1]: https://i.sstatic.net/xOfUV.png issue with axios request .post("https://bootcamp-2022.devtest.ge/api/application", { token: "7e475dfc-0daa-4108-81c8-d6c62a3df4ca", first_name: JSON.parse(localStorage.ge ...

Trigger a click event on a div element that is nested within a form

Having trouble displaying an alert when clicking on a disabled button because the user needs to first click on a terms checkbox. Here's my jQuery code: $('#divButton').on("click", function() { if ($('#buybutton').prop('d ...

Using Vue.js within Cordova platform allows developers to create dynamic

Having trouble integrating a Vue.js app into Cordova. Everything seems to be working fine, except I'm unsure how to capture Cordova events (deviceready, pause, etc.) within my Vue application. Using the Webpack template from vue-cli. This is my file ...