Ejemplo n.º 1
0
def render_ajax_graph(context):
    graph_data_range = context["data_range"]
    graph_render_options = context["render_options"]
    graph_recipe = context["definition"]

    start_time_var = html.request.var("start_time")
    end_time_var = html.request.var("end_time")
    step_var = html.request.var("step")
    if start_time_var is not None and end_time_var is not None and step_var is not None:
        start_time = float(start_time_var)
        end_time = float(end_time_var)
        step = float(step_var)
    else:
        start_time, end_time = graph_data_range["time_range"]
        step = graph_data_range["step"]

    size = graph_render_options["size"]

    resize_x_var = html.request.var("resize_x")
    resize_y_var = html.request.var("resize_y")

    if resize_x_var is not None and resize_y_var is not None:
        render_opt_x, render_opt_y = context["render_options"]["size"]
        size_x = max(min_resize_width,
                     float(resize_x_var) / html_size_per_ex + render_opt_x)
        size_y = max(min_resize_height,
                     float(resize_y_var) / html_size_per_ex + render_opt_y)
        config.user.save_file("graph_size", (size_x, size_y))
        size = (size_x, size_y)

    range_from_var = html.request.var("range_from")
    range_to_var = html.request.var("range_to")
    if range_from_var is not None and range_to_var is not None:
        vertical_range: Optional[Tuple[float, float]] = (float(range_from_var),
                                                         float(range_to_var))
    else:
        vertical_range = None

    if html.request.has_var("pin"):
        artwork.save_graph_pin()

    if html.request.has_var("consolidation_function"):
        graph_recipe["consolidation_function"] = html.request.var(
            "consolidation_function")

    graph_render_options["size"] = size
    graph_data_range["time_range"] = (start_time, end_time)
    graph_data_range["vertical_range"] = vertical_range
    graph_data_range["step"] = step

    # Persist the current data range for the graph editor
    if graph_render_options["editing"]:
        save_user_graph_data_range(graph_data_range)

    graph_artwork = artwork.compute_graph_artwork(graph_recipe,
                                                  graph_data_range,
                                                  graph_render_options)
    html_code = render_graph_html_content(graph_artwork, graph_data_range,
                                          graph_render_options)

    return {
        "html": html_code,
        "graph": graph_artwork,
        "context": {
            "graph_id": context["graph_id"],
            "definition": graph_recipe,
            "data_range": graph_data_range,
            "render_options": graph_render_options,
        }
    }
Ejemplo n.º 2
0
def render_ajax_graph(context):
    graph_data_range = context["data_range"]
    graph_render_options = context["render_options"]
    graph_recipe = context["definition"]

    if html.request.has_var("start_time"):
        start_time = float(html.request.var("start_time"))
        end_time = float(html.request.var("end_time"))
        step = float(html.request.var("step"))
    else:
        start_time, end_time = graph_data_range["time_range"]
        step = graph_data_range["step"]

    if html.request.has_var("resize_x"):  # then has always also resize_y
        size_x = max(
            min_resize_width,
            float(html.request.var("resize_x")) / html_size_per_ex +
            context["render_options"]["size"][0])
        size_y = max(
            min_resize_height,
            float(html.request.var("resize_y")) / html_size_per_ex +
            context["render_options"]["size"][1])
        size = size_x, size_y
        config.user.save_file("graph_size", size)
    else:
        size = context["render_options"]["size"]

    if html.request.has_var("range_from"):
        range_from = float(html.request.var("range_from"))
        range_to = float(html.request.var("range_to"))
        vertical_range = (range_from, range_to)
    else:
        vertical_range = None

    if html.request.has_var("pin"):
        artwork.save_graph_pin()

    if html.request.has_var("consolidation_function"):
        graph_recipe["consolidation_function"] = html.request.var(
            "consolidation_function")

    graph_render_options["size"] = size
    graph_data_range["time_range"] = (start_time, end_time)
    graph_data_range["vertical_range"] = vertical_range
    graph_data_range["step"] = step

    # Persist the current data range for the graph editor
    if graph_render_options["editing"]:
        save_user_graph_data_range(graph_data_range)

    graph_artwork = artwork.compute_graph_artwork(graph_recipe,
                                                  graph_data_range,
                                                  graph_render_options)
    html_code = render_graph_html_content(graph_artwork, graph_data_range,
                                          graph_render_options)

    return {
        "html": html_code,
        "graph": graph_artwork,
        "context": {
            "graph_id": context["graph_id"],
            "definition": graph_recipe,
            "data_range": graph_data_range,
            "render_options": graph_render_options,
        }
    }