Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Chart.js中将NaN值更改为0?_Javascript_Angularjs_Chart.js_Angular Chart - Fatal编程技术网

Javascript 如何在Chart.js中将NaN值更改为0?

Javascript 如何在Chart.js中将NaN值更改为0?,javascript,angularjs,chart.js,angular-chart,Javascript,Angularjs,Chart.js,Angular Chart,我有一张图表,其中我用AngularJS计算值。 当没有值时,Angular将其作为未定义抛出 当将这些值传递给图表时,图表不会显示它们(这很好,因为它与0相同)。但在工具提示中,悬停时,值显示为“非数字”(NaN) 如何使所有NaN值显示为0(零) 我不知道看我的代码是否真的有帮助,但这里是: $scope.Chart = { segmentShowStroke: false, responsive: true, maintainAspectRati

我有一张图表,其中我用AngularJS计算值。
当没有值时,Angular将其作为
未定义
抛出

当将这些值传递给图表时,图表不会显示它们(这很好,因为它与0相同)。但在工具提示中,悬停时,值显示为“非数字”(NaN)

如何使所有NaN值显示为0(零)

我不知道看我的代码是否真的有帮助,但这里是:

$scope.Chart = {
       segmentShowStroke: false,
       responsive: true,
       maintainAspectRatio: false,
       scales: {
           xAxes: [{
               stacked: true,
               ticks: {
                   autoSkip: false
               },
               gridLines: {
                   display: false
               }
           }],
           yAxes: [{
               stacked: true,
               ticks: {
                   beginAtZero: true
               },
               gridLines: {
                   display: true,
                   color: "rgb(150,150,150)"
               }
           }]
       },
       legend: {
           display: true,
           position: 'top',
           labels: {
               usePointStyle: true,
               fontColor: "white"
           }
       },
       plugins: {
           datalabels: {
               color: '#171d28',
               display: function(context) {
                   return context.dataset.data[context.dataIndex] !== 0;
               },
               anchor: 'center',
               align: 'center',
               clamp: true
           },
           deferred: {
               yOffset: '45%',
               delay: 200
           }
       }
   };
   $scope.Colors = [
       { backgroundColor: 'rgb(204,234,248)' },
       { backgroundColor: 'rgb(102,194,235)' },
       { backgroundColor: 'rgb(0,154,221)' },
       { backgroundColor: 'rgb(0,84,134)' }
   ];
   $scope.Series = ['1 - Poor', '2 - Average', '3 - Acceptable', '4 - Good'];
   $scope.Labels = ['Performance', 'Mobile Capability', 'Reporting'];
   $scope.Data = [
       [ ratingPerformance1, ratingMobileCapability1, ratingReporting1 ],
       [ ratingPerformance2, ratingMobileCapability2, ratingReporting2 ],
       [ ratingPerformance3, ratingMobileCapability3, ratingReporting3 ],
       [ ratingPerformance4, ratingMobileCapability4, ratingReporting4 ]
   ];

Chart.js提供了钩子,可供您自定义工具提示中的内容。它们用于舍入,但您可以快速修改它以显示
0
,而不是
NaN
。大致应该是这样的:

$scope.Chart={
...
选项:{
工具提示:{
回调:{
标签:函数(工具提示项、数据){
var label=data.datasets[tooltipItem.datasetIndex].label | |“”;
如果(标签){
标签+=':';
}
label+=isNaN(tooltipItem.yLabel)-“0”:tooltipItem.yLabel;
退货标签;
}
}
}
}
});
嘿,我和你犯了同样的错误,我试着在其中加入了很多条件 我的代码,但我失败后,我得到了我的最终结果。我得到了 成功

我的代码是这样的

  helper[key].NoOfReels += isNaN((parseFloat(o.NoOfReels)) == "" || parseFloat(o.NoOfReels));

isNaN(parseFloat(data[i].NoOfReels)) ? '0' : parseFloat(data[i].NoOfReels);

(最小的和相关的)代码在哪里?请拿着(你得到一个徽章!),四处看看,然后通读,我还特别推荐Jon Skeet的。有时我会问自己,如何从其他开发人员那里问这样的问题……至少他/她应该知道,查看源代码比任何图像都重要得多……@DenysSéguret当然,我添加了图表的代码@messerbill-工具提示处理程序包含在Chart.js库中。非常感谢!你刚刚用这个小代码救了我