В итоге получилось так.
Шаблон:
{% block extra_js %}
<script type="text/javascript" src="{% static "amcharts/amcharts.js" %}"></script>
<script type="text/javascript">
var chart;
AmCharts.ready(function () {
chart = new AmCharts.AmPieChart();
chart.balloonText = '[[title]]\n[[description]]';
chart.dataProvider = {{ reports.by_type|safe }};
chart.labelRadius = -30;
chart.labelText = '[[percents]]%';
chart.descriptionField = 'desc';
chart.outlineAlpha = 0.8;
chart.outlineColor = "#FFFFFF";
chart.outlineThickness = 2;
chart.titleField = "title";
chart.valueField = "count";
chart.write("pie_type");
});
</script>
{% endblock %}
{% block content %}
<div id="pie_type" style="width: 280px; height: 280px;"></div>
{% endblock %}
Подготовка данных для пайчарта:
def piechart_type():
titles = map(unicode, [
_('Office'),
_('Living'),
_('Retail'),
_('Entertainment'),
_('Unknown')])
titles_dict = dict(enumerate(titles, 1))
qs = models.Project.objects.won().values('ptype').order_by().annotate(count=Count('pk'))
qs_sorted = sorted([(x['ptype'], x['count']) for x in qs])
values = map(lambda x: x[1], qs_sorted)
desc = [unicode('%i %s' % (i, ungettext('project', 'projects', i))) for i in values]
result = [{'title': k, 'count': v, 'desc': d} for k, v, d in zip(titles, values, desc)]
return json.dumps(result)
Контекст в представлении:
context = dict(
reports={
'by_type': piechart_type(),
}
}