def role_report(request): print("ROLE") queryset = Employee.objects.all().values('role__name').annotate( total=Count('role')).order_by('total') data_source = ModelDataSource(queryset, fields=['role__name', 'total']) chart = gchart.PieChart(data_source) return render_to_response('account/graph.html', {"chart": chart})
def draw_chart1(agendas): data = [] options = { 'title': "Agenda Chart", 'colors': [ "#109618", "#660066", "#ff9900", "#3366cc", "#0099c6", "#ff6600", "#dc3912" ] } data.append(['status', 'No of task']) # for status, temp_dict in post_dict.items(): # data.append([status, len(temp_dict)]) CompletedStatus = agendas.filter(status="Completed").count() # if CompletedStatus !=0 : data.append(["Completed", CompletedStatus]) OngoingLongTermStatus = agendas.filter(status="Ongoing-Long-Term").count() # if OngoingLongTermStatus !=0 : data.append(["Ongoing-Long-Term", OngoingLongTermStatus]) OngoingShortTermStatus = agendas.filter( status="Ongoing-Short-Term").count() # if OngoingShortTermStatus !=0 : data.append(["Ongoing-Short-Term", OngoingShortTermStatus]) NotEvaluatedStatus = agendas.filter(status="Not-Evaluated").count() # if NotEvaluatedStatus !=0 : data.append(["Not-Evaluated", NotEvaluatedStatus]) NotEvaluatedSubjectiveStatus = agendas.filter( status="Not-Evaluated-Subjective").count() # if NotEvaluatedSubjectiveStatus !=0 : data.append(["Not-Evaluated-Subjective", NotEvaluatedSubjectiveStatus]) NotStartedStatus = agendas.filter(status="Not-Started").count() # if NotStartedStatus !=0 : data.append(["Not-Started", NotStartedStatus]) BrokenStatus = agendas.filter(status="Broken").count() # if BrokenStatus !=0 : data.append(["Broken", BrokenStatus]) # options = {'title': 'My Average Day', 'width': 550, 'height': 400} # var options = {'title':'My Average Day', 'width':550, 'height':400}; print(data) # DataSource object data_source = SimpleDataSource(data=data) # Chart object chart = gchart.PieChart(data_source, options=options) sum = 0 for i in range(1, 8): sum = int(data[i][1]) + int(sum) for i in range(1, 8): if sum != 0: data[i][1] = (int(data[i][1]) / int(sum)) * 100 # del data[0] print(data) return {'chart': chart, 'data': data}
def getGeneralInfoVillages(request): template = './generalInfo.html' village = request.GET["v"] context_dict = getCommonVillageData(village) px = AfgLndcrva.objects.all().filter(vuid=village).values('agg_simplified_description').annotate(totalpop=Sum('area_population'), totalarea=Sum('area_sqm')).values('agg_simplified_description','totalpop', 'totalarea') data1 = [] data2 = [] data1.append(['agg_simplified_description','area_population']) data2.append(['agg_simplified_description','area_sqm']) for i in px: data1.append([i['agg_simplified_description'],round(i['totalpop'] or 0,0)]) data2.append([i['agg_simplified_description'],round(i['totalarea']/1000000,1)]) context_dict['landcover_pop_chart'] = gchart.PieChart(SimpleDataSource(data=data1), html_id="pie_chart1", options={'title': _("# of Population"), 'width': 225,'height': 225, 'pieSliceText': _('percentage'),'legend': {'position': 'top', 'maxLines':3}}) context_dict['landcover_area_chart'] = gchart.PieChart(SimpleDataSource(data=data2), html_id="pie_chart2", options={'title': _("# of Area (KM2)"), 'width': 225,'height': 225, 'pieSliceText': _('percentage'),'legend': {'position': 'top', 'maxLines':3}}) context_dict['longitude'] = context_dict['position'].x context_dict['latitude'] = context_dict['position'].y context_dict.pop('position') return render_to_response(template, RequestContext(request, context_dict))
def summary(request): allsurveys = Survey.objects.all() graphs = [] for survey in allsurveys: allquestions = Question.objects.all().filter(survey_id=survey.id) for question in allquestions: choices = Choice.objects.filter(question = question.id) data_source = ModelDataSource(choices, fields=['choice_text', 'votes']) chart = gchart.PieChart(data_source, options={'is3D': True, 'pieSliceText': 'value', 'title': question.question_text}) graphs.append((chart, survey.survey_name)) return render(request, 'app/summary.html', {'graphs': graphs})
def results(request): context = {} election = models.Election.objects.get(status="active") positions = models.Position.objects.filter(election=election) for position in positions: data = [['Candidates','Count']] candidates = models.Candidate.objects.filter(position=position) for candidate in candidates: count = models.VoteRecord.objects.filter(position=position,candidate=candidate).count() data.append([candidate.full_name, count]) data_source = SimpleDataSource(data=data) chart = gchart.PieChart(data_source, options={'title': position.name}) context['%s' % position.name] = chart return render(request, 'results.html', {'context': context, 'election': election.type.upper()})
def detail(request, question_id): #If the item being passed is a survey, then get the #fqid try: survey = Survey.objects.get(pk=question_id) fqid = survey.first_qid.id #Otherwise, its a question except Survey.DoesNotExist: fqid=question_id question = get_object_or_404(Question, pk=fqid) if request.user.is_authenticated: # Example graph that works, but doesn't really make sense yet queryset = Choice.objects.filter(question=fqid) data_source = ModelDataSource(queryset, fields=[ 'choice_text', 'votes']) chart = gchart.PieChart(data_source, options={'is3D': True, 'pieSliceText': 'value'}) else: chart="" return render(request, 'app/detail.html', {'question': question, 'chart': chart } )
def gchart_demo(request): create_demo_accounts() queryset = Account.objects.all() data_source = ModelDataSource(queryset, fields=['year', 'sales']) line_chart = gchart.LineChart(data_source, options={'title': "Sales Growth"}) column_chart = gchart.ColumnChart(SimpleDataSource(data=data), options={'title': "Sales vs Expense"}) bar_chart = gchart.BarChart(data_source, options={'title': "Expense Growth"}) candlestick_chart = gchart.CandlestickChart( SimpleDataSource(data=candlestick_data)) pie_chart = gchart.PieChart(data_source) custom_chart = CustomGchart(data_source) context = { "line_chart": line_chart, "column_chart": column_chart, 'bar_chart': bar_chart, 'candlestick_chart': candlestick_chart, 'pie_chart': pie_chart, "custom_chart": custom_chart } return render(request, 'demo/gchart.html', context)
def getDemographicInfo(request): template = './demographic.html' village = request.GET["v"] data = getDemographicInfoCommon(village) context_dict = data['context'] i = data['source']['rowsgenderpercent'] # rowsgenderpercent = AfgPpltDemographics.objects.all().filter(vuidnear=village).values() # i = rowsgenderpercent[0] data3 = [data['by_gender']['columntitles']]+data['by_gender']['child'] # data3 = [] # data3.append(['Gender','Percent']) # data3.append(['Male', i['vuid_male_perc']]) # data3.append(['Female', i['vuid_female_perc']]) data4 = [data['by_age_gender']['columntitles']]+data['by_age_gender']['child'] # data4 = [] # data4.append(['Age','Male','Female']) data4options={'title': _("Gender Ratio by Age Group (in %)"), 'width': 510, 'isStacked': True, 'hAxis': {'format': ';','title': _('in percent of total population')}, 'vAxis': {'title': _('age group in years'),'direction': -1}, 'legend': {'position': 'top', 'maxLines':3}} if context_dict['vuid_population'] > 200: # data4.append(['0-4', -i['m_perc_yrs_0_4'], i['f_perc_yrs_0_4']]) # data4.append(['5-9', -i['m_perc_yrs_5_9'], i['f_perc_yrs_5_9']]) # data4.append(['10-14', -i['m_perc_yrs_10_14'], i['f_perc_yrs_10_14']]) # data4.append(['15-19', -i['m_perc_yrs_15_19'], i['f_perc_yrs_15_19']]) # data4.append(['20-24', -i['m_perc_yrs_20_24'], i['f_perc_yrs_20_24']]) # data4.append(['25-29', -i['m_perc_yrs_25_29'], i['f_perc_yrs_25_29']]) # data4.append(['30-34', -i['m_perc_yrs_30_34'], i['f_perc_yrs_30_34']]) # data4.append(['35-39', -i['m_perc_yrs_35_39'], i['f_perc_yrs_35_39']]) # data4.append(['40-44', -i['m_perc_yrs_40_44'], i['f_perc_yrs_40_44']]) # data4.append(['45-49', -i['m_perc_yrs_45_49'], i['f_perc_yrs_45_49']]) # data4.append(['50-54', -i['m_perc_yrs_50_54'], i['f_perc_yrs_50_54']]) # data4.append(['55-59', -i['m_perc_yrs_55_59'], i['f_perc_yrs_55_59']]) # data4.append(['60-64', -i['m_perc_yrs_60_64'], i['f_perc_yrs_60_64']]) # data4.append(['65-69', -i['m_perc_yrs_65_69'], i['f_perc_yrs_65_69']]) # data4.append(['70-74', -i['m_perc_yrs_70_74'], i['f_perc_yrs_70_74']]) # data4.append(['75-79', -i['m_perc_yrs_75_79'], i['f_perc_yrs_75_79']]) # data4.append(['80+', -i['m_perc_yrs_80pls'], i['f_perc_yrs_80pls']]) data4options['height'] = 450 data4options['vAxis']['textStyle'] = {'fontSize': 11} # else: # data4.append(['0-9', -i['m_perc_yrs_0_4'] -i['m_perc_yrs_5_9'], i['f_perc_yrs_0_4'] +i['f_perc_yrs_5_9']]) # data4.append(['10-19', -i['m_perc_yrs_10_14']-i['m_perc_yrs_15_19'], i['f_perc_yrs_10_14']+i['f_perc_yrs_15_19']]) # data4.append(['20-29', -i['m_perc_yrs_20_24']-i['m_perc_yrs_25_29'], i['f_perc_yrs_20_24']+i['f_perc_yrs_25_29']]) # data4.append(['30-39', -i['m_perc_yrs_30_34']-i['m_perc_yrs_35_39'], i['f_perc_yrs_30_34']+i['f_perc_yrs_35_39']]) # data4.append(['40-49', -i['m_perc_yrs_40_44']-i['m_perc_yrs_45_49'], i['f_perc_yrs_40_44']+i['f_perc_yrs_45_49']]) # data4.append(['50-59', -i['m_perc_yrs_50_54']-i['m_perc_yrs_55_59'], i['f_perc_yrs_50_54']+i['f_perc_yrs_55_59']]) # data4.append(['60-69', -i['m_perc_yrs_60_64']-i['m_perc_yrs_65_69'], i['f_perc_yrs_60_64']+i['f_perc_yrs_65_69']]) # data4.append(['70-79', -i['m_perc_yrs_70_74']-i['m_perc_yrs_75_79'], i['f_perc_yrs_70_74']+i['f_perc_yrs_75_79']]) # data4.append(['80+', -i['m_perc_yrs_80pls'], i['f_perc_yrs_80pls']]) for i, v in enumerate(data3): if i > 0: v[1] = {'v':v[1], 'f':str(abs(round(v[1] or 0, 1)))+' %'} for i, v in enumerate(data4): if i > 0: v[1] = {'v':v[1], 'f':str(abs(round(v[1] or 0, 1)))+' %'} v[2] = {'v':v[2], 'f':str(abs(round(v[2] or 0, 1)))+' %'} context_dict['gender_ratio_chart'] = gchart.PieChart(SimpleDataSource(data=data3), html_id="pie_chart3", width=510, options={'title': _("Gender Ratio (in %)"), 'width': 510, 'pieSliceText': _('value'),'pieHole':0.5,'legend': {'position': 'top', 'maxLines':3},'tooltip': {'text': 'value'}}) context_dict['gender_ratio_by_age_chart'] = gchart.BarChart(SimpleDataSource(data=data4), html_id="bar_chart1", width=510, options=data4options) context_dict.pop('position') return render_to_response(template, RequestContext(request, context_dict))
def sales(request): print("Sales Report") sellerEmailCookies = request.COOKIES['usmSouvenirShopSeller'] seller = list( Seller.objects.filter(email=sellerEmailCookies).values_list('pk', flat=True)) item_list = list( Item.objects.filter(seller=seller[0]).values_list('pk', flat=True)) order_item_list = [ list(OrderItem.objects.filter(item=item).values_list('pk', flat=True)) for item in item_list ] # may without payment order_item_list = [num for elem in order_item_list for num in elem] # may contain other seller item order_list = [ list( Order.objects.filter(items__isnull=False, payment__isnull=False, items=items).values_list('pk', 'items')) for items in order_item_list ] order_list = [num for elem in order_list for num in elem] order_items_list = [] order_items_order_id = [] # order_list for i in range(len(order_list)): order_pk = order_list[i][0] order_items = order_list[i][1] if order_items in order_item_list: # order item with payment and seller order_items_list.append( list(OrderItem.objects.filter(pk=order_items))) # order id with payment and seller order_items_order_id.append(order_pk) month_sales_list = [] month_sales_list.append(['Month', 'Amount(RM)']) category_sales_list = [] category_sales_list.append(['Category', 'Sales']) monthdict = { 'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6, 'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12 } for i in range(len(order_items_list)): order_pk = order_items_order_id[i] order_item_pk = order_items_list[i][0].pk quantity = order_items_list[i][0].quantity ordersales = order_items_list[i][0].get_total_item_price() category = order_items_list[i][0].item.category.name month = Order.objects.filter( items__isnull=False, payment__isnull=False, pk=order_pk, items=order_item_pk).order_by('ordered_date').values_list( 'ordered_date')[0][0].strftime('%B') month_sales = [monthdict[month], ordersales] category_sales = [category, quantity] month_sales_list.append(month_sales) category_sales_list.append(category_sales) month_data_list = [] temp_moth_list = [] for i in range(len(month_sales_list)): contain = False if len(month_data_list) == 0: month_data_list.append(month_sales_list[i]) else: for j in range(len(temp_moth_list)): if month_sales_list[i][0] == temp_moth_list[j][0]: temp_moth_list[j][ 1] = temp_moth_list[j][1] + month_sales_list[i][1] contain = True if contain == False: temp_moth_list.append(month_sales_list[i]) temp_moth_list.sort() monthkey = list(monthdict.keys()) for x in temp_moth_list: newx = [monthkey[x[0] - 1], x[1]] month_data_list.append(newx) category_data_list = [] for i in range(len(category_sales_list)): contain = False if len(category_data_list) == 0: category_data_list.append(category_sales_list[i]) else: for j in range(len(category_data_list)): if category_sales_list[i][0] == category_data_list[j][0]: category_data_list[j][1] = category_data_list[j][ 1] + category_sales_list[i][1] contain = True if contain == False: category_data_list.append(category_sales_list[i]) # Chart object chart = gchart.BarChart(SimpleDataSource(data=month_data_list), options={'title': 'Sales 2020 by Months'}) pie_chart = gchart.PieChart( SimpleDataSource(data=category_data_list), options={'title': 'Sales 2020 by Category (Sale Units)'}) context = {'chart': chart, 'pie_chart': pie_chart} seller = Seller.objects.filter(email=sellerEmailCookies) context['sellername'] = seller[0].name return render(request, 'salesreport.html', context)