def dashboard(request): tasks = Task.objects.all()[0:5] my_tasks = Task.objects.filter(tester=request.user)[0:5] attr = Results.objects.show_task_id() script_list = [] line = line_time_kf(attr) bar = bar_mp_kf(attr) # myechart = bar.render_embed() # myechart = line.render_embed() script_list.extend(line.get_js_dependencies()) # script_list = bar.get_js_dependencies() script_list.extend(bar.get_js_dependencies()) grid = Grid(width="auto") grid.add(line, grid_bottom="60%") grid.add(bar, grid_top="60%") myechart = grid.render_embed() script_list.extend(grid.get_js_dependencies()) # run_rtv_numbers = Results.objects.order_by('-id').values_list('id').first()[0] # seconds = sum([int(i[0]) for i in Results.objects.values_list('time')]) # m, s = divmod(seconds, 60) # h, m = divmod(m, 60) # d, h = divmod(h, 24) # time_cost = "%02dd:%02dh:%02dm:%02ds" % (d, h, m, s) return render(request, 'dashboard.html', { # 'run_rtv_numbers': run_rtv_numbers, # 'time_cost': time_cost, 'tasks': tasks, 'my_tasks': my_tasks, 'if_dashboard_active': 'active', 'myechart': myechart, 'script_list': script_list })
def echart1(request): template = loader.get_template('ewpyecharts.html') line = Line("压力分析", width=1200, height=700) attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高压力", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低压力", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], legend_top="50%", mark_line=["average"], # 设置 dataZoom 控制索引为 0,1 的 x 轴,即第一个和第二个 is_datazoom_show=True, datazoom_xaxis_index=[0, 1]) v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("考核表分析", title_top="50%") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, is_datazoom_show=True) grid = Grid() grid.add(line, grid_top="60%") grid.add(kline, grid_bottom="60%") # grid.render() context = dict( myechart=grid.render_embed(), host=REMOTE_HOST, script_list=grid.get_js_dependencies() ) return HttpResponse(template.render(context, request))
def get_realtime_data(): # 历史数据优先从hdf5中获取 获取不到则实时从接口获取并添加到hdf5中 store = pd.HDFStore(app.config['STOCKS_PRICE_HDF5'], mode='r+') code = request.args.get("code") # sh_index_data = ts.get_k_data('000001', index=True) real_data = ts.get_realtime_quotes(code) if 'sh_index_data' in store: sh_index_data = pd.read_hdf(app.config['STOCKS_PRICE_HDF5'], 'sh_index_data', mode='r+') else: sh_index_data = ts.get_k_data('000001', index=True) sh_index_data.to_hdf(app.config['STOCKS_PRICE_HDF5'], 'sh_index_data', format='table', data_columns=True) if code in store: hist_data = pd.read_hdf(app.config['STOCKS_PRICE_HDF5'], code, mode='r+') else: hist_data = ts.get_k_data(code) hist_data.to_hdf(app.config['STOCKS_PRICE_HDF5'], code, format='table', data_columns=True) store.close() kline = kline_chart(code, hist_data) # 这时还没转dict kline_sh = kline_chart_sh(sh_index_data) grid = Grid(width="100%") grid.add(kline, grid_bottom="60%") grid.add(kline_sh, grid_top="60%") real_data['delta'] = (real_data['price'].astype('float') - real_data['pre_close'].astype('float')) real_data['delta'] = real_data['delta'].apply(lambda x: round(x, 4)) real_data['delta%'] = real_data['delta'] / real_data['pre_close'].astype( 'float') real_data['delta%'] = real_data['delta%'].apply( lambda x: round(x * 100, 2)) real_data = real_data.to_dict('record') hist_data = hist_data.sort_values('date', ascending=False) hist_data = hist_data.to_dict('record') return render_template( "table_with_chart.html", basic_data=url_for('get_stock_basics_json', code=code), basic_data_columns=stock_basics_columns, real_data=url_for('get_realtime_data_json', code=code), real_data_columns=real_data_columns, hist_data_columns=hist_data_columns, hist_data=hist_data, title=('{0}:({1})股票实时价格'.format(real_data[0]['name'], code)), grid=grid.render_embed(), host=app.config['REMOTE_HOST'], grid_script_list=grid.get_js_dependencies(), )
def get(self, request, *args, **kwargs): template_name = 'echart/echart.html' bar = Bar() bar.add('服装', ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90]) line = Line() line.add("服装", ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"], [5, 20, 36, 10, 75, 90]) echart = Grid(page_title='组合图', width=1200, height=250) echart.add(bar, grid_left='50%') echart.add(line, grid_right='55%') context = dict(myechart=echart.render_embed(), host=REMOTE_HOST, script_list=echart.get_js_dependencies()) return render(request, template_name, context)
def test(): attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] v1 = [5, 20, 36, 10, 75, 90] v2 = [10, 25, 8, 60, 20, 80] bar = Bar("柱状图示例", height=720) bar.add("商家A", attr, v1, is_stack=True) bar.add("商家B", attr, v2, is_stack=True) line = Line("折线图示例", title_top="50%") attr = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"] line.add( "最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"], ) line.add( "最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], mark_line=["average"], legend_top="50%", ) grid = Grid() grid.add(bar, grid_bottom="60%") grid.add(line, grid_top="60%") grid_javascript_snippet = TRANSLATOR.translate(grid.options) return render_template( 'test.html', host=REMOTE_HOST, # 饼状图 grid_id=grid.chart_id, grid_renderer=grid.renderer, my_width=600, my_height=400, grid_custom_function=grid_javascript_snippet.function_snippet, grid_options=grid_javascript_snippet.option_snippet, script_list=grid.get_js_dependencies(), )
def echart1(request): template = loader.get_template('myfirstvis/pyecharts.html') # # # value = [20, 190, 253, 77, 65] # # attr = ['汕头市', '汕尾市', '揭阳市', '阳江市', '肇庆市'] # # map = Map("广东地图示例", width=1200, height=600) # # map.add("", attr, value, maptype='广东', is_visualmap=True, # # visual_text_color='#000') # #map.render() # # context = dict( # # myechart=map.render_embed(), # # host=REMOTE_HOST, # # script_list=map.get_js_dependencies() # # ) # # # gauge = Gauge("仪表盘示例") # # gauge.add("业务指标", "完成率", 66.66) # # gauge.show_config() # line = Line("折线图示例", width=1200, height=700) attr = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] line.add("最高气温", attr, [11, 11, 15, 13, 12, 13, 10], mark_point=["max", "min"], mark_line=["average"]) line.add("最低气温", attr, [1, -2, 2, 5, 3, 2, 0], mark_point=["max", "min"], legend_top="50%", mark_line=["average"], # 设置 dataZoom 控制索引为 0,1 的 x 轴,即第一个和第二个 is_datazoom_show=True, datazoom_xaxis_index=[0, 1]) v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("K 线图示例", title_top="50%") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, is_datazoom_show=True) grid = Grid() grid.add(line, grid_top="60%") grid.add(kline, grid_bottom="60%") # grid.render() context = dict( myechart=grid.render_embed(), host=REMOTE_HOST, script_list=grid.get_js_dependencies() ) # # value = [155, 10, 66, 78] # # attr = ["福建", "山东", "北京", "上海"] # # map = Map("全国地图示例", width=1200, height=600) # # map.add("", attr, value, maptype='china', # # is_visualmap=True, is_piecewise=True, # # visual_text_color="#000", # # visual_range_text=["", ""], # # pieces=[ # # {"max": 160, "min": 70, "label": "高数值"}, # # {"max": 69, "min": 0, "label": "低数值"}, # # ]) # # # # context = dict( # # myechart=map.render_embed(), # # host=REMOTE_HOST, # # script_list=map.get_js_dependencies() # # style = Style( # title_top="#fff", # title_pos="center", # width=1200, # height=600, # background_color="#404a59" # ) # # data_guangzhou = [ # ["广州", "上海"], # ["广州", "北京"], # ["广州", "南京"], # ["广州", "重庆"], # ["广州", "兰州"], # ["广州", "杭州"] # ] # geolines = GeoLines("GeoLines 示例", **style.init_style) # geolines.add("从广州出发", data_guangzhou, is_legend_show=False) # # context = dict( # myechart=geolines.render_embed(), # host=REMOTE_HOST, # script_list=geolines.get_js_dependencies() # ) return HttpResponse(template.render(context, request))
def dataManagerStudent(): stuID = (request.args.get('stuID')) if stuID == None: return render_template('dataManagerStudent.html', data=None) else: from pyecharts import Bar from pyecharts_javascripthon.api import TRANSLATOR REMOTE_HOST = "https://pyecha rts.github.io/assets/js" data, len_data = mongo.get_data_by_condition(request.args.get('stuID')) data_day_out = {} data_day_in = {} data_shop = [] data_shop_money = {} # 这个月花费的金额 data_shop_num = {} # 这个月消费的次数 data_shop_money_avg = {} # 每次消费平均金额 import time fun = lambda year, month: list( range(1, 1 + time.localtime(time.mktime((year, month + 1, 1, 0, 0, 0, 0, 0, 0)) - 86400).tm_mday)) days = fun(2017, 11) # for i in days: # if i < 10: # index = days.index(i) # days[index] = int('0' + str(i)) # print(days) for item in days: # '2017-11-' + data_day_in[str(item)] = 0 data_day_out[str(item)] = 0 for item in data: time = str(int(str(item[4]).split(' ')[0].split('-')[2])) # print(time) money = -float(item[3]) shop = str(item[2]) if shop == '407010002': pass else: shop = shop.replace(str(item[2])[:3], str(item[2])[:3] + '\n') if shop not in data_shop: data_shop.append(shop) data_shop_money[shop] = money data_shop_money[shop] = float(format(data_shop_money[shop], '.2f')) data_shop_num[shop] = 1 else: data_shop_money[shop] += money data_shop_money[shop] = float(format(data_shop_money[shop], '.2f')) data_shop_num[shop] += 1 if money > 0: data_day_out[time] += money data_day_out[time] = float(format(data_day_out[time], '.2f')) else: data_day_in[time] += money data_day_in[time] = float(format(data_day_in[time], '.2f')) # data_shop = data_shop.sort() # 计算每次在店消费的平均值 for i in data_shop: data_shop_money_avg[i] = float(format(data_shop_money[i] / data_shop_num[i], '.2f')) # data_shop.pop(data_shop.index('407010002')) # data_shop_money.pop('407010002') # data_shop_num.pop('407010002') # data_shop_money_avg.pop('407010002') # 拼接返回值 no_out_days_num = 0 no_out_days = [] for i in data_day_out.keys(): if data_day_out[i] == 0: no_out_days_num += 1 no_out_days.append(str(list(data_day_out.keys()).index(i)) + '号') else: print(i) ''' max 最大值 min 最小值 avg 平均值 in 充值金额 not_eat_days 没有消费的天数 ''' res_analyse = { 'max': format(max(list(data_day_out.values())), '.2f'), 'min': format(min(list(data_day_out.values())), '.2f'), 'avg': format(sum(list(data_day_out.values())) / len(days), '.2f'), 'sum_in': format(-sum(list(data_day_in.values())), '.2f'), 'sum_out': format(sum(list(data_day_out.values())), '.2f'), 'no_out_days_num': no_out_days_num, 'no_out_days': no_out_days.__str__().replace('[', '').replace(']', '').replace("'", ''), } ##########图 bar = Bar(height=600) # 柱形图 bar.add('消费金额', days, list(data_day_out.values()), mark_line=["average"], mark_point=["max", "min"], ) bar.add('充值金额', days, list(data_day_in.values()), bar_category_gap=0.05, mark_line=["average"], mark_point=["max", "min"]) overlap = Overlap() bar2 = Bar("消费地点分析", title_top="50%", ) bar2.add('消费金额', data_shop, list(data_shop_money.values()), mark_point=["max", "min"], legend_top="50%") bar2.add('消费次数', data_shop, list(data_shop_num.values()), mark_point=["max", "min"], legend_top="50%") line = Line() line.add('消费均值', data_shop, list(data_shop_money_avg.values()), legend_top="50%", mark_point=["max", "min"]) overlap.add(bar2) overlap.add(line) grid = Grid() grid.add(bar, grid_bottom="60%") grid.add(overlap, grid_top="60%") javascript_snippet = TRANSLATOR.translate(grid.options) return render_template('dataManagerStudent.html', data=res_analyse, chart_id=grid.chart_id, host=REMOTE_HOST, renderer=grid.renderer, my_width="100%", my_height=1200, custom_function=javascript_snippet.function_snippet, options=javascript_snippet.option_snippet, script_list=grid.get_js_dependencies())