def create_result_chart(data): instrulist = list(set(d.instrument for d in data)) # x = ["{} P{}".format(localtime(d.upload_time).strftime("%Y-%m-%d %X"), d.platenum) for d in data] x = ["{} P{}".format(d.upload_time.strftime("%Y-%m-%d %X"), d.platenum) for d in data] # 均值直方图 b = Bar(init_opts=opts.InitOpts(width='1900px')) b.add_xaxis(x) for instru in instrulist: mean = get_mean(data, instru) b.add_yaxis(instru, mean) b.set_global_opts(title_opts=opts.TitleOpts(title="结果mean图")) # sd折线图 l = Line(init_opts=opts.InitOpts(width='1900px')) l.add_xaxis(x) for instru in instrulist: sd = get_sd(data, instru) l.add_yaxis(instru, sd, is_connect_nones=True) l.set_global_opts(title_opts=opts.TitleOpts(title="结果sd图", pos_top='48%')) grid = Grid(init_opts=opts.InitOpts(width='1900px')) grid.add(b, grid_opts=opts.GridOpts(pos_bottom='60%')) grid.add(l, grid_opts=opts.GridOpts(pos_top='60%')) return grid
def v_techindex(self, end=yesterdayobj(), col=None, rendered=True, vopts=None): """ visualization on netvalue curve and specified indicators :param end: date string or obj, the end date of the figure :param col: list, list of strings for price col name, eg.['MA5','BBI'] remember generate these indicators before the visualization, these cols don't automatically generate for visualization :param vopts: dict, options for pyecharts instead of builtin settings """ partprice = self.price[self.price["date"] <= end] xdata = [d.date() for d in list(partprice.date)] netvaldata = list(partprice.netvalue) if vopts is None: vopts = line_opts line = Line() line.add_xaxis(xdata) line.add_yaxis(series_name="netvalue", y_axis=netvaldata, is_symbol_show=False) line.set_global_opts(**vopts) if col is not None: for ind in col: inddata = list(partprice[ind]) line.add_yaxis(series_name=ind, y_axis=inddata, is_symbol_show=False) if rendered: return line.render_notebook() else: return line
def v_tradecost(self, start=None, end=yesterdayobj(), vopts=None): """ visualization giving the average cost line together with netvalue line :param vopts: global option for line in pyecharts :returns: pyecharts.line """ funddata = [] costdata = [] pprice = self.aim.price[self.aim.price["date"] <= end] if start is not None: pprice = pprice[pprice["date"] >= start] for _, row in pprice.iterrows(): date = row["date"] funddata.append(row["netvalue"]) cost = 0 if (date - self.cftable.iloc[0].date).days >= 0: cost = self.unitcost(date) costdata.append(cost) line = Line() if vopts is None: vopts = line_opts line.add_xaxis([d.date() for d in pprice.date]) line.add_yaxis(series_name="基金净值", y_axis=funddata, is_symbol_show=False) line.add_yaxis(series_name="持仓成本", y_axis=costdata, is_symbol_show=False) line.set_global_opts(**vopts) return line.render_notebook()
def box_render_air(self): c = Line() c.add_xaxis(self.x_value) c.add_yaxis(series_name=self.y_name, y_axis=self.y_value, is_symbol_show=self.is_symbol_show, linestyle_opts=opts.LineStyleOpts(width=self.line_width), label_opts=opts.LabelOpts(is_show=self.is_show)) c.set_global_opts( title_opts=opts.TitleOpts(title=self.title, subtitle=self.subtitle), tooltip_opts=opts.TooltipOpts(trigger="axis"), legend_opts=opts.LegendOpts(pos_left="center", pos_top=self.pos_top, legend_icon=self.legend_icon), datazoom_opts=opts.DataZoomOpts( is_show=self.is_zoom, range_start=0, range_end=100, orient=self.is_orient # 表示横轴可滑动还是纵轴可滑动; ), yaxis_opts=opts.AxisOpts( type_="value", max_=self.y_max, min_=self.y_min, boundary_gap=self.boundary_gap, # 封闭坐标轴,左右都有顶上的刻度线; axislabel_opts=opts.LabelOpts(color="#7D7D7D"), axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts(width=1.5, color="#A0A7B3")), axistick_opts=opts.AxisTickOpts(is_show=True, is_inside=True), splitline_opts=opts.SplitLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts( color="#E2E2E2")), # 设置网格线; ), xaxis_opts=opts.AxisOpts( type_="category", min_=self.x_min, boundary_gap=self.boundary_gap, axislabel_opts=opts.LabelOpts(color="#7D7D7D"), axisline_opts=opts.AxisLineOpts( linestyle_opts=opts.LineStyleOpts(width=1.5, color="#A0A7B3")), axistick_opts=opts.AxisTickOpts(is_show=True, is_inside=True), splitline_opts=opts.SplitLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(color="#E2E2E2")), ), ) if len(self.args) > 0: for num in range(len(self.args[0])): c.add_yaxis( series_name=self.args[0][num], y_axis=self.args[1][num], is_symbol_show=self.is_symbol_show, linestyle_opts=opts.LineStyleOpts(width=self.line_width), label_opts=opts.LabelOpts(is_show=self.is_show)) self.c_render = c return c
def stat_area_trend(): df = load_data() df['发证月份'] = [i[:7] for i in df['发证日期']] summary = df.groupby('发证月份').sum() columns_area = ['预售建筑面积', '总建筑面积', '住宅面积', '办公面积', '其他面积', '车库面积'] columns_other = ['层数', '住宅套数'] chart = Line() chart.add_xaxis(summary.index.tolist()) for column in columns_area: chart.add_yaxis(column, summary[column].tolist(), areastyle_opts=opts.AreaStyleOpts(opacity=0.1), is_smooth=True) chart.set_series_opts(label_opts=opts.LabelOpts( is_show=False), ).extend_axis(yaxis=opts.AxisOpts( axislabel_opts=opts.LabelOpts(is_show=False), is_show=False)) bar = Bar() bar.add_xaxis(summary.index.tolist()) for column in columns_other: bar.add_yaxis(column, summary[column].tolist(), yaxis_index=1) chart.overlap(bar) min_date = min(df['发证日期']) max_date = max(df['发证日期']) chart.set_global_opts(title_opts=opts.TitleOpts( title="长沙预售房屋面积变化趋势", subtitle="统计日期:{}至{},数据来源:{}".format(min_date, max_date, DATA_SOURCE)), legend_opts=opts.LegendOpts(is_show=True), toolbox_opts=opts.ToolboxOpts(is_show=True)) filename = os.path.join('..', 'assets', '01_changsha_zhufangyushou_面积趋势.html') chart.render(filename)
def draw_balance_line(xaxis, yaxis, title="消费统计", markline=None, width=2000) -> Line: """ x = [月_日, 月_日, 月_日, ....] y = [(title1, [num1, num2, num3, num4, ...]), (title2, [num1, num2, num3, num4, ...])] :param xaxis: x轴 :param yaxis: y轴 :param title: 标题 :param markline: 标记辅助线 :param width: 宽 :return: Line """ line = Line() line.add_xaxis(xaxis) for name, axis in yaxis: line.add_yaxis(name, axis, is_symbol_show=True) line.set_global_opts(title_opts=opts.TitleOpts(title=title, ), datazoom_opts=[ opts.DataZoomOpts(range_start=0, range_end=100), opts.DataZoomOpts(type_="inside") ], tooltip_opts=opts.TooltipOpts( trigger='axis', axis_pointer_type='shadow')) line.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) if markline is not None: line.set_series_opts(markline_opts=opts.MarkLineOpts( data=[opts.MarkLineItem(y=markline, name='预算')])) return line
def line(self, data, xaxis=None, position=0): if not xaxis: xaxis = self.xaxis line = Line(init_opts=opts.InitOpts(**self.chart_opt)).add_xaxis(xaxis_data=xaxis) for d in data: line.add_yaxis( series_name=d["label"], y_axis=d["data"], is_smooth=True, is_selected=True, is_symbol_show=False, linestyle_opts=opts.LineStyleOpts(opacity=1), label_opts=opts.LabelOpts(is_show=False), ) line.set_global_opts( xaxis_opts=opts.AxisOpts( type_="category", grid_index=position, is_scale=True, axislabel_opts=opts.LabelOpts(is_show=False), ), yaxis_opts=opts.AxisOpts( is_scale=True, axistick_opts=opts.AxisTickOpts(is_show=False), axislabel_opts=opts.LabelOpts(is_show=False), splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1) ), ), ) return line
def line_markpoint_custom(): x, y = Faker.choose(), Faker.values() obj_l = Line() obj_l.add_xaxis(x) obj_l.add_yaxis("商家A", y, markpoint_opts=opts.MarkPointOpts( data=[opts.MarkPointItem(name="自定义标记点", coord=[x[2], y[2]], value=y[2])] ) ) obj_l.set_series_opts( # 设置该参数,则x对应的y值会显示出来,该参数默认为True label_opts=opts.LabelOpts(is_show=False), ) obj_l.set_global_opts( title_opts=opts.TitleOpts(title="Line-MarkPoint(自定义)",subtitle="自定义标注"), xaxis_opts=opts.AxisOpts( name="x轴", # 坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样,可设置x轴刻度顶格 boundary_gap=True, # 类目轴中在 boundaryGap 为 true 的时候有效,可以保证刻度线和标签对齐。 axistick_opts=opts.AxisTickOpts(is_align_with_label=True) ), ) return obj_l
def line_markline(): obj_l = Line() obj_l.add_xaxis(Faker.choose()) obj_l.add_yaxis("商家A", Faker.values(), markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]), # 该参数设置在这里只对商家A起作用 label_opts=opts.LabelOpts(is_show=False) ) obj_l.add_yaxis("商家B", Faker.values(), markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]), ) obj_l.set_series_opts( # 该参数设置在这里对商家A,商家B都起作用 # 设置该参数,则x对应的y值会显示出来,该参数默认为True label_opts=opts.LabelOpts(is_show=False), ) obj_l.set_global_opts( title_opts=opts.TitleOpts(title="Line-MarkLine", subtitle="平均值的线"), xaxis_opts=opts.AxisOpts( name="x轴", # 坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样,可设置x轴刻度顶格 boundary_gap=True, # 类目轴中在 boundaryGap 为 true 的时候有效,可以保证刻度线和标签对齐。 axistick_opts=opts.AxisTickOpts(is_align_with_label=True) ), ) return obj_l
def gen_atr(df1, n): high_list = df1.apply(lambda record: float(record['high']), axis=1).tolist() high_list = np.array(high_list) low_list = df1.apply(lambda record: float(record['low']), axis=1).tolist() low_list = np.array(low_list) close_list = df1.apply(lambda record: float(record['close']), axis=1).tolist() close_list = np.array(close_list) atr_1 = talib.ATR(high_list, low_list, close_list, 1) atr_n = talib.SMA(atr_1, n) line = Line() line.add_xaxis(xaxis_data=list(df1['datetime'])) line.add_yaxis( 'atr_' + str(n), y_axis=atr_n, xaxis_index=1, yaxis_index=1, label_opts=opts.LabelOpts(is_show=False), ) line.set_global_opts(xaxis_opts=opts.AxisOpts(is_show=False), legend_opts=opts.LegendOpts(is_show=True, pos_right="35%")) return line
def commentChannel(data, time): line = Line() temp = list(arrangeByTime(data).get(time).keys()) line.add_xaxis(temp) data = arrangeByChannel(data) # 按频道分 news = dict() for i in data.keys(): news[i] = arrangeByTime(data.get(i)).get(time) comment_channel = dict() for i in news.keys(): comment_channel[i] = dict() for j in news[i].keys(): comment_channel[i][j] = divideComment(news[i].get(j)) for i in comment_channel.keys(): y = [] for j in temp: if j in comment_channel.get(i).keys(): y.append(len(comment_channel.get(i).get(j))) else: y.append(None) if i not in ['js', 'jilin', 'video', 'live']: line.add_yaxis(tran(i), y, is_connect_nones=True, is_symbol_show=(time == 'monthly')) # line.render("NewPage/无权每月评论.html") line.set_global_opts(title_opts=options.TitleOpts( title=titleTransform("新浪新闻评论" + str(time) + "评论数量"), pos_top='55')) return line
def gen_er(df1, n): dt_list = list(df1['datetime']) close_list = df1.apply(lambda record: float(record['close']), axis=1).tolist() close_list = np.array(close_list) r = [np.nan] * (n - 1) size = len(close_list) for i in range(n, size + 1): c_list = close_list[:i] r.append(er(c_list, n)) line = Line() line.add_xaxis(xaxis_data=dt_list) line.add_yaxis( 'er_' + str(n), y_axis=r, xaxis_index=2, yaxis_index=2, label_opts=opts.LabelOpts(is_show=False), ) line.set_global_opts(xaxis_opts=opts.AxisOpts(is_show=False), legend_opts=opts.LegendOpts(is_show=True, pos_right="30%")) return line
def Line_charts(users, xaxis_data, yaxis_data, date) -> Line: """定义一个Line_charts函数""" c = Line() c.add_xaxis(xaxis_data=xaxis_data) # 设置图例信息 c.add_yaxis(series_name=str(users) + "users" + " " + "date: " + date, y_axis=yaxis_data, is_smooth=True) # c.add_yaxis(series_name='400users', y_axis=y2) data_zoom = { "show": False, "title": { "zoom": "data zoom", "back": "data zoom restore" } } # 数据项设置,全局只设置一次 c.set_global_opts( # 设置标题 title_opts=opts.TitleOpts(title="不同users对应的TPM值"), # 设置图例is_show=False是 不显示图例 legend_opts=opts.LegendOpts(is_show=True), # 设置提示项 tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='cross'), # 工具箱的设置 toolbox_opts=opts.ToolboxOpts( is_show=False, feature=opts.ToolBoxFeatureOpts(data_zoom=data_zoom))) return c
def plot_multiple_line(x_axis, y_axis_dict, title, double_ylabel_str, double_ylabel=False, ylabel_smooth=False, is_show=True): # 由于全部展示图例时数字太多影响效果,is_show用于控制是否展示或者都不展示 line = Line() line.add_xaxis(x_axis) for _name in y_axis_dict.keys(): if _name != double_ylabel_str: line.add_yaxis(_name, y_axis_dict[_name], label_opts=opts.LabelOpts(is_show=is_show)) line.set_global_opts(title_opts=opts.TitleOpts(title=title, pos_top=20), toolbox_opts=opts.ToolboxOpts(pos_top=30), legend_opts=opts.LegendOpts(pos_bottom=0, orient="horizontal")) if double_ylabel: line.extend_axis(yaxis=opts.AxisOpts( name=double_ylabel_str, axislabel_opts=opts.LabelOpts(interval=5))) line.add_yaxis(double_ylabel_str, y_axis_dict[double_ylabel_str], yaxis_index=1, is_smooth=ylabel_smooth) return line
def drawline(arrt, value, name): # 图表初始化配置 init_opts = opts.InitOpts(page_title=name) line = Line(init_opts=init_opts) # 标题配置 title = opts.TitleOpts(title=name, pos_left="10%") # 图例配置 legend_opts = opts.LegendOpts(orient="horizontal", pos_top="5%", pos_right="15%") # 工具箱配置 # feature = opts.ToolBoxFeatureOpts(save_as_image=True, restore=True, data_view=True, data_zoom=True) # 工具箱配置 toolbox_opts = opts.ToolboxOpts( orient="vertical", pos_bottom="15%", pos_left="90%", ) line.set_global_opts( title_opts=title, legend_opts=legend_opts, toolbox_opts=toolbox_opts, datazoom_opts=opts.DataZoomOpts(orient="vertical"), ) line.add_xaxis(arrt, ) line.add_yaxis(name, value, is_smooth=True, linestyle_opts=opts.LineStyleOpts(color="#E83132", width="4")) line.render('{0}.html'.format(name))
def line_areastyle(): obj_l = Line() obj_l.add_xaxis(Faker.choose()) obj_l.add_yaxis("商家A", Faker.values(), areastyle_opts=opts.AreaStyleOpts(opacity=0.5)) obj_l.add_yaxis("商家B", Faker.values(), areastyle_opts=opts.AreaStyleOpts(opacity=0.5)) obj_l.set_global_opts(title_opts=opts.TitleOpts(title="Line-面积图", subtitle="副标题")) return obj_l
def print_bar(self): bar = Bar(init_opts=opts.InitOpts(width='1080px', height='480px')) bar.add_xaxis(self.timedata) # category 是同系列直接的距离,gap是不同系列之间的距离 bar.add_yaxis('治愈人数', self.heal, gap='100%', label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color='#90EE90')) if self.name == '湖北': bar.add_yaxis('死亡人数', self.dead, gap='100%', label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color='#696969')) # 设置全局变量:x轴标签倾斜度,html主标题 bar.set_global_opts(datazoom_opts=[ opts.DataZoomOpts(), opts.DataZoomOpts(type_='inside') ], toolbox_opts=opts.ToolboxOpts(), xaxis_opts=opts.AxisOpts(name_rotate=-15), title_opts=opts.TitleOpts(self.name, subtitle='数据来自inews')) line = Line() line.add_xaxis(self.timedata) line.add_yaxis('确诊人数', self.confirm, label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color='#F08080')) print(self.name + '.html', '已成功生成到', os.getcwd()) # 在bar的基础上画line bar.overlap(line).render(self.name + '.html')
def line_power(): obj_l = Line() obj_l.add_xaxis(xaxis_data=["0","1", "2", "3", "4", "5", "6", "7", "8"]) obj_l.add_yaxis("2的指数图像", y_axis=[1, 2, 4, 8, 16, 32, 64, 128, 256],linestyle_opts=opts.LineStyleOpts(width=2), is_smooth=True) obj_l.set_series_opts( # 设置该参数,则x对应的y值会显示出来,该参数默认为True label_opts=opts.LabelOpts(is_show=True), ) obj_l.set_global_opts( title_opts=opts.TitleOpts(title="Line-2的指数图像",subtitle="副标题"), xaxis_opts=opts.AxisOpts( name="x轴", # 坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样,可设置x轴刻度顶格 boundary_gap=False # 类目轴中在 boundaryGap 为 true 的时候有效,可以保证刻度线和标签对齐。 # axistick_opts=opts.AxisTickOpts(is_align_with_label=True) ), yaxis_opts=opts.AxisOpts( # y轴名字 name="y轴", # 根据y轴刻度的横线 splitline_opts=opts.SplitLineOpts(is_show=True), # 只在数值轴中(type: 'value')有效 # is_scale = False ) ) return obj_l
def gen_rsi(df1): close_list = df1.apply(lambda record: float(record['close']), axis=1).tolist() close_list = np.array(close_list) rsi_5 = talib.RSI(close_list, 5) rsi_20 = talib.RSI(close_list, 20) line = Line() line.add_xaxis(xaxis_data=list(df1['datetime'])) line.add_yaxis( 'rsi_5', y_axis=rsi_5, xaxis_index=1, yaxis_index=1, label_opts=opts.LabelOpts(is_show=False), ) line.add_yaxis( 'rsi_20', y_axis=rsi_20, xaxis_index=1, yaxis_index=1, label_opts=opts.LabelOpts(is_show=False), ) line.set_global_opts( # xaxis_opts=opts.AxisOpts(is_show=False), xaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(is_show=False), ), legend_opts=opts.LegendOpts(is_show=True, pos_right="50%"), ) line.set_series_opts(label_opts=opts.LabelOpts(is_show=False), ) return line
def index(): date_ = db.session.query( func.distinct(CourseCount.update_at).label('update_at')).order_by( CourseCount.update_at.asc() # noqa ).all() name_ = db.session.query(Course.name).order_by(Course.id.asc() # noqa ).all() course_count_ = db.session.query( CourseCount.count, CourseCount.update_at, Course.name).join(Course, CourseCount.course_id == Course.id).order_by( CourseCount.update_at.asc(), # noqa CourseCount.course_id.asc() # noqa ).all() x_date = [_.update_at for _ in date_] y_name = [_.name for _ in name_] data = [] for name in y_name: d = list() for course_count in course_count_: if course_count.name == name: d.append(course_count.count) data.append((name, d)) c = Line().add_xaxis(x_date).set_global_opts(title_opts=opts.TitleOpts( title="腾讯课堂-课程数量走势")) for d in data: c.add_yaxis(d[0], d[1], is_connect_nones=True) return Markup(c.render_embed())
def v_totvalue(self, end=yesterdayobj(), rendered=True, vopts=None): """ visualization on the total values daily change of the aim """ partp = self.price[self.price["date"] >= self.cftable.iloc[0].date] # 多基金账单时起点可能非该基金持有起点 partp = partp[partp["date"] <= end] date = [d.date() for d in partp.date] valuedata = [ self.briefdailyreport(d).get("currentvalue", 0) for d in partp.date ] line = Line() if vopts is None: vopts = line_opts line.add_xaxis(date) line.add_yaxis(series_name="持仓总值", y_axis=valuedata, is_symbol_show=False) line.set_global_opts(**vopts) if rendered: return line.render_notebook() else: return line
def line(): cursor = connection.cursor() cursor.execute("""SELECT DATE_FORMAT( rev.create_time, '%Y-%m-%d' ) create_time, revt.`name`, count( * ) FROM resource_event rev LEFT JOIN resource_eventtype revt ON rev.event_type_id = revt.id GROUP BY create_time, event_type_id""") rows = cursor.fetchall() name_list = [] event_type_list = [] for row in rows: name = row[0] if name not in name_list: name_list.append(name) event_type = row[1] if event_type not in event_type_list: event_type_list.append(event_type) line = Line(init_opts=opts.InitOpts(height="350px")) line.add_xaxis(name_list) for event_type in event_type_list: tmp_list = [0 for i in range(len(name_list))] for row in rows: if event_type == row[1]: index = name_list.index(row[0]) tmp_list[index] = row[2] line.add_yaxis(event_type, tmp_list, is_smooth=True) line.set_global_opts(title_opts=opts.TitleOpts(title="数量变化统计")) return line.render_embed()
def report(request): """返回慢SQL历史趋势""" checksum = request.GET.get('checksum') cnt_data = ChartDao().slow_query_review_history_by_cnt(checksum) pct_data = ChartDao().slow_query_review_history_by_pct_95_time(checksum) cnt_x_data = [row[1] for row in cnt_data['rows']] cnt_y_data = [int(row[0]) for row in cnt_data['rows']] pct_y_data = [str(row[0]) for row in pct_data['rows']] line = Line(init_opts=opts.InitOpts(width='800', height='380px')) line.add_xaxis(cnt_x_data) line.add_yaxis("慢查次数", cnt_y_data, is_smooth=True, markline_opts=opts.MarkLineOpts(data=[ opts.MarkLineItem(type_="max", name='最大值'), opts.MarkLineItem(type_="average", name='平均值') ])) line.add_yaxis("慢查时长(95%)", pct_y_data, is_smooth=True, is_symbol_show=False) line.set_series_opts(areastyle_opts=opts.AreaStyleOpts(opacity=0.5, )) line.set_global_opts( title_opts=opts.TitleOpts(title='SQL历史趋势'), legend_opts=opts.LegendOpts(selected_mode='single'), xaxis_opts=opts.AxisOpts( axistick_opts=opts.AxisTickOpts(is_align_with_label=True), is_scale=False, boundary_gap=False, ), ) result = {"status": 0, "msg": '', "data": line.render_embed()} return HttpResponse(json.dumps(result), content_type='application/json')
def createPlotLine(df, xAxisTitle, yAxisTitle, title=None): line = Line() line.add_xaxis(df[xAxisTitle].tolist()) line.add_yaxis(yAxisTitle, df[yAxisTitle].tolist()) if not title: title = "" line.set_global_opts( tooltip_opts=opts.TooltipOpts(trigger='axis', axis_pointer_type='cross'), title_opts=opts.TitleOpts(title=title) ) ptg75 = df[yAxisTitle].quantile(.75) ptg50 = df[yAxisTitle].quantile(.5) ptg25 = df[yAxisTitle].quantile(.25) line.set_series_opts( markline_opts=opts.MarkLineOpts( data=[ opts.MarkLineItem(y=ptg75, name="75%"), opts.MarkLineItem(y=ptg50, name="50%"), opts.MarkLineItem(y=ptg25, name="25%") ] ), ) return line
def v_netvalue(self, end=yesterdayobj(), benchmark=True, rendered=True, vopts=None): """ visulaization on netvalue curve :param end: dateobject for indicating the end date in the figure, default to yesterday :param benchmark: bool, whether include benchmark's netvalue curve, default true :param vopts: dict, options for pyecharts instead of builtin settings """ if getattr(self, "bmprice", None) is None: benchmark = False if benchmark: a, b = self.comparison(end) else: a = self.price if vopts is None: vopts = line_opts line = Line() line.add_xaxis([d.date() for d in list(a.date)]) line.add_yaxis( y_axis=list(a.netvalue), series_name=self.name, is_symbol_show=False ) line.set_global_opts(**vopts) if benchmark: line.add_yaxis( series_name=self.benchmark.name, y_axis=list(b.netvalue), is_symbol_show=False, ) if rendered: return line.render_notebook() else: return line
def gen_cci(self, df1, n): high_list = df1.apply(lambda record: float(record['high']), axis=1).tolist() high_list = np.array(high_list) low_list = df1.apply(lambda record: float(record['low']), axis=1).tolist() low_list = np.array(low_list) close_list = df1.apply(lambda record: float(record['close']), axis=1).tolist() close_list = np.array(close_list) rsi_n = talib.CCI(high_list, low_list, close_list, n) line = Line() line.add_xaxis(xaxis_data=list(df1['datetime'])) line.add_yaxis( 'cci_' + str(n), y_axis=rsi_n, xaxis_index=1, yaxis_index=1, label_opts=opts.LabelOpts(is_show=False), ) line.set_global_opts( yaxis_opts=opts.AxisOpts(min_=-150, max_=150), # xaxis_opts=opts.AxisOpts(is_show=False), xaxis_opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(is_show=False), ), legend_opts=opts.LegendOpts(is_show=True, pos_right="38%"), ) line.set_series_opts(label_opts=opts.LabelOpts(is_show=False), ) return line
def gen_line(df1, symbol, price_min, price_max): #df1['datetime'] = df1['date'] + ' ' + df1['time'] df1['datetime'] = df1['date'] dt_list = list(df1['datetime']) close_list = df1.apply(lambda record: float(record['close']), axis=1).tolist() close_list = np.array(close_list) line1 = Line(init_opts=opts.InitOpts(height='700px', width='1300px')) line1.set_global_opts( yaxis_opts=opts.AxisOpts( min_=price_min, #min_=999, max_=price_max, splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)), ), datazoom_opts=[ opts.DataZoomOpts( is_show=True, type_="slider", range_start=0, range_end=100, ), ], ) line1.add_xaxis(xaxis_data=dt_list) line1.add_yaxis( symbol, y_axis=close_list, ) line1.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) return line1
def gen_line_two(self, df1): df1['datetime'] = df1['date'] + ' ' + df1['time'] dt_list1 = list(df1['datetime']) # print( len(dt_list1) ) # dt_list1 = [s[5:10] for s in dt_list1] close_list1 = df1.apply(lambda record: float(record['close']), axis=1).tolist() close_list1 = np.array(close_list1) kline = Line() kline.add_xaxis(dt_list1) kline.add_yaxis('resid', close_list1, xaxis_index=1, yaxis_index=1, label_opts=opts.LabelOpts(is_show=False)) kline.set_global_opts(yaxis_opts=opts.AxisOpts( is_scale=True, splitarea_opts=opts.SplitAreaOpts( is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1))), legend_opts=opts.LegendOpts(is_show=True, pos_right="40%") #xaxis_opts=opts.AxisOpts(type_='time')) ) return kline
def gen_boll(df1, n, dev): close_list = df1.apply(lambda record: float(record['close']), axis=1).tolist() close_list = np.array(close_list) dt_list = df1['datetime'].tolist() up, down = boll(close_list, n, dev) line = Line() line.add_xaxis(xaxis_data=dt_list) line.add_yaxis( 'boll_up', y_axis=up, label_opts=opts.LabelOpts(is_show=False), ) line.add_yaxis( 'boll_down', y_axis=down, label_opts=opts.LabelOpts(is_show=False), ) line.set_global_opts(xaxis_opts=opts.AxisOpts(is_show=False), legend_opts=opts.LegendOpts(is_show=True, pos_right="40%")) return line
def generate_line_chart_with_slider(title: str, xaxis_caption: str, groups: List[DateCounterGroup]) -> Line: chart = Line() chart.set_global_opts( title_opts=opts.TitleOpts(title=title), datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=100), yaxis_opts=opts.AxisOpts( name='Samples', name_textstyle_opts=opts.global_options.TextStyleOpts( font_size=16)), xaxis_opts=opts.AxisOpts( name=xaxis_caption, name_textstyle_opts=opts.global_options.TextStyleOpts( font_size=16))) # X axis chart.add_xaxis(groups[0].captions) # Y axis for group in groups: # Samples per day chart.add_yaxis(group.file_type, group.counts, linestyle_opts=opts.LineStyleOpts(width=2), label_opts=opts.LabelOpts(is_show=False)) # Simple moving average (SMA) chart.add_yaxis(group.simple_moving_average_legend, group.simple_moving_average, linestyle_opts=opts.LineStyleOpts(width=2, type_='dashed'), label_opts=opts.LabelOpts(color='black', background_color='white')) return chart