def dataStatistics(request): supplierNumber = SupplierList.objects.values('supplier_class').annotate( dcount=Count('supplier_class')).order_by('-dcount') supplier_count = list(supplierNumber.values_list('dcount')) supplier_count = [x[0] for x in supplier_count] supplier_class = list(supplierNumber.values_list('supplier_class')) supplier_class = [x[0] for x in supplier_class] chart = Echart('评审供应商数量', 'TaTaTa') chart.use(Bar('China', supplier_count)) chart.use(Legend(['GDP'])) chart.use(Axis('category', 'bottom', data=supplier_class)) supplierNumber = SupplierList.objects.values('position_abbr').annotate( dcount=Count('position_abbr')).order_by('-dcount') abbr_count = list(supplierNumber.values_list('dcount')) abbr_count = [x[0] for x in abbr_count][:10] position_abbr = list(supplierNumber.values_list('position_abbr')) position_abbr = [x[0] for x in position_abbr][:10] chart2 = Echart('供应商分布', 'TaTaTa') chart2.use(Bar('China', abbr_count)) chart2.use(Legend(['GDP'])) chart2.use(Axis('category', 'bottom', data=position_abbr)) return render(request, 'dataStatistics.html', locals())
def client_x9_load_scenes_draw_bar(filelist): pname, datas = arrage_logs(filelist) tags = list() json_data = list() # 绘图对象,包含标题设置 chart = Echart(title, subtext) # 绘图数据 for data in datas: tags.append(data) chart.use(Bar(data, datas[data], stack='time', label={'normal':{'show':True}})) # x轴 chart.use(Axis('category', 'bottom', '设备', pname, axisLabel={'rotate':40,'interval':0}, axisTick={'interval':0})) # y轴 chart.use(Axis('value', 'left', '秒')) # 标签,与之相似的还有个叫visualmap,但未实现 chart.use(Legend(tags, top=80)) # 添加工具栏,默认在右上角 chart.use(Toolbox()) # 设置绘图区域的位置,默认上留120,下留100 chart.use(Grid()) chart.json json_data.append(chart.get_opt()) return json_data
def main(): # urls=['http://www.weather.com.cn/textFC/hb.shtml', # 'http://www.weather.com.cn/textFC/db.shtml', # 'http://www.weather.com.cn/textFC/hd.shtml', # 'http://www.weather.com.cn/textFC/hz.shtml', # 'http://www.weather.com.cn/textFC/hn.shtml', # 'http://www.weather.com.cn/textFC/xb.shtml', # 'http://www.weather.com.cn/textFC/xn.shtml', # ] # for url in urls: # get_temperature(url) # time.sleep(2) # # line = json.dumps(TEMPERATUR_LIST, ensure_ascii=False) # with open('temperature.json','w') as fp: # fp.write(line.encode('utf-8')) with open('temperature.json', 'r') as fp: TEMPERATUR_LIST = json.load(fp, encoding='utf-8') SORTED_TEMPERATURE_LIST = sorted( TEMPERATUR_LIST, lambda x, y: cmp(int(y['max']), int(x['max']))) TOP20_TEMPERATURE_LIST = SORTED_TEMPERATURE_LIST[0:20] TOP20_CITY_LIST = [] TOP20_MAX_LIST = [] for city_max in TOP20_TEMPERATURE_LIST: TOP20_CITY_LIST.append(city_max['city']) TOP20_MAX_LIST.append(city_max['max']) echart = Echart(u'全国最高温度排名', u'ZG爬虫') bar = Bar(u'最高温度', TOP20_MAX_LIST) axis = Axis('category', 'bottom', data=TOP20_CITY_LIST) echart.use(bar) echart.use(axis) echart.plot()
def bar(): chart = Echart('GDP', 'This is a fake chart') chart.use(Bar('China', [2, 3, 4, 5])) chart.use(Legend(['GDP'])) chart.use(Axis('category', 'bottom', data=['Nov', 'Dec', 'Jan', 'Feb'])) return jsonify(chart.json)
def main(): urls = [ 'http://www.weather.com.cn/textFC/hz.shtml'] # 'http://www.weather.com.cn/textFC/hb.shtml', # 'http://www.weather.com.cn/textFC/db.shtml', # 'http://www.weather.com.cn/textFC/hd.shtml', # 'http://www.weather.com.cn/textFC/hn.shtml', # 'http://www.weather.com.cn/textFC/xb.shtml', # 'http://www.weather.com.cn/textFC/xn.shtml for url in urls: get_temperature(url) time.sleep(2) SORTED_TEMPERATURE_LIST = sorted(TEMPERATURE_LIST, key=lambda x: int(x["min"]), reverse=True) TOP20_TEMPERATURE_LIST = SORTED_TEMPERATURE_LIST[0:20] print(TOP20_TEMPERATURE_LIST) TOP20_CITY_LIST = [] TOP20_MIN_LIST = [] for city_min in TOP20_TEMPERATURE_LIST: TOP20_CITY_LIST.append(city_min['city']) TOP20_MIN_LIST.append(city_min['min']) # #获取 # 排序问题等到后续解决 echart = Echart(u'全国最高温度排名', u'知了课堂贡献') bar = Bar(u'最高温度', TOP20_MIN_LIST) axis = Axis('category', 'bottom', data=TOP20_CITY_LIST) echart.use(bar) echart.use(axis) echart.plot()
def get_friends(): friends = itchat.get_friends(update=True)[0:] male = female = other = 0 for i in friends[1:]: sex = i['Sex'] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 total = len(friends[1:]) chart = Echart('friends', 'from WeChat') chart.use( Pie('WeChat', [{ 'value': male, 'name': 'male: %.2f%%' % (float(male) / total * 100) }, { 'value': female, 'name': 'male: %.2f%%' % (float(female) / total * 100) }, { 'value': other, 'name': 'male: %.2f%%' % (float(other) / total * 100) }], radius=["%50", "%70"])) chart.use(Legend(['male', 'female', 'other'])) del chart.json['xAxis'] del chart.json['yAxis'] chart.plot()
def city_ratio(self): c_list = [] for i in self.friends: city = i['City'] if city != '': c_list.append(city) from collections import Counter c_dict = dict(Counter(c_list)) # 使用echarts from echarts import Echart, Legend, Pie chart = Echart(u'%s的微信各城市好友数量' % (self.friends[0]['NickName']), 'from WeChat') chart.use( Pie('WeChat', [{ 'value': value, 'name': u'{0}-{1}'.format(name, value) } for name, value in c_dict.items()], radius=["30%", "70%"])) chart.use(Legend(list(name for name, value in c_dict.items()))) del chart.json['xAxis'] del chart.json['yAxis'] chart.plot() # 保存图表 import os d = os.path.dirname(__file__) chart.save( os.path.dirname(__file__) + os.sep, '%s的好友城市人数' % (self.friends[0]['NickName']))
def totalSex(friends): male = female = other = 0 for i in friends[1:]: sex = i["Sex"] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 total = len(friends[1:]) print u"总共好友:%d" % total print u"男性好友:%.2f%%" % (float(male) / total * 100) print u"女性好友:%.2f%%" % (float(female) / total * 100) print u"未知性别:%.2f%%" % (float(other) / total * 100) chart = Echart(u'%s的微信好友性别比例' % (friends[0]['NickName']), 'from WeChat by xuqidong') chart.use( Pie('WeChat', [{ 'value': male, 'name': u'男性 %.2f%%' % (float(male) / total * 100) }, { 'value': female, 'name': u'女性 %.2f%%' % (float(female) / total * 100) }, { 'value': other, 'name': u'未知 %.2f%%' % (float(other) / total * 100) }], radius=["50%", "70%"])) chart.use(Legend(["male", "female", "other"])) del chart.json["xAxis"] del chart.json["yAxis"] chart.plot()
def echart_pie(friends): total = len(friends) - 1 male = female = other = 0 for friend in friends[1:]: sex = friend["Sex"] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 from echarts import Echart, Legend, Pie chart = Echart('%s的微信好友性别比例' % (friends[0]['Name']), 'from WeChat') chart.use( Pie('WeChat', [{ 'value': male, 'name': '男性 %.2f%%' % (float(male) / total * 100) }, { 'value': female, 'name': '女性 %.2f%%' % (float(female) / total * 100) }, { 'value': other, 'name': '其他 %.2f%%' % (float(other) / total * 100) }], radius=["50%", "70%"])) chart.use(Legend(["male", "female", "other"])) del chart.json["xAxis"] del chart.json["yAxis"] chart.plot()
def yidi_salary_dis(self): lst = [] with codecs.open(r"E:\51job\data\yidi.csv", "r", encoding="utf-8") as f: f_csv = csv.reader(f) for row in f_csv: for i in range(5): lst.append(int(row[i])) chart = Echart(u'异地招聘-工资范围分布', '[x,y]包含边界值x和y') chart.use( Pie('Guangzhou', [{ 'value': lst[0], 'name': '[0,1.0]: %d' % (lst[0]) }, { 'value': lst[1], 'name': '[1.1,1.5]: %d' % (lst[1]) }, { 'value': lst[2], 'name': '[1.6,2.0]: %d' % (lst[2]) }, { 'value': lst[3], 'name': '[2.1,2.5]: %d' % (lst[3]) }, { 'value': lst[4], 'name': '>2.5: %d' % (lst[4]) }], radius=["50%", "70%"])) #chart.use(Legend(['Beijing'])) del chart.json["xAxis"] del chart.json["yAxis"] chart.plot()
def display_temperature(): #url各区为华北,东北,华东,华中,华南,西北,西南 urls = [ 'http://www.weather.com.cn/textFC/hb.shtml', 'http://www.weather.com.cn/textFC/db.shtml', 'http://www.weather.com.cn/textFC/hd.shtml', 'http://www.weather.com.cn/textFC/hz.shtml', 'http://www.weather.com.cn/textFC/hn.shtml', 'http://www.weather.com.cn/textFC/xb.shtml', 'http://www.weather.com.cn/textFC/xn.shtml' ] #循环传各区url参数并执行get气温函数 for url in urls: get_temperature(url) time.sleep(2) #每爬一个页面停顿防止过度干扰服务器不友好 #对执行完获得全国最高温进行排序 SORTED_TEMPERATURE_LIST = sorted(TEMPERATURE_LIST, key=(lambda x: (int(x['max']))), reverse=True) #,页面显示不了这么多,故筛选排前20的并定义新变量赋值 TOP20_TEMPERATURE_LIST = SORTED_TEMPERATURE_LIST[0:20] TOP20_CITY_LIST = [] OP20_MAX_LIST = [] for city_max in TOP20_TEMPERATURE_LIST: TOP20_CITY_LIST.append(city_max['city']) TOP20_MAX_LIST.append(city_max['max']) #用echarts库进行可视化制图 echart = Echart(u'全国最高气温排名TOP1~20') bar = Bar(u'最高温度', TOP20_MAX_LIST) axis = Axis('category', 'left', data=TOP20_CITY_LIST) echart.use(bar) echart.use(axis) echart.plot()
def index(request): # data = '我的名字叫蒋文华' # return render(request, 'index.html', {'data': data}) if request.method == 'POST': form = SqlForm(request.POST) if form.is_valid(): sql = form.cleaned_data['sql'] title = form.cleaned_data['title'] x = form.cleaned_data['x_label'] y = form.cleaned_data['y_label'] x1, y1 = conn('wboss', 'postgres', '', '10.0.0.141', '5432', sql) # y1 = [2, 3, 4, 5, 6] # x1 = ['Nov', 'Dec', 'Jan', 'Feb', 'marth'] chart = Echart(title, '') chart.use(Bar(y, y1)) chart.use(Legend(['GDP'])) chart.use(Axis('category', 'bottom', data=x1, name=x)) chart.use(Tooltip()) chart.plot() # return HttpResponse(res) else: form = SqlForm() return render(request, 'index.html', {'form': form})
def test_axis(): chart = Echart('Axis', 'Proportion of Browser') assert not chart.json['xAxis'] and not chart.json['yAxis'] chart.use(Axis('category', 'bottom', 'proportion', inverse=True)) assert chart.json['xAxis'] chart.use(Axis('category', 'left', 'proportion', inverse=True)) assert chart.json['yAxis'] map(repr, chart.x_axis)
def main(): # target_urls = ['http://www.weather.com.cn/textFC/hb.shtml', # 'http://www.weather.com.cn/textFC/db.shtml', # 'http://www.weather.com.cn/textFC/hd.shtml', # 'http://www.weather.com.cn/textFC/hz.shtml', # 'http://www.weather.com.cn/textFC/hn.shtml', # 'http://www.weather.com.cn/textFC/xb.shtml', # 'http://www.weather.com.cn/textFC/xn.shtml'] # # all_data = get_temperature(target_urls) # print(all_data) # 将数据保存到文件 # with open('temperature.json', 'w', encoding='utf-8') as fw: # fw.write(json.dumps(all_data)) # 从文件中读取数据 with open('temperature.json', 'r') as fr: all_data = json.loads(fr.read()) # print(all_data) # 重新构造list结构 temp_city_list = [] for data in all_data: provence = data['provence'] city_list = data['city_list'] for city in city_list: city['city'] = '%s-%s' % (provence, city['city']) temp_city_list.append(city) print(temp_city_list) # 按照最高温度排序 sort_max_temp_list = sorted( temp_city_list, key=lambda t: int(t.__getitem__('max_temperature'))) # 按照最低温度排序 sort_min_temp_list = sorted( temp_city_list, key=lambda t: int(t.__getitem__('min_temperature'))) print(sort_max_temp_list) print(sort_min_temp_list) # 获取温度最高的10条数据 top10_max = sort_max_temp_list[-10:] top10_max_city = [] top10_max_temp = [] print(top10_max) for item in top10_max: top10_max_city.append(item['city']) top10_max_temp.append(item['max_temperature']) # 获取温度最低的10条数据 top10_min = sort_min_temp_list[:10] # 使用第三方Echarts库在浏览器中输出图形统计界面 echart = Echart(u'全国温度统计') bar = Bar(u'最高温度', top10_max_temp) axis = Axis('category', 'bottom', data=top10_max_city) echart.use(bar) echart.use(axis) echart.plot()
def pie(): lst = [2, 3, 4, 5] month = ['Nov', 'Dec', 'Jan', 'Feb'] data = [dict(value=i, name=j) for i, j in zip(lst, month)] chart = Echart('GDP', 'This is a fake chart', axis=False) chart.use(Pie('China', data)) chart.use(Legend(['GDP'])) return jsonify(chart.json)
def bar(): # set bar color color = dict(color='#EE5500') chart = Echart('GDP', 'This is a fake chart') chart.use(Bar('China', [2, 3, 4, 5], itemStyle=dict(normal=color))) chart.use(Legend(['GDP'])) chart.use(Axis('category', 'bottom', data=['Nov', 'Dec', 'Jan', 'Feb'])) return jsonify(chart.json)
def viewableData(dataJson): statisticInfo = json.loads(dataJson) # 拆分Json文件,拆成多个列表 # 网站列表,最大rtt时间,平均rtt时间,丢包率 hostList, maxList, avgList, dropRateList = [], [], [], [] for i in statisticInfo: hostList.append(i) maxList.append(statisticInfo[i]['max_rtt']) avgList.append(statisticInfo[i]['median_rtt']) dropRateList.append(float(statisticInfo[i]['drop_rate'][:-1])) # 对网页进行初始化处理 chart = Echart(title='ping结果统计', width='auto', height='auto') # 设置三条数据 chart.use(Bar('maxList', maxList, tpye='vertical')) chart.use(Bar('avgList', avgList, tpye='vertical')) chart.use(Bar('dropRateList', dropRateList, tpye='certical')) # 设置y轴 chart.use( Axis('category', 'left', data=hostList, axisLabel={'interval': 0})) # 启用显示 chart.use(Tooltip(trigger='axis')) # 启用图标 chart.use( Legend(['maxList', 'avgList', 'dropRateList'], position=('center', 'top'))) # 启用工具栏 feature = { 'dataZoom': { 'show': True, 'title': { 'dataZoom': '区域缩放', 'dataZoomReset': '区域缩放后退' } }, 'magicType': { 'show': True, 'type': ['line', 'bar', 'stack', 'tiled'] }, 'restore': { 'show': True, 'title': '还原' } } # 开启工具盒子 chart.use(Toolbox(feature=feature, orient='vertical', show=True)) # 展示可视化数据 chart.plot() # 存储html chart.save(os.getcwd() + '/', 'ping可视化数据')
def xx(self, data): _PR = PR.getInstance() chart = Echart('GDP', 'This is a fake chart') chart.use(Bar('China', [2, 3, 4, 5])) chart.use(Legend(['GDP'])) chart.use(Axis('category', 'bottom', data=['Nov', 'Dec', 'Jan', 'Feb'])) _chart = chart.json print(_chart) _chart["tooltip"] = {} _PR.setResult(_chart) return _PR.getPRBytes()
def sexratio(self): male = female = other = 0 for i in self.friends[1:]: sex = i['Sex'] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 total = len(self.friends[1:]) male_ratio = float(male) / total * 100 female_ratio = float(female) / total * 100 other_ratio = float(other) / total * 100 print(u"男性好友:%.2f%%" % male_ratio) print(u"女性好友:%.2f%%" % female_ratio) print(u"其他:%.2f%%" % other_ratio) # 使用echarts from echarts import Echart, Legend, Pie chart = Echart(u'%s的微信好友性别比例' % (self.friends[0]['NickName']), 'from WeChat') chart.use( Pie('WeChat', [{ 'value': male, 'name': u'男性 %.2f%%' % male_ratio }, { 'value': female, 'name': u'女性 %.2f%%' % female_ratio }, { 'value': other, 'name': u'其他 %.2f%%' % other_ratio }], radius=["50%", "70%"])) chart.use(Legend(['male', 'female', 'other'])) del chart.json['xAxis'] del chart.json['yAxis'] chart.plot() # 保存图表 import os d = os.path.dirname(__file__) chart.save( os.path.dirname(__file__) + os.sep, '%s的好友性别比例' % (self.friends[0]['NickName']))
def data_visualization(temperature_list): top20_temperature_list = sorted( temperature_list, lambda x, y: cmp(int(x['min']), int(y['min'])))[0:20] city20_list = [] min20_list = [] for city_min in top20_temperature_list: city20_list.append(city_min['city']) min20_list.append(city_min['min']) echart_show = Echart(u'全国最低温度排名', u'小白打酱油提供', u'这是气温排名') bar = Bar(u'最低温度', min20_list) axis = Axis('category', 'bottom', data=city20_list) echart_show.use(bar) echart_show.use(axis) echart_show.plot()
def draw(datas, **kwargs): title = '缺少标题' if 'title' in kwargs: title = kwargs['title'] subtext = '' if 'subtext' in kwargs: subtext = kwargs['subtext'] # 绘图对象,包含标题设置 chart = Echart(title, subtext) # 绘图数据 tags = list() for data in datas: tags.append(data) chart.use(Line(data, datas[data])) x_name = '' y_name = '' x_line = [] if 'label' in kwargs: x_name = kwargs['label'][0] y_name = kwargs['label'][1] x_line = kwargs['label'][2] # x轴 chart.use(Axis('category', 'bottom', x_name, data=x_line)) # y轴 chart.use(Axis('value', 'left', y_name)) # 标签 t_top = 60 if 'top' in kwargs: t_top = kwargs['top'] chart.use(Legend(tags, top=t_top)) # 添加工具栏 chart.use(Toolbox()) # 设置绘图区域的位置,默认上留120,下留100 chart.use(Grid()) # tooltip chart.use(Tooltip()) # 打开浏览器并绘图 # chart.plot() return chart.get_opt()
def main(): urls = [ 'http://www.weather.com.cn/textFC/hb.shtml', 'http://www.weather.com.cn/textFC/db.shtml', 'http://www.weather.com.cn/textFC/hd.shtml', 'http://www.weather.com.cn/textFC/hz.shtml', 'http://www.weather.com.cn/textFC/hn.shtml', 'http://www.weather.com.cn/textFC/xb.shtml', 'http://www.weather.com.cn/textFC/xn.shtml', ] for url in urls: get_temperature(url) time.sleep(2) echarts = Echart(u'温度排名') bar_x = Bar(u'最低温度', MIN_LIST) bar_y = Bar(u'最高温度', MAX_LIST) axis = Axis('category', 'bottom', data=CITY_LIST) echarts.use(bar_x) echarts.use(bar_y) echarts.use(axis) echarts.plot()
def get_sexInfo(self): '''初始化计数器''' male = female = other = 0 '''遍历list,列表里的第一个是自己''' for i in self.all_friends: sex = i['Sex'] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 total = len(self.all_friends[1:]) chart = Echart(u"%s的微信好友性别比例" % (self.all_friends[0]['NickName']), 'from Wechat') chart.use( Pie( 'Wechat', [ { 'value': male, 'name': u"男性 %.2f%%" % (float(male) / total * 100) }, { 'value': female, 'name': u"女性 %.2f%%" % (float(female) / total * 100) }, { 'value': other, 'name': u"其他 %.2f%%" % (float(other) / total * 100) }, ], )) chart.use(Legend(["male", "female", "other"])) chart.plot()
def main(): urls=['http://www.weather.com.cn/textFC/hb.shtml', 'http://www.weather.com.cn/textFC/db.shtml', 'http://www.weather.com.cn/textFC/hd.shtml'] for url in urls: get_temperature(url) #top10_max=max_list[0:10] #top10_cit=city_list[0:10] sorted_temprature_list=sorted(temperature_list,lambda x,y:cmp(int(x['max']),int(y['max']))) top10_temprature_list=sorted_temprature_list[0:8] top10_city_list=[] top10_max_list=[] for city_max in top10_temprature_list: top10_city_list.append(city_max['city']) top10_max_list.append(city_max['max']) print city_max['city'],city_max['max'] echart=Echart(u'全国最高温度',u'3.19号') bar=Bar(u'最高温度',top10_max_list) axis=Axis('category','bottom',data=top10_city_list) echart.use(bar) echart.use(axis) echart.plot()
def main(): urls = ['http://www.weather.com.cn/textFC/hb.shtml', 'http://www.weather.com.cn/textFC/db.shtml', 'http://www.weather.com.cn/textFC/hd.shtml', 'http://www.weather.com.cn/textFC/hz.shtml', 'http://www.weather.com.cn/textFC/hn.shtml', 'http://www.weather.com.cn/textFC/xb.shtml', 'http://www.weather.com.cn/textFC/xn.shtml'] for url in urls: get_temperature(url) time.sleep(2) TOP20_MIN_LIST = MINTEMP_LIST[0:20] TOP20_CITY_LIST = CITY_LIST[0:20] print(TOP20_CITY_LIST) echart = Echart('全国最低温度统计', 'Author: AlexZ33') bar = Bar('最低温度', TOP20_MIN_LIST) axis = Axis('category', 'bottom', data = TOP20_CITY_LIST) echart.use(bar) echart.use(axis) # echart.use(Legend(['℃'])) echart.plot()
print i if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 total = len(friends[1:]) print total print "男性好友:%.2f%%" % (float(male) / total * 100) print "女性好友:%.2f%%" % (float(female) / total * 100) print "未备注好友:%.2f%%" % (float(other) / total * 100) chart = Echart('%s的微信好友性别比例' % (friends[0]), 'from WeChat') chart.use( Pie('WeChat', [{ 'value': male, 'name': "男性好友:%.2f%%" % (float(male) / total * 100) }, { 'value': male, 'name': "女性好友:%.2f%%" % (float(female) / total * 100) }, { 'value': male, 'name': "未备注好友:%.2f%%" % (float(other) / total * 100) }], radius=["20%", "40%"])) chart.use(Legend(["male", "female", "other"])) del chart.json["xAxis"]
friends = itchat.get_friends(update=True)[0:] # 初始化计数器,有男有女,当然,有些人是不填的 male = female = other = 0 # 遍历这个列表,列表里第一位是自己,所以从"自己"之后开始计算 # 1表示男性,2女性 for i in friends[1:]: sex = i["Sex"] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 # 总数算上,好计算比例啊~ total = len(friends[1:]) print "男性好友:%.2f%%" % (float(male) / total * 100) print "女性好友:%.2f%%" % (float(female) / total * 100) print "其他:%.2f%%" % (float(other) / total * 100) chart = Echart(u'%s的微信好友性别比例' % (friends[0]['NickName']), 'from WeChat') chart.use(Pie('WeChat', [{'value': male, 'name': u'男性 %.2f%%' % (float(male) / total * 100)}, {'value': female, 'name': u'女性 %.2f%%' % (float(female) / total * 100)}, {'value': other, 'name': u'其他 %.2f%%' % (float(other) / total * 100)}], radius=["50%", "70%"])) chart.use(Legend(["male", "female", "other"])) del chart.json["xAxis"] del chart.json["yAxis"] chart.plot()
sex = friend['Sex'] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 self = friends[0] total = len(friends) - 1 male_rate = float(male) / total * 100 female_rate = float(female) / total * 100 other_rate = float(other) / total * 100 chart = Echart(u'{}的微信好友的性别比例'.format(self['NickName']), "from WeChat") chart.use( Pie('WeChat', [{ 'value': male, 'name': u'男性 {:.2f}'.format(male_rate) }, { 'value': female, 'name': u'女性 {:.2f}'.format(female_rate) }, { 'value': other, 'name': u'其他 {:.2f}'.format(other_rate) }])) chart.use(Legend(['male', 'female', 'other'])) del chart.json['xAxis'] del chart.json['yAxis'] chart.plot()
itchat.login() friends = itchat.get_friends(update=True)[0:] male = female = other = 0 for i in friends[1:]: sex = i["Sex"] if sex == 1: male += 1 elif sex == 2: female += 1 else: other += 1 total = len(friends[1:]) chart = Echart('%sfrends' % (friends[0]['NickName']), 'from WeChat') chart.use( Pie('WeChat', [{ 'value': male, 'name': u'man %.2f%%' % (float(male) / total * 100) }, { 'value': female, 'name': u'woman %.2f%%' % (float(female) / total * 100) }, { 'value': other, 'name': u'other %.2f%%' % (float(other) / total * 100) }], radius=["50%", "70%"])) chart.use(Legend(["male", "female", "other"])) del chart.json["xAxis"] del chart.json["yAxis"]
# html = open(html_path,'w') # line = 0 # for s in lines: # line = line + 1 # if line==32: # html.write('data: %s\n' % valueList) # if line==26: # html.write('data: [') # for str in nameList: # html.write('\"%s\",' % str.encode('utf-8')) # html.write(']\n') # else: # html.write(s) # 用echart画图 chart = Echart(u'WeChat 好友省份分布图', 'data from xuqidong') chart.use(Bar('China', valueList)) chart.use(Legend(['GDP'])) chart.use(Axis('category', 'left', data=nameList)) chart.plot() # 获取好友列表 # friends = itchat.get_friends(update=True)[0:] # 初始化计数器,有男有女,当然,有些人是不填的 male = female = other = 0 # 遍历这个列表,列表里第一位是自己,所以从"自己"之后开始计算 # 1表示男性,2女性 for i in friends[1:]: sex = i["Sex"] if sex == 1: male += 1