I am using the below code to create a barchart in sharepoint. The code is working but doesn't return the right information. The field "Reply" can contain three values: Nill Reply, Partly answered and Completely Answered. I want to display how many items have Nill Reply, how many have Partly answered and how many Completely answered. So he should count each of the three values and create the bar accordingly. How to get this in my code?
<script type="text/javascript">
var barChart;
var barColors = [];
var xValues = [];
var yValues = [];
var myYear;
var today = new Date();
myYear = today.getFullYear();
var requestUri = "/_api/web/Lists/GetByTitle('MyList')/items?$orderby= nummer desc";
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + requestUri,
type: "GET",
async:false,
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
$.each(data.d.results, function (i, item){
var stat = item.MijnStatus;
var ltiov = item.LTIOV;
var reply = item.Reply;
var periode = item.Jaar;
if (periode == myYear && stat =="Completed") {
console.log(ltiov);
console.log(reply);
if (reply != null){
xValues.push(item.Reply);
}
if (ltiov != null){
yValues.push(item.LTIOV);
}
var randomColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
barColors.push(randomColor);
}
})
fnGenerateChart(xValues, yValues);
},
error: function () {
alert("Something went wrong");
}
});
function fnGenerateChart(xValues, yValues) {
var barChart = new Chart("barChart", {
type: "bar",
data: {
labels: xValues,
datasets: [{
data: yValues,
label: 'Reply on the question',
backgroundColor: barColors
}]
}
});
}
</script>