简体   繁体   English

错误:未捕获类型错误:非法构造函数 JavaScript

[英]error: Uncaught TypeError: Illegal constructor JavaScript

const cases = document.querySelector('coronovirus-input');
const deaths = document.querySelector('deaths-input');
const recovered =document.querySelector('recovered-input');

const ctx = document.getElementById('mychart').getContext("2d");
let mychart = new CharacterData(ctx , {

  type:'pie',
  data: {
    labels : ['Active Cases','Deaths','Recovered'],
    datasets : [
      {
        label:'# of votes',
        data : [0,0,0],
        backgroundColor:['#2adece','#dd3b79','#ff766b'],
        borderWidth:1
      }
    ]
    
  }
});

const updateChartValue = (input, dataOrder) => {

  input.addEventListener ('change', e => {
    mychart.data.datasets[0].data[DataOrder] = e.target.value;
    mychart.update();
  });

};

updateChartValue(cases,0);
updateChartValue(deaths,1);
updateChartValue(recovered,2);

Hi Guys I cant solve this problem... i want to make pie chart but I cant I am newbie to share any post in this website sorry if I did any mistakes This Error嗨,伙计们,我无法解决这个问题...我想制作饼图,但我是新手,无法在本网站上分享任何帖子,如果我犯了任何错误,对不起这个错误

I see several issues in the code you posted, but most notably the constructor for initialising new mychart() should be Chart rather than new CharacterData() (possible typo due to auto-completion in your editor?).我在您发布的代码中看到了几个问题,但最值得注意的是初始化new mychart()的构造函数应该是Chart而不是new CharacterData() (由于您的编辑器中的自动完成可能会出现拼写错误?)。

The other one is in the updateChartValue that shows a mismatch in the case of the dataOrder argument and where it is used.另一个在updateChartValue中,它在dataOrder参数的情况下和使用它的位置显示不匹配。

See solution here https://codepen.io/beezital/pen/zYEmKRG在此处查看解决方案https://codepen.io/beezital/pen/zYEmKRG

const cases = document.getElementById('coronovirus-input');
const deaths = document.getElementById('deaths-input');
const recovered =document.getElementById('recovered-input');

const ctx = document.getElementById('mychart').getContext("2d");
let mychart = new Chart(ctx , {

  type:'pie',
  data: {
    labels : ['Active Cases','Deaths','Recovered'],
    datasets : [
      {
        label:'# of votes',
        data : [10,20,30],
        backgroundColor:['#2adece','#dd3b79','#ff766b'],
        borderWidth:1
      }
    ]
    
  }
});

const updateChartValue = (input, dataOrder) => {

  input.addEventListener ('change', e => {
    mychart.data.datasets[0].data[dataOrder] = e.target.value;
    mychart.update();
  });

};

updateChartValue(cases,0);
updateChartValue(deaths,1);
updateChartValue(recovered,2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:非法调用JavaScript - Uncaught TypeError: Illegal invocation javascript 未捕获的TypeError:JavaScript中的非法调用 - Uncaught TypeError: Illegal invocation in javascript Javascript:未捕获的TypeError,不是构造函数 - Javascript: Uncaught TypeError, not a constructor CORDOVA FILE PLUGIN未捕获的TypeError:非法构造函数 - CORDOVA FILE PLUGIN Uncaught TypeError: Illegal constructor 未捕获的类型错误:URL 不是 javascript 中的构造函数 - Uncaught TypeError: URL is not a constructor in javascript Javascript jQuery-未捕获的TypeError:非法调用 - Javascript jquery - Uncaught TypeError: Illegal invocation JavaScript方法中的“未捕获的TypeError:非法调用” - “Uncaught TypeError: Illegal Invocation” in a javascript method 未捕获的TypeError:JavaScript中的非法调用(地理位置) - Uncaught TypeError: Illegal invocation in Javascript (geolocation) 如何调试JavaScript错误“未捕获的TypeError:X不是构造函数” - How to debug the JavaScript error “Uncaught TypeError: X is not a constructor” addeventlistener drop error“Uncaught TypeError:Illegal invocation” - addeventlistener drop error “Uncaught TypeError: Illegal invocation”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM