Пример #1
0
def vtradecost(self,
               cftable,
               unitcost=False,
               start=None,
               end=yesterdayobj(),
               rendered=True):
    """
    visualization giving the average cost line together with netvalue line as well as buy and sell points

    :returns: pyecharts.line
    """
    funddata = []
    costdata = []
    pprice = self.price[self.price["date"] <= end]
    pcftable = cftable
    if start is not None:
        pprice = pprice[pprice["date"] >= start]
        pcftable = pcftable[pcftable["date"] >= start]
    for _, row in pprice.iterrows():
        date = row["date"]
        funddata.append(row["netvalue"])
        if unitcost:
            cost = 0
            if (date - self.cftable.iloc[0].date).days >= 0:
                cost = self.unitcost(date)
            costdata.append(cost)

    coords = []
    # pcftable = pcftable[abs(pcftable["cash"]) > threhold]
    for i, r in pcftable.iterrows():
        coords.append(
            [r.date, pprice[pprice["date"] <= r.date].iloc[-1]["netvalue"]])

    upper = pcftable.cash.abs().max()
    lower = pcftable.cash.abs().min()
    if upper == lower:
        upper = 2 * lower + 1  # avoid zero in denominator

    def marker_factory(x, y):
        buy = pcftable[pcftable["date"] <= x].iloc[-1]["cash"]
        if buy < 0:
            color = "#ff7733"
        else:

            color = "#3366ff"
        size = (abs(buy) - lower) / (upper - lower) * 5 + 5
        return opts.MarkPointItem(
            coord=[x.date(), y],
            itemstyle_opts=opts.ItemStyleOpts(color=color),
            # this nested itemstyle_opts within MarkPointItem is only supported for pyechart>1.7.1
            symbol="circle",
            symbol_size=size,
        )

    line = Line()

    line.add_xaxis([d.date() for d in pprice.date])

    if unitcost:
        line.add_yaxis(
            series_name="持仓成本",
            y_axis=costdata,
            is_symbol_show=False,
        )
    line.add_yaxis(
        series_name="基金净值",
        y_axis=funddata,
        is_symbol_show=False,
        markpoint_opts=opts.MarkPointOpts(
            data=[marker_factory(*c) for c in coords], ),
    )
    line.set_global_opts(
        datazoom_opts=[
            opts.DataZoomOpts(is_show=True,
                              type_="slider",
                              range_start=50,
                              range_end=100),
            opts.DataZoomOpts(
                is_show=True,
                type_="slider",
                orient="vertical",
                range_start=50,
                range_end=100,
            ),
        ],
        tooltip_opts=opts.TooltipOpts(
            is_show=True,
            trigger="axis",
            trigger_on="mousemove",
            axis_pointer_type="cross",
        ),
    )
    if rendered:
        return line.render_notebook()
    else:
        return line
Пример #2
0
def plot_7d_weather(cip):
    url = "http://www.weather.com.cn/weather/{}.shtml".format(cip)
    response = requests.get(url, headers=headers)
    response.encoding = "utf-8"
    html = response.text

    # 解析数据
    soup = BeautifulSoup(html, "lxml")
    lis = soup.select("div.c7d > ul > li")
    date_list = []
    weather_list = []
    lowest_temp_list = []
    highest_temp_list = []
    for li in lis:
        date_ = re.findall("(.*?)(.*?)", li.select("h1")[0].text)[0]
        if len(date_) == 2:
            date_ = "0" + date_
        which_day = re.findall(".*?((.*?))", li.select("h1")[0].text)[0]
        weather = li.select("p")[0].text
        temp = li.select("p")[1].text.strip().replace("℃", "")
        if "/" in temp:
            highest_temp = temp.split("/")[0]
            lowest_temp = temp.split("/")[1]
        else:
            highest_temp = temp
            lowest_temp = temp
        date_list.append(f"{date_}\n({which_day})")
        weather_list.append(weather)
        lowest_temp_list.append(int(lowest_temp))
        highest_temp_list.append(int(highest_temp))

    line = Line(init_opts=opts.InitOpts(width="450px", height="170px", theme=ThemeType.MACARONS))
    line.add_xaxis(
        xaxis_data=date_list
    )
    line.add_yaxis(
        series_name="最高温度",
        y_axis=highest_temp_list,
        symbol_size=8,
        is_hover_animation=False,
        label_opts=opts.LabelOpts(font_size=15)
    )
    line.add_yaxis(
        series_name="最低温度",
        y_axis=lowest_temp_list,
        symbol_size=8,
        is_hover_animation=False,
        label_opts=opts.LabelOpts(
            position="bottom",
            font_size=15
        )
    )
    line.set_global_opts(
        title_opts=opts.TitleOpts(
            # subtitle="数据来源于中国天气网",
            # pos_top="-5px"
        ),
        legend_opts=opts.LegendOpts(pos_right="30px"),
        xaxis_opts=opts.AxisOpts(
            type_="category",
            axislabel_opts=opts.LabelOpts(font_size=13)
        ),
        yaxis_opts=opts.AxisOpts(
            # is_scale=True,
            min_=min(lowest_temp_list)-10 if min(lowest_temp_list)-10 > 0 else 0,
            max_=max(highest_temp_list)+5,
            interval=10,
            name="温度(°C)",
            name_location="center",
            # name_gap=25,
            name_gap=5,
            name_textstyle_opts=opts.TextStyleOpts(
                font_size=13,
                color="black"
            ),
            type_="value",
            axislabel_opts=opts.LabelOpts(is_show=False),
            # axistick_opts=opts.AxisTickOpts(length=5)
            axistick_opts=opts.AxisTickOpts(length=0)
        )
    )
    line.set_series_opts(
        linestyle_opts=opts.LineStyleOpts(width=2.5),
        markpoint_opts=opts.MarkPointOpts(
            data=[opts.MarkPointItem(coord=[z[0], z[1]], value=z[2]) for z in zip(date_list, highest_temp_list, weather_list)],
            symbol_size=1,
            label_opts=opts.LabelOpts(
                color="black",
                rotate=25,
                distance=28,
                font_size=11
            )
        )
    )
    line.render("time-temp-curve.html")
Пример #3
0
def plot_1d_weather(cip):
    url = "http://www.weather.com.cn/weather1d/{}.shtml#input".format(cip)
    response = requests.get(url, headers=headers)
    response.encoding = "utf-8"
    html = response.text
    # print(response.text)
    soup = BeautifulSoup(html, "lxml")
    script = soup.select("div#today > script")[1].text.strip().strip("var hour3data=")
    json_ = json.loads(script)
    # print(json_)
    data = json_["1d"]
    # print(data)
    time_list = []
    temp_list = []
    weather_list = []
    for i in range(len(data)):
        time_ = data[i].split(",")[0]
        wind = data[i].split(",")[2]
        temp = data[i].split(",")[3]
        time_list.append(time_)
        weather_list.append(wind)
        temp_list.append(temp)
    # time_list = [re.findall("\d+", i)[0] + "-" + re.findall("\d+", i)[1] for i in time_list]
    time_list = time_list[:-1]
    temp_list = [int(i.strip("℃")) for i in temp_list][:-1]
    weather_list = weather_list[:-1]

    line = Line(init_opts=opts.InitOpts(width="450px", height="170px", theme=ThemeType.MACARONS))
    # line = Line(init_opts=opts.InitOpts(width="1000px", height="500px", theme=ThemeType.MACARONS))
    line.add_xaxis(xaxis_data=time_list)
    line.add_yaxis(
        series_name="",
        y_axis=temp_list,
        symbol_size=8,
        is_hover_animation=False
    )
    line.set_global_opts(
        title_opts=opts.TitleOpts(
            subtitle="数据来源于中国天气网",
            # pos_left="10px",
            pos_left="center",
            pos_top="-5px"
        ),
        xaxis_opts=opts.AxisOpts(
            type_="category",
            boundary_gap=True,
            axislabel_opts=opts.LabelOpts(
                rotate=30,
                font_size=12
            ),
            axistick_opts=opts.AxisTickOpts(is_align_with_label=False)
        ),
        yaxis_opts=opts.AxisOpts(
            # is_scale=False,
            min_=min(temp_list)-10,
            max_=max(temp_list)+2,
            interval=5,
            name="温度(°C)",
            name_location="center",
            # name_gap=25,
            name_gap=5,
            name_textstyle_opts=opts.TextStyleOpts(
                font_size=13,
                color="black"
            ),
            type_="value",
            axislabel_opts=opts.LabelOpts(
                font_size=12,
                is_show=False
            ),
            # axistick_opts=opts.AxisTickOpts(length=5)
            axistick_opts=opts.AxisTickOpts(length=0)
        ),
    )
    line.set_series_opts(
        label_opts=opts.LabelOpts(
            font_size=15,
            position="bottom",
            color="#f68227"
        ),
        linestyle_opts=opts.LineStyleOpts(width=2.5),
        markpoint_opts=opts.MarkPointOpts(
            symbol_size=1,
            data=[
                opts.MarkPointItem(name="天气", coord=[time_list[i], temp_list[i]], value=weather_list[i]) for i in range(len(time_list))
            ],
            label_opts=opts.LabelOpts(
                color="black",
                font_size=14,
                distance=10
            )
        )
    )
    line.render("time-temp-curve.html")
Пример #4
0
def kline_pro(kline: List[dict],
              fx: List[dict] = None,
              bi: List[dict] = None,
              xd: List[dict] = None,
              bs: List[dict] = None,
              title: str = "缠中说禅K线分析",
              width: str = "1400px",
              height: str = '580px') -> Grid:
    """绘制缠中说禅K线分析结果

    :param kline: K线
    :param fx: 分型识别结果
    :param bi: 笔识别结果
        {'dt': Timestamp('2020-11-26 00:00:00'),
          'fx_mark': 'd',
          'start_dt': Timestamp('2020-11-25 00:00:00'),
          'end_dt': Timestamp('2020-11-27 00:00:00'),
          'fx_high': 144.87,
          'fx_low': 138.0,
          'bi': 138.0}
    :param xd: 线段识别结果
    :param bs: 买卖点
    :param title: 图表标题
    :param width: 图表宽度
    :param height: 图表高度
    :return: 用Grid组合好的图表
    """
    # 配置项设置
    # ------------------------------------------------------------------------------------------------------------------
    bg_color = "#1f212d"  # 背景
    up_color = "#F9293E"
    down_color = "#00aa3b"

    init_opts = opts.InitOpts(bg_color=bg_color,
                              width=width,
                              height=height,
                              animation_opts=opts.AnimationOpts(False))
    title_opts = opts.TitleOpts(
        title=title,
        pos_top="1%",
        title_textstyle_opts=opts.TextStyleOpts(color=up_color, font_size=20),
        subtitle_textstyle_opts=opts.TextStyleOpts(color=down_color,
                                                   font_size=12))

    label_not_show_opts = opts.LabelOpts(is_show=False)
    legend_not_show_opts = opts.LegendOpts(is_show=False)
    red_item_style = opts.ItemStyleOpts(color=up_color)
    green_item_style = opts.ItemStyleOpts(color=down_color)
    k_style_opts = opts.ItemStyleOpts(color=up_color,
                                      color0=down_color,
                                      border_color=up_color,
                                      border_color0=down_color,
                                      opacity=0.8)

    legend_opts = opts.LegendOpts(is_show=True,
                                  pos_top="1%",
                                  pos_left="30%",
                                  item_width=14,
                                  item_height=8,
                                  textstyle_opts=opts.TextStyleOpts(
                                      font_size=12, color="#0e99e2"))
    brush_opts = opts.BrushOpts(tool_box=["rect", "polygon", "keep", "clear"],
                                x_axis_index="all",
                                brush_link="all",
                                out_of_brush={"colorAlpha": 0.1},
                                brush_type="lineX")

    axis_pointer_opts = opts.AxisPointerOpts(is_show=True,
                                             link=[{
                                                 "xAxisIndex": "all"
                                             }])

    dz_inside = opts.DataZoomOpts(False,
                                  "inside",
                                  xaxis_index=[0, 1, 2],
                                  range_start=80,
                                  range_end=100)
    dz_slider = opts.DataZoomOpts(True,
                                  "slider",
                                  xaxis_index=[0, 1, 2],
                                  pos_top="96%",
                                  pos_bottom="0%",
                                  range_start=80,
                                  range_end=100)

    yaxis_opts = opts.AxisOpts(is_scale=True,
                               axislabel_opts=opts.LabelOpts(
                                   color="#c7c7c7",
                                   font_size=8,
                                   position="inside"))

    grid0_xaxis_opts = opts.AxisOpts(
        type_="category",
        grid_index=0,
        axislabel_opts=label_not_show_opts,
        split_number=20,
        min_="dataMin",
        max_="dataMax",
        is_scale=True,
        boundary_gap=False,
        axisline_opts=opts.AxisLineOpts(is_on_zero=False))

    tool_tip_opts = opts.TooltipOpts(
        trigger="axis",
        axis_pointer_type="cross",
        background_color="rgba(245, 245, 245, 0.8)",
        border_width=1,
        border_color="#ccc",
        position=JsCode("""
                    function (pos, params, el, elRect, size) {
    					var obj = {top: 10};
    					obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30;
    					return obj;
    				}
                    """),
        textstyle_opts=opts.TextStyleOpts(color="#000"),
    )

    # 数据预处理
    # ------------------------------------------------------------------------------------------------------------------
    dts = [x['dt'] for x in kline]
    # k_data = [[x['open'], x['close'], x['low'], x['high']] for x in kline]
    k_data = [
        opts.CandleStickItem(
            name=i, value=[x['open'], x['close'], x['low'], x['high']])
        for i, x in enumerate(kline)
    ]

    vol = []
    for i, row in enumerate(kline):
        item_style = red_item_style if row['close'] > row[
            'open'] else green_item_style
        bar = opts.BarItem(name=i,
                           value=row['vol'],
                           itemstyle_opts=item_style,
                           label_opts=label_not_show_opts)
        vol.append(bar)

    close = np.array([x['close'] for x in kline], dtype=np.double)
    diff, dea, macd = MACD(close)

    ma5 = SMA(close, timeperiod=5)
    ma34 = SMA(close, timeperiod=34)
    ma233 = SMA(close, timeperiod=233)

    macd_bar = []
    for i, v in enumerate(macd.tolist()):
        item_style = red_item_style if v > 0 else green_item_style
        bar = opts.BarItem(name=i,
                           value=round(v, 4),
                           itemstyle_opts=item_style,
                           label_opts=label_not_show_opts)
        macd_bar.append(bar)

    diff = diff.round(4)
    dea = dea.round(4)

    # K 线主图
    # ------------------------------------------------------------------------------------------------------------------
    chart_k = Kline()
    chart_k.add_xaxis(xaxis_data=dts)
    chart_k.add_yaxis(series_name="Kline",
                      y_axis=k_data,
                      itemstyle_opts=k_style_opts)

    chart_k.set_global_opts(legend_opts=legend_opts,
                            datazoom_opts=[dz_inside, dz_slider],
                            yaxis_opts=yaxis_opts,
                            tooltip_opts=tool_tip_opts,
                            axispointer_opts=axis_pointer_opts,
                            brush_opts=brush_opts,
                            title_opts=title_opts,
                            xaxis_opts=grid0_xaxis_opts)

    # 均线图
    # ------------------------------------------------------------------------------------------------------------------
    chart_ma = Line()
    chart_ma.add_xaxis(xaxis_data=dts)

    ma_keys = {"MA5": ma5, "MA34": ma34, "MA233": ma233}
    ma_colors = ["#39afe6", "#da6ee8", "#00940b"]
    for i, (name, ma) in enumerate(ma_keys.items()):
        chart_ma.add_yaxis(series_name=name,
                           y_axis=ma,
                           is_smooth=True,
                           is_selected=False,
                           symbol_size=0,
                           label_opts=label_not_show_opts,
                           linestyle_opts=opts.LineStyleOpts(
                               opacity=0.8, width=1.0, color=ma_colors[i]))

    chart_ma.set_global_opts(xaxis_opts=grid0_xaxis_opts,
                             legend_opts=legend_not_show_opts)
    chart_k = chart_k.overlap(chart_ma)

    # 缠论结果
    # ------------------------------------------------------------------------------------------------------------------
    if fx:
        fx_dts = [x['dt'] for x in fx]
        fx_val = [x['fx'] for x in fx]
        chart_fx = Scatter()
        chart_fx.add_xaxis(fx_dts)
        chart_fx.add_yaxis(series_name="FX",
                           y_axis=fx_val,
                           is_selected=False,
                           symbol="circle",
                           symbol_size=6,
                           label_opts=label_not_show_opts,
                           itemstyle_opts=opts.ItemStyleOpts(
                               color="rgba(152, 147, 193, 1.0)", ))

        chart_fx.set_global_opts(xaxis_opts=grid0_xaxis_opts,
                                 legend_opts=legend_not_show_opts)
        chart_k = chart_k.overlap(chart_fx)

    if bi:
        bi_dts = [x['dt'] for x in bi]
        bi_val = [x['bi'] for x in bi]
        chart_bi = Line()
        chart_bi.add_xaxis(bi_dts)
        chart_bi.add_yaxis(series_name="BI",
                           y_axis=bi_val,
                           is_selected=True,
                           symbol="diamond",
                           symbol_size=10,
                           label_opts=label_not_show_opts,
                           itemstyle_opts=opts.ItemStyleOpts(
                               color="rgba(184, 117, 225, 1.0)", ),
                           linestyle_opts=opts.LineStyleOpts(width=1.5))

        chart_bi.set_global_opts(xaxis_opts=grid0_xaxis_opts,
                                 legend_opts=legend_not_show_opts)
        chart_k = chart_k.overlap(chart_bi)

    if xd:
        xd_dts = [x['dt'] for x in xd]
        xd_val = [x['xd'] for x in xd]
        chart_xd = Line()
        chart_xd.add_xaxis(xd_dts)
        chart_xd.add_yaxis(series_name="XD",
                           y_axis=xd_val,
                           is_selected=True,
                           symbol="triangle",
                           symbol_size=10,
                           itemstyle_opts=opts.ItemStyleOpts(
                               color="rgba(37, 141, 54, 1.0)", ))

        chart_xd.set_global_opts(xaxis_opts=grid0_xaxis_opts,
                                 legend_opts=legend_not_show_opts)
        chart_k = chart_k.overlap(chart_xd)

    if bs:
        b_dts = [x['dt'] for x in bs if x['mark'] == 'buy']
        if len(b_dts) > 0:
            b_val = [x['price'] for x in bs if x['mark'] == 'buy']
            chart_b = Scatter()
            chart_b.add_xaxis(b_dts)
            chart_b.add_yaxis(series_name="BUY",
                              y_axis=b_val,
                              is_selected=False,
                              symbol="arrow",
                              symbol_size=8,
                              itemstyle_opts=opts.ItemStyleOpts(
                                  color="#f31e1e", ))

            chart_b.set_global_opts(xaxis_opts=grid0_xaxis_opts,
                                    legend_opts=legend_not_show_opts)
            chart_k = chart_k.overlap(chart_b)

        s_dts = [x['dt'] for x in bs if x['mark'] == 'sell']
        if len(s_dts) > 0:
            s_val = [x['price'] for x in bs if x['mark'] == 'sell']
            chart_s = Scatter()
            chart_s.add_xaxis(s_dts)
            chart_s.add_yaxis(series_name="SELL",
                              y_axis=s_val,
                              is_selected=False,
                              symbol="pin",
                              symbol_size=12,
                              itemstyle_opts=opts.ItemStyleOpts(
                                  color="#45b97d", ))

            chart_s.set_global_opts(xaxis_opts=grid0_xaxis_opts,
                                    legend_opts=legend_not_show_opts)
            chart_k = chart_k.overlap(chart_s)

    # 成交量图
    # ------------------------------------------------------------------------------------------------------------------
    chart_vol = Bar()
    chart_vol.add_xaxis(dts)
    chart_vol.add_yaxis(series_name="Volume", y_axis=vol, bar_width='60%')
    chart_vol.set_global_opts(
        xaxis_opts=opts.AxisOpts(
            type_="category",
            grid_index=1,
            axislabel_opts=opts.LabelOpts(is_show=True,
                                          font_size=8,
                                          color="#9b9da9"),
        ),
        yaxis_opts=yaxis_opts,
        legend_opts=legend_not_show_opts,
    )

    # MACD图
    # ------------------------------------------------------------------------------------------------------------------
    chart_macd = Bar()
    chart_macd.add_xaxis(dts)
    chart_macd.add_yaxis(series_name="MACD", y_axis=macd_bar, bar_width='60%')
    chart_macd.set_global_opts(
        xaxis_opts=opts.AxisOpts(
            type_="category",
            grid_index=2,
            axislabel_opts=opts.LabelOpts(is_show=False),
        ),
        yaxis_opts=opts.AxisOpts(
            grid_index=2,
            split_number=4,
            axisline_opts=opts.AxisLineOpts(is_on_zero=False),
            axistick_opts=opts.AxisTickOpts(is_show=False),
            splitline_opts=opts.SplitLineOpts(is_show=False),
            axislabel_opts=opts.LabelOpts(is_show=True, color="#c7c7c7"),
        ),
        legend_opts=opts.LegendOpts(is_show=False),
    )

    line = Line()
    line.add_xaxis(dts)
    line.add_yaxis(series_name="DIFF",
                   y_axis=diff,
                   label_opts=label_not_show_opts,
                   is_symbol_show=False,
                   linestyle_opts=opts.LineStyleOpts(opacity=0.8,
                                                     width=1.0,
                                                     color="#da6ee8"))
    line.add_yaxis(series_name="DEA",
                   y_axis=dea,
                   label_opts=label_not_show_opts,
                   is_symbol_show=False,
                   linestyle_opts=opts.LineStyleOpts(opacity=0.8,
                                                     width=1.0,
                                                     color="#39afe6"))

    chart_macd = chart_macd.overlap(line)

    grid0_opts = opts.GridOpts(pos_left="0%",
                               pos_right="1%",
                               pos_top="12%",
                               height="58%")
    grid1_opts = opts.GridOpts(pos_left="0%",
                               pos_right="1%",
                               pos_top="74%",
                               height="8%")
    grid2_opts = opts.GridOpts(pos_left="0%",
                               pos_right="1%",
                               pos_top="86%",
                               height="10%")

    grid_chart = Grid(init_opts)
    grid_chart.add(chart_k, grid_opts=grid0_opts)
    grid_chart.add(chart_vol, grid_opts=grid1_opts)
    grid_chart.add(chart_macd, grid_opts=grid2_opts)
    return grid_chart
Пример #5
0
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(init_opts=opts.InitOpts(width='600', height='380px'))
    bar1.add_xaxis(attr)
    bar1.add_yaxis("", value)

    # 工单按组统计
    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(init_opts=opts.InitOpts(width='600', height='380px'))
    pie1.set_global_opts(title_opts=opts.TitleOpts(title=''),
                         legend_opts=opts.LegendOpts(orient="vertical",
                                                     pos_top="15%",
                                                     pos_left="2%",
                                                     is_show=False))
    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(init_opts=opts.InitOpts(width='600', height='380px'))
    bar2.add_xaxis(attr)
    bar2.add_yaxis("", value)

    # 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(init_opts=opts.InitOpts(width='600', height='380px'))
    line1.set_global_opts(title_opts=opts.TitleOpts(title=''),
                          legend_opts=opts.LegendOpts(selected_mode='single'))
    line1.add_xaxis(attr)
    line1.add_yaxis("检索行数",
                    effect_value,
                    is_smooth=True,
                    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(init_opts=opts.InitOpts(width='600', height='380px'))
    pie4.set_global_opts(title_opts=opts.TitleOpts(title=''),
                         legend_opts=opts.LegendOpts(orient="vertical",
                                                     pos_top="15%",
                                                     pos_left="2%",
                                                     is_show=False))
    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(init_opts=opts.InitOpts(width='600', height='380px'))
    pie5.set_global_opts(title_opts=opts.TitleOpts(title=''),
                         legend_opts=opts.LegendOpts(orient="vertical",
                                                     pos_top="15%",
                                                     pos_left="2%",
                                                     is_show=False))
    pie5.set_series_opts(
        label_opts=opts.LabelOpts(formatter="{b}: {c}", position="left"))
    pie5.add("", [list(z) for z in zip(attr, value)])

    # 可视化展示页面
    chart = {
        "bar1": bar1.render_embed(),
        "pie1": pie1.render_embed(),
        "bar2": bar2.render_embed(),
        "pie2": pie2.render_embed(),
        "line1": line1.render_embed(),
        "pie4": pie4.render_embed(),
        "pie5": pie5.render_embed(),
    }

    # 获取统计数据
    try:
        dashboard_count_stats = cache.get('dashboard_count_stats')
    except Exception:
        dashboard_count_stats = {
            "sql_wf_cnt": SqlWorkflow.objects.count(),
            "query_wf_cnt": QueryPrivilegesApply.objects.count(),
            "user_cnt": Users.objects.count(),
            "ins_cnt": Instance.objects.count()
        }
    else:
        dashboard_count_stats = dashboard_count_stats or {
            "sql_wf_cnt": SqlWorkflow.objects.count(),
            "query_wf_cnt": QueryPrivilegesApply.objects.count(),
            "user_cnt": Users.objects.count(),
            "ins_cnt": Instance.objects.count()
        }
        cache.set("dashboard_count_stats", dashboard_count_stats, 600)

    return render(request, "dashboard.html", {
        "chart": chart,
        "count_stats": dashboard_count_stats
    })
Пример #6
0
def generate_html(folder_path):
    """
    info_dict = dict(
                    time=time.strftime("%H:%M:%S", time.localtime(timestamp)),
                    device=device_name,
                    cpu=cpu,
                    mem=mem,
                    fps=fps,
                )
    """
    pickle_folder_path = conf.info_path + os.sep + folder_path
    if os.listdir(pickle_folder_path):
        pass
    else:
        print("该目录下没有任何性能数据")
        exit(1)
    total_data = {}
    page = Page(page_title="移动端性能图表")
    cpu_line = Line(init_opts=opts.InitOpts(width="1200px", height="600px", theme=ThemeType.DARK))
    mem_line = Line(init_opts=opts.InitOpts(width="1200px", height="600px", theme=ThemeType.DARK))
    fps_line = Line(init_opts=opts.InitOpts(width="1200px", height="600px", theme=ThemeType.DARK))
    # 给所有折线图增加title和工具栏
    cpu_line.set_global_opts(title_opts=opts.TitleOpts(title="CPU", subtitle="所有设备CPU数据(%)"),
                             toolbox_opts=opts.ToolboxOpts())
    mem_line.set_global_opts(title_opts=opts.TitleOpts(title="内存", subtitle="所有设备内存数据(Mb)"),
                             toolbox_opts=opts.ToolboxOpts())
    fps_line.set_global_opts(title_opts=opts.TitleOpts(title="FPS", subtitle="所有设备FPS数据(帧)"),
                             toolbox_opts=opts.ToolboxOpts())

    for file in os.listdir(pickle_folder_path):
        device_name = file.split(".")[0]
        total_data[device_name] = op.read(pickle_folder_path + os.sep + file)
    longest_result = sorted([(len(total_data.get(k)), k) for k in total_data.keys()])[-1][1]  # get the longest result
    x_coordinate = [i.get("time") for i in total_data.get(longest_result)]
    cpu_line.add_xaxis(x_coordinate)

    mem_line.add_xaxis(x_coordinate)
    fps_line.add_xaxis(x_coordinate)
    for k in total_data.keys():  # 按设备加折线数据
        data = total_data.get(k)
        cpu_line.add_yaxis(data[0].get("device"), [i.get("cpu") for i in data], )
        mem_line.add_yaxis(data[0].get("device"), [i.get("mem") for i in data], )
        fps_line.add_yaxis(data[0].get("device"), [i.get("fps") for i in data], )

    for line in (cpu_line, mem_line, fps_line):  # 给所有折线图添加最大,最小,平均值
        (
            line.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
                                 markpoint_opts=opts.MarkPointOpts(
                                     data=[
                                            opts.MarkPointItem(type_="max", name="最大值"),
                                            opts.MarkPointItem(type_="min", name="最小值")
                                        ], symbol_size=40),
                                 markline_opts=opts.MarkLineOpts(
                                                data=[
                                                    opts.MarkLineItem(type_="average", name="平均值", )
                                                ],
                                                symbol="none"
                                 ))
        )
    page.add(cpu_line, mem_line, fps_line)
    page.render(conf.report_path + os.sep + folder_path + ".html")
    print("html文件生成在report目录下")
Пример #7
0
# -*- coding: utf-8 -*-
# @Time    : 2020/1/21 13:47
# @Author  : Mqz
# @FileName: tuxing_show.py

from pyecharts.charts import Bar
from pyecharts.charts import Line
from pyecharts import options as opts

# 不习惯链式调用的开发者依旧可以单独调用方法
# bar = Bar()
# bar.add_xaxis(["测试A", "测试B", "测试C", "测试D", "测试E", "测试F"])
# bar.add_yaxis("隧道ip", [5993, 5976, 6448, 6361, 6257, 90])
# bar.add_yaxis("直连ip", [7128, 6450, 5664, 6915, 6979, 10])
# bar.set_global_opts(title_opts=opts.TitleOpts(title="IP测试", subtitle="每个ip10分钟测试"))
# bar.render()
line = Line()
line.add_xaxis(["测试A", "测试B", "测试C", "测试D", "测试E", "测试F"])
line.add_yaxis("隧道ip", [5993, 5976, 5664, 6361, 6257, 5312])
line.add_yaxis("直连ip", [7128, 6450, 6448, 6915, 6979, 7101])
line.set_global_opts(
    title_opts=opts.TitleOpts(title="IP测试", subtitle="每个ip10分钟采集数量测试"))
line.render()
Пример #8
0
def analysis_total():
    # 处理数据
    x_data = pdata['年份'].tolist()
    # 将人口单位转换为亿
    y_data1 = pdata['年末总人口(万人)'].map(lambda x: "%.2f" % (x / 10000)).tolist()
    y_data2 = pdata['人口自然增长率(‰)'].tolist()
    y_data3 = pdata['人口出生率(‰)'].tolist()
    y_data4 = pdata['人口死亡率(‰)'].tolist()

    # 总人口柱状图
    bar = Bar(init_opts=opts.InitOpts(width="1200px", height="500px"))
    bar.add_xaxis(x_data)
    bar.add_yaxis("年末总人口(亿)",
                  y_data1,
                  category_gap="10%",
                  label_opts=opts.LabelOpts(rotate=90, position="inside"))
    bar.set_global_opts(
        title_opts=opts.TitleOpts(title="年末总人口变化情况",
                                  pos_bottom="bottom",
                                  pos_left="center"),
        xaxis_opts=opts.AxisOpts(
            type_="category",
            name='年份',
            # 坐标轴名称显示位置
            name_location='end',
            # x轴数值与坐标点的偏移量
            # boundary_gap=False,
            axislabel_opts=opts.LabelOpts(is_show=True,
                                          margin=10,
                                          color="#000",
                                          interval=1,
                                          rotate=90),
            # axisline_opts=opts.AxisLineOpts(is_show=True, symbol="arrow"),
            axistick_opts=opts.AxisTickOpts(is_show=True,
                                            is_align_with_label=True),
            axispointer_opts=opts.AxisPointerOpts(
                type_="line", label=opts.LabelOpts(is_show=True))),
        # y轴相关选项设置
        yaxis_opts=opts.AxisOpts(
            type_="value",
            position="left",
        ),
        legend_opts=opts.LegendOpts(is_show=True))

    # bar.render('bartest.html')

    # 自然增长率、出生率、死亡率折线图
    line = Line(init_opts=opts.InitOpts(width="1400px", height="500px"))
    line.add_xaxis(x_data)
    line.add_yaxis(series_name="自然增长率(‰)",
                   y_axis=y_data2,
                   label_opts=opts.LabelOpts(is_show=False))
    line.add_yaxis('出生率(‰)', y_data3, label_opts=opts.LabelOpts(is_show=False))
    line.add_yaxis('死亡率(‰)', y_data4, label_opts=opts.LabelOpts(is_show=False))
    line.set_global_opts(
        title_opts=opts.TitleOpts(title="人口自然增长率、出生率、死亡率",
                                  pos_bottom="bottom",
                                  pos_left="center"),
        xaxis_opts=opts.AxisOpts(
            name='年份',
            name_location='end',
            type_="value",
            min_="1949",
            max_interval=1,
            # 设置x轴不必与y轴的0对齐
            axisline_opts=opts.AxisLineOpts(is_on_zero=False),
            axislabel_opts=opts.LabelOpts(is_show=True,
                                          color="#000",
                                          interval=0,
                                          rotate=90),
            axistick_opts=opts.AxisTickOpts(is_show=True,
                                            is_align_with_label=True),
            axispointer_opts=opts.AxisPointerOpts(
                type_="shadow", label=opts.LabelOpts(is_show=True))),
        # y轴相关选项设置
        yaxis_opts=opts.AxisOpts(name='比例',
                                 type_="value",
                                 position="left",
                                 min_=-10,
                                 axislabel_opts=opts.LabelOpts(is_show=True)),
        legend_opts=opts.LegendOpts(is_show=True))

    # 渲染图像,将多个图像显示在一个html中
    # DraggablePageLayout表示可拖拽
    page = Page(layout=Page.DraggablePageLayout)
    page.add(bar)
    page.add(line)
    page.render('population_total.html')
Пример #9
0
def analysis_sex():
    x_data = pdata['年份'].tolist()
    # 历年男性人口数
    y_data_man = pdata['男性人口(万人)']
    # 历年女性人口数
    y_data_woman = pdata['女性人口(万人)']
    # 2019年男女比饼图
    sex_2019 = pdata[pdata['年份'] == 2019][['男性人口(万人)', '女性人口(万人)']]

    # 两列相减,获得新列
    y_data_man_woman = pdata['男性人口(万人)'] - pdata['女性人口(万人)']

    pie = Pie()
    pie.add("", [list(z) for z in zip(['男', '女'], np.ravel(sex_2019.values))])
    pie.set_global_opts(title_opts=opts.TitleOpts(
        title="2019中国男女比", pos_bottom="bottom", pos_left="center"))
    pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {d}%"))
    pie.render('nvpie.html')

    line = Line(init_opts=opts.InitOpts(width="1400px", height="500px"))
    line.add_xaxis(x_data)
    line.add_yaxis(
        series_name="男女差值",
        y_axis=y_data_man_woman.values,
        # 标出关键点的数据
        markpoint_opts=opts.MarkPointOpts(data=[
            opts.MarkPointItem(type_="min"),
            opts.MarkPointItem(type_="max"),
            opts.MarkPointItem(type_="average")
        ]),
        label_opts=opts.LabelOpts(is_show=False),
        markline_opts=opts.MarkLineOpts(
            data=[opts.MarkLineItem(type_="average")]))
    line.set_global_opts(
        title_opts=opts.TitleOpts(title="中国70年(1949-2019)男女差值(万人)",
                                  pos_left="center",
                                  pos_top="bottom"),
        legend_opts=opts.LegendOpts(is_show=False),
        xaxis_opts=opts.AxisOpts(
            name='年份',
            name_location='end',
            type_="value",
            min_="1949",
            max_interval=1,
            # 设置x轴不必与y轴的0对齐
            axisline_opts=opts.AxisLineOpts(is_on_zero=False),
            axislabel_opts=opts.LabelOpts(is_show=True,
                                          color="#000",
                                          interval=0,
                                          rotate=90),
            axistick_opts=opts.AxisTickOpts(is_show=True,
                                            is_align_with_label=True),
            axispointer_opts=opts.AxisPointerOpts(
                type_="shadow", label=opts.LabelOpts(is_show=True))),
        yaxis_opts=opts.AxisOpts(name='差值(万人)',
                                 type_="value",
                                 position="left",
                                 axislabel_opts=opts.LabelOpts(is_show=True)),
    )

    # 5、渲染图像,将多个图像显示在一个html中
    page = Page(layout=Page.DraggablePageLayout)
    page.add(pie)
    page.add(line)
    page.render('population_sex.html')
Пример #10
0
def draw_roc(fpr: list,
             tpr: list,
             area_color: list = [],
             is_show_ks: bool = True,
             title: str = "ROC") -> Line:
    """
    Description:
    1. Draw roc

    Params:
    fpr
    tpr
    area_color: [area_under_yx, area_under_curve]
    is_show_ks
    title

    Return:
    None
    """
    area_color = area_color or [
        LIGHT_COLORS["COLD"][3], LIGHT_COLORS["WARM"][5]
    ]
    # Draw roc
    line = Line().add_xaxis(fpr)
    line.add_yaxis(
        "",
        tpr,
        is_smooth=True,
        areastyle_opts=opts.AreaStyleOpts(
            opacity=0.5,
            color=area_color[1],
        ),
        label_opts=opts.LabelOpts(is_show=None),
        color=area_color[1],
        symbol_size=0,
    )
    line.add_yaxis(
        "",
        fpr,
        is_smooth=True,
        areastyle_opts=opts.AreaStyleOpts(
            opacity=0.5,
            color=area_color[0],
        ),
        label_opts=opts.LabelOpts(is_show=None),
        color=area_color[1],
        symbol_size=0,
    )
    line.set_global_opts(title_opts=opts.TitleOpts(title=title,
                                                   pos_left="center"),
                         xaxis_opts=opts.AxisOpts(type_="value", name="FPR"),
                         yaxis_opts=opts.AxisOpts(type_="value", name="TPR"),
                         toolbox_opts=opts.ToolboxOpts())
    if is_show_ks:
        line.set_series_opts(markline_opts=opts.MarkLineOpts(data=[
            opts.MarkLineItem(
                name="KS",
                type_="value",
                x=max(zip(tpr - fpr, fpr))[1],
            )
        ]))
    return line