I am working on creating a graph using ChartJs to visualize my expenses.
Below is a snippet of my view:
@login_required
def index(request):
truncate_month = connection.ops.date_trunc_sql('month', 'date_reg')
expense = Expense.objects.extra({'month': truncate_month}).values('month').annotate(Sum('total_amount'))
return render(request, 'home/index.html', {"expense": expense})
Within my template:
{{ expense }}
The data is displayed in the format shown below:
[{
'month': '2015-12-01',
'total_amount__sum': 900.0
}, {
'month': '2016-01-01',
'total_amount__sum': 19334.0
}]
I would like to know how to format this data to be compatible with chartjs.
Example structure for ChartJs:
labels: ["January", "February", "March", "April", "May", "June", "July"], //Here represent the months
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40] //Here corresponds to 'total_amount__sum'
},
...