Hi, I'm developing a Django application that creates graphics. And I've already implemented a line chart using GCharts insert the link description here but it only has one line, and in the example I I've seen how to create the same graph with several lines, but only with a fixed number. And for my case I need to create for n lines (where n is the number of Tags I have in the Database).
Here is the required classes Model:
class Tag(models.Model):
objects = GChartsManager()
idtag = models.IntegerField(db_column='idTAG', primary_key=True, editable=False, verbose_name = 'Id') # Field name made lowercase.
desvio = models.FloatField(db_column='DEVIATION', blank=True, null=True, verbose_name = 'Diversion') # Field name made lowercase.
tempo_max = models.IntegerField(db_column='TIME_MAX', blank=True, null=True, verbose_name = 'Max time') # Field name made lowercase.
conv_rate = models.IntegerField(db_column='CONV_RATE', blank=True, null=True, verbose_name = 'Convertion rate') # Field name made lowercase.
taginfo_idtaginfo1 = models.ForeignKey('Taginfo', db_column='tagInfo_idTAGINFO', verbose_name = 'Tag') # Field name made lowercase.
datasource_idestacao_meteo = models.ForeignKey(Datasource, db_column='datasource_idDATASOURCE', blank=True, null=True, verbose_name = 'Datasource') # Field name made lowercase.
def __unicode__(self):
return u'id = %s / id tagInfo => %s' % (self.idtag, self.taginfo_idtaginfo1)
class Meta:
managed = False
db_table = 'tag'
class Taginfo(models.Model):
objects = GChartsManager()
idtaginfo = models.IntegerField(db_column='idTAGINFO', primary_key=True, editable=False, verbose_name = 'Id') # Field name made lowercase.
nome = models.CharField(db_column='NAME', max_length=45, verbose_name = 'Name') # Field name made lowercase.
descricao = models.CharField(db_column='DESCRIPTION', max_length=255, blank=True, verbose_name = 'Details') # Field name made lowercase.
def __unicode__(self):
return u'name = %s / description = %s' % (self.nome, self.descricao)
class Meta:
managed = False
db_table = 'taginfo'
class Valores(models.Model):
objects = GChartsManager()
idvalores = models.IntegerField(db_column='idVALUES', primary_key=True, editable=False, verbose_name = 'Id') # Field name made lowercase.
valor = models.FloatField(db_column='VALUE', verbose_name = 'Value') # Field name made lowercase.
datahora = models.DateTimeField(db_column='DATETIME', verbose_name = 'Date time') # Field name made lowercase.
tag_idtag = models.ForeignKey(Tag, db_column='tag_idTAG', verbose_name = 'Tag') # Field name made lowercase.
def __unicode__(self):
return u'Tag = %s / DATETIME = %s / Value = %s' % (self.tag_idtag, self.datahora, self.valor)
class Meta:
managed = False
db_table = 'values'
And the view I made to generate the chart with a line:
def tag(request, tag_id = 1):
# if there is
if Tag.objects.filter(idtag=tag_id).count()>0 :
result = Tag.objects.get(idtag=tag_id)
hasInfo = False
tagInfo = None
#if there is information about this tag
if Taginfo.objects.filter(idtaginfo = result.taginfo_idtaginfo1.idtaginfo).count() > 0:
tagInfo = Taginfo.objects.get(idtaginfo = result.taginfo_idtaginfo1.idtaginfo)
hasInfo = True
#if there is values on this tag
if Valores.objects.filter(tag_idtag = tag_id).count>0:
aux = Valores.objects.filter(tag_idtag = tag_id)
#this verify is only to put the description on the graph
if hasInfo:
values = aux.order_by('-datahora').to_json(order=("datahora", "valor"),
labels={"valor": tagInfo.descricao})
else:
values = aux.order_by('-datahora').to_json(order=("datahora", "valor"),
labels={"valor": "Values"})
else:
values = None
return render_to_response('Tag/tag.html',
{'tag' : result, 'values' : values, 'tagInfo' : tagInfo}
)
else:
return render_to_response('Tag/tagDoesNotExist.html',
{'tag_id' : tag_id }
)
But how would you do to create a function that would return a line graph with n lines ??