JavaScript Chart js shows old data on mouse hover chart shows data changes,chart shows data,the chart shows

How can I fix chart js bug like it shows old data when I move the mouse on the chart.

My Js file

   $('#select-question').change(function(){
   var questionId = $("option:selected",this).val();
   $.ajax({
          type : "GET",
          dataType:"JSON",
          url : '/get-percentage',
          data : 
                {
                  'questionId' : questionId
                },
                success: function(data)
                {

                    console.log(data)
                    if(data == '')
                    {
                      alert('No Data')
                    }
                    var option = [];
                    var label = [];
                     for(var i=0;i < data.example.length; i++)
                     {
                    option.push(data.example[i]);
                    label.push(data.labels[i]);

                }

                var chartdata = {
                labels: label,
                datasets : [
                {
                        label: 'FeedBack Report Graph',
                        backgroundColor: [
                            'rgba(255, 99, 132, 0.2)',
                            'rgba(54, 162, 235, 0.2)',
                            'rgba(255, 206, 86, 0.2)',
                            'rgba(75, 192, 192, 0.2)',
                            'rgba(153, 102, 255, 0.2)',
                            'rgba(255, 159, 64, 0.2)'
                        ],
                        borderColor: [
                            'rgba(255,99,132,1)',
                            'rgba(54, 162, 235, 1)',
                            'rgba(255, 206, 86, 1)',
                            'rgba(75, 192, 192, 1)',
                            'rgba(153, 102, 255, 1)',
                            'rgba(255, 159, 64, 1)'
                        ],
                        hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                        hoverBorderColor: 'rgba(200, 200, 200, 1)',
                        data: option
                        }
                      ]
             };

        var ctx = $("#mycanvas");
                var myChart = new Chart(ctx, { type: 'pie', data: chartdata, options:{
                                   legend: {
                                   display: true
                              }
                            }  });
                myChart.destroy();    

        var barGraph = new Chart(ctx, {
                            type: 'pie',
                            data: chartdata,
                            options: {
                                   legend: {
                                   display: true
                              }
                            }
            });
          } 
   })

 })

I can able to display data from JSON response in chart js but the problem with it shows previous values when I just move the mouse on the chart.

I have tried destroy method but it did not work.How to prevent this problem??.

Is there any way to fix this error.Can any one help on this, please.

Answer:1

First off, why are you creating two same type of chart, with same data? There is no need!

You indeed need to use destroy method, but the reason it­'s not working is because, you haven't declared the chart variable in global scope. In order to destroy any instance of chart, the chart instance must be available in global scope (meaning, the chart variable should be globally accessible).

So, in your case, you should define the chart variable as :

myChart = new Chart(...);

or,

window.myChart = new Chart(...);

also, you need to destroy the previous instance of chart, before creating a new one, as such :

...
 if (myChart) myChart.destroy();
 myChart = new Chart(...);
...
Answer:2

Above solution worked for me but when I have two charts on the same page, Only one is showing. Other charts are becoming empty. Here is the solution for that.

  var ctx = document.getElementById("bar-chart").getContext("2d");  

  //Destroy the previous chart;
  //Rename the "bar" according to your component
  if(window.bar != undefined)
  window.bar.destroy();
  window.bar = new Chart(ctx , {});

if you don't change the "bar", only one chart will show in your page.

Answer:3

I am integrating my client website with Google APIs, and I have a task to validate the user address by using the user's postcode (UK), and to get the address, I have used https://ideal-postcodes.co.uk/...

I am integrating my client website with Google APIs, and I have a task to validate the user address by using the user's postcode (UK), and to get the address, I have used https://ideal-postcodes.co.uk/...

  1. google maps google maps
  2. google maps google sheets
  3. google maps google car
  4. google maps google docs
  5. google maps google drive
  6. google maps google play store
  7. google maps google is not defined
  8. google maps google snake
  9. google maps google maps directions
  10. google maps google maps go
  11. google maps google my business
  12. google maps google play services error
  13. google maps google cloud
  14. google maps google street view
  15. google maps google flights
  16. google maps google earth satellite view
  17. google maps google translate
  18. google maps google headquarters
  19. google maps google sheets function
  20. google maps google maps satellite

I was having some trouble when trying to sort and group the array items in JavaScript. Here is the sample inputs: var arr = [ {merchantName: '', branchName: 'e', branchAddress: '', total: 10.5}, ]; ...

I was having some trouble when trying to sort and group the array items in JavaScript. Here is the sample inputs: var arr = [ {merchantName: '', branchName: 'e', branchAddress: '', total: 10.5}, ]; ...

  1. javascript array sorting algorithm
  2. javascript array sorting methods
  3. javascript array sorting objects
  4. javascript array sorting
  5. javascript array sorting function
  6. javascript array sorting numbers
  7. javascript array sorting library
  8. javascript array sorting example
  9. javascript array sorting by key
  10. javascript array sorting date
  11. javascript array sort by property
  12. javascript array sort by value
  13. javascript array sort alphabetically
  14. javascript array sort string
  15. javascript array sort ascending
  16. javascript array sort by object property
  17. javascript array sort reverse
  18. javascript array sort desc
  19. javascript array sort not working
  20. javascript array sort compare function

We have multiple lists on the page that can have hundreds of items so for performance reasons, we'd like to use one-time bindings for things and only update when we know it needs to be updated and ...

We have multiple lists on the page that can have hundreds of items so for performance reasons, we'd like to use one-time bindings for things and only update when we know it needs to be updated and ...

Counting the items contained in an object, I used to have: Object.keys(state.posts).length Now, I have changed my posts state into an immutablejs Map, like so: Map({ "1": {title: "First Title"}, "2"...

Counting the items contained in an object, I used to have: Object.keys(state.posts).length Now, I have changed my posts state into an immutablejs Map, like so: Map({ "1": {title: "First Title"}, "2"...