HighStock chart malfunctioning with inaccurate epoch datetime display

I am working on a project that involves creating a dynamic Highstock chart to showcase the daily influx of emails. The data is stored in a JSON file that gets updated every day, and you can see a snippet of it below:

[{
"name": "Month",
"data": [1471993200000, 1472079600000, 1472166000000, ...]
},{
"name": "numOfEmails:",
"data": [6973, 19953, 24802, ...]
}]

Currently, I'm facing an issue with the formatting of the dates on the xAxis when using type: 'datetime'. It displays as 00:00:00.010, 00:00:00.020, ....

https://i.stack.imgur.com/ITdg5.png

The JavaScript code snippet responsible for rendering the chart is provided below:

$(document).ready(function() {
    var options = {
        chart: {
            renderTo: 'container_chart',
            type: 'area',
        },
        title: {
            text: 'Emails received daily'
        },
        subtitle: {
            text: document.ontouchstart === undefined ?
                    'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
            },

         xAxis: {
                type: 'datetime'
        },

        yAxis: {
            title: {
                text: 'Number received'
            },
        },
        legend: {
            enabled: false
        },

        plotOptions: {
            area: {
                stacking: 'normal',
                lineColor: '#666666',
                lineWidth: 1,
                marker: {
                    lineWidth: 1,
                    lineColor: '#666666'
                }
            }
        },
        series: []
    }

    $.getJSON("./emailsCaptured_c2.json", function(json) {
    options.xAxis.categories = json[0]['data'];
        options.series[0] = json[1];
        chart = new Highcharts.stockChart(options);
    });
});

I've attempted to adjust the xAxis labels using the format {value:%d/%m/%Y}, but without success. I also experimented with altering the JSON file to match the desired date format instead of EPOCH time, which worked in HighCharts but not in HighStock. My primary motivation behind choosing HighStock was for its slider functionality and enhanced date range options.

Answer №1

Your error lies in attempting to define categories for the axis. Highstock does not accommodate category axes, and as a result, your data lacks x values, causing the chart to start from 0000000000000.

To resolve this, you can structure the values in the format [timestamp, value] as demonstrated below:

var series = {
  name: json[1].name,
  data: (function () {
    var data = [], i = 0, dataLen = json[0].data.length;

    for (; i < dataLen; i++) {
      data.push([json[0].data[i], json[1].data[i]]);
    }

    return data;
  })()
};

See an example here: https://jsfiddle.net/vqhuo3hf/1/

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

Using Custom GeoJSON Data with HighMaps Tutorial

Currently, I have created a custom .geo.json file from a county shapefile using ogr2ogr. My goal is to manually add values for each county. After studying a jsfiddle example (link provided), I'm unsure how to merge the two together. The specific line ...

Node.js & Express: Bizarre file routes

It's quite strange how my local paths are functioning. Let me show you an example of my directory structure: public > css > bootstrap.css public > js > bootstrap.js templates > layout > page.ejs (default template for any page) tem ...

Encountering an issue with NPM while attempting to install Parcel

After trying multiple solutions from various online sources, I am still unable to resolve the issue. Any advice or recommendations would be highly appreciated! npm ERR! code 1 npm ERR! path C:\Users\Tarun\Desktop\NamasteReact\node_ ...

Strategies for managing database modifications in a MEAN stack project

Online resources are scarce on this topic, but I am facing a common issue in application development. My project involves multiple users accessing and editing a shared database simultaneously. The challenge is ensuring that all users have an accurate and ...

Utilizing ES6 array methods to convert multidimensional arrays into chart-ready data

Seeking help with converting an array to a specific data format for chart display. The chrart.js library requires data in the following format: dataset = [ { label: 'one', data: []}, {label: 'two', data: []} ]; I ...

Encountering a 400 error when utilizing the Google Translate free API with Curl

I am attempting to utilize the free Google Translate API that is derived from Firefox's S3 Google Translator addon, by incorporating the following code: https://translate.google.com/translate_a/single?client=t&sl=auto& tl=en&hl=en&dt= ...

ReactJS | Display or Conceal an Array of Objects depending on a specified condition

The Challenge: I am currently working on a task that involves hiding/showing react elements dynamically based on a property of the constructed Object. To be more specific, let's consider the Array of Objects below: const Apps = [ {name: App1, permi ...

What is the implementation of booleans within the Promise.all() function?

I am looking to implement a functionality like the following: statusReady: boolean = false; jobsReady: boolean = false; ready() { return Promise.all([statusReady, jobsReady]); } ...with the goal of being able to do this later on: this.ready().then(() ...

Verify if there are multiple elements sharing the same class and initiate a certain action

I am working with three products that have a similar structure: tickbox | label (same text but different value) | Qty dropdown (different input name) These products fall into three different label "classes": 1st - <label for="related-checkbox-708745 ...

Utilizing JS setTimeout to iterate through AJAX requests in a loop, however the excessive frequency of setTimeout calls becomes an issue

I recently encountered an issue that was affecting my code functionality. After some investigation, I discovered that the function myEventMoveTrainManaul() was being called from multiple locations within the code, causing unexpected behavior. I want to exp ...

Is there a way to combine properties from two different objects?

I am working with several JavaScript objects: { x: 5, y: 10, z: 3 } and { x: 7, y: 2, z: 9 } My task is to add these two objects together based on their keys. The resulting object should look like this: { x: 12, y: 12, z: 12 } Do you ...

Sending a single data point via Ajax on the client side

I am facing an issue with connecting frontend and backend systems. I want to send a single value (radio1Value) from a JavaScript function using jQuery AJAX upon clicking. Is the function structured correctly as I have written it? If yes, how should the R ...

Using Nuxtjs/Toast with personalized image emblems

Would it be possible to include an icon in a toast error message, or is there a need to install another module for this functionality? I am currently using vue and attempting to integrate a component as an icon, but so far without success. this.$toast.er ...

Obtaining POST request parameters in an Express server

Below is the code snippet for adding a product using an angular service. The product is passed as the body. addProduct(product): Observable<any> { return this.http.post<Observable<any>>( `http://localhost:4401/api/products`, ...

The Express Validator is unable to send headers to the client once they have already been processed

I recently integrated express-validator in my Express JS project, but encountered a warning when sending invalid fields to my api: UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client This ...

How can I change a PHP for loop to Javascript?

Can the following code be converted to JavaScript? for (let lift of liftDetails) { document.write('<option>'+ lift["LiftMakes"] +'</option>'); } ...

Oops! The type in React.jsx is not valid - it should be a string for built-in components. You may want to consider converting your class component to a

The error message I am encountering is as follows: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it&ap ...

Tips for entering Tamil characters in text boxes on a form field

Within my form, I have a set of text boxes. I am looking to input text in Tamil font for specific text boxes - around 5 out of the total 10 text boxes in the form. If you know how to enable Tamil font input for multiple text boxes (rather than just one as ...

The React Bootstrap modal fails to display the value of an argument passed through a parameter

I am currently working on a project that utilizes React for its front end development. Below is the code snippet: import React, {useState} from 'react'; import Tree from 'react-d3-tree'; import { useCenteredTree } from "./helpers&q ...

Loading an OBJ file from a blob using three.js

I am currently attempting to load an OBJ file from a blob using Three.js. After referring to this resource and successfully loading STL files, I encountered an issue with loading OBJ files. The error message I received is as follows: TypeError: text.indexO ...