Example #1
0
def generate_scatter_html_with_multi_tables(tables,
                                            title="可转债分布情况",
                                            subtitle=None,
                                            select=None,
                                            use_personal_features=True,
                                            price_field_name='转债价格'):
    chart_id = str(abs(hash(title)))
    scatter = Scatter(
        opts.InitOpts(height='700px',
                      width='1424px',
                      theme=ThemeType.LIGHT,
                      chart_id=chart_id))
    scatter.add_js_funcs('chart_' + chart_id +
                         """.on('click', function(params){
            // alert(params)
            popWin.showWin("1200","600", params['data']['value'][3]);
        })
    """)

    for label, table in tables.items():
        if select is not None and label not in select:
            continue

        x = []
        y = []

        point_items = []
        rows = table._rows
        for row in rows:
            record = get_record(table, row)
            if record['溢价率'] is None:
                continue

            x1 = record[price_field_name]
            x.append(x1)
            y1 = record['溢价率'].replace('%', '') * 1
            amount = record.get("持有数量", 0)
            bond_name = record['名称'].replace('转债', '')
            bond_code = record['bond_code']
            bond_code = trade_utils.rebuild_bond_code(bond_code)
            y.append([y1])

            # todo symbol 轮出:triangle 轮入:diamond
            if use_personal_features and record.get(
                    'hold_id') is not None and amount > 0:
                point_items.append(
                    opts.MarkPointItem(
                        coord=[x1, y1],
                        symbol_size=amount,
                        itemstyle_opts=opts.ItemStyleOpts(
                            opacity=0.5,
                            border_color='#000',
                            border_width=1,
                        ),
                        value=[bond_name, x1, y1, bond_code, amount]))
            else:
                point_items.append(
                    opts.MarkPointItem(
                        coord=[x1, y1],
                        # symbol_size=symbol_size,
                        itemstyle_opts=opts.ItemStyleOpts(
                            # color='#fff',
                            # border_color='#000'
                        ),
                        value=[bond_name, x1, y1, bond_code]))

        scatter.add_xaxis(x)

        scatter.add_yaxis(
            label,
            y,
            label_opts=opts.LabelOpts(is_show=False),
            markpoint_opts=opts.MarkPointOpts(
                symbol='circle',
                symbol_size=10,
                data=point_items,
                label_opts=opts.LabelOpts(
                    position='bottom',
                    formatter=JsCode(
                        'function(params){return params.value[0]}'))),
            markline_opts=opts.MarkLineOpts(
                linestyle_opts=opts.LineStyleOpts(type_='dashed'),
                is_silent=True,
                label_opts=opts.LabelOpts(
                    position='end',
                    formatter=JsCode(
                        "function(params){return params.data['name'];}")),
                data=[
                    opts.MarkLineItem(x=utils.trade_utils.MID_X,
                                      name='中位数(' +
                                      str(utils.trade_utils.MID_X) + '元)'),
                    opts.MarkLineItem(y=utils.trade_utils.MID_Y,
                                      name='中位数(' +
                                      str(utils.trade_utils.MID_Y) + '%)'),
                ]))

    # scatter.add_xaxis(x)

    scatter.set_global_opts(
        title_opts=opts.TitleOpts(title=title,
                                  subtitle=subtitle,
                                  pos_left='center'),
        tooltip_opts=opts.TooltipOpts(formatter=get_hover_js_code()),
        legend_opts=opts.LegendOpts(pos_bottom=-5,
                                    # selected_mode='single'
                                    ),
        toolbox_opts=opts.ToolboxOpts(feature={
            'dataZoom': {},
        }),
        # visualmap_opts=opts.VisualMapOpts(
        #     type_="color", max_=150, min_=20, dimension=1
        # ),
        xaxis_opts=opts.AxisOpts(
            # data=None,
            type_='value',
            name='转债价格(元)',
            name_gap=30,
            is_scale=True,
            name_location='middle',
            splitline_opts=opts.SplitLineOpts(is_show=False),
            axislabel_opts=opts.LabelOpts(formatter='{value}元'),
            axisline_opts=opts.AxisLineOpts(is_on_zero=False,
                                            symbol=['none', 'arrow'])),
        yaxis_opts=opts.AxisOpts(
            type_='value',
            name='转股溢价率(%)',
            name_rotate=90,
            name_gap=35,
            name_location='middle',
            is_scale=True,
            axislabel_opts=opts.LabelOpts(formatter='{value}%'),
            splitline_opts=opts.SplitLineOpts(is_show=False),
            axisline_opts=opts.AxisLineOpts(is_on_zero=False,
                                            symbol=['none', 'arrow'])))
    scatter_html = scatter.render_embed('template.html', env)
    return scatter_html
Example #2
0
def create_scatter(title,
                   sub_title,
                   field_name,
                   label_y,
                   point_items,
                   x,
                   y,
                   hover_maker=None,
                   click_maker=None):
    chart_id = str(abs(hash(title)))
    scatter = Scatter(
        opts.InitOpts(height='700px', width='1424px', chart_id=chart_id))
    if click_maker is None:
        click_maker = get_click_js_code

    scatter.add_js_funcs(click_maker(chart_id))
    scatter.add_xaxis(xaxis_data=x)
    show_label = True
    if len(point_items) > 100:
        show_label = False

    scatter.add_yaxis(
        series_name="",
        y_axis=y,
        color=choice(colors),
        label_opts=opts.LabelOpts(is_show=False, ),
        markline_opts=opts.MarkLineOpts(
            linestyle_opts=opts.LineStyleOpts(type_='dashed'),
            is_silent=True,
            label_opts=opts.LabelOpts(
                position='end',
                formatter=JsCode(
                    "function(params){return params.data['name'];}")),
            data=[
                opts.MarkLineItem(x=utils.trade_utils.MID_X,
                                  name='中位数(' + str(utils.trade_utils.MID_X) +
                                  '元)'),
                opts.MarkLineItem(y=utils.trade_utils.MID_Y,
                                  name='中位数(' + str(utils.trade_utils.MID_Y) +
                                  '%)'),
            ]),
        markpoint_opts=opts.MarkPointOpts(
            symbol='circle',
            symbol_size=12,
            data=point_items,
            label_opts=opts.LabelOpts(
                is_show=show_label,
                position='bottom',
                formatter=JsCode('function(params){return params.value[0]}'))))
    if title is not None and title.strip(' ') != '':
        title = "=========" + title + "========="
    if hover_maker is None:
        hover_maker = get_hover_js_code

    scatter.set_global_opts(
        title_opts=opts.TitleOpts(title=title,
                                  subtitle=sub_title,
                                  pos_left='center'),
        tooltip_opts=opts.TooltipOpts(formatter=hover_maker(field_name), ),
        toolbox_opts=opts.ToolboxOpts(feature={
            'dataZoom': {},
        }),
        xaxis_opts=opts.AxisOpts(
            # data=None,
            type_='value',
            name='转债价格(元)',
            name_gap=30,
            is_scale=True,
            name_location='middle',
            splitline_opts=opts.SplitLineOpts(is_show=False),
            axislabel_opts=opts.LabelOpts(formatter='{value}元'),
            axisline_opts=opts.AxisLineOpts(is_on_zero=False,
                                            symbol=['none', 'arrow'])),
        yaxis_opts=opts.AxisOpts(
            type_='value',
            name=label_y,
            name_rotate=90,
            name_gap=35,
            name_location='middle',
            is_scale=True,
            axislabel_opts=opts.LabelOpts(formatter='{value}%'),
            splitline_opts=opts.SplitLineOpts(is_show=False),
            axisline_opts=opts.AxisLineOpts(is_on_zero=False,
                                            symbol=['none', 'arrow'])),
    )
    scatter_html = scatter.render_embed('template.html', env)
    return "<br/>" + scatter_html