Create interactive charts with Highcharts by incorporating AJAX requests

Below is the code snippet for an AJAX request to retrieve data and then call a function to pass the data to a graph.

function getGraphs(){

 $.ajax({

    type : "POST",
    async: true,
    url : "<?php echo base_url(); ?>" + "Graphs/get_graphs",
    dataType: 'json',
    data :{hotel_name_realm:$("#hotel_names").val()},
    success : function(res){
     obj = JSON.parse(res);

     plot_graph(obj);
        // console.log(obj.upload);
      }
    });
}

The Highchart graph plotting function is shown below. The AJAX success function calls this function passing data. The data object can be accessed as ss.download and ss.upload, but the loaded graph does not display the data properly.

function plot_graph(ss){

 Highcharts.chart('container', {

        chart: {
        type: 'spline',
        scrollablePlotArea: {
        minWidth: 600,
        scrollPositionX: 1
    }
  },

title: {
    text: 'Wind speed during two days',
    align: 'left'
  },
  subtitle: {
    text: '13th & 14th of February, 2018 at two locations in Vik i Sogn, Norway',
    align: 'left'
  },
  xAxis: {
    type: 'datetime',
    labels: {
      overflow: 'justify'
    }
  },    
  tooltip: {
    valueSuffix: ' m/s'
  },
  plotOptions: {  
    spline: {
      lineWidth: 4,
      states: {
        hover: {
          lineWidth: 5
        }
      },
      marker: {        
        enabled: false
      },    
              pointInterval: 60000, // one hour
              pointStart: Date.UTC(2019, 4, 9, 14, 35, 0)
            }
          },
          series: [{
            name: 'Download',
            data: [
            ss.download
            ]

          }, {
            name: 'Upload',
            data: [
            ss.upload
            ]
          }],
          navigation: {
            menuItemStyle: {
              fontSize: '10px'
            }
          }
        });    
}

Answer №1

If the data for ss.download and ss.upload appears as shown below:

ss.download = [
  [Date.UTC(2019, 4, 9, 14, 35, 0), 20.9],
  [Date.UTC(2019, 4, 9, 15, 01, 0), 50.5],
  [Date.UTC(2019, 4, 9, 16, 55, 0), 80.4]
];
ss.upload = [
  [Date.UTC(2019, 4, 9, 14, 35, 0), 29.9],
  [Date.UTC(2019, 4, 9, 15, 01, 0), 71.5],
  [Date.UTC(2019, 4, 9, 16, 55, 0), 106.4]
];

In order to display this data correctly, it is recommended to remove the brackets surrounding them:

  series: [{
    name: 'Download',
    data: ss.download
    }, {
    name: 'Upload',
    data: ss.upload    
  }]

Check out this example

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

What is the best way to set values for columns in jqGrid?

I am currently using jqGrid in conjunction with CodeIgniter 2.1.0. I am facing a challenge regarding how to set a value for a specific column based on a particular event. For example, when entering quantity and rate values in the respective columns, I wan ...

Retrieving URL parameters and excluding a particular parameter from the list

I am looking to extract a specific parameter from the URL. Here is my scenario: Initially, the page loads with a URL like this: Upon loading the page, a function is triggered to add an entry to the history using pushState, resulting in a new URL such as ...

What is the best way to eliminate the comma at the end of the final array

<% for(var i=0; i < 10; i++) { %> <a href="#"><%= related[i] %></a><% if(i !== 9) { %>, <% } %> <% } %> Displayed above is some code that includes a loop to display related items. The goal is to remove the comm ...

What are the HTML5 elements that include the "load event" within their onload attribute?

Mozilla's MDN provides information regarding the load Event with this brief explanation: The load event is triggered when a resource and its dependent resources have completed loading. and references standard 1, which states Trusted Targets: ...

Accessing and sending only the body part of an HTTP response in Angular 7 test cases: A comprehensive guide

Currently, I am working on creating unit test cases in Angular 7 for a Component that utilizes an asynchronous service. This is the content of my component file: submitLoginForm() { if (this.loginForm.valid) { // send a http request to save t ...

Updating the state after receiving API results asynchronously following a function call

I am attempting to update the state asynchronously when my fetchWeather function is executed from my WeatherProvider component, which calls an axios request to a weather API. The result of this request should be mapped to a forecast variable within the fet ...

JQuery Accordion SubMenu/Nested Feature malfunctioning

I have successfully implemented a JQuery Accordion on my website and it is functioning properly, opening and closing as expected. However, I am facing an issue when trying to add a submenu within the accordion. The submenu does not work as intended and lac ...

Inserting only the initial three values into the MySQL Workbench from Next.js

My app.js file has the following code: app.post('/api/add-order-cui', async (req, res) => { const { cui, email, phone, billingCUI, // Add other fields if necessary } = req.body; console.log('Received request body:&a ...

What is the purpose of using CORS with Express?

Here is how my express server setup looks: const cors = require('cors'); const express = require('express'); const app = express(); const port = 8000; app.use(cors({origin: 'http://localhost:8000'})); // Handle requests of c ...

Invoke a Wordpress shortcode using AJAX

Trying to implement a shortcode in Wordpress that can be triggered by clicking a button using jQuery. After researching various websites and tutorials, I am still having trouble understanding the issue. In my function.php file, I have the following code: ...

What specific method of sorting does this code utilize?

Here's a JavaScript code snippet that sorts an array of n numbers in ascending order. I've used this code in several interviews, but I'm still unsure about which sorting algorithm it represents. let arr = [7, 9, 2, 11, 5] for (let i = 0; i ...

What is the easiest method to iterate through nested objects within an object?

Hey there! I'm working on implementing a "your cart" feature for an ecommerce website in my project, and I could use some assistance with coding to loop through an object within an object. Below is the code snippet I've been working on: ...

Experiencing difficulties integrating react-moveable with NEXTjs: Error encountered - Unable to access property 'userAgent' as it is undefined

I've been grappling with this problem for the past few hours. I have successfully implemented react-moveable in a simple node.js app, but when I attempt to integrate it into a NEXTjs app, an error crops up: TypeError: Cannot read property 'userAg ...

Unable to open Node.js Interactive Window in Visual Studio Enterprise 2017

Having some trouble with NodeJS development in Visual Studio 2017 Enterprise. Whenever I attempt to open the NodeJS Interactive Window, an error message pops up like this: https://i.sstatic.net/c02jJ.png Tried uninstalling and reinstalling Node.js tools ...

Utilizing AJAX for data transmission within CakePHP

$.ajax({ url : "../update_request", type : "GET", data : {reqdata, reqdata}, success : function(data) { window.location = "<?php echo $this->Html->url(array('controller' => '', 'action' => & ...

Replicating a tag and inputting the field content

Scenario: An issue arises with copying a label text and input field as one unit, instead of just the text. Solution Needed: Develop a method to copy both the text and the input field simultaneously by selecting the entire line. Challenge Encountered: Pre ...

Unable to extract a particular value from a JSON data structure

This situation has been on my mind for a good couple of hours now. I have this json object with example values like so: { "events": [ { "id": 64714, "live": false, "start": "1399117500", "league_ ...

Aligning the Bootstrap 5 navbar dropdown to the right side

I'm having trouble getting a part of my navbar to align right in bootstrap 5. I've followed the new documentation, but I think I might be adding the text in the wrong place. Can someone assist me in moving my dropdown to the right side of the nav ...

JavaScript tip: How to disregard symbols that affect the length of text in a textbox?

I am working on a task which involves counting only the text, excluding symbols, entered in a textbox. For instance, if a user types email_ex_3/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05374568646c692b666a68">[email ...

The Maginific popup feature appears to be malfunctioning

I'm currently working on a website and I've encountered an issue. After clicking on the search icon, a search window should open as shown in this image, but I'm getting an error in the console: index.php:113 Uncaught TypeError: $(...).magnif ...