|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
from chartkick.django import PieChart
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
|
from django.db.models import Count
|
|
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
@ -51,7 +52,39 @@ def poll_results(
|
|
|
|
|
).annotate(
|
|
|
|
|
count=Count("option")
|
|
|
|
|
)
|
|
|
|
|
context["results"] = results
|
|
|
|
|
|
|
|
|
|
colors = [
|
|
|
|
|
"#dd966b",
|
|
|
|
|
"#a1b0f7",
|
|
|
|
|
"#ffd83d",
|
|
|
|
|
"#8ac3a3",
|
|
|
|
|
"#dec2cb",
|
|
|
|
|
]
|
|
|
|
|
legend = []
|
|
|
|
|
index = 0
|
|
|
|
|
for vote in results:
|
|
|
|
|
legend.append(
|
|
|
|
|
{
|
|
|
|
|
"color": colors[index % len(colors)],
|
|
|
|
|
"option": vote.option.text,
|
|
|
|
|
"count": vote.count,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
|
|
chart_data = {
|
|
|
|
|
vote.option.text: vote.count
|
|
|
|
|
for vote in results
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context["chart"] = PieChart(
|
|
|
|
|
data=chart_data,
|
|
|
|
|
width="384px",
|
|
|
|
|
height="384px",
|
|
|
|
|
legend=False,
|
|
|
|
|
colors=colors,
|
|
|
|
|
)
|
|
|
|
|
context["legend"] = legend
|
|
|
|
|
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
|