admin管理员组文章数量:1026416
I am trying to show no data message when chart has no data. Is it correct?
var ctx = document.getElementById('mycanvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["aa", "bb", "cc", "dd"],
datasets: [{
label: "My First dataset",
backgroundColor: ['red', 'blue', 'green', 'yello'],
data: data.length ? data: [{ label: "No Data", value: 100 }],
}]
},
options: {
legend: {
position: 'right',
labels: {
boxWidth: 12
}
},
tooltips: { bodyFontSize: 12 }
}
});
I am not getting No data message when data length is 0.
I am trying to show no data message when chart has no data. Is it correct?
var ctx = document.getElementById('mycanvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["aa", "bb", "cc", "dd"],
datasets: [{
label: "My First dataset",
backgroundColor: ['red', 'blue', 'green', 'yello'],
data: data.length ? data: [{ label: "No Data", value: 100 }],
}]
},
options: {
legend: {
position: 'right',
labels: {
boxWidth: 12
}
},
tooltips: { bodyFontSize: 12 }
}
});
I am not getting No data message when data length is 0.
Share Improve this question edited Dec 11, 2017 at 12:01 Lizzy 2,1833 gold badges21 silver badges33 bronze badges asked Dec 11, 2017 at 9:58 krishkrish 1,1176 gold badges26 silver badges54 bronze badges 3- Your ternary seems to be false – Zooly Commented Dec 11, 2017 at 10:01
-
return
data.length
and check the result, if it return0
trydata.length <= 0
but i think this way goes nothing.. – Pedram Commented Dec 11, 2017 at 10:01 -
What I can advice to you is manage it in the view. Don't display chart at all if there isn't data.
<canvas id="mycanvas" ng-if="data.length > 0"></canvas>
&<span ng-if="data.length == 0">No data</span>
– Zooly Commented Dec 11, 2017 at 10:03
1 Answer
Reset to default 3Easiest is to use conditional rendering.
Assuming you're using AngularJS (you referenced tag), you can use ngIf
directive. If data array is empty, then don't display chart.
<canvas id="myChart" ng-if="data != 0"></canvas>
<span ng-if="data == 0">No data</span>
Else, you can solve it in your script, by checking before or after draw the data length. I remend you this snippet.
Other solution following your wish could be to adapt drawn data to received data.
if($scope.requestedLeaveTypeCount.length > 0){
data = {
labels: ["EL", "AL", "PL", "CL"],
datasets: [{
label: "My First dataset",
backgroundColor: ['#F0CB8C', '#A9D5D4', '#E8A3D7', '#CFA3FD'],
data: $scope.requestedLeaveTypeCount,
}]
}
} else {
data = {
labels: ["No data"],
datasets: [{
labels:'No data',
backgroundColor: ['#D3D3D3'],
data: [100]
}]
}
}
https://plnkr.co/edit/QXljAeeM4Ul3Y47EPcbq?p=preview
I am trying to show no data message when chart has no data. Is it correct?
var ctx = document.getElementById('mycanvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["aa", "bb", "cc", "dd"],
datasets: [{
label: "My First dataset",
backgroundColor: ['red', 'blue', 'green', 'yello'],
data: data.length ? data: [{ label: "No Data", value: 100 }],
}]
},
options: {
legend: {
position: 'right',
labels: {
boxWidth: 12
}
},
tooltips: { bodyFontSize: 12 }
}
});
I am not getting No data message when data length is 0.
I am trying to show no data message when chart has no data. Is it correct?
var ctx = document.getElementById('mycanvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ["aa", "bb", "cc", "dd"],
datasets: [{
label: "My First dataset",
backgroundColor: ['red', 'blue', 'green', 'yello'],
data: data.length ? data: [{ label: "No Data", value: 100 }],
}]
},
options: {
legend: {
position: 'right',
labels: {
boxWidth: 12
}
},
tooltips: { bodyFontSize: 12 }
}
});
I am not getting No data message when data length is 0.
Share Improve this question edited Dec 11, 2017 at 12:01 Lizzy 2,1833 gold badges21 silver badges33 bronze badges asked Dec 11, 2017 at 9:58 krishkrish 1,1176 gold badges26 silver badges54 bronze badges 3- Your ternary seems to be false – Zooly Commented Dec 11, 2017 at 10:01
-
return
data.length
and check the result, if it return0
trydata.length <= 0
but i think this way goes nothing.. – Pedram Commented Dec 11, 2017 at 10:01 -
What I can advice to you is manage it in the view. Don't display chart at all if there isn't data.
<canvas id="mycanvas" ng-if="data.length > 0"></canvas>
&<span ng-if="data.length == 0">No data</span>
– Zooly Commented Dec 11, 2017 at 10:03
1 Answer
Reset to default 3Easiest is to use conditional rendering.
Assuming you're using AngularJS (you referenced tag), you can use ngIf
directive. If data array is empty, then don't display chart.
<canvas id="myChart" ng-if="data != 0"></canvas>
<span ng-if="data == 0">No data</span>
Else, you can solve it in your script, by checking before or after draw the data length. I remend you this snippet.
Other solution following your wish could be to adapt drawn data to received data.
if($scope.requestedLeaveTypeCount.length > 0){
data = {
labels: ["EL", "AL", "PL", "CL"],
datasets: [{
label: "My First dataset",
backgroundColor: ['#F0CB8C', '#A9D5D4', '#E8A3D7', '#CFA3FD'],
data: $scope.requestedLeaveTypeCount,
}]
}
} else {
data = {
labels: ["No data"],
datasets: [{
labels:'No data',
backgroundColor: ['#D3D3D3'],
data: [100]
}]
}
}
https://plnkr.co/edit/QXljAeeM4Ul3Y47EPcbq?p=preview
本文标签: javascriptShow No data message when chart has no dataStack Overflow
版权声明:本文标题:javascript - Show No data message when chart has no data - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745639996a2160687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论