def get_graph(length=100, filter=Q()): end = datetime.now().date() start = end - timedelta(days=100) results = ReportMalnutrition.objects\ .filter(Q()).exclude(muac=None)\ .filter(entered_at__gt=start)\ .filter(entered_at__lte=end)\ .order_by("-entered_at")\ .values_list("muac", "entered_at") results1 = [ [ time.mktime(r[1].timetuple()) * 1000, r[0] ] for r in results ] results2 = [ [ time.mktime(r[1].timetuple()) * 1000, r[0]+10 ] for r in results ] muac_data = [{'data': results1, 'label': "Muac"}, {'data': results2, 'label': "Bigger Muac"}] g = FlotGraph() g.set_data(muac_data) g.set_xaxis_mode("time") g.set_key_position(0) g.set_display_title("Muac over time") g.set_width(400) g.set_height(200) g.set_title("muac") g.set_zoomable(1) g.set_key_position(1) g.set_xaxis_min_ticksize("[1, \"day\"]") start_string = ("(new Date(\"%s\")).getTime()" % start.strftime("%Y/%m/%d")) g.set_xaxis_min(start_string) end_string = ("(new Date(\"%s\")).getTime()" % end.strftime("%Y/%m/%d")) g.set_xaxis_max(end_string) g.set_time_format("%b %d") g.generate_javascript() results = { "start":'"%s"' % start.strftime("%Y/%m/%d"), "end":'"%s"' % end.strftime("%Y/%m/%d"), "results":results, "g":g} return results
def case_view(request, object_id): case = get_object_or_404(Case, id=object_id) nonhtml, tables = get(request, [ ["malnutrition", Q(case=case)], ["diagnosis", Q(case=case)], ["malaria", Q(case=case)], ["event", Q(content_type="case", object_id=object_id)], ]) if nonhtml: return nonhtml results = ReportMalnutrition.objects\ .filter(Q(case=case)).exclude(muac=None)\ .order_by("-entered_at")\ .values_list("muac", "entered_at") results1 = [ [ time.mktime(r[1].timetuple()) * 1000, r[0] ] for r in results ] muac_data = [{'data': results1, 'label': "Muac"}] g = FlotGraph() g.set_data(muac_data) g.set_xaxis_mode("time") g.set_key_position(0) g.set_display_title("Muac over time") g.set_width(400) g.set_height(200) g.set_title("muac") g.set_zoomable(1) g.set_key_position(1) g.set_xaxis_min_ticksize("[1, \"day\"]") g.set_time_format("%b %d") g.generate_javascript() context = { "app": app, "object": case, "malnutrition": tables[0], "diagnosis": tables[1], "malaria": tables[2], "event": tables[3], "graph": g, "raw_data": results1, } return as_html(request, "caseview.html", context)