How to display a graph with the sum of objects with Django / Python / SQlite?
In this case we have'internal_boxes '(y-axis) for' teams '(x-axis) that are rendering'internal_boxes' for each 'team' insert in the PCdastro model.
Example generated: link
How to show just the sum of 'internal_boxes' by 'team' in the graph instead of showing all the values entered in this model and having repeated by ex:
on the x-axis that represents' team 'repeat for each value of'internal_boxes'. 1 - > 5; 1 - > 22; 1 - > 15 .... that is, team 1 has 5 boxes, team 1 has 22 boxes, team 1 has 15 boxes ...
How to show only, team 1 - > 42 boxes; Team 2 - > 50 boxes; team 3 - > 30 boxes ....?
views.py
def grafico_pcadastro(request):
queryset = PCadastro.objects.all().order_by('equipe')
equipes = [obj.equipe for obj in queryset]
caixas_internos = [obj.caixas_interno for obj in queryset]
context = {
'equipes': json.dumps(equipes),
'caixas_internos': json.dumps(caixas_internos),
}
return render(request, 'pintura/graficos.html', context)
graficos.html
<canvas id="grafico_pcadastroChart" width="200" height="200"></canvas>
<script>
var ctx = document.getElementById("grafico_pcadastroChart");
var equipes = JSON.parse('{{ equipes|safe }}');
var caixas_internos = JSON.parse('{{ caixas_internos|safe }}');
var grafico_pcadastroChart = new Chart(ctx, {
type: 'bar',
data: {
labels: equipes,
datasets: [{
label: 'Caixas de Reboco',
data: caixas_internos,
backgroundColor: [
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)'
],
borderColor: [
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 1)',
'rgba(54, 162, 235, 1)'
],
borderWidth: 0.5
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
</script>
models.py
class PCadastro(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,)
equipe = models.CharField(max_length=2)
caixas_interno = models.CharField(max_length=2, blank=True)
def __str__(self):
return self.equipe