Gchart with Django

Asked

Viewed 100 times

0

Hi I am developing a Django application that creates graphics. And I have implemented a line chart using Gcharts insert link description here but it only has one line, and in the example 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 (n being the number of Tags I own in the Database).

Here is Model of the required classes:

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 graph 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 I create a function that would return me a line chart with n lines??

  • Nathan, why are you wearing db_column='nome_da_coluna'? Why define a primary_key if Django does it automatically? I particularly look at your code and get confused by these nomenclatures you use (eg taginfo_idtaginfo1).

  • 2

    Actually this project is a joint project. It’s a university Scientific Initiation project, me and another student work together. And the teacher wanted Django to use the DB that already existed, so I used command python manage.py inspextdb for Django to create models from existing DB. And in this DB was created by the other component, I had no participation with the nomenclature of the fields in DB.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.