Adjusting the rotation transform by deleting and reapplying canvas is causing the chart to not display data after redrawing

I am facing a challenge with my previous issue and I am exploring different solutions. One approach I am considering is to remove the canvas element completely and then re-add it, effectively resetting all rotations on the canvas.

Although this method almost works, I encounter an issue when trying to redraw the chart using chart.js - nothing appears on the canvas.

I have inserted some console.log statements inside the onComplete function of chart.js and while data is logged in the console, it does not reflect on the canvas.

View the current jsfiddle where onComplete data is not visible.

Refer to this jsfiddle for the rotation issue that I am trying to address.

Previous question: Read about it here

I am creating a rotating wheel on a canvas and after rotating it, I want to reset the rotation to 0. However, due to the CSS property:

-webkit-transition: -webkit-transform 15s ease;
, resetting the rotation results in a 15-second transition from r -> 0. Is there any way to reset the rotation without triggering the transform 15s ease?

I need to redraw the data on the canvas immediately after the rotation transformation completes, requiring an instantaneous reset.

Thank you!

var r=-(3600 + random);
$("#wheel").css("transform","rotate("+r+"deg)");
$("#wheel").css("-moz-transform","rotate("+r+"deg)");
$("#wheel").css("-webkit-transform","rotate("+r+"deg)");
$("#wheel").css("-o-transform","rotate("+r+"deg)");

$("#wheel").one('webkitTransitionEnd', function() {
    $("#wheel").css("transform","none");
    $("#wheel").css("-moz-transform","none");
    $("#wheel").css("-webkit-transform","none");
    $("#wheel").css("-o-transform","none");
});

Answer №1

Your code is almost there, just a few adjustments needed

  1. It seems like the issue lies with the version of Chart.js you are using. I updated it to the latest version (2.1 @ https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.min.js)

  2. Prior to executing $('#wheel').remove();, remember to include myChart.destroy(); to properly remove the chart from Chart.js's internal object list for resizing purposes

  3. The section in your code is clearing all data

    ...
    for(i=chartData.datasets[0].data.length-1; i>=0; i--) {
      chartData.datasets[0].data.splice(i,1);
      chartData.datasets[0].backgroundColor.splice(i,1);
    }
    ...

By initializing your chart with chartData, you end up trying to display an empty chart. This might have been a mistake during pasting. In the revised fiddle, I modified it to only remove the last sector like this:

var i=chartData.datasets[0].data.length-1;
chartData.datasets[0].data.splice(i,1);
chartData.datasets[0].backgroundColor.splice(i,1);

If you wish to restore all sectors for subsequent runs, simply delete that segment.


Fiddle - https://jsfiddle.net/wzbxuo5n/

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

`Need to clean parameters for safe use in JavaScript code?`

I am working with the php code below: <?php $redirect_lp = $_GET['lp']; ?> <script> setTimeout(function(){ window.location.href = "<?php echo $redirect_lp; ?>"; }, 10) </script> How can I properly sanitiz ...

Automated vertical alignment of rows within the Bootstrap table

I'm currently working on a table for my personal project that populates with data from the database. I am trying to get the rows to display vertically under headings (see screenshot at the bottom of this question). I have attempted various solutions f ...

Obtain the dimensions of an element using the method `Sys

Recently, I have been working on an asp.net 4.0 web application where I encountered a challenge while trying to retrieve the bounds of a panel using Sys.UI.DomElement.getBounds method. My approach involved utilizing JQuery with the following code snippets: ...

Successful Mongoose query in Node.js, however the array remains empty even after using forEach loop. Issue with MongoDB integration

After performing a forEach inside an asynchronous method, I am trying to return an array of names. The issue is that despite the forEach working correctly, the array always ends up empty. On the website side, here is my request: function retrieveCharity( ...

Issues with the node.js and discord.js API due to superagent integration

Hey there, I've been working with an API that provides information based on ID inputs. Everything was running smoothly until I encountered an issue with invalid IDs. Instead of displaying a message like Invalid ID, my bot crashes when a wrong ID is en ...

Can cucumber steps be executed conditionally within a single scenario?

I would like to streamline my testing process by using one feature file for both desktop and mobile tests. I am looking to run two separate tests, with one tagged as @mobile and the other as @desktop. By doing this, I can avoid creating a duplicate feature ...

Display unique information according to the value in the form field (email domain)

I've developed an innovative ajax signup form that integrates jQuery and CSS3. The unique feature of this form is its multi-step process. In the first step, users are required to enter their email address and password. What sets this form apart is i ...

The process of unescaping characters in jQuery.post() operations

I am currently working on a website that allows users to submit code, which is then processed by a PHP script and saved onto the server. To send the code to the PHP script, I am utilizing jQuery's $.post() method. However, I am encountering an issue w ...

What could be causing the Ioncol not triggering the Onclick event?

I am facing an issue where my onclick event is not working on an ion-col. The error message says that the method I call "is not defined at html element.onclick". Here is a snippet of my code: <ion-row style="width:100%; height:6%; border: 1px solid # ...

Incorporate various Vue.js components into the main parent component

I am currently utilizing Vue.js to create a user interface for my HTML5 game. I have a scenario where I want to define UI containers that essentially group other UI components and position them on the screen. Here's an example of what I'd like to ...

What is the best way to perform a keyword search in AngularJS?

My data consists of code:description pairs (e.g. cmsc100: Web Programming). I have a search box where users can enter either the code or description. Upon clicking the button, the program should display the data that matches the entered code/description or ...

Exploring JSON data with JQuery: A comprehensive guide to looping through with each

I am currently facing an issue with looping through cached JSON data obtained from an Ajax call. Specifically, I keep receiving the error message 'Cannot read property 'title' of undefined'. Can someone provide assistance? $.each(cach ...

Adjust div elements to fit the size of the window

I'm currently working on designing a new web application's skeleton layout. I have come across some issues, particularly with CSS layouts and DIV. 1) Boxes 1 and 2 in the first column do not align with boxes 3 and 4 in the second and third colum ...

Setting URL parameters dynamically to the action attribute of a form using JavaScript code

I'm having trouble posting Form data to my Server. I am dynamically setting the element and its URL parameters for the action attribute, but it seems like it's not recognizing those attributes. Can you help me figure out what I'm doing wrong ...

Extract the necessary fields from the JSON Schema

I am in search of a solution to extract the required fields from a JSON Schema and Data combination. Currently, we are utilizing AJV for getting error messages in our forms using JSON Schema, and it has been performing really well. I am looking for a met ...

Every time I attempt to submit data, I encounter a 404 error with AXIOS

Struggling to figure out why I keep encountering an error when trying to send form data from my website to the database using axios? Despite attempting various solutions, the problem persists. Although I can successfully retrieve manually entered data from ...

What could be causing bundle.js to remain in a pending status on my website?

Whenever I try to open a page from my project, the browser keeps showing loading forever and in the network tab, the status of bundle.js is pending. (Interestingly, if I open other routes of the project, everything works fine). If I remove all product var ...

Struggling to Retrieve Specific Keys for Individual Values in Firebase with React Native

I am currently experiencing difficulty obtaining a unique key for each value in the 'users1' table. firebase.database().ref('users1').once('value').then(snapshot => { var items = []; snapshot.forEach((child) => { ...

What is causing Firefox to consistently shave off 1px from the border of table cells?

Why is Firefox removing 1px from the border's value as defined in the CSS file? .aprovGriditem th { border-collapse: collapse; border: 4px solid #BBC6E3; padding: 0; } UPDATE <table cellpadding="0" cellspacing = "1" runat="server" id= ...

Tips for successfully passing an object with a list property in an ajax request

I have encountered a similar problem to This previous question, but I am struggling to implement the solutions provided. I am unsure of where to include the ModelBinder code mentioned in the responses, so I will explain my issue here. I am working with a s ...