Week3 hw3:Draw a Line Chart

Target

Target

Basic 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 category

Utility 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

Result

That's all. Thank u for your visiting.

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 12,138评论 0赞 23
  • 现实生活中经常存在这样的场景,某件事情的基本流程已定,但每步或其中几步流程具体如何实施却可自由发挥,这种场景映射到...
    简xiaoyao阅读 638评论 0赞 0
  • 是的,没得治了。他没救了,我没药。我也没救了,你也没有药。那是她常说的一句话,只要我劝解她就会说的话。她就是这样充...
    透冰阅读 244评论 0赞 0
  • 书看多了,人就傻了。 这是娭毑说水英的话。 “哼!”水英听了只会把大辫子一甩,扭过身子去,水光潋滟的大眼睛翻起来白...
    林上雪阅读 1,113评论 0赞 7
  • 人有时会发生一件很奇怪的事——至少我有过很多这样的经验,就是之前无意识看的东西(例如一句话,一篇文章抑或一部电影)...
    莱茵茵茵阅读 775评论 1赞 0

友情链接更多精彩内容