I want to get data (my output) from database and set it into chartjs by month. Like Month 5 (May) have 1 orders. I think they most be first mapped and setting into chart.js. Or i can make two arrays and set all information there in and then set it into the chartjs, but i dont know how.
delivered = await Order.findAll({ where: {status: 'DELIVERED',}, attributes: [
[Order.sequelize.fn('MONTH', Order.sequelize.col('created_at')), 'month'],
[Order.sequelize.fn('COUNT', Order.sequelize.col('id')), 'orders']
], group: [Order.sequelize.fn('MONTH', Order.sequelize.col('created_at'))]},
{where: {[Order.sequelize.fn('YEAR', Order.sequelize.col('created_at'))]: moment().format('YYYY')}})
.catch((err) => {
});
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
],
datasets: [
{
label: 'Number of Deliveries per month',
data: [],
borderColor: '#4dc9f6',
backgroundColor: '#f67019',
tension: 0.2,
},
],
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'Chart.js Line Chart',
},
},
},
},
});
My output:
order {
dataValues: { month: 5, orders: 1 },
_previousDataValues: { month: 5, orders: 1 },
uniqno: 1,
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
}