Morris.js is throwing an error message, stating "Uncaught TypeError: Unable to access the 'label' property as it

I have successfully implemented a bar chart along with a date picker using Bootstrap. The bar chart loads data accurately when selecting a specific day. However, upon inspecting the developer tools, I encounter the following errors:

Uncaught Type Error: Cannot read property 'label' of undefined Morris 1590 Uncaught Type Error: Cannot read property 'length' of undefined Morris 424

I am seeking guidance on how to resolve these errors as I aim to eliminate them completely.

     function hourString(hour) {
     if (hour === 0) {
         return "12:00 am";
     }
     if (hour < 12) {
         return hour + ":00 am";
     }
     if (hour === 12) {
         return "12:00 pm";
     }
     return (hour - 12) + ":00 pm";
 }

  var ordersPackChart =
     Morris.Bar({
         element: 'morris-bar-chart',
         data: [],
         xkey: 'y',
         ykeys: ['a'],
         ymax: 1000,
         labels: ['Pack per hour'],
         hideHover: true,
         resize: true,
         barColors: ['#ed5565'],
         parseTime: false
     });

 function packResult(historydate, result) {
     result = JSON.parse(result);

     var data = [];
     for (var hour = 6; hour < 24; hour++) {

         var numberPack = 0;
         for (var i = 0; i < result.Result.length; i++) {
             if (result.Result[i].PACK_HOUR == hour) {
                 numberPack = result.Result[i].NUM_ORDER_PACK;
                 break;
             }
         }

         data.push({ y: hourString(hour), a: numberPack });
     }

     ordersPackChart.setData(data);
 }


 $(document).ready(function () {        
     $("#fromdate").datepicker({
         autoclose: true
     }).change(dateChanged)
       .on('changeDate', dateChanged);
      });

 function dateChanged(ev) {
     $(this).datepicker('hide');

     var day = $('#fromdate').val();

     $.ajax({
         type: "GET",
         url: '@Url.Action("GetQueryResult")',
         context: document.body,
         data: {
             querySetName: 'dashboard-packorder-statistics',
             queryName: 'OrderPack',
             historydate: day
         },
         success: function (result) {
             packResult(ordersPackChart.HISTORY_DATE, result);
         },
         error: function (xhr) {
             var message = "ErrorStatus: " + xhr.status + "  ReadyState: " + xhr.readyState;
         }
     });
 }

 // Reload the Morris chart
 jQuery(function ($) {
     $('#fromdate').on('change', function () {
         ordersPackChart.options.fromdate = $(this).is('changeDate');
         ordersPackChart.redraw();
     });
 });

Answer №1

I resolved the issue that was causing errors whenever my mouse hovered over a chart with no data. I also fixed the mistake of calling the datepicker event twice.

Below are the modifications I made:

       function hourString(hour) {
     if (hour === 0) {
         return "12:00 am";
     }
     if (hour < 12) {
         return hour + ":00 am";
     }
     if (hour === 12) {
         return "12:00 pm";
     }
     return (hour - 12) + ":00 pm";
 }

  var ordersPackChart;

 function packResult(historydate, result) {
     result = JSON.parse(result);

     var data = [];
     for (var hour = 6; hour < 24; hour++) {

         var numberPack = 0;
         for (var i = 0; i < result.Result.length; i++) {
             if (result.Result[i].PACK_HOUR == hour) {
                 numberPack = result.Result[i].NUM_ORDER_PACK;
                 break;
             }
         }

         data.push({ y: hourString(hour), a: numberPack });
     }

     if (ordersPackChart != null) {
         ordersPackChart.setData(data);
     } else {
         ordersPackChart =
             Morris.Bar({
                 element: 'morris-bar-chart',
                 data: data,
                 xkey: 'y',
                 ykeys: ['a'],
                 ymax: 1000,
                 labels: ['Pack per hour'],
                 hideHover: true,
                 resize: true,
                 barColors: ['#ed5565'],
                 parseTime: false
             });
     }
 }


 $(document).ready(function () {        
     $("#fromdate").datepicker({
         autoclose: true
     }).change(dateChanged);
 });

 var currentDay;

 function dateChanged(ev) {
     $(this).datepicker('hide');

     var day = $('#fromdate').val();
     if (day == currentDay)
         return;

     currentDay = day;

     $.ajax({
         type: "GET",
         url: '@Url.Action("GetQueryResult")',
         context: document.body,
         data: {
             querySetName: 'dashboard-packorder-statistics',
             queryName: 'OrderPack',
             historydate: day
         },
         success: function (result) {
             packResult(day, result);
         },
         error: function (xhr) {
             var message = "ErrorStatus: " + xhr.status + "  ReadyState: " + xhr.readyState;
         }
     });
 }

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

Unable to show an image within the <div> element when hovering the mouse

Is it necessary to use the background style image in order to display an image on mouseover of a name? I have implemented a controller and would like the image to be replaced by text within a div tag. Below is my HTML code: <!DOCTYPE html> <html& ...

"The Ajax POST request to MyUrl returned a 404 error, indicating that the resource

While working on my Grails code, I encountered an error when the Ajax function received a response from the controller action. The parameters are being passed successfully by the Ajax function, and the controller function is executed. However, upon return ...

Navigating through different pages and updating the URL using AJAX requests

Greetings! I am currently in the process of learning ajax/jquery, so please forgive any potential rookie mistakes. As I scoured the internet for a solution to my current dilemma, I stumbled upon bits and pieces of information here and there, but I am stru ...

What is the best way to showcase a JSON Array in a Blade view?

Check out my database table structure Incorporating a view composer to retrieve data from the mentioned table and return it to my view class NavComposer { public function compose(View $view) { if (Auth::check()) { $view->with ...

Struggling to locate an element with jQuery's closest() method?

Currently, I am utilizing jQuery and Ruby on Rails for my project. Within a table with 3 columns, there is a form element in the form of a select box that utilizes AJAX to submit the result. My goal is to change the color of a check mark by adding or remov ...

Ways to pass a message from index.html to a Vue.js 3 instance

Picture this scenario: You have a Vue index.html file that also loads a custom script: <!DOCTYPE html> <html lang="en"> <head> ... ... <script type="text/javascript"> languagePluginLoader.then(fun ...

Using Python to convert JSON to a JSON array and add the date at the start

I am working with the following JSON data: [{"UGC_TECH_PLATEFORME": "youtube", "UGC_TECH_ID": "UCu93VC-rD_TolBF4Pe5yz_Q"}] My desired result is: [{"2020-09-23":{"UGC_TECH_PLATEFORME": "youtu ...

Does Angular's compile function operate asynchronously?

In my service, I use $compile to compile the template. The JavaScript functions are executed one after another, but in order to retrieve the final HTML content, I have to place html() within a timeout callback. Otherwise, the template includes {{ placeho ...

Building an Angular 4 universal application using @angular/cli and integrating third-party libraries/components for compilation

While attempting to incorporate server side rendering using angular universal, I referenced a post on implementing an angular-4-universal-app-with-angular-cli and also looked at the cli-universal-demo project. However, I ran into the following issue: Upon ...

Phonegap and the Importance of HTTP Requests

Having an unusual issue with Phonegap where my XMLHttpRequests are firing twice. Currently, I am working on an app that involves using XMLHttpRequests to generate a dynamic list of events for users to choose from. In addition to this, I am utilizing jQuery ...

Delivering an XML file generated by PHP to a JavaScript parser

I'm in the process of creating a smart TV app that streams live content. The app functions properly when I provide it with a valid XML playlist. However, when I attempt to use PHP to generate the XML file (which generates without any issues), it fail ...

Navigating a page without embedding the URL in react-router-dom

In my application, I am utilizing react-router-dom v5 for routing purposes. Occasionally, I come across routes similar to the following: checkup/step-1/:id checkup/step-2/:id checkup/step-3/:id For instance, when I find myself at checkup/step-1/:id, I int ...

After the component has been initialized for the second time, the elementId is found to be null

When working with a component that involves drawing a canvas chart, I encountered an issue. Upon initializing the component for the first time, everything works fine. However, if I navigate away from the component and return to it later, document.getElemen ...

In what ways can I incorporate Django template tags into my JavaScript code?

I've encountered an issue with implementing my model data into a FullCalendar library template in JavaScript. Despite seeing others do the same successfully, I keep getting errors as the template tags fail to register properly. <script> documen ...

JavaScript - Utilizing an image file in relation to a URL pathway

Is there a way to reference an image URL using a relative path in a JavaScript file similar to CSS files? To test this, I created two divs and displayed a gif in the background using CSS in one and using JavaScript in the other: -My file directory struct ...

Eliminating blank attributes within an array of objects

I'm currently working on a task that involves creating an array to summarize another array. I've received valuable suggestions from various sources, including this discussion on Stack Overflow. Although the solutions provided are effective, they ...

bash: The command "nodemon" could not be located

My experience with nodemon has been smooth for the past few months. However, today I encountered an error that I couldn't resolve. Despite uninstalling and reinstalling nodemon, as well as force installing it, I still received the same error message w ...

What are the steps for applying a Bootstrap class to an element?

I keep encountering this error in the console: Uncaught DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('si col-md-4') contains HTML space characters, which are not valid in tokens. Below is a ...

Issue with `styles` argument after updating from Material version 4 to 5: MUI

After recently upgrading from Material V4 to V5, I encountered the following error message: MUI: The `styles` argument provided is invalid. You are providing a function without a theme in the context. One of the parent elements needs to use a ThemeProvider ...

Embedding HTML Tags within an array element

The task at hand involves adding an HTML element from Array Value to the Document Object Model template: { 0: { h1: '<h1>Hi</h1>' }, 1: { h2: '<h2>Hi</h2>' }, 2: { h3: &a ...