예제 #1
0
def generate(width=None,
             height=None,
             bounds=None,
             glyphs=None,
             background="background",
             map_layers={},
             save_tool="save",
             active_scroll="wheel_zoom",
             toolbar_location="above"):
    """Map-figure from supplied bokeh input parameters."""
    x_range = Range1d(start=bounds[0], end=bounds[2], bounds=None)

    y_range = Range1d(start=bounds[1], end=bounds[3], bounds=None)

    map_hover = HoverTool(tooltips=[("Locatie",
                                     "@shortName"), ("ID", "@locationId")])

    map_hover.toggleable = False

    tools = ["tap", "wheel_zoom", "pan", "reset", map_hover, save_tool]

    map_fig = figure(tools=tools,
                     active_scroll=active_scroll,
                     height=height,
                     width=width,
                     x_range=x_range,
                     y_range=y_range,
                     toolbar_location=toolbar_location)

    map_fig.axis.visible = False
    map_fig.toolbar.logo = None
    map_fig.toolbar.autohide = True
    map_fig.xgrid.grid_line_color = None
    map_fig.ygrid.grid_line_color = None
    map_fig.select(type=TapTool)

    # add background
    tile_source = get_tileource(background)
    map_fig.add_tile(tile_source, name="background")

    # add additionalmap_layers if any
    if map_layers:
        layer_names = list(map_layers.keys())
        layer_names.reverse()
        for layer_name in layer_names:
            tile_source = get_tileource(layer_name, urls=map_layers)
            if "alpha" in map_layers[layer_name].keys():
                alpha = map_layers[layer_name]["alpha"]
            else:
                alpha = 1
            map_fig.add_tile(tile_source,
                             name=layer_name,
                             visible=map_layers[layer_name]["visible"],
                             alpha=alpha)

    if glyphs:
        for glyph in glyphs:
            glyph_type = glyph["type"]
            glyph.pop("type")
            getattr(map_fig, glyph_type)(**glyph)
        map_fig.legend.click_policy = "hide"

    return map_fig
예제 #2
0
def generate(
    width=None,
    height=None,
    sizing_mode=None,
    x_bounds=None,
    y_bounds=None,
    x_range=None,
    y_range=None,
    title="",
    x_axis_visible=True,
    x_axis_label="",
    y_axis_label="",
    show_toolbar=True,
    bound_limits=None,
    glyphs=None,
    active_scroll="xwheel_zoom",
    active_drag="box_zoom",
    save_tool="save",
    toolbar_location="above",
    title_visible=False,
):
    """Generate a time-figure from supplied bokeh input parameters."""
    time_hover = HoverTool(tooltips=[("datum-tijd", "@datetime{%F}"),
                                     ("waarde", "@value{(0.00)}")],
                           formatters={"@datetime": "datetime"})

    time_hover.toggleable = False

    #    pan_tool = PanTool()
    tools = [
        "pan", "box_zoom", "xwheel_zoom", "zoom_in", "zoom_out", "reset",
        "undo", "redo", save_tool, time_hover
    ]

    if not x_range:
        x_range = Range1d(start=x_bounds['start'],
                          end=x_bounds['end'],
                          bounds=bound_limits)
    x_range.min_interval = pd.Timedelta(hours=1)

    if not y_range:
        y_range = Range1d(start=y_bounds['start'],
                          end=y_bounds['end'],
                          bounds=bound_limits)
    y_range.min_interval = 0.1
    time_fig = figure(
        title=title,
        tools=tools,
        height=height,
        width=width,
        sizing_mode=sizing_mode,
        x_axis_label=x_axis_label,
        y_axis_label=y_axis_label,
        x_range=x_range,
        y_range=y_range,
        active_scroll=active_scroll,
        active_drag=active_drag,
        toolbar_location=toolbar_location,
    )

    wheel_zoom = next((i for i in time_fig.tools if type(i) == WheelZoomTool),
                      None)
    if wheel_zoom:
        wheel_zoom.speed = 0.0001

    time_fig.toolbar.logo = None
    time_fig.toolbar.autohide = False

    time_fig.title.align = "center"

    time_fig.xaxis.formatter = DatetimeTickFormatter(
        hours=["%H:%M"],
        days=["%d-%m-%Y"],
        months=["%d-%m-%Y"],
        years=["%d-%m-%Y"],
    )
    time_fig.xaxis.visible = x_axis_visible
    time_fig.title.visible = title_visible
    time_fig.yaxis[0].formatter = NumeralTickFormatter(format="0.0")
    if glyphs:
        for glyph in glyphs:
            glyph_type = glyph["type"]

            glyph.pop("type")
            getattr(time_fig, glyph_type)(x="datetime", y="value", **glyph)

        if next((True for glyph in glyphs if "legend_label" in glyph.keys()),
                False):
            time_fig.legend.click_policy = "hide"

            time_fig.add_layout(time_fig.legend[0], "right")
            time_fig.legend[0].label_text_font_size = "9pt"
    return time_fig