JavaScript cannot properly plot data for stacked bar charts on highcharts

I am encountering an issue with plotting data in a bar highchart. Despite having values when I alert them, they do not appear on the barchart. I am using the following code snippet:

chart.series[i].addPoint(data[i]['total_check']);
. I am unsure if my code is incorrect. Here is the code snippet where I plot the points on the barchart:

function getbarseries(month) {
    $.ajax({
      url: siteurl+"patients_report/bardataclinic/"+month,
      type: "POST",
      dataType: "JSON",
        success: function(data) {
           for(var i in data) {
               chart.series[i].addPoint(data[i]['total_check']);
               //alert(data[i]['total_check']);
           }
        }
    });
}

I am dynamically plotting a bar highchart by recursively obtaining categories (months) and then plotting the data. The main part of my code is as follows:

function getbarxAxis() {
 $.ajax({
   url: siteurl+"patients_report/bardata_date",
   type: "POST",
   dataType: "JSON",
   success: function(data) {
     var categories = new Array();
     for (var i in data) {
       categories.push(data[i]["datemonths"]);
     }
    loadChart(categories);
    getallclinics();
    var arrayLength = categories.length;
    for(var loop = 0;loop<arrayLength;loop++) {
        getbarseries(categories[loop]);
    }

   }
 });
}

To initiate the process, I first retrieve the categories (months) before invoking the loadChart(categories); function to plot the categories.

function loadChart(categories) {
   chart = Highcharts.chart('container', {
    chart: {
        type: 'bar'
    },
    title: {
        text: ''
    },
    xAxis: {
        categories: categories
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Detailed patient per clinic'
        }
    },
    legend: {
        reversed: true
    },
    plotOptions: {
        series: {
            stacking: 'normal'
        }
    },
    plotOptions: {
            series: {
                stacking: 'normal'
            }
    }
});
}

Once the categories are plotted, the next step involves fetching the series data names. This is handled by the getallclinics(); function, which queries the database and plots all the data series names.

function getallclinics() {
   $.ajax ({
          url: siteurl+"patients_report/seriesclinics",
          type: "POST",
          async: false,
          dataType: "JSON",
          success: function(data) {
            for(var i in data) {
              chart.addSeries({
                  name: data[i]['clinic_name']
              });
            }
          }
   });
}

After retrieving the clinic names, the final part involves plotting the data for each series name. This is where I am facing difficulty, specifically with the getbarseries function and

chart.series[i].addPoint(data[i]['total_check']);
code snippet.

Below is the complete code snippet for my getbarseries function:

function getbarseries(month) {
    $.ajax({
      url: siteurl+"patients_report/bardataclinic/"+month,
      type: "POST",
      dataType: "JSON",
        success: function(data) {
           for(var i in data) {
               chart.series[i].addPoint(data[i]['total_check']);
               //alert(data[i]['total_check']);
           }
        }
    });
}

For each month, the getbarseries function retrieves the total checkup data. The console output below shows the results for the two recursive loops:

First recursive loop:

[{"clinic_name":"Clinic 1","total_check":"0"},{"clinic_name":"Clinic 2","total_check":"1"},{"clinic_name":"Clinic 3","total_check":"0"},{"clinic_name":"Clinic 4","total_check":"0"}]

Second recursive loop:

[{"clinic_name":"Clinic 1","total_check":"2"},{"clinic_name":"Clinic 2","total_check":"0"},{"clinic_name":"Clinic 3","total_check":"0"},{"clinic_name":"Clinic 4","total_check":"0"}]

Answer №1

Your data appears to be in string format, which needs to be converted to numbers.

To accomplish this, you can refer to this resource: Convert argument to number

function getbarseries(month) {
    $.ajax({
      url: siteurl+"patients_report/bardataclinic/"+month,
      type: "POST",
      dataType: "JSON",
        success: function(data) {
           for(var i in data) {
               chart.series[i].addPoint(Number(data[i]['total_check']));
           }
        }
    });
}

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 tab indicator in Material-UI fails to update when the back button is clicked

My code is currently functioning well: The tab indicator moves according to the URL of my tab. However, there is a peculiar issue that arises when the back button of the browser is pressed - the URL changes but the indicator remains on the same tab as befo ...

How can you Use CSS to Float Elements Left and Right while adjusting their Width Dynam

Is it possible to have two columns side by side with dynamic text using only CSS? I'm trying to make the left main text "break" into a new line when it reaches the right category, which is also dynamic. Do I need to use JavaScript for this? https://i ...

Issue with passing parameter values in MVC Html.ActionLink

Currently, I am experimenting with MVC for a demonstration and have managed to put together some components. However, I am encountering difficulties with an Html.ActionLink. The goal is to display a series of dropdown lists to the user that must be selecte ...

Display dynamic content with AJAX in a pop-up modal window while also updating the

Currently, when I click on a button, a modal pops up with data retrieved from an ajax action that has been given an id. I would like the URL to update when this happens. Additionally, I want the option for the modal to preload if someone directly accesses ...

Discovering the window scroll unit when clicking, as a page smoothly transitions to the top for a specified target position

Is there a way to achieve parallax scrolling on window scroll and navigation click simultaneously? When navigating and animating the page to the top to show the target, I also want to get the window scroll unit. How can I obtain the window scroll unit on ...

Is it possible to arrange JSON Objects vertically on a webpage using tables, flexboxes, divs, and Javascript?

Within my JSON data, I have multiple products defined. My goal is to loop through this JSON and display these products side by side on a web page for easy comparison. Initially, I envision structuring them in columns and then rows: https://i.sstatic.net/K ...

Fill a dropdown menu with options from a JSON object, arranging them in ascending order

I have a JSON hash that I am using to populate a combo box with the following code: $.each(json_hash, function(key, value) { $("#select").append("<option value='" + key + "'>" + value + "</option>"); }); The functionality w ...

The setState method fails to properly update the state

I am currently utilizing React JS. Below is the code for my React class: class MyReactComponent extends React.Component{ constructor(props){ super(props); this.state = { passAccount: { email: "Email&quo ...

What might be causing certain ajax buttons to malfunction?

There are 5 buttons displayed here and they are all functioning correctly. <button type="submit" id="submit_button1">Img1</button> <button type="submit" id="submit_button2">Img2</button> <button type="submit" id="submit_button3" ...

template strings receive null value

I am currently facing an issue with the execution of certain template literals in my jQuery functions. Although some are functioning as expected, there is a specific block that is not being executed. function viewUserDetails(data) { // retrieves integer v ...

The picture is displayed just once, despite the fact that it was supposed to be returned 50 times within the loop

I'm encountering an issue while trying to use a for loop to render an image in a React component. The loop is not functioning as expected, resulting in the image being displayed only once on the screen even though the intention is to show the same ima ...

Steps to develop a log-in API using Node.js

In the process of developing my web application, I have utilized node js exclusively for all functionalities and the web user interface has been successfully implemented. An issue that has come to light is that users are able to access the services API wi ...

Modify button behavior on click after the initial press

Struggling to make this work the way I want. I have a button that should execute Javascript function A when clicked, and in function A, I need to change the onclick attribute to function B. This way, on the second tap of the button, it will trigger functio ...

Encountered a network error 500 when attempting to access a CodeIgniter controller action through Ajax

I am facing an issue with my admin controller. Within this controller, I have a processReq function that is triggered by a button click event. However, every time I click the button, I encounter an error message: "NetworkError: 500 Internal Server Error ...

Tips for extracting data from various select drop-down menus within a single webpage

As a newcomer to JQuery, I apologize if this question seems basic. I have a page with 20 categories, each offering a selection of products in a drop-down menu. The user will choose a product from each category, triggering an ajax call to retrieve the pric ...

Encountering an error message when trying to launch an application: Module

Every time I try to launch my react-native app in git-bash, I encounter the following error message. I have already uninstalled and reinstalled node.js, cleared the cache, and attempted various solutions from Stack Overflow and GitHub with no success. ...

Error message "auth/code-expired" is triggered when Firebase Multi Factor Authentication for a web application detects that the verification code has expired

While working on adding multi-factor authentication to my Angular web application, I encountered an error message stating: "auth/code-expired", "The SMS code has expired. Please resend the verification code to try again.", even though I ...

Incorporating a stylish background image into an EJS template

I'm able to successfully display an image from my app.js file in the main ejs file, but when I try to set it as a background image, it doesn't show up. Any thoughts on what might be causing this issue? This is my app.js file: const express = re ...

Having trouble resolving a component from a component library while utilizing NPM link

My React application is set up with Create React App and a separate component library. I'm currently experimenting with using 'npm link' to test changes in the component library directly on my local machine. To achieve this, I first run &ap ...

The issue persists with the $slice function in MongoDb

I am attempting to retrieve records using an aggregate function in MongoDB, but I keep encountering the error message stating that the operator $slice is invalid: db.getCollection('test').aggregate( [ { $match: { 'subjectId': &apos ...