Target
TargetBasic Moves
First things first, we use these code to connect our mongoDB.
import pymongo
from datetime import date
from datetime import timedelta
import charts
client = pymongo.MongoClient('localhost',27017)
myDB = client['ganjiDB']
myCollection = myDB['bjGanji']
Then, we must find out how to get a item with the special category. So I get it form a key named cates. Its third element can tell us what type of itself. It worked like this below:
Get the categoryUtility Functions
# arg1: myday = date(2015,12,10)
# arg2: ['北京二手笔记本','北京二手台式机/配件','北京二手手机']
# return a type's number in the given date
def get_day_post(aDate, iType):
dateStr = aDate.strftime('%Y.%m.%d')
count = 0
for i in myCollection.find({'pub_date':dateStr},{'cates':1,'pub_date':1,'_id':0}).limit(50):
if i['cates'][2] == iType:
count += 1
# print(i)
return count
# for i in date_generate(myday,3):
# generate a Navigatable Series for days
def date_generate(startDate, spendDay):
delta = timedelta(days=1)
while spendDay > 0:
yield startDate
spendDay -= 1
startDate += delta
# combine upper function together
def series_generate(startDate,spendDay,iType):
for eachType in iType:
postList = []
for eachDay in date_generate(startDate,spendDay):
sPostCount = get_day_post(eachDay,eachType)
postList.append(sPostCount)
data = {
'name':eachType,
'data':postList,
'type':'line'
}
yield data
These functions is used for generating a series which will be use in highchart's Drawing. It's process is quiet simple, I use two for loop to get the exact number in the condition I need.
The only thing need to add to my code Library is the Date Module. It's powerful and easy to learn when dealing with time or date class.
Draw the chart
As usual, we have to build the parameters first.
startDate = date(2016,1,1)
series = [i for i in series_generate(startDate,15,['北京二手笔记本','北京二手台式机/配件','北京二手手机'])]
# make options for highchart
options = {
'chart':{'zoomType':'xy'},
'title':{'text':'Line Chart'},
'subtitle':{'text':'made by Jet'},
'xAxis':{'categories':[i.strftime('%Y.%m.%d') for i in date_generate(startDate,15)]},
'yAxis':{'title': {'text': 'PostNumber'}}
}
And then type in the magic word.
charts.plot(series,show='inline',options=options)
Bingo!
How the Result looks like
ResultThat's all. Thank u for your visiting.
网友评论