Hey, I implemented a line chart and this is my code:

View:

def render_chart(request):
    months =
['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    month_values = [0,0,0,0,0,0,0,0,0,0,0,0]
    list_of_items = MyModel.objects.all()
    for item in list_of_items:
        if item.creation_date.strftime("%b") ==
months[item.creation_date.month - 1]:
            month_values[item.creation_date.month - 1] =
month_values[item.creation_date.month - 1] + 1

    d = {'user':request.user, 'months':months, 'month_values':month_values}
    return render_to_response('chart.html', d)

And this is the template:

script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Month');
        data.addColumn('number', 'bookings');
        data.addRows(12);
        {% for month in months %}

        data.setValue({{forloop.counter0}}, 0, '{{month}}');
        {% for value in month_values %}
        data.setValue({{forloop.counter0}}, 1, {{value}});
            {%endfor%}
        {%endfor%}
        var chart = new
google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 760, height: 380, title: 'Stats'});
      }
</script

I'm a Django/Python beginner, but the above works for me and it plots the
number of items per month, where the Y-axis is the number of items and
X-axis is the month names.

I hope that helps!

On 8 November 2011 16:20, skysb...@gmail.com <skysb...@gmail.com> wrote:

>  On 2011年11月07日 21:26, Andre Terra wrote:
>
> Pass serialized json as a template variable?
>
> yes of course, pupulate data from django is not special, it's a normal
> process just like other language. we can output json to the chart
>
>
> That's what I'm doing atm. But JKM has built an app to help reduce the
> need to write javascript [1]. I haven't used it myself, but it looks
> interesting and promising.
>
>
> Cheers,
> AT
>
> [1] https://github.com/jacobian/django-googlecharts
>
>
> On Mon, Nov 7, 2011 at 8:16 AM, kenneth gonsalves 
> <law...@thenilgiris.com>wrote:
>
>> On Mon, 2011-11-07 at 04:28 -0500, Joey Espinosa wrote:
>> > Here's the Quick Start page: http://goo.gl/g5I7X
>>
>>  that is easy - how does one populate with data from django?
>>  --
>> regards
>> Kenneth Gonsalves
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-users@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to