0
How to show 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' values for each 'team' insertion in the Pcdastro model.
Generated example: https://drive.google.com/file/d/1BHptQcI4quZxTrh8RHHvn2wFmJPHO0QM/view
How to show only the sum of 'internal boxes' per 'team' on the chart instead of showing all values entered in this model and repeated for ex:
on the x-axis representing 'team' repeat for each 'inner box' value. 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.... ?
py views.
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)
Graphics.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>
py.models
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
would be able to display json content
equipes
?equipe
is a foreignkey in the modelPCadastro
?– JonatasCD
@Jonatascd
equipes
is represented by the columnequipe
ofclass PCadastro(models.Model):
.equipes
, but in the case,equipes = [obj.equipe for obj in queryset]
. models.py. class Pcadastro(models.Model): team = models.Charfield(max_length=2) internal boxes = models.Charfield(max_length=2, Blank=True) def str(self): Return self.team ie,equipe
is a Charfield on the modelPCadastro
– Douglas P