Issue with parsing dates while importing external JSON data into Highcharts Line Graph

Seeking help to create a highstock chart that will display the remote JSON data below as a line graph. I only need a simple rendering of the data, but please note that the date in the dataset includes hourly values.

poolprice

container: panelone

[{"date":"11/24/2012 19","price":"126.44"},{"date":"11/24/2012 18","price":"244.31"},{"date":"11/24/2012 17","price":"90.83"},...

Appreciate your assistance!

AC

Answer №1

The information provided is not arranged in a sorted manner and contains a date format that cannot be interpreted by Highstock. To utilize the data effectively, the date needs to be converted into a Date object, then transformed into a timestamp for Highstock usage while ensuring proper sorting.

// Converting data into an (unordered) array of timestamps and values
var newData = new Array();
for (var i = 0; i < rawData.length; i++) {
    var row = rawData[i];
    var date = new Date(row.date.substring(6, 10), parseInt(row.date.substring(0, 2)) - 1, row.date.substring(3, 5), row.date.substring(11, 13), 0, 0);
    var data = [date.getTime(), parseFloat(row.price)];
    newData.push(data);
}

// Sorting the data based on time. Credits to Ben Blank for the solution: http://stackoverflow.com/questions/5199901/how-to-sort-an-associative-array-by-its-values-in-javascript
var tuples = new Array();
for (var key in newData) {
    tuples.push([key, newData[key]]);
}

tuples.sort(function(a, b) {
    a = a[1];
    b = b[1];
    return a < b ? -1 : (a > b ? 1 : 0);
});

var orderedData = [];
for (var i = 0; i < tuples.length; i++) {
    var key = tuples[i][0];
    var value = tuples[i][1];
    orderedData.push(value);
}

You can view your line chart here through this fiddle link.

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

Transferring an array object from PHP to Python

This is the code I have developed: $dataraw = $_SESSION['image']; $datagambar = json_encode($dataraw); echo '<pre>'; print_r($dataraw); echo '</pre>'; print($escaped_json); $type1 = gettype($dataraw); print($typ ...

Triggering JavaScript Functions with React Button Click Events

I'm struggling to grasp how JavaScript functions interact with React components. I have a "Button" element with an "onClick" event that I want to use to clear an "input" text field. Unfortunately, my attempts haven't been successful so far. Sol ...

Transforming form inputs into JSON structure

<form name = 'test' > <input type='text' name = 'login'> <input type='email' name = 'email'> </form> When I try to convert the form data using JSON.serialize($(form)).serial ...

The default styling of Next.js react-chatbot-kit is not functioning properly

import Chatbot from 'react-chatbot-kit' import config from './config' import ActionProvider from './actionProvider' import MessageParser from './messageParser' const ChatBotFlight = () => { return ( <div> ...

Display or Hide certain Items/Elements depending on the chosen Category

I have multiple images within a div, each image belonging to a specific category. I want to display only the images from a selected category and hide all others. Initially, all images are in the category all. When selecting the category blue, for example, ...

Three.js Image Processing with Effect Composer

After reviewing the example located at: I am intrigued by the possibility of applying post-processing effects on a duplicate of the initial data set. Essentially, I am interested in showcasing the original rendering of my scene in one container while simu ...

Can I assign a custom form id before manually triggering submission using AJAX?

I have developed an interesting code snippet that uses AJAX to submit a form only if the form has an ID attached to it. $(document).delegate('form', 'submit', function(event) { var $form = $(this); var id = $form.attr('id& ...

Upon the second visit, Bootstrap popover functions as expected

After creating a website on SharePoint using Bootstrap and populating the content with JavaScript, I encountered an issue with popover functionality. Oddly enough, the popover only works after refreshing or reloading the page with F5. Strangely, popovers ...

Obtain JSON encoded data from multiple tables using foreign keys in CodeIgniter

I need assistance with a query that involves joining two tables. Here are the tables: Categories: id category 1 BBA 2 BSCS 3 BEE Sub_category: id category_id sub_category_name 1 1 Accountant 2 1 Manager 3 ...

Using Discord JS to remove a replied message

I'm attempting to remove a specific message with a specific event using the following code: await interaction.reply({ content: `content` }) .then(message => { console.log(message); client.on('message ...

There is an issue with transmitting data from an HTML page to the server and then retrieving data from the server

Upon running this code, I encountered an issue with sending and receiving data. I kindly request assistance in correcting my code. Highlighted below is the server-side code var http = require("http") ; var fs = require("fs") ; http.createServer(function( ...

My regular expression isn't functioning properly. Can someone please assist me in troubleshooting and resolving this issue?

Here is my code snippet: property_unit_plan.post('/bulkAdd',(req, res) =>{ Array.prototype.forEach.call(req.body, element => { db.sequelize.query('CALL sp_property_unit_plan_add_bulk( :unit_size_range, :no_of_bedrooms, :no_ ...

Retrieve the parent's current state by accessing it through a callback function in React

My callback function onRestore is only logging the history until the id of the clicked snapshot. Why is it showing incorrect state? I've exhausted all options, so any help would be appreciated. import React, { useState, useEffect, useRef } from "re ...

Challenges with Access-Control-Allow-Origin within the website's domain

Why am I encountering an issue when attempting to make an XMLHTTPRequest from a JavaScript file to a web service on the same domain, resulting in: Access-Control-Allow-Origin error stating that the origin is not allowed? Switching mydomain.com to localh ...

Struggling with displaying a PDF file from the local directory in a NextJS application

I've been facing trouble importing and displaying my resume, a local file, within a react component. The code import myResume from '../assets/pdfs/myResume.pdf' is resulting in the error: Error: Cannot find module '../assets/pdfs/myRes ...

What is the best method for integrating both Jquery and Angularjs into a single project?

As we prepare to incorporate Jquery and AngularJs into our upcoming project, I've come across conflicting information. Some sources suggest using Jquery before AngularJs, while others recommend the opposite. This has left me a bit perplexed. For exam ...

Combine the information from 3 separate subscriptions into a single object using RxJS in Angular 9

I am seeking assistance with merging data from 3 different sensors into one object. I am using Cordova plugins to retrieve accelerometer, gyroscope, and magnetometer data. However, I am facing an issue in subscribing to all three observables simultaneously ...

"Confusion arises when handling undefined arguments within async.apply in the context of async

While working on my project, I encountered a strange issue with the async library. Some of my arguments end up being "undefined" in my function calls. For example (this is just simplifying my problem): var token; async.series([ function getToken (do ...

Converting a data-attribute list into an array

My current setup includes an image with the following properties: <img id="img" data-list="['image2.jpg', 'image3.jpg', 'image4.jpg']" src="image1.jpg"> I am wondering how I can extract the list of images from the data ...

What is the reason behind appengine-rest-server returning JSON strings instead of integers for properties?

I set up a basic REST server on GAE utilizing the appengine-rest-server. The structure of my database is as follows: SomeString = db.StringProperty() SomeInt = db.IntegerProperty(default=0) SomeFloat = db.FloatProperty(default=-1.0) SomeDateTime = db.Dat ...