charts 画折线图

主题:指定日期内,不同地区的发布信息的数量的变化曲线

  数据库是mongod

  数据是58同城的发布的信息  

 整体思路:

  1由于从数据库中拿到的数据,格式等方面并不一样能完全满足需求,需要对数据库中的数据进行整理

  2 更新数据库

  3 数据可视化

出发点:

  从哪天开始,到哪天结束

  具体区域

追求点:

  以上区域在 限定时间内的发帖变化。

 

part1:拿到截止天和开始天之间的每一天。

  为什么把这个功能单独拿出来作为一个函数?

  because 后面生成图表是,横坐标是 每一天。

  所以,这样设计是有意义的。

  这里用到datetime模块中的date,timedelta,和strftime方法。

def get_all_date(date1,date2):
    delta = timedelta(days=1)
    d1 = date(int(date1.split('-')[0]),int(date1.split('-')[1]),int(date1.split('-')[2]))
    d2 = date(int(date2.split('-')[0]),int(date2.split('-')[1]),int(date2.split('-')[2]))
    while d1<=d2:
        yield d1.strftime('%Y-%m-%d')
        d1 += delta

part2:使用find函数查找指定数据

  

def get_one_area_one_date(area,date1):
    return col.find({'area':area,'pub_date':date1}).count()

实际上这个函数,可以用aggregate,$group,更简单,更迅速的实现。
from datetime import date,timedelta

def get_one_area_many_date(area,date1,date2):
    delta = timedelta(days=1)
    d1 = date(int(date1.split('-')[0]),int(date1.split('-')[1]),int(date1.split('-')[2]))
    d2 = date(int(date2.split('-')[0]),int(date2.split('-')[1]),int(date2.split('-')[2]))
    while d1<=d2:
        yield col.find({'area':area,'pub_date':d1.strftime('%Y-%m-%d')}).count()
        d1 += delta
def get_many_area_many_date(areas,date1,date2):
    for area in areas:
        yield {'name':area,'data':[i for i in get_one_area_many_date(area,date1,date2)],'type':'line'}

part3:   转化数据

最终显示:

import charts
options = {
    'title':{
        'text':'发帖量统计'
    },
    'subtitle':{
        'text':'北京地区'
    },
    'chart':{
        'zoomType':'xy',
    },
    'xAxis':{
        'categories':[i for i in get_all_date('2015-11-01','2016-01-10') ]  #这便是用到的第一个函数,坐标值,每一天。是在这里。
    },
    'yAxis':{
        'title':{
            'text':'数量'
        }
    }
}
charts.plot(ll,show='inline',options=options)

原文地址:https://www.cnblogs.com/654321cc/p/8796056.html