Charts.js sets canvas width/height to 0 and displays nothing

前端 未结 3 1523
眼角桃花
眼角桃花 2021-01-11 09:28

I am trying to use Charts.js to make the default line plot that they show in their example dynamically and put it in a div that I pop up on a user click. My code is like th

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 10:02

    It appears that the issue is that the canvas and all its parent nodes cannot have display none at the time the chart call is made so if you are using a chart in a popup you need to show the popup, construct the chart and then hide the popup.

    As this Fiddle shows, if you try and construct a chart in a hidden div and then show it on a timeout it does not work.

    If you instead show the div, make the chart and then hide the div, it does work.

    http://jsfiddle.net/bjudheoq/4/

    //This will break it
    //this.div.style.display = 'none';
    this.chart = new Chart(this.ctx).Line(data);
    this.div.style.display = 'none';
    

    The above fiddle works, but if you uncomment line 40 it does not.

提交回复
热议问题