Giter Site home page Giter Site logo

Comments (4)

AuxProc avatar AuxProc commented on August 26, 2024

I'm not sure I understand what your current issue is. Could you provide some examples of the data youre giving to django-graphos and what is being returned?

The steps I take to pass datetime to django-graphos using Google looks something like this in my views:

queryset_black_levels = combined_black_levels.order_by('toner_date')[:180]
        if len(queryset_black_levels) > 0:
            haxis_labels = int(len(queryset_black_levels) / 4)
            for item in queryset_black_levels:
                item.toner_date = datetime.date(item.toner_date)
                item.toner_date = item.toner_date.isoformat()

The key is to convert to isoformat. You may also find my chart settings helpful:

chart_black = gchart.LineChart(data_source_black_levels, options={'title': 'Black Levels', 'chartArea': {'width': '75%', 'height': '60%'}, 'legend': { 'position': 'none' }, 'vAxis': { 'format': 'percent', 'viewWindowMode': 'explicit', 'viewWindow': { 'max': 1, 'min': 0}}, 'colors': ['#000000'], 'hAxis': {'format': 'M/d/yy', 'gridlines': {'count': 5}, 'showTextEvery': haxis_labels}}, height=150, width='100%')```

from django-graphos.

akshar-raaj avatar akshar-raaj commented on August 26, 2024

@recordnotfound Ultimately we need to send this data to frontend and so need to serialize this. So you need to convert your data to something which could be serialized.

You need to override get_data() of existing DataSource and convert datetime field into something which could be serialized.

Assuming you are using a Python list as data, then you need to do:

class MySimpleDataSource(SimpleDataSource):
    def get_data(self):
        data = super(MySimpleDataSource, self).get_data()
        header = data[0]
        data_without_header = data[1:]
        for row in data_without_header:
            # Assuming first column contains datetime
            row[0] = row[0].year
        data_without_header.insert(0, header)
        return data_without_header

And data has

d1 = datetime(2015, 7, 8, 1, 1)
d2 = datetime(2016, 7, 8, 3, 1)

data1 = [
         ['Year', 'Sales', 'Expenses', 'Items Sold', 'Net Profit'],
         [d1, 1000, 400, 100, 600],
         [d2, 1170, 460, 120, 310],
 ]

chart = flot.LineChart(MySimpleDataSource(data=data1))

If you are planning to use queryset with ModelDataSource, then you would create following class

class MyModelDataSource(ModelDataSource):
    def get_data(self):
        data = super(MyModelDataSource, self).get_data()
        header = data[0]
        data_without_header = data[1:]
        for row in data_without_header:
            # Assuming second column contains datetime
            row[1] = row[1].year
        data_without_header.insert(0, header)
        return data_without_header

And you would use this class like:

queryset = Account.objects.all()
chart = flot.LineChart(MyModelDataSource(queryset=queryset, fields=['sales', 'datetime_field','expenses']))

from django-graphos.

akshar-raaj avatar akshar-raaj commented on August 26, 2024

@recordnotfound It should be doable with the code snippets mentioned in last comment, so closing this ticket. Feel free to open this again if you need further help.

from django-graphos.

ndunn219 avatar ndunn219 commented on August 26, 2024

In the workaround, @akshar-raaj gets the year, which is serializable, but @recordnotfound asked about a datetime field. I have the same issue using date values. I've tried to replace row[0] = row[0].year with row[0] = row[0].date(), but that returns dates as strings, which JavaScript doesn't recognize as dates. How would you return serializable dates or datetimes?

from django-graphos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.