Mismatch of data types in Google Visualization

I am working with Google Visualization and receiving Unix Epoch timestamps that I need to convert into an array of strings for use in Google Charts. However, I keep encountering an error:

Type mismatch. Value 2017-8-25 16:23:54,2017-8-25 16:11:54,... does not match type string in column index 0

This is the code snippet causing the issue:

 var jsonData = $.ajax({
    url: "JSONurl",
    type: "GET",
    dataType: 'json',
}).done(function (dataJson) {
    var mass = [];
    for (var i = 0; i < dataJson.length; i++) {
        var dateEx = new Date(dataJson[i].dateExecutes);
        var year = dateEx.getFullYear();
        var month = dateEx.getMonth()+1;
        var day = dateEx.getDate();
        var hours = dateEx.getHours();
        var mins = dateEx.getMinutes();
        var secs = dateEx.getSeconds();
        var newDate = (year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + secs);
        mass[i] = newDate;
    }
    
    var data = new google.visualization.DataTable();
    data.addColumn('string', mass);
    data.addColumn('number', 'passed');
    data.addColumn('number', 'failed');
    
    dataJson.forEach(function (row) {
            data.addRow([
            mass,
            row.passed,
            row.failed
        ])
    });

I need guidance on how to properly use an array in a column and how to construct cells from an array. Any assistance would be greatly appreciated.

Answer №1

It seems like your goal is not entirely clear.

If you simply want a nicely formatted date string in the first column, I suggest utilizing Google's DateFormat class.

You don't need to use two loops; the date can be formatted directly within the forEach statement.

Please refer to the code snippet below:

var jsonData = $.ajax({
  url: "JSONurl",
  type: "GET",
  dataType: 'json',
}).done(function (dataJson) {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Date');
  data.addColumn('number', 'passed');
  data.addColumn('number', 'failed');

  var formatDate = new google.visualization.DateFormat({
    pattern: 'yyyy-MM-dd hh:mm:ss'
  });

  dataJson.forEach(function (row) {
    var rowDate = new Date(row.dateExecutes);
    data.addRow([
      formatDate.formatValue(rowDate),
      row.passed,
      row.failed
    ]);
  });
});

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

implement a dropdown feature for a vertical menu with the help of Jquery

Struggling to implement a dropdown feature on a vertical navigation using Jquery. The HTML includes a nav section with 1st level list items and nested li's for second level menu items. On clicking the 1st level li, the intention is to toggle SHOW/HID ...

What is the purpose of passing data into the 'success' function of an AJAX request?

Recently, I embarked on a journey to learn jQuery and AJAX, but I found myself tangled in confusion when it came to AJAX requests. To gain practice, I decided to create a simple TodoApp using Node, jQuery, and Bootstrap. While I managed to grasp GET and P ...

The #each helper in Handlebars is used to iterate over an array

I have a function that generates an array as output. I am looking for a way to iterate over this array using the each method. Can anyone provide guidance on how to achieve this? Consider if the handlebars helper produces the following array: details: [{ ...

The WCF response has been deemed invalid because of the presence of an unexpected string combination involving WCF and JSON

I recently set up a WCF service to interact with JSON data, but I'm encountering an issue where all entries are being escaped. [ { "rel":"http:\/\/localhost:3354\/customer\/1\/order", &qu ...

Extract keys enclosed in quotation marks using regular expressions

My coding challenge involves working with JSON data in a specific format: define({ somekey : "Some {text} here:", 'some-key' : "Text:", 'key/key' : 'Some : text', key: 'some value', 'my-key' : &a ...

Is it possible to turn off ajax within an iframe?

I'm currently developing a project that allows users to customize their page. What is the best way to prevent ajax functionality without having to disable javascript in an iframe? ...

How can I transfer form data to a PHP variable using an AJAX request?

Encountering some difficulties, any insights? I have included only the necessary parts of the code. Essentially, I have an HTML form where I aim to extract the value from a field before submission, trigger an ajax call, and fill in another field. It seems ...

Issue with passing boolean parameter in Javascript not functioning properly

I have a function that contains multiple if statements, designed to execute when a parameter is true. However, I've noticed that passing false as a parameter does not seem to have any effect. Can you help me figure out what I'm doing wrong? fu ...

When attempting to import Three.js canvas on Github Pages, a 404 error is received

While attempting to host my webpage with a three.js background, I encountered an issue where everything loads properly when hosted locally, but nothing loads when pushed to GitHub pages - only the HTML is visible. I am utilizing Vite to package my code an ...

Django form submission triggers Jquery modal to confirm object deletion

I'm currently working with Django and Crispy forms while also utilizing the Shopify Embedded Apps SDK. My goal is to implement a modal window that prompts the user to confirm the deletion of an object. My progress code is attached below. Although I h ...

Something is overriding the style created by makestyle material UI that I had implemented

How can I increase the importance of my makeStyles classes over default Material UI styles? Here is my code: import { createTheme, ThemeProvider } from '@mui/material/styles'; import { makeStyles, createStyles } from '@mui/styles'; co ...

Tips for reusing a form within a one-page website

I am looking for a way to handle a form's submit action using JavaScript. My goal is to hide the form when it is submitted and then be able to reuse it later. However, the code I currently have seems to have a hidden state that prevents the submit act ...

What is the method for executing a server-side script without it being transmitted to the browser in any way?

Although the title may be misleading, my goal is to have my website execute a php file on the server side using arguments extracted from the html code. For example: document.getElementById("example").value; I want to run this operation on the se ...

Update the document by sending the data and making changes based on the AJAX response

Currently, I am avoiding using jQuery and trying to work with the following code in PHP: <?php header ( 'Content-Type: text/xml; charset=utf-8' ); $con = @mysql_connect ( "localhost", "root", "" ) or die ( "Couldn't connect to database" ...

No results found by Mongoose find() method

I've set up a route that utilizes a model named Todo as shown below: app.get('/api/todos', function(req, res) { Todo.find({},function(err, todos) { if (err) res.send(err); console.log("number of todos " + tod ...

Textfield removal from the input range is requested

I recently encountered an issue with my input range HTML code. Here is the initial code snippet: <input class="sliderNoTextbox" id="ti_monatlich" type="range" name="ti_monatlich" data-theme="d"> After some adjustments based on this helpful answer, ...

Navigating between interfaces without the need to constantly refresh or reload

Currently, I am in the process of developing a website using ASP.NET MVC that allows users to navigate between pages without refreshing each time. My approach involves treating views as 'areas' or mini master pages, utilizing partial views inste ...

Ensuring nested JSON key existence before rendering in React

When retrieving data from MarvelAPI, there is no certainty that the key being accessed will be available. Therefore, I am implementing if statements to check each key before accessing them. The current code below appears quite messy. Is there a more effic ...

Troubleshooting: jQuery toggle() issue in Firefox 3.0.12

My jQuery code for toggling is working perfectly in IE6 but not in FF3. I'm wondering what could be causing this issue and if there is a workaround available. <button>Toggle Me</button> <p>Hi</p> <p>Learning jQuery&l ...

There seems to be an issue with the Typescript version compatibility in the node_modules folder for the Angular Material table cell component

While working on my project with Angular, I encountered an issue. I attempted to downgrade my TypeScript version to 3.9.7 but the problem persists. Below are the dependencies listed in my package.json: "private": true, "dependencies&qu ...