I am trying to display two elements from my array one as the label ie "name of certain type of crop" and the other is the data itself i.e "quantity of the crop", my problem is that my chart if same crop appears more than ones it displayed separately with its quantity Here is the resulting image
on my app.js(express server)
app.get('/crop', function (req, res) {
return new Promise(function(resolve, reject){
db.serialize(function(){
db.all('SELECT * FROM harvests', function(err, rows){
if (!err) {
resolve(rows)
}else{
reject(err)
}
})
})
}).then(function(rows){
res.json(rows);
});
});
On the Html page
<canvas id="chart" ></canvas>
on the script
$(document).ready(function(){
$.ajax({
url: 'http://localhost:3000/crop',
type: 'get',
dataType: 'json'
}).then(function(rows){
let labels = rows.map(function(e) {
return e.keyCrop;
});
let data = rows.map(function(e) {
return e.quantity;
});
let ctx = document.getElementById('chart').getContext('2d');
let myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'What Farmers are Growing',
data: data,
backgroundColor:
'rgba(255, 99, 132, 0.2)',
borderColor:
'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
}
});
});
})
I would expect a case where if the crop is same it adds their total quantity and display on one name (label)