When data is loaded asynchronously, the Highcharts-ng stock chart encounters an error stating "TypeError: Cannot read property 'hoverSeries' of undefined."

Using the 'highcharts-ng' custom directive in my AngularJS app to generate charts has been successful when using scope-defined data. However, I encountered an error when attempting to fetch data from a REST API using $resource calls through a Factory:

Check out the JSFiddle using scope-defined data


For the REST-driven version, here is the function for querying data:

function getData(start, end) {
    return measurementsFactory
        .getMeasurements(start, end)
            .then(function(response) {
                return response.data;
            });
    }

The initChart function that is driven by REST then looks like this:

function initChart() {
    vm.chartConfig.title.text = 'New Title';

    var yAxis = inityAxis();
    vm.chartConfig.yAxis = yAxis;

    getData(moment(0).valueOf(), moment().valueOf()).then(function(data) {
        var series = initSeries(data);
        vm.chartConfig.series = series;
        vm.chartConfig.navigator.series = series;
    });
}

The 'data' array returned is identical to the 'vm.testData array' in the JSFiddle. The chart loads without errors and appears the same as with the scope-defined data JSFiddle.

The REST-driven version of afterSetExtremes:

function afterSetExtremes(e) {
    if (e.trigger != undefined) {
        getData(Math.round(e.min), Math.round(e.max)).then(function(data) {
            var series = initSeries(data);
            vm.chartConfig.series = series;
        });
    }
}

The Challenge:

Upon attempting to set the time range using buttons, input, or the navigator, an error occurs:

TypeError: Cannot read property 'hoverSeries' of undefined
    at p.destroy (highstock.js:305)
    // more error messages here

and the navigator series disappears. How should I proceed?


Edit 1 (2017-09-01)

Here's a modified JSFiddle accessing a simulated REST service

But now, a different error occurs when drawing the chart.

TypeError: Cannot read property 'breaks' of undefined
    // more error messages here

Edit 2 (2017-09-05)

Take a look at this updated Highcharts JSFiddle that functions as intended (with minor axis modifications).

Answer №1

After conducting thorough research, I have successfully identified the issue:

The problem stems from non-existent axes at the moment the chart bounds are set. To resolve this issue, it is necessary to refresh the chart after updating the data.

The main culprit here is the afterSetExtremes function. It fails to notify the view of any changes, resulting in the chart not being redrawn properly even though its configuration object is updated and the xAxis coordinates become inaccurate.

I encountered the following error: https://www.highcharts.com/errors/18

Here is the revised afterSetExtremes function:

function afterSetExtremes(e, scope) {
 if (e.trigger != undefined) {
   getData(Math.round(e.min), Math.round(e.max)).then(function(data) {
     var series = initSeries(data);
     scope.$apply(function() {
       scope.chartConfig.series = series;
     });
   });
 }
}

This function should be called within the chart's config object by passing in the scope to enable the usage of $apply():

xAxis: [{
  type: 'datetime',
  minRange: 3600 * 1000,
  events: {
    afterSetExtremes: afterSetExtremes(vm)
  }
}],

I also included a function in the Highcharts config object as a precaution since ng-highcharts has been known to cause issues with chart redrawing:

func: function(chart) {
  vm.$evalAsync(function() {
    chart.reflow();
  });
}

To see the solution in action, visit this functional 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

Using Javascript or ES6, you can compare a nested array object with another array of elements and generate a new array based on

I am dealing with a complicated array structure as shown below sectionInfo = [{id: 1, name:'ma'}, {id: 2, name:'na'}, {id: 3, name:'ra'}, {id: 4, name:'ka'}, {id: 5, name:'pa'}]; abc = [{id:'1' ...

I am looking to present a nested array within an array in a tabular format

This is the structure of my database: [{ "firstName": "Shaun", "salary": [ { "id":1, "rate": 250, }, { "id":2, "rate": 290, } ] },{ "firstName": "Julian", "salary": [ { "id":1, "rate": 750, ...

Convert object to JSON format using AJAX request to a PHP file

Despite receiving a 200 green response, my data is still not getting written to the json file and it remains blank. The JavaScript: $(function() { $('form#saveTemp').submit(function() { let savdAta = JSON.stringify($('form#save ...

triggering a function from a child component in React

I am facing a challenge in calling a function from the parent component that is stored in a child component. I understand how to do it from child to parent using props, but unsure about how to achieve it from parent to child. In the example below, you can ...

What are some effective strategies for designing the HTML/CSS layout when utilizing Electron?

Are people usually keying in the values manually, or is there a user interface (UI) builder available that works like the drag-and-drop UI builders found in Android and iOS? Just like how many websites today utilize site builders rather than requiring ma ...

Error encountered in ES6 destructuring syntax

Could you please assist me in figuring out what is causing the issue here: var foo = { bar: 1, baz: 2 }; var { bar, baz } = foo; I encountered an error SyntaxError: Unexpected token {. I am using node v5.4.1 and I am unsure if the problem lies wit ...

loop through nested arrays

My goal is to use ng repeat in Angular to iterate through a child array of a multidimensional array. The json object I am working with is as follows: $scope.items = [{ "id":1, "BasisA":"1", "Basis":true, "personSex": ...

Executing a search and obtaining XML data from an external source

Currently, I am faced with the challenge of using Ajax to submit a query to an external database located at http://foreignserver:1234/database?query="SELECT FROM WHERE". The goal is to execute the query, generate an XML file, and have it returned to me. Th ...

Unable to substitute a value using the useState hook in React

Whenever I click a key, I attempt to update the value of a variable but it appears not to be working as intended. ↓ The current implementation is not effective and needs improvement import React, { useState, useEffect } from 'react'; const Li ...

Create a dynamic menu dropdown with absolute positioning using React

I recently made the transition to React and now I am in the process of converting my old vanillaJS website into ReactJS. One issue I am facing is with a button that is supposed to trigger the opening of a dropdown menu. <button type="button&qu ...

What is the method for determining the numerical worth of the px containers?

https://i.stack.imgur.com/0K2TD.png Total width of the bar is set to 500px, with the red box at a width of 150px, the yellow box at 200px, and the green box at 50px. CSS Styles: .box { float:left; width:150px; box-shadow:3px 3p ...

Managing repeated calls to a specific get function in nodejs

Utilizing an Ajax call, I am invoking the following GET function every 10 seconds to monitor the status of various URLs. app.get('/getUrl', function(req, res) { var response = {}; var keyArr = []; var urlData ...

Calculate the position of an element's top property using dynamic JavaScript calculations

I am currently working on a script to control the scrolling speed of elements on a webpage relative to their parent div. The code I have written so far is as follows: $('#two').css({'top' : 600-($(this).scrollTop() / 1.2)+"px"}); The ...

Issue with using Sinon FakeServer with Mocha

I'm currently in the process of setting up a test for an API call. In my attempt to create a fake server within the before method, I have encountered issues with testing the basic implementation using $.ajax compared to my actual api call. Strangely, ...

The jQuery gallery is experiencing some functionality issues

Having an issue with the gallery on my website (currently on my computer, not yet uploaded to a server). Here is the script for the gallery (loaded from the server using PHP): $(document).ready(function() { $('.gallery').hide(); $(' ...

JavaScript function only activates on the second click

Can anyone assist me with this problem? I am attempting to retrieve the value of the image src upon clicking the tag with the class name downloadModal. I have made several attempts, and sometimes it only works on the second click, while other times it does ...

margin-top: automatic adjustment, with a minimum of 50 pixels

I am trying to add a minimum of 50px margin to the top of my footer tag using CSS, but I haven't been successful with the min() function. I'm not sure if I am overlooking something or if there is another correct approach to achieve this. * { ...

Creating a nested JSON output from a MySQL query in Node.js - A comprehensive guide

My database consists of 2 MySQL tables, referred to as kj (parent) and jj (children). Currently, my NodeJS code has an SQL query that returns the following output: [{ "id_kj": 1, "title_kj": "title1", "description_kj": "description1", "i ...

How can I effectively address issues with jqGrid's sorting and paging functionality?

After making changes to the server-side code, it now looks like this: int nm = objects.ToList().Count; if (objects.ToList().Count > 0) return new PagedList(objects, nm, 1, 25, null); else return null; The JSON data has been updated as follows ...

Loading a gallery dynamically using AJAX in a CakePHP application

I am currently working with Cakephp 2.8.0 and I am facing an issue with implementing ajax in my application. I have a list of categories displayed as li links, and upon clicking on a category, I need to remove certain html code, locate the necessary catego ...