The specific class for $attr.googleChart in google.visualization is not available for initialization

I'm currently working with AngularJS Google Charts and attempting to display a Line chart and Gantt chart. However, I am encountering difficulties specifically with showing the Gantt chart.

When trying to display the Gantt chart on the webpage, an error is being displayed in the console:

angular.js:5754 TypeError: google.visualization[$attr.googleChart] is not a constructor

Any insights or suggestions would be greatly appreciated.

Here is the JavaScript code snippet:

"use strict";

/*We need to manually start angular as we need to
wait for the google charting libs to be ready*/
google.setOnLoadCallback(function () {
    angular.bootstrap(document.body, ['my-app']);
});
google.load('visualization', '1', { packages: ['corechart'] });
 function daysToMilliseconds(days) {
      return days * 24 * 60 * 60 * 1000;
    } 

var myApp = myApp || angular.module("my-app", ["google-chart"]);

myApp.controller("IndexCtrl", function ($scope) {
    $scope.data1 = {};
    $scope.data1.dataTable = new google.visualization.DataTable();
     $scope.data1.dataTable.addColumn('string', 'Task ID');
     $scope.data1.dataTable.addColumn('string', 'Task Name');
      $scope.data1.dataTable.addColumn('date', 'Start Date');
      $scope.data1.dataTable.addColumn('date', 'End Date');
     $scope.data1.dataTable.addColumn('number', 'Duration');
     $scope.data1.dataTable.addColumn('number', 'Percent Complete');
     $scope.data1.dataTable.addColumn('string', 'Dependencies');

   $scope.data1.dataTable.addRows([
        ['Research', 'Find sources',
         new Date(2015, 0, 1), new Date(2015, 0, 5), null,  100,  null],
        ['Write', 'Write paper',
         null, new Date(2015, 0, 9), daysToMilliseconds(3), 25, 'Research,Outline'],
        ['Cite', 'Create bibliography',
         null, new Date(2015, 0, 7), daysToMilliseconds(1), 20, 'Research'],
        ['Complete', 'Hand in paper',
         null, new Date(2015, 0, 10), daysToMilliseconds(1), 0, 'Cite,Write'],
        ['Outline', 'Outline paper',
         null, new Date(2015, 0, 6), daysToMilliseconds(1), 100, 'Research']
      ]);


    $scope.data3 = {};
    $scope.data3.dataTable = new google.visualization.DataTable();
    $scope.data3.dataTable.addColumn("string", "Name")
    $scope.data3.dataTable.addColumn("number", "Qty")
    $scope.data3.dataTable.addRow(["Test", 1]);
    $scope.data3.dataTable.addRow(["Test2", 2]);
    $scope.data3.dataTable.addRow(["Test3", 3]);
});

Answer №1

If you're experiencing issues with the API, it might be due to using an outdated version. Try switching to loader.js instead of jsapi.js

With loader.js, you can easily load all the necessary libraries by specifying them in the packages array. For example, include packages: ['corechart', 'gantt']

Check out the updated demo here

In your index.html file

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 

And in your main.js file

    google.charts.load('visualization', '1', { packages: ['corechart', 'gantt'] });
    google.charts.setOnLoadCallback(function () {
        angular.bootstrap(document.body, ['my-app']);
    });

Remember:

  1. Use google.charts namespace instead of directly calling google.load
  2. Ensure that you call google.charts.load before google.charts.setOnLoadCallback
  3. Make sure to load all the necessary packages

For more information on older versions and limitations when loading charts, refer to Google's documentation here

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

AngularJs, Dynamically Adjusting URL Parameters Based on User Input

I am currently working on integrating my ASP.NET Web API with AngularJs. I am facing an issue where I need to pass optional parameters to the URL based on user input from 2 HTML text boxes, but I am unsure of how to accomplish this. Below is a snippet of ...

What is the best way to apply index-based filtering in Angular JS?

I am working on a tab system using ng-repeat to display tabs and content, but I'm facing some challenges in making it work seamlessly. Below are the tabs being generated: <ul class="job-title-list"> <li ng-repeat="tab in tabBlocks"> ...

Combining Rails API with AngularJS and IonicFramework

I'm embarking on a new project and feeling unsure about my chosen setup/approach. The project involves creating a list directory that doesn't require high computing power. My initial plan is to develop a website using Rails Api and AngularJs (al ...

express.js and socket.io compatibility perplexity

Server-Side Code: var server = require("http").Server(express); var io = require("socket.io")(server); server.listen(5000); io.on('connection', function(client) { client.on('order', function(data) { io.emit('place_orde ...

Automatically updating values in AngularJS

I currently have an array of dates stored in $scope.dates = [] ($scope.dates[0].date). My goal is to create a new array with durations that update automatically. $scope.dates[i].duration = Date.now() - $scope.dates[i].date. The objective is to display a ...

Is the username you want available?

I am facing a challenge in my registration form validation process where I need to verify the username input using javascript and flask in python, but the implementation is unclear to me. The requirement is to utilize $.get in the javascript segment along ...

What is the best way to instruct PhantomJS to pause until an AngularJS $resource is resolved before proceeding with testing the data it returns?

I am currently working on an AngularJS controller test script that utilizes PhantomJS. The purpose of the test is to check if the controller successfully loads "users" data from a database through a RESTFul web service using AngularJS' $resource servi ...

change the return value to NaN instead of a number

Hey there, I have something similar to this: var abc1 = 1846; var abc2 = 1649; var abc3 = 174; var abc4 = 27; if(message.toLowerCase() == ('!xyz')) { client.say(channel, `abc1` +`(${+ abc1.toLocaleString()})` +` | abc2 `+`(${+ abc2.toLocaleStri ...

How can you bypass classList being `undefined` in older browsers?

On modern browsers, the following code works perfectly fine. However, on legacy browsers it throws an error. What is the best way to resolve this issue? Error: TypeError: Result of expression 'document.getElementById("profile").classList' [unde ...

Invoke a controller class method using es6 syntax from within a directive template

Is it possible to invoke a controller class method using ES6 syntax from a directive template in AngularJS? For example, consider the following directive: import angular from 'angular'; function dpGrid() { return { restrict: ' ...

Stop the occurrence of OpenCPU javascript error pop-up notifications

I'm currently experiencing an error related to CORs during a test deployment of OpenCPU. While I may create a separate question for this issue in the future, for now, I am wondering if it is possible for the deployment to fail without alerting the end ...

How to insert an array into another array in JavaScript

Using Node, express, and mongoose to work on a project. I am attempting to include an Array as an element within another Array through a callback function. app.get('/view', function(req, res){ var csvRows = []; Invitation.find({}, func ...

Conducting an AngularJS AJAX call within a Symfony2 environment and utilizing Doctrine to generate a JSON

Currently, I am working on a project involving Symfony2, Doctrine, and AngularJS. While Symfony2 and Doctrine are not causing any issues, I am facing difficulties when using an ajax request with AngularJS. The problem lies in either the data not loading pr ...

Generating fresh dynamic IDs using JavaScript

I'm working on a piece of code that generates a grid-like arrangement of chocolate pieces based on user input for the dimensions. I want each chocolate piece to have its own unique ID (like imgPos1, imgPos2, etc.), but I'm struggling to implement ...

.submit fails to function when the bound object has been updated via an ajax call

I managed to implement an AJAX commenting system where, upon clicking the Post Comment button, an ajax call is made instead of submitting the form traditionally. Everything was working smoothly until I tried refreshing the page with the comment submit butt ...

Sending URL parameters from a React frontend to an Express API call involves crafting the request URL with

I'm currently working on a project where I need to make a fetch request from React to Express. The API URL requires parameters, but I want the last part of the URL to be dynamic. How can I pass a parameter from React to the Express API URL? Here' ...

Troubleshooting problem with AJAX returning JSON values

Utilizing a jQuery post request in the following manner: $.post('url', {data: some_data}, function(data, textStatus, jqXHR) { console.log(data); //for debugging console.log(data.status == "ok"); //for debugging .... }); The url hits ...

What is the most efficient way to iterate through array elements within a div element sequentially using React?

Currently, I am working with a large array of 2D arrays that represent the steps of my backtracking algorithm for solving Sudoku. My goal is to display each step in a table format, similar to animations that illustrate how backtracking works. Although I ha ...

Express is throwing an error indicating that the specified route cannot be found. Is there a way to dynamically

After dynamically adding routes and sending a post request through Postman, I encountered a "not found" error message. In the app, a 404 is logged next to the endpoint, indicating that Express cannot locate it. However, I know that I added the router dynam ...

After utilizing Geolocation, the input fields in Ionic/Angular JS are failing to update accurately

Currently, I am in the process of developing a new App using Ionic and Angular JS. Specifically, in one of the app tabs, I am utilizing geolocation to populate four fields (street name, street number, latitude, and longitude). Below is the snippet of my c ...