最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chart disappears just after finishing animation - Stack Overflow

programmeradmin5浏览0评论

I'm trying to use chartjs, so I copied sample code from official document like this.

<canvas id="myChart" width="400" height="400"></canvas>
<script>
 var data = {
   labels : ["January","February","March","April","May","June","July"],
   datasets : [
 {
   fillColor : "rgba(220,220,220,0.5)",
   strokeColor : "rgba(220,220,220,1)",
   pointColor : "rgba(220,220,220,1)",
   pointStrokeColor : "#fff",
   data : [65,59,90,81,56,55,40]
 },
 {
   fillColor : "rgba(151,187,205,0.5)",
   strokeColor : "rgba(151,187,205,1)",
   pointColor : "rgba(151,187,205,1)",
   pointStrokeColor : "#fff",
   data : [28,48,40,19,96,27,100]
 }
   ]
 }
 //Get the context of the canvas element we want to select
 var ctx = document.getElementById("myChart").getContext("2d");
 var myNewChart = new Chart(ctx).PolarArea(data);
 new Chart(ctx).Line(data);
</script>

Example: /

The Chart appears at first, but when first animation ends it disappears. Javascript console shows nothing. And when a option animation is defined false, then it appears only split second and disappears likewise.

I tried in Firefox and Chrome in Mac, but results were same.

How does it happens, and how can I fix it?

I'm trying to use chartjs, so I copied sample code from official document like this.

<canvas id="myChart" width="400" height="400"></canvas>
<script>
 var data = {
   labels : ["January","February","March","April","May","June","July"],
   datasets : [
 {
   fillColor : "rgba(220,220,220,0.5)",
   strokeColor : "rgba(220,220,220,1)",
   pointColor : "rgba(220,220,220,1)",
   pointStrokeColor : "#fff",
   data : [65,59,90,81,56,55,40]
 },
 {
   fillColor : "rgba(151,187,205,0.5)",
   strokeColor : "rgba(151,187,205,1)",
   pointColor : "rgba(151,187,205,1)",
   pointStrokeColor : "#fff",
   data : [28,48,40,19,96,27,100]
 }
   ]
 }
 //Get the context of the canvas element we want to select
 var ctx = document.getElementById("myChart").getContext("2d");
 var myNewChart = new Chart(ctx).PolarArea(data);
 new Chart(ctx).Line(data);
</script>

Example: http://jsfiddle/DerekL/FZW9z/

The Chart appears at first, but when first animation ends it disappears. Javascript console shows nothing. And when a option animation is defined false, then it appears only split second and disappears likewise.

I tried in Firefox and Chrome in Mac, but results were same.

How does it happens, and how can I fix it?

Share Improve this question edited Feb 24, 2014 at 9:19 Derek 朕會功夫 94.5k45 gold badges198 silver badges253 bronze badges asked Feb 24, 2014 at 9:11 ironsandironsand 15.2k18 gold badges89 silver badges188 bronze badges 1
  • Related: stackoverflow./questions/33501151/… – Shog9 Commented Feb 23, 2016 at 22:28
Add a ment  | 

2 Answers 2

Reset to default 4

You are actually calling the new Chart() twice. Please see the below code. I connected out the unwanted line. Also please see the updated JSFiddle http://jsfiddle/sajith/VW4U5/

<canvas id="myChart" width="400" height="400"></canvas>
<script>
 var data = {
   labels : ["January","February","March","April","May","June","July"],
   datasets : [
 {
   fillColor : "rgba(220,220,220,0.5)",
   strokeColor : "rgba(220,220,220,1)",
   pointColor : "rgba(220,220,220,1)",
   pointStrokeColor : "#fff",
   data : [65,59,90,81,56,55,40]
 },
 {
   fillColor : "rgba(151,187,205,0.5)",
   strokeColor : "rgba(151,187,205,1)",
   pointColor : "rgba(151,187,205,1)",
   pointStrokeColor : "#fff",
   data : [28,48,40,19,96,27,100]
 }
   ]
 }
 //Get the context of the canvas element we want to select
 var ctx = document.getElementById("myChart").getContext("2d");
 //var myNewChart = new Chart(ctx).PolarArea(data);
 new Chart(ctx).Line(data);
</script>

If you are using Bootstrap, note that

I ran into the same problem. I am also using Bootstrap inside of a Laravel project. It turns out there is some sort of conflict - whenever Bootstrap's JS is loaded it causes the chart to disappear. My solution was to remove the "defer" attribute from the code that loads my app.js file, as described here: Laravel - Bootstrap Javascript's conflict with Chart.js

发布评论

评论列表(0)

  1. 暂无评论