D3和Javascript:UncaughtTypeError:无法读取属性';应用';未定义的

D3和Javascript:UncaughtTypeError:无法读取属性';应用';未定义的,javascript,d3.js,nvd3.js,Javascript,D3.js,Nvd3.js,我对D3和JavaScript非常陌生,我正试图建立一个基本的甜甜圈图表,但由于这个错误,我陷入了困境。我可以看到值进入donutChartData,但图表仍然无法在浏览器上显示。 第行调用(donutChart)时出错 例外情况: Uncaught TypeError: Cannot read property 'apply' of undefined at Array.Co.call (d3.min.js:3) at Socket.<anonymous> (mai

我对D3和JavaScript非常陌生,我正试图建立一个基本的甜甜圈图表,但由于这个错误,我陷入了困境。我可以看到值进入donutChartData,但图表仍然无法在浏览器上显示。 第行调用(donutChart)时出错

例外情况:

Uncaught TypeError: Cannot read property 'apply' of undefined
    at Array.Co.call (d3.min.js:3)
    at Socket.<anonymous> (main.js:137)
    at Socket.Emitter.emit (socket.io.js:7414)
    at Socket.onevent (socket.io.js:7130)
    at Socket.onpacket (socket.io.js:7088)
    at Manager.<anonymous> (socket.io.js:7520)
    at Manager.Emitter.emit (socket.io.js:7414)
    at Manager.ondecoded (socket.io.js:2823)
    at Decoder.<anonymous> (socket.io.js:7520)
    at Decoder.Emitter.emit (socket.io.js:2285)

您的代码应该如下所示:

var donutChart;
var donutChartData;
function drawChart() {
//Donut chart example
nv.addGraph(function() {
donutChart = nv.models.pieChart()
  .x(function(d) { return d.label })
  .y(function(d) { return +d.value })
  .showLabels(true)     //Display pie labels
  .labelThreshold(.05)  //Configure the minimum slice size for labels to show up
  .labelType("percent") //Configure what type of data to show in the label. Can be "key", "value" or "percent"
  .donut(true)          //Turn on Donut mode. Makes pie chart look tasty!
  .donutRatio(0.35);  //Configure how big you want the donut hole size to be.     

  d3.select("#Chart4 svg")
    .datum(donutChartData)
    //.transition().duration(350)
    .call(donutChart); /// ERROR OCCURRED ON THIS LINE

 return donutChart;
});
}


socket.on("donutChartData",function(dChartData){
    donutChartData=dChartData; 
    console.log("Donut Chart: ",dChartData[0].label,dChartData[0].value);
    drawChart();
});

您必须在获得图表数据后绘制图表,而您现在没有这样做。

非常感谢Prajwal的快速响应,我尝试了您的代码,现在我得到了未捕获的类型错误:无法读取同一行上未定义的属性“map”。请检查代码中是否使用了名为“map”的变量,但从未分配过价值。你能创建JSFIDLE来展示你的代码的实际工作吗?我已经在这个问题上创建了一个新的帖子,里面有完整的代码,请看一看。JSFIDDLE出现了一些问题,您对donutChart和其他图表也做了更改,但仍然是相同的问题。在对代码进行了一些更正之后,我能够在您的帮助下解决这个问题。谢谢:)
var donutChart;
var donutChartData;
function drawChart() {
//Donut chart example
nv.addGraph(function() {
donutChart = nv.models.pieChart()
  .x(function(d) { return d.label })
  .y(function(d) { return +d.value })
  .showLabels(true)     //Display pie labels
  .labelThreshold(.05)  //Configure the minimum slice size for labels to show up
  .labelType("percent") //Configure what type of data to show in the label. Can be "key", "value" or "percent"
  .donut(true)          //Turn on Donut mode. Makes pie chart look tasty!
  .donutRatio(0.35);  //Configure how big you want the donut hole size to be.     

  d3.select("#Chart4 svg")
    .datum(donutChartData)
    //.transition().duration(350)
    .call(donutChart); /// ERROR OCCURRED ON THIS LINE

 return donutChart;
});
}


socket.on("donutChartData",function(dChartData){
    donutChartData=dChartData; 
    console.log("Donut Chart: ",dChartData[0].label,dChartData[0].value);
    drawChart();
});