我的成果
我的代碼
代碼的部分其實不難毅戈,就是挺繁瑣的,東拼拼西湊湊沐悦。
貼一下我的views.py
吧:
from django.shortcuts import render
# 調(diào)用modules中的類
from final_45.models import ItemInfo
# 分頁工具 Paginator
from django.core.paginator import Paginator
cate_list = [
'北京二手美容/保健', '北京二手平板電腦', '北京二手文體/戶外/樂器',
'北京二手家電', '北京二手圖書/音像/軟件', '北京二手母嬰/兒童用品',
'北京二手設(shè)備', '北京二手數(shù)碼產(chǎn)品','北京二手手機', '北京二手辦公用品/設(shè)備',
'北京二手服裝/鞋帽/箱包', '北京二手臺式機/配件', '北京其他二手物品',
'北京二手筆記本', '北京二手家具'
]
# *----------------------發(fā)帖總量柱狀圖----------------------*
def every_cate_post(cate_list):
post_counts = []
for i in cate_list:
data = ItemInfo._get_collection().find({'cates': i}).count()
post_counts.append(data)
data = {
'name': '各個類別',
'type': 'column',
'data': post_counts
}
return data
series_ch1 = [every_cate_post(cate_list)]
# *----------------------一天內(nèi)交易物品分布:餅狀圖----------------------*
# 按種類分布
def oneday_cates():
pipeline = [
{'$match':{'time':1}},
{'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}},
{'$sort':{'counts':-1}}
]
for i in ItemInfo._get_collection().aggregate(pipeline):
data = [i['_id'][0][4:], i['counts']]
yield data
series_ch2 = [i for i in oneday_cates()]
# 按地區(qū)分布
def oneday_areas():
pipeline = [
{'$match':{'time':1}},
{'$group':{'_id':{'$slice':['$area',0,1]},'counts':{'$sum':1}}},
{'$sort':{'counts':-1}}
]
for i in ItemInfo._get_collection().aggregate(pipeline):
data = [i['_id'][0], i['counts']]
yield data
series_ch3 = [i for i in oneday_areas()]
# *----------------------地區(qū)發(fā)帖量前三----------------------*
def hot_in_areas(area, limit):
pipeline = [
{'$match': {'area': area}},
{'$group': {'_id': {'$slice': ['$cates', 2, 1]}, 'counts': {'$sum': 1}}},
{'$sort': {'counts': -1}},
{'$limit': limit}
]
for i in ItemInfo._get_collection().aggregate(pipeline):
data = {
'name': i['_id'][0],
'type': 'column',
'data': [i['counts']]
}
yield data
series_LD = [i for i in hot_in_areas('朝陽', 3)]
series_PK = [i for i in hot_in_areas('海淀', 3)]
series_PR = [i for i in hot_in_areas('豐臺', 3)]
# *----------------------文字詳情頁----------------------*
def index(request):
limit = 10
iteminfo = ItemInfo.objects # 提取數(shù)據(jù)
paginator = Paginator(iteminfo, limit) # 把提取的數(shù)據(jù)分頁绳军,每頁10個
page = request.GET.get('page', 1) # 從request提取出頁碼
loaded = paginator.page(page)
content = {
'ItemInfo': loaded,
'counts': iteminfo.count(),
}
return render(request, 'index3_p1.html', content)
# *----------------------圖表頁----------------------*
def charts(request):
context = {
'chart_LD': series_LD,
'chart_PK': series_PK,
'chart_PR': series_PR,
'chart_01': series_ch1,
'chart_02': series_ch2,
'chart_03': series_ch3,
}
return render(request, 'index3_p2.html', context)
# print(reuqest) == <WSGIRequest: GET'/index/'>
# print(request.GET) == <QueryDict:{}>
值得一提的是:如何讓柱狀圖的每一個柱子不同顏色?如果你只想(能)通過簡單的設(shè)置“參數(shù)”來實現(xiàn)的話匀奏,可以這樣:閱讀'colorByPoint: Boolean'鞭衩。注:在修改JS可行,而在jupyter notebook里面則不行娃善。