3-3




3_3HOMEWORK














In [2]:



import pymongo
import charts
from datetime import timedelta,date






In [10]:



client = pymongo.MongoClient('localhost', 27017)
ceshi = client['ceshi']
item_info = ceshi['item_infoS']






In [4]:



def get_all_dates(date1,date2):
the_date = date(int(date1.split('.')[0]),int(date1.split('.')[1]),int(date1.split('.')[2]))
end_date = date(int(date2.split('.')[0]),int(date2.split('.')[1]),int(date2.split('.')[2]))
days = timedelta(days=1)

while the_date <= end_date:
    yield (the_date.strftime('%Y.%m.%d'))
    the_date = the_date + days






In [14]:



def get_data_within(date1,date2,cates):
for cate in cates: # 第一次循环得到列表中的单个日期
cate_day_posts = [] # 创建一个列表,时间段内每天的发帖数量装进这个列表中
for date in get_all_dates(date1,date2):
a = list(item_info.find({'pub_date':date,'cates':cate}))
each_day_post = len(a)
cate_day_posts.append(each_day_post)
data = {
'name': cate,
'data': cate_day_posts,
'type': 'line'
}
yield data






In [22]:



for i in get_data_within('2016.01.01','2016.01.07',['北京二手手机','北京二手笔记本','北京二手台式机']):
print(i)






{'data': [82, 105, 118, 103, 92, 96, 110], 'name': '北京二手手机', 'type': 'line'}
{'data': [46, 53, 53, 84, 50, 70, 75], 'name': '北京二手笔记本', 'type': 'line'}
{'data': [20, 20, 16, 12, 29, 30, 22], 'name': '北京二手台式机', 'type': 'line'}






In [23]:



options = {
'chart' : {'zoomType':'xy'},
'title' : {'text': '发帖量统计'},
'subtitle': {'text': '可视化统计图表'},
'xAxis' : {'categories': [i for i in get_all_dates('2016.01.01','2016.01.07')]},
'yAxis' : {'title': {'text': '数量'}}
}

series = [i for i in get_data_within('2016.01.01','2016.01.07',['北京二手手机','北京二手笔记本','北京二手台式机'])]

charts.plot(series, options=options,show='inline')
# options=dict(title=dict(text='Charts are AWESOME!!!'))




Out[23]:












Adjust chart settings

.json








In [ ]:










你可能感兴趣的:(3-3)