def proposal_countries_map(request, proposal_id, extension): filename = join(settings.MEDIA_DIRECTORY, 'img', 'trends', 'proposal', "%s-countries-map.%s" % (proposal_id, extension)) cache = get_content_cache(request, filename) if cache: if extension == "png": return HttpResponse(cache, mimetype="image/png") return HttpResponse(cache, mimetype="image/svg+xml") proposal = get_object_or_404(Proposal, id=proposal_id) countries = {} for country in Country.objects.order_by('code'): countries[country.code] = Score.objects.filter(proposal=proposal, representative__mep__countrymep__country=country).aggregate(average_score=Avg('value'))['average_score'] current_country = None out = "" for line in open(join(settings.STATIC_ROOT, "svg", "grey_europe_map.svg"), "r").readlines(): if ' {}' in line: get = re.match('.*([a-z][a-z]).*', line) if get and get.group(1).upper() in countries.keys(): current_country = get.group(1).upper() # HAHAHAHA blam those who can't write a human uzable xml lib for python if current_country: if countries[current_country]: line = re.sub("{}", "{fill:rgb(%s, %s, %s);}" % color(countries[current_country]), line) current_country = None out += line check_dir(filename) open(filename, "w").write(out) if extension == "png": png_filename = ".".join(filename.split(".")[:-1]) + ".png" os.system("convert %s %s" % (filename, png_filename)) return HttpResponse(open(png_filename).read(), mimetype="image/png") return HttpResponse(out, mimetype="image/svg+xml")
def group_proposal_score_heatmap(request, proposal_id): filename = join(settings.STATIC_ROOT, 'img', 'trends', 'group', get_language() + "-groups-%s-repartition-heatmap.png" % proposal_id) cache = get_content_cache(request, filename) if cache: return cache proposal = get_object_or_404(Proposal, id=proposal_id) countries= proposal.countries groups = proposal.groups if proposal.institution != "EU": return HttpResponse("") fig = pyplot.figure(figsize=(len(countries) / 2.8, len(groups) / 2.8)) ax = fig.add_subplot(111) ax.set_xlim(0, len(proposal.countries)) ax.set_ylim(0, len(groups)) biggest_group_of_a_country = 0 for country in countries: for group in groups: meps = MEP.objects.filter(score__proposal=proposal, groupmep__end__gte=proposal.date, groupmep__begin__lte=proposal.date, countrymep__begin__lte=proposal.date, countrymep__end__gte=proposal.date, countrymep__country=country, groupmep__group=group).distinct().count() if not meps: meps = MEP.objects.filter(score__proposal=proposal, countrymep__country=country, groupmep__group=group).distinct().count() if meps > biggest_group_of_a_country: biggest_group_of_a_country = meps biggest_group_of_a_country = float(biggest_group_of_a_country) a = 0.5 for country in countries: b = 0.5 for group in groups: meps = MEP.objects.filter(score__proposal=proposal, groupmep__end__gte=proposal.date, groupmep__begin__lte=proposal.date, countrymep__begin__lte=proposal.date, countrymep__end__gte=proposal.date, countrymep__country=country, groupmep__group=group).distinct().count() if not meps: meps = MEP.objects.filter(score__proposal=proposal, countrymep__country=country, groupmep__group=group).distinct().count() score = Score.objects.filter(proposal=proposal, representative__mep__groupmep__group=group, representative__mep__groupmep__begin__lte=proposal.date, representative__mep__groupmep__end__gte=proposal.date, representative__mep__countrymep__country=country, representative__mep__countrymep__begin__lte=proposal.date, representative__mep__countrymep__end__gte=proposal.date).aggregate(Avg('value'))['value__avg'] if not score: score = Score.objects.filter(proposal=proposal, representative__mep__groupmep__group=group, representative__mep__countrymep__country=country).aggregate(Avg('value'))['value__avg'] if score: ax.scatter(a, b, s=280*(meps/biggest_group_of_a_country), c=map(lambda x: x/255., color(score)), alpha=0.6) b += 1 a += 1 pyplot.title(ugettext("Heatmap of group/country on %(proposal)s") % {"proposal": proposal.short_name if proposal.short_name else proposal.title}) pyplot.xticks(map(lambda x: x + 0.5, range(len(countries))), map(lambda x: x.code, countries)) pyplot.yticks(map(lambda x: x + 0.5, range(len(groups))), map(lambda x: x.abbreviation, groups)) check_dir(filename) pyplot.savefig(filename, format="png", bbox_inches='tight') pyplot.figure(figsize=(8, 6)) pyplot.clf() return send_file(request, filename, content_type="image/png")
def color_tuple(self): return color(self.value)
def score_color(self): return "rgb(%s, %s, %s)" % color(self.total_score)
def color(self): red, green, _ = color(self.value) return "rgb(%d, %d, 0)" % (red, green)