我的成果
我的代码
代码的部分其实不难,就是挺繁琐的,东拼拼西凑凑。
贴一下我的views.py
吧:
from django.shortcuts import render
# 调用modules中的类
from final_45.models import ItemInfo
# 分页工具 Paginator
from django.core.paginator import Paginator
cate_list = [
'北京二手美容/保健', '北京二手平板电脑', '北京二手文体/户外/乐器',
'北京二手家电', '北京二手图书/音像/软件', '北京二手母婴/儿童用品',
'北京二手设备', '北京二手数码产品','北京二手手机', '北京二手办公用品/设备',
'北京二手服装/鞋帽/箱包', '北京二手台式机/配件', '北京其他二手物品',
'北京二手笔记本', '北京二手家具'
]
# *----------------------发帖总量柱状图----------------------*
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)]
# *----------------------一天内交易物品分布:饼状图----------------------*
# 按种类分布
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()]
# 按地区分布
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()]
# *----------------------地区发帖量前三----------------------*
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 # 提取数据
paginator = Paginator(iteminfo, limit) # 把提取的数据分页,每页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) ==
# print(request.GET) ==
值得一提的是:如何让柱状图的每一个柱子不同颜色?如果你只想(能)通过简单的设置“参数”来实现的话,可以这样:阅读'colorByPoint: Boolean'。注:在修改JS可行,而在jupyter notebook里面则不行。