Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Polar Chart inside bootstrap's Tabs] Failed to execute 'arc' on 'CanvasRenderingContext2D': The radius provided (-190) is negative. #4119

Closed
sandip-stats opened this issue Apr 6, 2017 · 7 comments

Comments

@sandip-stats
Copy link

Seeing the following issue when using Polar Charts inside bootstrap tabs.

Scripts used along with their versions:
jquery.js - 2.2.1 version
Chart.bundle.min.js - 2.1.5 version
bootstrap.min.js - 3.3.7 version

Have 3 tabs - 1st & 2nd tab contains bar charts, 3rd has the polar chart
Do note that the data is fetched via Ajax call to the server.

Here is the Stack:-

Uncaught DOMException: Failed to execute 'arc' on 'CanvasRenderingContext2D': The radius provided (-190) is negative.
at https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:15:16979
at Object.r.each (https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:13:18227)
at n.draw (https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:15:16701)
at t.Controller. (https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:13:10032)
at Object.r.each (https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:13:18227)
at t.Controller.draw (https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:13:10005)
at n.o.render (https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:13:9704)
at Object.startDigest (https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:13:5255)
at https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.5/Chart.bundle.min.js:13:4724

A resize of window is required to render the graphs.

Strange thing being if i move the polar chart to the 1st tab, all works fine.


My JS looks like the following:

function drawBarChart() {
  var jsonData = $.ajax({
    url: location.origin+'/build-report-3.0.1/getBuildUsageStats/builduserstats',
    dataType: 'json',
  }).done(function (results) {
    var tempData = {
      labels: [],
      datasets: [{
          data : [],
          backgroundColor : [],
      }]
    };
    var ct =0;
    for (var key in results) {
        tempData.labels[ct] = key;
        tempData.datasets[0].data[ct] = results[key];
        tempData.datasets[0].backgroundColor[ct] = 'rgba(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',0.4)';
        ct++;
   }
    // Get the context of the canvas element we want to select
    var ctx = document.getElementById("userSuccessRate");
    // set the Options
    var option = {
       animation: {
               duration:5000
       },
       title: {
           display:true,
           text:" INDIVIDUAL DEVELOPER's RUN RATE "
       },
       legend: {
           position:'bottom'
       },         
       deferred: {
    	   xOffset: 150, 
    	   delay: 250
       }
    }; 
    //Chart.defaults.polarArea.scale.lineArc = true;
    // Instantiate a new chart
    var myBarChart = new Chart(ctx, {
       type: 'polarArea',
       data:tempData,
       options:option,    
    });
  });
}
drawBarChart();

Am i missing something or doing anything incorrectly?
Any help would be really appreciated.

Regards,
Sandip

@etimberg
Copy link
Member

etimberg commented Apr 6, 2017

I believe this is fixed in later versions. Can you try version 2.5.0?

@etimberg
Copy link
Member

etimberg commented Apr 7, 2017

Thanks for checking @sandip-stats. Is it possible to drop this in a fiddle so we can debug?

@sandip-stats
Copy link
Author

sandip-stats commented Apr 7, 2017

Hello @etimberg
You can use the following - https://jsfiddle.net/sandybose/obuyoyq7/
You would notice if you change:

<div role="tabpanel" class="tab-pane fade in active" id="buildusage">
    ..
  </div>
  <div role="tabpanel" class="tab-pane fade" id="issuescaught">
    ..
  </div>
  <div role="tabpanel" class="tab-pane fade" id="developerstat">
    <canvas id="userSuccessRate"></canvas>
  </div>

TO

<div role="tabpanel" class="tab-pane fade in active" id="buildusage">
    <canvas id="userSuccessRate"></canvas>
  </div>
  <div role="tabpanel" class="tab-pane fade" id="issuescaught">
    ..
  </div>
  <div role="tabpanel" class="tab-pane fade" id="developerstat">
    ...
  </div>

the chart works.

Do note this is just a mock up; so that the issue can be showcased. In my code the data is being fetched via an ajax call.

Regards,
Sandip

@nicdcs
Copy link

nicdcs commented May 26, 2017

This is nothing bootstrap specific. See this fiddle https://jsfiddle.net/L7k6Lxfb/

If the display style of the container around the canvas is "none" or "inline" the exception is thrown.
(Uncaught DOMException: Failed to execute 'arc' on...)

So you can't initialize a polar chart in a "collapsed" area :/

@etimberg
Copy link
Member

etimberg commented Jul 4, 2017

I think this is the same issue as #4397 which is already fixed

@etimberg etimberg closed this as completed Jul 4, 2017
@etimberg etimberg added this to the Version 2.7 milestone Jul 4, 2017
@MarciaLing
Copy link

Using Chart.js the latest version 2.7.2.
Tried all those solution and this problem still exits.
Finally I modified the source file Chart.boundle.js which was downloaded from cdnjs.
and set every radius value before passed to arc function to be positive.
This way works perfectly fine.

ctx.arc(vm.x, vm.y, Math.abs(vm.outerRadius), sA, eA);
ctx.arc(vm.x, vm.y, Math.abs(vm.innerRadius), eA, sA, true);
ctx.arc(x, y, Math.abs(radius), 0, Math.PI * 2);
ctx.arc(scale.xCenter, scale.yCenter, Math.abs(radius), 0, Math.PI * 2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants