var chart_data = [ {% for row in chart_data %} [{{ row.0 }}, {{ row.1 }}], {% endfor %} ];
$ pip install django-google-charts # $ easy_install django-google-charts
'googlecharts'
to INSTALLED_APPS
. {% load googlecharts %} {% googlecharts %} {% data " " %} {# () #} {% col "" "" %}...{% endcol %} {# #} {% col "" "" %}...{% endcol %} {# #} ... {% enddata %} {% options " " %} {# () #} ... {% endoptions %} {% graph "#id" "" "" %} {# #} {% endgooglecharts %}
{% googlecharts %}...{% endgooglecharts %}
{% data "" %}...{% enddata %}
"default"
).{% col "" "" %}...{% endcol %}
'string' 'number' 'boolean' 'date' 'datetime' 'timeofday'
val
is passed inside the tag, in this place it can be formatted: {% col "string" "Date" %}'{{ val|date:"M j" }}'{% endcol %} {# , #} {% col "number" %}{{ val|floatformat:2 }}{% endcol %}
{% data %}
block: [['foo', 32], ['bar', 64], ['baz', 96]]
{% col %}
, according to the number of elements in the input line. The first will get 'foo', 'bar' and 'baz' to the input; the second, respectively, 32, 64 and 96. The implementation (the simplest) may look like this: {% col "string" "Name" %}"{{ val }}"{% endcol %} {% col "number" "Value" %}{{ val }}{% endcol %}
{% options "" %}...{% endoptions %}
{% options %} kind: "LineChart", options: { width: 300, height: 240 // ... } {% endoptions %}
{% graph "id_" "" "" %}
"default" "default"
. class Payment(models.Model): amount = models.DecimalField(max_digits=11, decimal_places=4) datetime = models.DateTimeField()
from qsstats import QuerySetStats def view_func(request): start_date = ... end_date = ... queryset = Payment.objects.all() # ... qsstats = QuerySetStats(queryset, date_field='datetime', aggregate=Count('id')) # ... values = qsstats.time_series(start_date, end_date, interval='days') return render_to_response('template.html', {'values': values})
time_series
method returns data in the following form: [[date, value], [date, value], ...]
{% load googlecharts %} <div id="count_graph"></div> {% googlecharts %} {% data values "count" %} {% col "string" "Date" %}"{{ val|date:"M j" }}"{% endcol %} {% col "number" "# of payments" %}{{ val }}{% endcol %} {% enddata %} {% options %} kind: "LineChart", options: { backgroundColor: "#f9f9f9", colors: ["#09f"], gridlineColor: "#ddd", legend: "none", vAxis: {minValue: 0}, chartArea: {left: 40, top: 20, width: 240, height: 180}, width: 300, height: 240 } {% endoptions %} {% graph "count_graph" "count" %} {# #} {% endgooglecharts %}
# summary = qsstats.time_series(start_date, end_date, interval='days', aggregate=Sum('amount')) return render_to_response('template.html', {'values': values, 'summary': summary})
<div id="count_sum"></div> ... {% data summary "sum" %} {% col "string" "Date" %}"{{ val|date:"M j" }}"{% endcol %} {% col "number" "Paid amount, USD" %}{{ val|floatformat:2 }}{% endcol %} {% enddata %} ... {% graph "count_sum" "sum" %} {# , #}
$ python manage.py syncdb --noinput # sqlite /tmp $ python manage.py populatedb # $ python manage.py runserver
{% googlecharts %}
tag per page;Source: https://habr.com/ru/post/126704/
All Articles