def draw(self, data_list: typing.List[ClassifierResult], report_path: str = None): # draw line = self._draw_line(data_list) bar = self._draw_bar(data_list) # merge charts page = Page() page.add(line) page.add(bar) # insert extras template = Template(TEMPLATE) template_content = template.render( chart=Markup(page.render_embed()), dir_link_list=self.dir_link_list, thumbnail_list=self.thumbnail_list, ) # save to file if not report_path: report_path = f'{toolbox.get_timestamp_str()}.html' with open(report_path, "w") as fh: fh.write(template_content) logger.info(f'save report to {report_path}')
def seller_chart(): follower_num = [] order_num = [] for s in Seller.objects.order_by('-order_num')[:30]: order_num.append([s.name, s.order_num]) for s in Seller.objects.filter().order_by('-followers_num')[:30]: follower_num.append([s.name, s.followers_num]) follower_num_pie = Pie(init_opts=opts.InitOpts(js_host='../static/js/', width='1100px') ).add('', follower_num, radius=["10%", "75%"], # center=["25%", "50%"], rosetype="radius", ).set_global_opts( title_opts=opts.TitleOpts(title="订阅数"), legend_opts=opts.LegendOpts( orient="vertical", pos_top="15%", pos_left="100px", type_='scroll' ), ) order_num_pie = Pie(init_opts=opts.InitOpts(js_host='../static/js/', width='1100px') ).add('', order_num, radius=["10%", "75%"], # center=["25%", "50%"], rosetype="radius", ).set_global_opts( title_opts=opts.TitleOpts(title="订单数"), legend_opts=opts.LegendOpts( orient="vertical", pos_top="15%", pos_left="100px", type_='scroll' ), ) page = Page(layout=Page.SimplePageLayout, js_host='../static/js/') page.add(order_num_pie, follower_num_pie) return page.render_embed()
def Page_Layout(request): page = Page(layout=Page.DraggablePageLayout) page.add(Rating_Pie(), Rating_line_bar(), Score_by_time_douban(), Score_by_time_maoyan(), Map_amount(), scatter_rating_likes(), grid_liquid_favorate_rate(), WordCloud_douban_high(), WordCloud_douban_low(), WordCloud_maoyan_high(), WordCloud_maoyan_low(), sex_distribution_bar(), sex_distribution_pie(), level_distribution_bar(), level_distribution_pie(), Evaluate_bar()) return HttpResponse(page.render_embed())
def index(request): request.encoding = 'utf-8' if 'optionsRadios' in request.GET and request.GET['optionsRadios']: rq1 = request.GET['rq1'] rq2 = request.GET['rq2'] rq1 = time.strftime("%Y.%m.%d", time.strptime(rq1, "%Y-%m-%d")) rq2 = time.strftime("%Y.%m.%d", time.strptime(rq2, "%Y-%m-%d")) selet = request.GET['optionsRadios'] page1 = Page() if selet == 'zhou': page1 = zhou(rq1, rq2) return HttpResponse(page1.render_embed())
def draw(self, data_list: typing.List[ClassifierResult], report_path: str = None, cut_result: VideoCutResult = None): """ draw report file :param data_list: classifierResult list, output of classifier :param report_path: your report will be there :param cut_result: more charts would be built :return: """ # draw line = self._draw_line(data_list) bar = self._draw_bar(data_list) # merge charts page = Page() page.add(line) page.add(bar) if cut_result: sim_line = self._draw_sim(cut_result) page.add(sim_line) # insert thumbnail if not self.thumbnail_list: logger.debug('auto insert thumbnail ...') _, unstable = cut_result.get_range() for each in unstable: self.add_thumbnail( f'{each.start}({each.start_time}) - {each.end}({each.end_time})', cut_result.thumbnail(each), ) # insert extras template = Template(TEMPLATE) template_content = template.render( chart=Markup(page.render_embed()), dir_link_list=self.dir_link_list, thumbnail_list=self.thumbnail_list, extras=self.extra_dict, ) # save to file if not report_path: report_path = f'{toolbox.get_timestamp_str()}.html' with open(report_path, "w") as fh: fh.write(template_content) logger.info(f'save report to {report_path}')
def index(request): # # page.render() from demo.urls import urlpatterns from django import urls from django.urls import path, re_path class_name = 'BarView' # 类的父类 class_bases = (View, ) # 类体 # 注意,这里还非要前面不能有空格 class_body = """ def get(self, request, *args, **kwargs): return JsonResponse(json.loads(json_bar_base())) """ class_dic = {} exec(class_body, globals(), class_dic) print("class_dic===", class_dic) # 步骤二:调用元类type(也可以自定义)来产生类Chinense BarViewClasss = type(class_name, class_bases, class_dic) # 实例化type得到对象Foo,即我们用class定义的类Foo bar = "bar" urlpatterns.append(re_path('%s/' % bar, BarViewClasss.as_view())) urlpatterns.append(re_path('line/', LineView.as_view())) print("qwqwq", urlpatterns) # # ->常常出现在python函数定义的函数名后面,为函数添加元数据,描述函数的返回类型,从而方便开发人员使用。 # def json_line_base() -> Line: # 这里不能使用-> func = """ def line_base(): js = "$(function () {getData(chart_qw);setInterval(getData, 2000);});function getData() {$.ajax({type: 'GET',url: 'http://127.0.0.1:8000/demo/line',success: function (result) {chart_qw.setOption(result.data);}});}" c = ( Line(init_opts=opts.InitOpts(chart_id="qw",height="200px",width="100%")) .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .add_yaxis("商家B", Faker.values()) .set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例")) .add_js_funcs(js) ) return c """ foo_code = compile(func, "<string>", "exec") print("--------------------------", foo_code.co_consts[0]) line_base = FunctionType(foo_code.co_consts[0], globals()) page = Page() page.add(line_base(), bar_base()) return HttpResponse(page.render_embed())
def member_chart(): purchase_num = [] sell_num = [] post_num = [] for m in Member.objects.filter().order_by( '-bitcoin_total_sale_amount')[:30]: sell_num.append([m.name, float(m.bitcoin_total_sale_amount)]) for m in Member.objects.filter().order_by( '-bitcoin_total_purchase_amount')[:30]: purchase_num.append([m.name, float(m.bitcoin_total_purchase_amount)]) for m in Member.objects.filter().order_by('-on_sell_number')[:30]: post_num.append([m.name, m.on_sell_number]) purchase_num_pie = Pie( init_opts=opts.InitOpts(js_host='../static/js/', width='1100px')).add( '', purchase_num).set_global_opts( title_opts=opts.TitleOpts(title="购买总额(比特币)"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="100px", type_='scroll'), ) sell_num_pie = Pie( init_opts=opts.InitOpts(js_host='../static/js/', width='1100px')).add( '', sell_num).set_global_opts( title_opts=opts.TitleOpts(title="出售总额(比特币)"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="100px", type_='scroll'), ) post_num_pie = Pie( init_opts=opts.InitOpts(js_host='../static/js/', width='1100px')).add( '', post_num).set_global_opts( title_opts=opts.TitleOpts(title="发帖数"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="100px", type_='scroll'), ) page = Page(layout=Page.SimplePageLayout, js_host='../static/js/') page.add(purchase_num_pie, sell_num_pie, post_num_pie) return page.render_embed()
def plot_all(kw): page = Page(layout=Page.SimplePageLayout) kw_map = { "BaseQuantity": [geo_1, line_smooth], "Collection": [map_visualmap], "Practitioner": [bar3, practitioner_bar], "Visitors": [bar_base_with_animation, visitor_bar2], "GDP": [GDP_line, DIC_scatter, compare_bar3], "Population": [GDP_line, DIC_scatter, compare_bar3], "BookCollection": [book_distribute_map, library_and_museum_distribute], "PublicCulturalFinance": [bar_stack0, museum_related_info_bar], "CultureSportsMediaFinance": [bar_stack0, museum_related_info_bar], "NumberOfLibraryInstitutions": [book_distribute_map, library_and_museum_distribute], "DisposableIncomePerCapita": [GDP_line, DIC_scatter, compare_bar3] } for f in kw_map[kw]: page.add(f()) return page.render_embed()
def jajj(data): page = Page(layout=Page.SimplePageLayout) map = None for i in data: title = i['title'] db = i['db'] sql = i['sql'] type = i['type'] d_soure = queryFromSql(db, sql, 0).toechart() y_bar_list = [] y_line_list = [] x_list = [] bar_line_list = [] for i in range(len(d_soure)): try: if type[i] == 0: x = d_soure[i][1:] x_list.append(x) elif type[i] == 1: y = {d_soure[i][0]: d_soure[i][1:]} y_bar_list.append(y) elif type[i] == 2: y = {d_soure[i][0]: d_soure[i][1:]} y_line_list.append(y) try: bar = bar_datazoom_slider(x_list, y_bar_list, title) except Exception as e: logging.exception(e) try: if y_line_list != []: line = line_markpoint(x_list, y_line_list, title) except Exception as e: logging.exception(e) finally: if 2 in type: map = bar.overlap(line) else: map = bar except Exception as e: logging.exception(e) page.add(map) return Markup(page.render_embed())
def country_chart(): from_num = [] to_num = [] max_num = 0 for c in Country.objects.filter(Q(to_number__gt=0) | Q(from_number__gt=0)).order_by('-to_number'): to_num.append([c.name, c.to_number]) from_num.append([c.name, c.from_number]) if c.to_number + c.from_number > max_num and c.name != 'WorldWide': max_num = c.to_number + c.from_number from_num = sorted(from_num, key=lambda d: d[1], reverse=True) from_num_pie = Pie(init_opts=opts.InitOpts(js_host='../static/js/', width='1100px') ).add('', from_num).set_global_opts( title_opts=opts.TitleOpts(title="发货地址"), legend_opts=opts.LegendOpts( orient="vertical", pos_top="15%", pos_left="100px", type_='scroll' ), ) to_num_pie = Pie(init_opts=opts.InitOpts(js_host='../static/js/', width='1100px') ).add('', to_num).set_global_opts( title_opts=opts.TitleOpts(title="收货地址"), legend_opts=opts.LegendOpts( orient="vertical", pos_top="15%", pos_left="100px", type_='scroll' ), ) c = ( Map(init_opts=opts.InitOpts(width='1100px', height='550px', js_host='../static/js/')) .add("收货地址", to_num, "world") .add("发货地址", from_num, "world") .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts( title_opts=opts.TitleOpts(title="收发货地址"), visualmap_opts=opts.VisualMapOpts(max_=max_num, pos_top='50px'), ) ) page = Page(layout=Page.SimplePageLayout, js_host='../static/js/') page.add(c, from_num_pie, to_num_pie) return page.render_embed()
def render_scheme(scheme): y = yaml.safe_load(open(f'schemata/{scheme}.yaml')) page = Page() for comp in y: data = DataSources[comp['datasource']](**comp['query']) chart_c = getattr(pyecharts.charts, comp['type']) chart = chart_c().set_global_opts( title_opts=opts.TitleOpts(comp['title']), # visualmap_opts=opts.VisualMapOpts(range_color=range_color) ) chart.add( "", [i[0] for i in data.values], xaxis3d_opts=opts.Axis3DOpts(type_="category"), yaxis3d_opts=opts.Axis3DOpts(type_="category"), zaxis3d_opts=opts.Axis3DOpts(type_="value"), ) page.add(chart) return Markup(page.render_embed())
def draw( self, classifier_result: ClassifierResult, report_path: str = None, unstable_ranges: typing.List[VideoCutRange] = None, cut_result: VideoCutResult = None, compress_rate: float = None, *_, **__, ): """ draw report file :param classifier_result: classifierResult, output of classifier :param report_path: your report will be there :param unstable_ranges: for marking unstable ranges :param cut_result: more charts would be built :param compress_rate: :return: """ # default: compress_rate if not compress_rate: compress_rate = 0.2 if not unstable_ranges: unstable_ranges = [] # draw line = self._draw_line(classifier_result) bar = self._draw_bar(classifier_result) # merge charts page = Page() page.add(line) page.add(bar) # insert pictures if cut_result: # sim chart sim_line = self._draw_sim(cut_result) page.add(sim_line) # mark range for each in unstable_ranges: classifier_result.mark_range_unstable(each.start, each.end) offset = classifier_result.get_offset() stage_range = classifier_result.get_stage_range() for cur_index in range(len(stage_range)): each = stage_range[cur_index] middle = each[len(each) // 2] if middle.is_stable(): label = self.LABEL_STABLE frame = toolbox.compress_frame(middle.get_data(), compress_rate=compress_rate) else: # todo: looks not good enough. `unspecific` looks a little weird but I have no idea now if middle.stage == constants.UNKNOWN_STAGE_FLAG: label = self.LABEL_UNSPECIFIC else: label = self.LABEL_UNSTABLE # add a frame if cur_index + 1 < len(stage_range): new_each = [*each, stage_range[cur_index + 1][0]] else: new_each = each frame = np.hstack([ toolbox.compress_frame(i.get_data(), compress_rate=compress_rate) for i in new_each ]) first, last = each[0], each[-1] self.add_thumbnail( f"{label} range {first.frame_id}({first.timestamp}) - {last.frame_id}({last.timestamp + offset}), " f"duration: {last.timestamp - first.timestamp + offset}", frame, ) # calc time cost cost_dict = classifier_result.calc_changing_cost() # time stamp timestamp = toolbox.get_timestamp_str() # video self.add_extra("video path", classifier_result.video_path) self.add_extra("frame count", str(classifier_result.get_length())) self.add_extra("offset between frames", str(classifier_result.get_offset())) # insert extras template = Template(get_template()) template_content = template.render( chart=Markup(page.render_embed()), thumbnail_list=self.thumbnail_list, extras=self.extra_dict, background_color=constants.BACKGROUND_COLOR, cost_dict=cost_dict, timestamp=timestamp, version_code=__VERSION__, ) # default: write to current dir default_name = f"{timestamp}.html" if not report_path: report_path = default_name # somewhere specific # existed dir? elif os.path.isdir(report_path): report_path = os.path.join(report_path, default_name) logger.debug(f"trying to save report to {report_path}") # write file with open(report_path, "w", encoding=constants.CHARSET) as fh: fh.write(template_content) logger.info(f"save report to {report_path}")
def draw(cls, data_list: typing.List[ClassifierResult], report_path: str = None, data_path: str = None): # draw line chart x_axis = [str(i.timestamp) for i in data_list] y_axis = [i.stage for i in data_list] line = Line() line.add_xaxis(x_axis) line.add_yaxis("stage", y_axis, is_step=True, is_symbol_show=True) line.set_global_opts( title_opts=opts.TitleOpts(title='Trend'), toolbox_opts=opts.ToolboxOpts(is_show=True), tooltip_opts=opts.TooltipOpts(is_show=True, trigger='axis', axis_pointer_type='cross'), ) # draw bar chart bar = Bar() x_axis = sorted(list(set([i.stage for i in data_list]))) bar.add_xaxis(x_axis) y_axis = list() offset = data_list[1].timestamp - data_list[0].timestamp for each_stage_name in x_axis: each_stage = sorted( [i for i in data_list if i.stage == each_stage_name], key=lambda x: x.frame_id) time_cost = each_stage[-1].timestamp - each_stage[ 0].timestamp + offset y_axis.append(time_cost) bar.add_yaxis('time cost', y_axis) bar.set_global_opts( title_opts=opts.TitleOpts(title="Time Cost"), toolbox_opts=opts.ToolboxOpts(is_show=True), ) logger.debug(f'time cost: {dict(zip(x_axis, y_axis))}') # draw pie chart pie = Pie() pie.add('', [list(z) for z in zip(x_axis, y_axis)]) pie.set_global_opts(toolbox_opts=opts.ToolboxOpts(is_show=True), ) # merge charts page = Page() page.add(line) page.add(bar) page.add(pie) # insert sample pictures' path if data_path and os.path.isdir(data_path): data_path = os.path.abspath(data_path) stage_list = [ os.path.join(data_path, i) for i in os.listdir(data_path) ] else: logger.warning(f'data path {data_path} not existed') stage_list = [] # save to file if not report_path: report_path = f'{toolbox.get_timestamp_str()}.html' template = Template(TEMPLATE) template_content = template.render( chart=Markup(page.render_embed()), pic_list=stage_list, ) with open(report_path, "w") as fh: fh.write(template_content) logger.info(f'save report to {report_path}')
def pyecharts(request): """dashboard view""" # 工单数量统计 chart_dao = ChartDao() data = chart_dao.workflow_by_date(30) today = date.today() one_month_before = today - relativedelta(days=+30) attr = chart_dao.get_date_list(one_month_before, today) _dict = {} for row in data['rows']: _dict[row[0]] = row[1] value = [_dict.get(day) if _dict.get(day) else 0 for day in attr] bar1 = Bar() bar1.add_xaxis(attr) bar1.add_yaxis("月统计", value) bar1.set_global_opts(title_opts=opts.TitleOpts(title='SQL上线工单统计(数量)'), datazoom_opts=opts.DataZoomOpts(is_show=True), toolbox_opts=opts.ToolboxOpts(is_show=True)) # 工单按组统计 data = chart_dao.workflow_by_group(30) attr = [row[0] for row in data['rows']] value = [row[1] for row in data['rows']] pie1 = Pie() pie1.set_global_opts(title_opts=opts.TitleOpts(title='SQL上线工单统计(组)'), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%")) pie1.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) pie1.add("", [list(z) for z in zip(attr, value)]) # 工单按人统计 data = chart_dao.workflow_by_user(30) attr = [row[0] for row in data['rows']] value = [row[1] for row in data['rows']] bar2 = Bar() bar2.add_xaxis(attr) bar2.add_yaxis("月统计", value) bar2.set_global_opts(title_opts=opts.TitleOpts(title='SQL上线工单统计(用户)')) # SQL语句类型统计 data = chart_dao.syntax_type() attr = [row[0] for row in data['rows']] value = [row[1] for row in data['rows']] pie2 = Pie() pie2.set_global_opts(title_opts=opts.TitleOpts(title='SQL上线工单统计(类型)'), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%")) pie2.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) pie2.add("", [list(z) for z in zip(attr, value)]) # SQL查询统计(每日检索行数) attr = chart_dao.get_date_list(one_month_before, today) effect_data = chart_dao.querylog_effect_row_by_date(30) effect_dict = {} for row in effect_data['rows']: effect_dict[row[0]] = int(row[1]) effect_value = [ effect_dict.get(day) if effect_dict.get(day) else 0 for day in attr ] count_data = chart_dao.querylog_count_by_date(30) count_dict = {} for row in count_data['rows']: count_dict[row[0]] = int(row[1]) count_value = [ count_dict.get(day) if count_dict.get(day) else 0 for day in attr ] line1 = Line() line1.set_global_opts(title_opts=opts.TitleOpts(title='SQL查询统计'), legend_opts=opts.LegendOpts(selected_mode='single')) line1.add_xaxis(attr) line1.add_yaxis("检索行数", effect_value, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(type_="average")])) line1.add_yaxis("检索次数", count_value, is_smooth=True, markline_opts=opts.MarkLineOpts(data=[ opts.MarkLineItem(type_="max"), opts.MarkLineItem(type_="average") ])) # SQL查询统计(用户检索行数) data = chart_dao.querylog_effect_row_by_user(30) attr = [row[0] for row in data['rows']] value = [int(row[1]) for row in data['rows']] pie4 = Pie() pie4.set_global_opts(title_opts=opts.TitleOpts(title='SQL查询统计(用户检索行数)'), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%")) pie4.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) pie4.add("", [list(z) for z in zip(attr, value)]) # SQL查询统计(DB检索行数) data = chart_dao.querylog_effect_row_by_db(30) attr = [row[0] for row in data['rows']] value = [int(row[1]) for row in data['rows']] pie5 = Pie() pie5.set_global_opts(title_opts=opts.TitleOpts(title='SQL查询统计(DB检索行数)'), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%")) pie5.set_series_opts( label_opts=opts.LabelOpts(formatter="{b}: {c}", position="left")) pie5.add("", [list(z) for z in zip(attr, value)]) # 可视化展示页面 page = Page(layout=Page.SimplePageLayout) page.add(bar1, pie1, bar2, pie2, line1, pie4, pie5) return render(request, "dashboard.html", {"chart": page.render_embed()})
def draw(self, data_list: typing.List[ClassifierResult], report_path: str = None, cut_result: VideoCutResult = None, *args, **kwargs): """ draw report file :param data_list: classifierResult list, output of classifier :param report_path: your report will be there :param cut_result: more charts would be built :return: """ # draw line = self._draw_line(data_list) bar = self._draw_bar(data_list) # merge charts page = Page() page.add(line) page.add(bar) # calc time cost cost_dict = DataUtils.calc_changing_cost(data_list) # insert pictures if cut_result: # sim chart sim_line = self._draw_sim(cut_result) page.add(sim_line) _, unstable = cut_result.get_range(*args, **kwargs) # insert thumbnail if not self.thumbnail_list: logger.debug('auto insert thumbnail ...') for each in unstable: self.add_thumbnail( f'{each.start}({each.start_time}) - {each.end}({each.end_time}), ' f'duration: {each.end_time - each.start_time}', cut_result.thumbnail(each, *args, **kwargs), ) # insert stable frames stable_stage_sample = self.get_stable_stage_sample(data_list, compress_rate=0.2) stable_stage_sample = toolbox.np2b64str(stable_stage_sample) # insert extras template = Template(TEMPLATE) template_content = template.render( chart=Markup(page.render_embed()), dir_link_list=self.dir_link_list, thumbnail_list=self.thumbnail_list, stable_sample=stable_stage_sample, extras=self.extra_dict, background_color=BACKGROUND_COLOR, cost_dict=cost_dict, ) # save to file if not report_path: report_path = f'{toolbox.get_timestamp_str()}.html' with open(report_path, "w") as fh: fh.write(template_content) logger.info(f'save report to {report_path}')
def draw( self, classifier_result: ClassifierResult, report_path: str = None, unstable_ranges: typing.List[VideoCutRange] = None, cut_result: VideoCutResult = None, compress_rate: float = None, *_, **__, ): """ draw report file :param classifier_result: classifierResult, output of classifier :param report_path: your report will be there :param unstable_ranges: for marking unstable ranges :param cut_result: more charts would be built :param compress_rate: :return: """ # default: compress_rate if not compress_rate: compress_rate = 0.2 if not unstable_ranges: unstable_ranges = [] # draw line = self._draw_line(classifier_result) bar = self._draw_bar(classifier_result) # merge charts page = Page() page.add(line) page.add(bar) # insert pictures if cut_result: # sim chart sim_line = self._draw_sim(cut_result) page.add(sim_line) # mark range for each in unstable_ranges: classifier_result.mark_range_unstable(each.start, each.end) offset = classifier_result.get_offset() for each in classifier_result.get_stage_range(): middle = each[len(each) // 2] if middle.is_stable(): label = "stable" frame = toolbox.compress_frame(middle.get_data(), compress_rate=compress_rate) else: label = "unstable" frame = np.hstack([ toolbox.compress_frame(i.get_data(), compress_rate=compress_rate) for i in each ]) first, last = each[0], each[-1] self.add_thumbnail( f"{label} range {first.frame_id}({first.timestamp - offset}) - {last.frame_id}({last.timestamp}), " f"duration: {last.timestamp - first.timestamp + offset}", frame, ) # calc time cost cost_dict = classifier_result.calc_changing_cost() # time stamp timestamp = toolbox.get_timestamp_str() # video self.add_extra("video path", classifier_result.video_path) self.add_extra("frame count", str(classifier_result.get_length())) self.add_extra("offset between frames", str(classifier_result.get_offset())) # insert extras template = Template(get_template()) template_content = template.render( chart=Markup(page.render_embed()), thumbnail_list=self.thumbnail_list, extras=self.extra_dict, background_color=constants.BACKGROUND_COLOR, cost_dict=cost_dict, timestamp=timestamp, version_code=__VERSION__, ) # save to file if not report_path: report_path = f"{timestamp}.html" with open(report_path, "w", encoding=constants.CHARSET) as fh: fh.write(template_content) logger.info(f"save report to {report_path}")
def output_charts(request): url = request.META.get('HTTP_REFERER', "") if url == "": return render(request, 'error_400.html', status=400) url_params = parse_url_param(url) queryset = get_queryset(url_params, 'OutputRecord') queryset = queryset.filter(verified=True) if queryset.count() == 0: messages.error(request, '无数据') return redirect(url) # summary_line date_range = list(queryset.values_list('date', flat=True).order_by('date')) start_date, end_date = date_range[0], date_range[-1] date_list = make_date_list(start_date, end_date) data = pd.DataFrame(date_list, columns=['日期']) data_db = pd.DataFrame( queryset.values('user__last_name', 'user__first_name', 'output__name', 'weight_quantity', 'date')) data_db['name'] = data_db['user__last_name'] + data_db['user__first_name'] data_db = data_db[['name', 'output__name', 'weight_quantity', 'date']] data_db = data_db.rename( columns={ 'name': '姓名', 'output__name': '产出名称', 'weight_quantity': '数额', 'date': '日期' }) data_db['日期'] = data_db['日期'].apply(lambda x: x.strftime('%Y-%m-%d')) data = pd.merge(data, data_db, on='日期', how='left') data = pd.pivot_table(data, values=['数额'], index=['日期'], columns=['产出名称'], aggfunc=np.sum) data = data.fillna(0) _, second_list = zip(*data.columns.tolist()) second_list = set(second_list) line = Line(init_opts=opts.InitOpts()).set_global_opts( title_opts=opts.TitleOpts(title='产出趋势', subtitle='总体')) line.add_xaxis(data.index.values.tolist()) for value in second_list: line.add_yaxis(value, data["数额"][value], symbol_size=10, is_smooth=True) output_summary_line = (line) # summary_bar data = pd.DataFrame( queryset.values('user__last_name', 'user__first_name', 'output__name', 'weight_quantity')) data['name'] = data['user__last_name'] + data['user__first_name'] data = data[['name', 'output__name', 'weight_quantity']] data = data.rename(columns={ 'name': '姓名', 'output__name': '产出名称', 'weight_quantity': '数额' }) data = pd.pivot_table(data, values=['数额'], index=['产出名称'], aggfunc=np.sum) bar = Bar(init_opts=opts.InitOpts()).set_global_opts( title_opts=opts.TitleOpts(title='产出数额统计', subtitle="总体")) bar.add_xaxis(data.index.values.tolist()) bar.add_yaxis('产出名称', data['数额'].values.tolist()) output_summary_bar = (bar) # summary_bar data = pd.DataFrame( queryset.values('user__last_name', 'user__first_name', 'output__name', 'weight_quantity')) data['name'] = data['user__last_name'] + data['user__first_name'] data = data[['name', 'output__name', 'weight_quantity']] data = data.rename(columns={ 'name': '姓名', 'output__name': '产出名称', 'weight_quantity': '数额' }) data = pd.pivot_table(data, values=['数额'], index=['姓名', '产出名称'], aggfunc=np.sum) first_list, second_list = zip(*data.index.values) first_list = set(first_list) bar = Bar(init_opts=opts.InitOpts()).set_global_opts( title_opts=opts.TitleOpts(title='产出数额统计', subtitle='按姓名')) bar.add_xaxis(second_list) for value in first_list: bar.add_yaxis(value, data.loc[value]['数额'].values.tolist()) output_summary_by_name_bar = (bar) page = Page(layout=Page.SimplePageLayout, page_title='产出统计') page.add(output_summary_line) page.add(output_summary_bar) page.add(output_summary_by_name_bar) return HttpResponse(page.render_embed())
def reward_charts(request): url = request.META.get("HTTP_REFERER", "") if url == "": return render(request, "error_400.html", status=400) url_params = parse_url_param(url) queryset = get_queryset(url_params, 'RewardRecord') if queryset.count() == 0: messages.error(request, "无数据") return redirect(url) # summary_line date_range = list(queryset.values_list('date', flat=True).order_by('date')) start_date, end_date = date_range[0], date_range[-1] date_list = make_date_list(start_date, end_date) data = pd.DataFrame(date_list, columns=['日期']) data_db = pd.DataFrame( queryset.values('user__last_name', 'user__first_name', 'reward__name', 'date')) data_db['name'] = data_db['user__last_name'] + data_db['user__first_name'] data_db = data_db[['name', 'reward__name', 'date']] data_db = data_db.rename(columns={ 'name': '姓名', 'reward__name': '奖惩名称', 'date': '日期' }) data_db['日期'] = data_db['日期'].apply(lambda x: x.strftime('%Y-%m-%d')) data = pd.merge(data, data_db, on='日期', how='left') data['次数'] = data['奖惩名称'] data = pd.pivot_table(data, values=['次数'], index=['日期'], columns=['奖惩名称'], aggfunc=np.count_nonzero) data = data.fillna(0) _, second_list = zip(*data.columns.tolist()) second_list = set(second_list) line = Line(init_opts=opts.InitOpts()).set_global_opts( title_opts=opts.TitleOpts(title='奖惩趋势', subtitle='总体')) line.add_xaxis(data.index.values.tolist()) for value in second_list: line.add_yaxis(value, data['次数'][value], symbol_size=10, is_smooth=True) reward_summary_line = (line) # summary_bar data = pd.DataFrame( queryset.values('user__last_name', 'user__first_name', 'reward__name')) data['name'] = data['user__last_name'] + data['user__first_name'] data = data[['name', 'reward__name']] data = data.rename(columns={'name': '姓名', 'reward__name': '奖惩名称'}) data['次数'] = data['姓名'] data = pd.pivot_table(data, values=['次数'], index=['奖惩名称'], aggfunc=np.count_nonzero) bar = Bar(init_opts=opts.InitOpts()).set_global_opts( title_opts=opts.TitleOpts(title='奖惩统计', subtitle='总体')) bar.add_xaxis(data.index.values.tolist()) bar.add_yaxis('奖惩名称', data['次数'].values.tolist()) reward_summary_bar = (bar) # summary_bar data = pd.DataFrame( queryset.values('user__last_name', 'user__first_name', 'reward__name')) data['name'] = data['user__last_name'] + data['user__first_name'] data = data[['name', 'reward__name']] data = data.rename(columns={'name': '姓名', 'reward__name': '奖惩名称'}) data['次数'] = data['姓名'] data = pd.pivot_table(data, values=['次数'], index=['姓名', '奖惩名称'], aggfunc=np.count_nonzero) first_list, second_list = zip(*data.index.values) first_list = set(first_list) bar = Bar(init_opts=opts.InitOpts()).set_global_opts( title_opts=opts.TitleOpts(title="奖惩统计", subtitle="按姓名")) bar.add_xaxis(second_list) for value in first_list: bar.add_yaxis(value, data.loc[value]['次数'].values.tolist()) reward_summary_by_name_bar = (bar) page = Page(layout=Page.SimplePageLayout, page_title='奖惩统计') page.add(reward_summary_line) page.add(reward_summary_bar) page.add(reward_summary_by_name_bar) return HttpResponse(page.render_embed())
def show_host_monitor_data(request, ip): from monitor.urls import urlpatterns from django.urls import re_path monitor_item_set = MonitorItem.objects.filter( template_host__host__ip=ip).distinct() page = Page() for monitor_item_obj in monitor_item_set: # 监控项的名称 name = monitor_item_obj.name item_table_name = get_table_name(name) # 数据库中存的是秒,前端是定时任务中的单位是毫秒,所以要乘以1000 time_interval = int(monitor_item_obj.time_interval) * 1000 conn = get_pymysql_conn() # 游标 # cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 测试的时候,使用该语句, select_sql = "select * from %s where to_days(get_data_time) <= to_days(now()); " % item_table_name # 实际应用时使用的语句 # 显示当日的数据 # select * from monitor_item_cpu_d9747e2da3 where year(get_data_time)=year(now()) and month(get_data_time)=month(now()) and day(get_data_time)=day(now()) cursor.execute(select_sql) # 如果表存在则删除 todays_data_rows = cursor.fetchall() cursor.close() conn.close() # ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] item_xaxis = [] # {"load1":[1,2,3,4,5,6,7,8]} item_yaxis = {} for todays_data_row in todays_data_rows: # ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] item_xaxis.append( todays_data_row.get("get_data_time").strftime("%X")) # load1:10_load2:23_load3:34 monitor_item_data = todays_data_row.get("data") monitor_item_data_list = monitor_item_data.split( "_") # ['load1:10', 'load2:23', 'load3:34'] for per_item_data in monitor_item_data_list: per_item_data_list = per_item_data.split(":") item_name = per_item_data_list[0] # load1 data = per_item_data_list[1].strip("\n") if item_name not in item_yaxis: item_yaxis[item_name] = [data] else: item_yaxis[item_name].append(data) my_add_xaxis = '.add_xaxis(%s)' % item_xaxis my_add_yaxis = '' for item_y_key, item_y_value in item_yaxis.items(): my_add_yaxis += '.add_yaxis("%s", %s)' % (item_y_key, item_y_value) # 这里是异步获取数据的类 # 类名 class_name = '%sView' % name # 类的父类 class_bases = (View, ) # 类体 # 注意,这里还非要前面不能有空格 class_body = """ def get(self, request, *args, **kwargs): c = ( Line() %s %s .set_global_opts(title_opts=opts.TitleOpts(title="%s", subtitle="各项数据"), datazoom_opts=opts.DataZoomOpts()) .dump_options() ) return MyJsonResponse(json.loads(c)) """ % (my_add_xaxis, my_add_yaxis, name) class_dic = {} exec(class_body, globals(), class_dic) print("class_dic===", class_dic) # 步骤二:调用元类type(也可以自定义)来产生类Chinense # ViewClasss = type(class_name, class_bases, class_dic) # 实例化type得到对象Foo,即我们用class定义的类Foo urlpatterns.append( re_path('%s/' % name, type(class_name, class_bases, class_dic).as_view())) # # ->常常出现在python函数定义的函数名后面,为函数添加元数据,描述函数的返回类型,从而方便开发人员使用。 # def json_line_base() -> Line: # 这里不能使用-> # js = """ # $(function () # {get_bar_Data(chart_%s); # setInterval(get_bar_Data, 2000);} # ); # function get_bar_Data() { # $.ajax({ # type: 'GET', # url: 'http://10.0.0.61:8000/monitor/%s', # success: function (result) # {chart_%s.setOption(result.data);}}); # } # """ % (name, name, name) # js = "console.log('hello world')" # 由于有{},所以就不用.format了,只能这样了 # 不能用上面的方式,报错 # 这里是动态添加到前端的js,动态定时获取显示的数据 js = "$(function () {getData_%s(chart_%s);setInterval(getData_%s, %s);});" \ "function getData_%s() {" \ "$.ajax({" \ "type: 'GET'," \ "url: 'http://10.0.0.61:8000/monitor/%s'," \ "success: function (result) " \ "{chart_%s.setOption(result.data);}});}" % (name, name, name, time_interval, name, name, name) # 最初的函数,为前端添加js,初始数据 func = """ def line_base(): js = "{line_js}" c = ( Line(init_opts=opts.InitOpts(chart_id='{name}',height="200px",width="100%")) {my_add_xaxis} {my_add_yaxis} .set_global_opts(title_opts=opts.TitleOpts(title="{name}"), datazoom_opts=opts.DataZoomOpts()) .add_js_funcs(js) ) return c """.format(name=name, my_add_xaxis=my_add_xaxis, my_add_yaxis=my_add_yaxis, line_js=js) foo_code = compile(func, "<string>", "exec") line_base = FunctionType(foo_code.co_consts[0], globals()) page.add(line_base()) return HttpResponse(page.render_embed())
def draws(request): start = request.GET.get('day') need_cache = False if not start: start = datetime.date.today() else: try: if start in get_cache_html(): return render(request, 'caches/%s.html' % start) need_cache = True start = datetime.datetime.strptime(start, '%Y-%m-%d') except Exception as msg: start = datetime.date.today() end = start + datetime.timedelta(days=1) power_sql = 'SELECT DATE_FORMAT(time, "%%Y-%%m-%%d %%H:%%i:%%S") as time, voltage, current, rate, consumption from tbl_power where time>=%s and time<%s order by time' temp_sql = 'SELECT DATE_FORMAT(time, "%%Y-%%m-%%d %%H:%%i:%%S") as time, code, value FROM tbl_temperature where time>=%s AND time<%s order by code, time' action_sql = 'SELECT DATE_FORMAT(time, "%%Y-%%m-%%d %%H:%%i:%%S") as time, yaw, pitch, roll, acc_x, acc_y, acc_z from tbl_door where time>=%s and time<%s order by time' with connection.cursor() as c: c.execute(power_sql, (start, end)) power_rows = c.fetchall() c.execute(temp_sql, (start, end)) temp_rows = c.fetchall() c.execute(action_sql, (start, end)) action_rows = c.fetchall() time_list, voltage_list, current_list, rate_list, consumption_list = [ [] for _ in range(5) ] for time, voltage, current, rate, consumption in power_rows: time_list.append(time) voltage_list.append(voltage) current_list.append(current) rate_list.append(rate) consumption_list.append(consumption) power_line = (Line( init_opts=opts.InitOpts(bg_color="rgba(255, 250, 205, 0.2)", width="2400px", height="400px", theme=opts.global_options.ThemeType.MACARONS) ).add_xaxis(time_list).add_yaxis("电压", voltage_list).add_yaxis( "电流", current_list).add_yaxis("功率", rate_list).add_yaxis( "累计耗电量", consumption_list).set_global_opts( title_opts=opts.TitleOpts(title="耗电信息"), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), legend_opts=opts.LegendOpts(item_width=60, item_height=30, selected_mode="multiple"), yaxis_opts=opts.AxisOpts( is_scale=True, splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)), ), datazoom_opts=opts.DataZoomOpts(is_show=True, range_start=0, range_end=100), toolbox_opts=opts.ToolboxOpts(is_show=True))) time_list, lines = [], [] for key, content in groupby(temp_rows, key=lambda x: x[1]): if not time_list: values = [] for time, _, value in content: time_list.append(time) values.append(value) else: values = [value for _, _, value in content] name = layer_map.get(key) if name: lines.append((name, values)) temp_line = Line( init_opts=opts.InitOpts(bg_color="rgba(255, 250, 205, 0.2)", width="2400px", height="400px", theme=opts.global_options.ThemeType.WHITE)) temp_line.add_xaxis(time_list) for name, content in lines: temp_line.add_yaxis(name, content) temp_line.set_global_opts( title_opts=opts.TitleOpts(title="温度信息"), tooltip_opts=opts.TooltipOpts( trigger="axis", axis_pointer_type="cross", ), legend_opts=opts.LegendOpts(item_width=60, item_height=30, selected_mode="multiple"), yaxis_opts=opts.AxisOpts( is_scale=True, splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)), ), datazoom_opts=opts.DataZoomOpts(is_show=True, range_start=0, range_end=100), toolbox_opts=opts.ToolboxOpts(is_show=True)) time_list, yaw_list, pitch_list, roll_list, accx_list, accy_list, accz_list = [ [] for _ in range(7) ] for time, yaw, pitch, roll, acc_x, acc_y, acc_z in action_rows: time_list.append(time) yaw_list.append(yaw) pitch_list.append(pitch) roll_list.append(roll) accx_list.append(acc_x) accy_list.append(acc_y) accz_list.append(acc_z) action_line = ( Line(init_opts=opts.InitOpts( bg_color="rgba(255, 250, 205, 0.2)", width="2400px", height="500px", )).add_xaxis(time_list).add_yaxis("yaw", yaw_list).add_yaxis( "pitch", pitch_list).add_yaxis("roll", roll_list). add_yaxis("acc_x", accx_list).add_yaxis("acc_y", accy_list).add_yaxis( "acc_z", accz_list).set_global_opts( # title_opts=opts.TitleOpts(title="门的开关信息", pos_top="100%"), title_opts=opts.TitleOpts(title="门的开关信息"), tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), legend_opts=opts.LegendOpts(item_width=60, item_height=30, selected_mode="multiple"), yaxis_opts=opts.AxisOpts( is_scale=True, splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)), ), datazoom_opts=opts.DataZoomOpts(is_show=True, range_start=0, range_end=100), toolbox_opts=opts.ToolboxOpts(is_show=True))) # grid = ( # Grid(init_opts=opts.InitOpts(width="2600px", height="1200px")) # .add(power_line, grid_opts=opts.GridOpts(pos_bottom="60%")) # .add(temp_line, grid_opts=opts.GridOpts(pos_top="60%")) # # .add(action_line, grid_opts=opts.GridOpts(pos_top="80%")) # ) from pyecharts.charts import Page grid = Page(layout=Page.SimplePageLayout) grid.add( power_line, temp_line, action_line, ) if need_cache: path = os.path.join(settings.BASE_DIR, 'data', 'templates', 'caches', '%s.html' % start.strftime('%Y-%m-%d')) grid.render(path) return HttpResponse(grid.render_embed())
def draw( self, data_list: typing.List[ClassifierResult], report_path: str = None, cut_result: VideoCutResult = None, language: str = None, *args, **kwargs, ): """ draw report file :param data_list: classifierResult list, output of classifier :param report_path: your report will be there :param cut_result: more charts would be built :param language: 'en' or 'zh' :return: """ # draw line = self._draw_line(data_list) bar = self._draw_bar(data_list) # merge charts page = Page() page.add(line) page.add(bar) # calc time cost cost_dict = DataUtils.calc_changing_cost(data_list) # insert pictures if cut_result: # sim chart sim_line = self._draw_sim(cut_result) page.add(sim_line) _, unstable = cut_result.get_range(*args, **kwargs) # insert thumbnail if not self.thumbnail_list: logger.debug("auto insert thumbnail ...") for each in unstable: self.add_thumbnail( f"{each.start}({each.start_time}) - {each.end}({each.end_time}), " f"duration: {each.end_time - each.start_time}", cut_result.thumbnail(each, *args, **kwargs), ) # insert stable frames stable_stage_sample = self.get_stable_stage_sample(data_list, compress_rate=0.2) stable_stage_sample = toolbox.np2b64str(stable_stage_sample) # time stamp timestamp = toolbox.get_timestamp_str() # insert extras # default: zh_cn report if not language: language = "zh" template = Template(get_template(language)) template_content = template.render( chart=Markup(page.render_embed()), dir_link_list=self.dir_link_list, thumbnail_list=self.thumbnail_list, stable_sample=stable_stage_sample, extras=self.extra_dict, background_color=BACKGROUND_COLOR, cost_dict=cost_dict, timestamp=timestamp, version_code=__VERSION__, ) # save to file if not report_path: report_path = f"{timestamp}.html" with open(report_path, "w", encoding=constants.CHARSET) as fh: fh.write(template_content) logger.info(f"save report to {report_path}")
def classification_chart(): # 各类别商品数量分析使用饼图显示 xaxis = [ '帖子数', '交易量', '成交额(美元)', ] bar = (Bar(init_opts=opts.InitOpts(js_host='../static/js/', width='1100px')).add_xaxis(xaxis)) pie_list = [[], [], []] for c in Classification.objects.all(): # yaxis_values:'商品数量','已售商品数量','销售额()' yaxis_values = [0, 0, 0] posts = Post.objects.filter(classification=c) for post in posts: yaxis_values[0] += 1 yaxis_values[1] += post.trade_number yaxis_values[2] += post.trade_number * post.dollar_price pie_list[0].append([c.name, yaxis_values[0]]) pie_list[1].append([c.name, yaxis_values[1]]) pie_list[2].append([c.name, yaxis_values[2]]) bar.add_yaxis(c.name, yaxis_values) pie0 = Pie( init_opts=opts.InitOpts(js_host='../static/js/', width='1100px')).add( '', pie_list[0], radius=["40%", "75%"], ).set_global_opts( title_opts=opts.TitleOpts(title="各类别帖子数饼图"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="100px"), ) pie1 = Pie( init_opts=opts.InitOpts(js_host='../static/js/', width='1100px')).add( '', pie_list[1]).set_global_opts( title_opts=opts.TitleOpts(title="各类别交易量饼图"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="100px"), ) pie2 = Pie( init_opts=opts.InitOpts(js_host='../static/js/', width='1100px')).add( '', pie_list[2]).set_global_opts( title_opts=opts.TitleOpts(title="各类别成交额(美元)饼图"), legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="100px"), ) bar.set_global_opts( # toolbox_opts=opts.ToolboxOpts(pos_top='25px', pos_left='0px'), legend_opts=opts.LegendOpts(), datazoom_opts=opts.DataZoomOpts(orient='vertical', range_start=0, range_end=100), ).set_series_opts(label_opts=opts.LabelOpts(position='top', horizontal_align='right', vertical_align='middle', rotate=-90), ) page = Page(layout=Page.SimplePageLayout, js_host='../static/js/') page.add(bar, pie0, pie1, pie2) return page.render_embed()