Issue with Ajax Call preventing animation in Google Chart Gauge on Google API

setInterval(function () {
    refreshGauge();
}, 5000);

   var chart1 = null;
  function refreshGauge() {

        $.ajax({
            type: "POST",
            url: "Default.aspx/GetGuageData",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var data;

                var options = {
                            title: 'Run-rate',
                            width: 500, height: 500,
                            min: 0, max: 120,
                            redFrom: 0, redTo: 80,
                            yellowFrom: 101, yellowTo: 120,
                            greenFrom: 81, greenTo: 100,
                            minorTicks: 5,
                            majorTicks: ['0', '30', '60', '90', '120'],
                            animation: {
                                duration: 1000,
                                easing: 'inAndOut'
                            },
                        };
                if (chart1 === null) {
                    chart1 = new google.visualization.Gauge($("#guageChart")[0]);
                     data = google.visualization.arrayToDataTable([
                        ['Label', 'Value'],
                        ['Production', 0]
                    ]);
                    chart1.draw(data, options);
                    google.visualization.events.addOneTimeListener(chart1, 'ready', function () {
                              data = google.visualization.arrayToDataTable(response.d);
                              chart1.draw(data, options);
                            });
                }
                else {
                    data = google.visualization.arrayToDataTable(response.d); 
                }
                chart1.draw(data, options);
            },
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });
}

The Google Gauge chart code above is having an issue with the animation not working properly. The needle on the gauge stays fixed to one value and does not move as intended at regular intervals. When the data is updated, the gauge lines are redrawn instead of smoothly animated. Looking for a solution to fix this cool animation effect.

Answer №1

Seems like the chart is being drawn with the data before the 'ready' event can respond.

Try placing the last 'draw' statement inside the 'if' statement as indicated in the comments below:

            if (chart1 === null) {
                chart1 = new google.visualization.Gauge($("#guageChart")[0]);
                 data = google.visualization.arrayToDataTable([
                    ['Label', 'Value'],
                    ['Production', 0]
                ]);
                chart1.draw(data, options);
                google.visualization.events.addOneTimeListener(chart1, 'ready', function () {
                          data = google.visualization.arrayToDataTable(r.d);
                          chart1.draw(data, options);
                        });
            }
            else {
                data = google.visualization.arrayToDataTable(r.d); 
            }
            chart1.draw(data, options);  // <-- Move this up one line

            if (chart1 === null) {
                chart1 = new google.visualization.Gauge($("#guageChart")[0]);
                 data = google.visualization.arrayToDataTable([
                    ['Label', 'Value'],
                    ['Production', 0]
                ]);
                chart1.draw(data, options);
                google.visualization.events.addOneTimeListener(chart1, 'ready', function () {
                          data = google.visualization.arrayToDataTable(r.d);
                          chart1.draw(data, options);
                        });
            }
            else {
                data = google.visualization.arrayToDataTable(r.d); 
                chart1.draw(data, options);  // <-- to 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

The surprising twist of hasOwnProperty's behavior

I am currently working on a function that is designed to check whether an object contains keys such as 'id' or 'serif:id'. However, I have encountered some issues with its functionality. function returnIdPreferSerifId(object) { if ...

Learn how to simultaneously play two audio files using the same identifier in JavaScript

I'm facing an issue with two audio files that have a progress bar and both have the same ID: <audio id="player" src="<?=base_url('mp3/'.$rec->file)?>"></audio> </p> ...

Tips for accessing basic information from these websites without encountering CORS issues

Having issues retrieving data from the following two sites: and Eurolottery. The CORS issue is causing the problem, and I was able to retrieve data using a Chrome CORS extension and the provided code below: var HttpClient = function() { this.get = fu ...

The TS2769 error occurs when trying to change the react calendar due to no matching overload in the

The calendar functionality in my project was implemented using the response calendar library. Suddenly, I encountered an onChange prop error in the default code. This was working fine before. What steps should I take to resolve this issue? Here is my cod ...

Tips for incorporating a multiple tag search feature within my image collection using JavaScript?

I am facing a challenge with my image gallery search feature. Currently, users can search for images by typing in the title or tag of an image. However, I need to enhance this functionality to allow for multiple tags to be searched at once. For example, if ...

The attempt to compress the code in the file from './node_modules/num2persian' using num2persian was unsuccessful

I have been using the num2persian library to convert numbers into Persian characters. However, whenever I run the command npm run build, I encounter the following error: An error occurred while trying to minimize the code in this file: ./node_modules/num ...

Embark on a journey through a preorder traversal of a Binary Tree using TypeScript

Hello! I've been tasked with creating a function that iterates over a binary tree and returns all its values in pre-order. Here is the code snippet: interface BinTree { root: number; left?: BinTree; right?: BinTree; }; const TreePreArray ...

Is it possible to show one element while hiding others upon clicking using JavaScript?

Concept My idea is to create a website with a navigation menu where only one section is visible at a time. Each section would become visible upon clicking a specific button in the navigation bar. Challenge I attempted to achieve this using the following ...

How can I use JavaScript to access my templates directory in Django 3?

Perhaps I am taking the wrong approach here. My goal is to dynamically replace the content within the <nav> element using jQuery.load() when a specific <div> is clicked by the user. However, I am encountering difficulty in accessing the HTML fi ...

Are you looking for a scheduling tool that functions similar to a construction crew, handling all

Trying to import numerous product feeds is time-consuming. I attempted to set the Server.ScriptTimeout on the importfeeds.aspx page to an hour and run my tasks both sequentially and asynchronously. However, I am looking for a different solution. I would p ...

Guide to merging two endpoints in express.js to create a single endpoint

I currently have 2 existing endpoints named /balance and /transactions. What is the most effective approach to create a new endpoint called /balance-and-transactions without having to refactor the existing code? For example: a('/balance', () =&g ...

What is the best way to ensure that a Material UI transition component fully occupies the space of its parent component?

I've been experimenting with a Material UI transition component in an attempt to make it completely fill its parent container. So far, I've achieved some success by setting the width and height to 100% and applying a Y-axis translation for the co ...

JavaScript - Toggle Checkbox State Based on Array Values

Currently, I'm in the process of building a Laravel user management system for a project. This system will allow admins to edit any selected user who is registered on our website. Upon clicking the edit button, a modal form containing the user's ...

Stop users from repeating an action

We are encountering challenges with users repeating a specific action, even though we have measures in place to prevent it. Here is an overview of our current approach: Client side: The button becomes disabled after one click. Server side: We use a key h ...

Ways to extract data from form inputs with the help of jQuery

My HTML form allows users to enter data and upon submission, I need to use jQuery to capture the entered values. <form class="my-form needs-validation" method="POST"> <input type="text" id="firstName" name="First Name"> <input type="tex ...

Maintain the vertexShader's movement even when the mesh is rotating

Currently, I am adjusting the vertices of my cube along the y-axis. Although this method works well when simply moving the cube, issues arise when I begin rotating it. The movements continue to follow the global y-axis even during rotation. Is there a way ...

What is the best way to transmit data as a reply from a Node.js server to an AJAX client?

Currently, I am utilizing a function to transmit data regarding an HTML element as an object: function postItem(input) { $.ajax({ type: "POST", url: "http://localhost:8080", data: input, success: function(message) { Hconsole.log(&a ...

What is the best way to edit a span when there are two of them?

Attempting to modify the content of a span, but encountering a conflict with another span using the same class. -the one I don't wish to alter- <span id="chat-subrooms-toggle" class="chat-column-title"> Chat </span> However, this is the ...

Verifying that objects are eligible for garbage collection

My program in node.js receives a high volume of messages. Each time a message is received, I create a new object and pass the message content to it. Inside the constructor of the new object, various operations are performed, including some mongo tasks with ...

Generate a URL link based on the form input when the submit button is clicked

I am aiming to create a feature where a user can input a date to retrieve forecast data from the selected date. The URLs for this function are structured like wwww.mywebsite.com/forecasts/region/{{date}} using Pyramid's URL dispatch. I want users to b ...