def graph(temp_unit, real, state): print('Total city in graph: {}'.format(len(final_city))) if temp_unit == 'c': del temp_unit temp_unit = 'Celsius' temp_unit_abbr = 'C' else: del temp_unit temp_unit = 'Fahrenheit' temp_unit_abbr = 'F' if real == 'y': del real real = 'Yes' else: del real real = 'No' canvas = echarts.Echart('Cold?', 'Temp unit: ' + temp_unit + ' RealFeel: ' + real, html_title='Cold?') canvas.use(echarts.Axis('category', 'bottom', data=final_city)) canvas.use(echarts.Bar(data=final_min)) canvas.use(echarts.Tooltip(trigger='item')) name_graph = '/Cold {0} {1}'.format(state, temp_unit_abbr) canvas.save(path=os.getcwd(), name=name_graph) saved_graph_path = os.getcwd() + '/Cold {0} {1}{2}'.format(state, temp_unit_abbr, '.html') print('Saved to: {0} {1}'.format(saved_graph_path, ':)')) while True: openn = str(input('Open ' + saved_graph_path + ' (y/n):')) if openn == 'y': webbrowser.open('file://' + saved_graph_path) exit(0) elif openn == 'n': exit(0) else: print('Invalid choice') continue
def save_graph(max_list): #保存图表 if not max_list == []: sorted_min_temp = sorted( max_list, lambda x, y: cmp(int(x['max']), int(y['max'])), reverse=True) # lambda比较温度,升序排列 final_list = sorted_min_temp[0:5] # 取前五城市 final_city = [] final_max = [] for city_min in final_list: final_city.append(city_min['city']) final_max.append(city_min['max']) hotest_city_raw = str(final_city[0]) hotest_city = hotest_city_raw.decode('utf-8') hotest_temp = str(final_max[0]) messagebox.showinfo('最热城市', '最热城市:' + ' ' + hotest_city + ' ' + '最高温度:' + ' ' + hotest_temp) # 显示最冷城市 min_canvas = echarts.Echart('最热前五城市', '来自中国天气网') min_bar = echarts.Bar('最高温度', final_max) # 柱状图 min_axis = echarts.Axis('category', 'bottom', data=final_city) # 横向坐标 min_canvas.use(min_bar) min_canvas.use(min_axis) if platform.system() is 'Windows': try: min_canvas.save(os.getcwd(), '\High Graph') except: messagebox.showerror('错误', '文件未储存或储存失败,直接打开图表。', icon='warning') min_canvas.plot() else: try: min_canvas.save(os.getcwd(), '/High Graph') except: messagebox.showerror('错误', '文件未储存或储存失败,直接打开图表。', icon='warning') min_canvas.plot() if os.path.exists(os.getcwd() + '/High Graph.html') is True: if messagebox.askquestion( '确认', '文件保存在' + ' ' + os.getcwd() + '/' + 'High Graph' + '.html' + ' ' + '打开文件?') == 'no': pass else: if platform.system() == 'Windows': webbrowser.open(os.getcwd() + '\High Graph' + '.html') elif platform.system == 'Linux': webbrowser.open(os.getcwd() + '/High Graph' + '.html') else: webbrowser.open('file://' + os.getcwd() + '/High Graph' + '.html') exit() else: messagebox.showerror('错误', '文件未储存或储存失败,直接打开图表。', icon='warning') min_canvas.plot() else: messagebox.showerror('请先获取数据', '请先获取数据,再保存图表', icon='error') return ()
def save_graph(final_min, final_city): # 保存图表 min_canvas = echarts.Echart('最低温度统计', '来自中国天气网') min_bar = echarts.Bar('最低温度', final_min) # 柱状图 min_axis = echarts.Axis('category', 'bottom', data=final_city) # 横向坐标 min_canvas.use(min_bar) min_canvas.use(min_axis) if platform.system() is 'Windows': min_canvas.save(os.getcwd(), '\Graph') chk_saved(min_canvas) else: min_canvas.save(os.getcwd(), '/Graph') chk_saved(min_canvas)