예제 #1
0
    def add_reset_button(self, idx=0):
        """

        :param idx: Index to return graph to.
        :return:
        """
        # Making HTML button.
        self.make(id=escape('reset'), onclick=escape("onReset()"), value=escape("Reset"), type=escape("button"))
        self.graph.script[self.gdiv_id] += "function onReset() { \n\t"                      # Action when button pressed

        for line in self.graph.script[self.gdiv_id].split("\n"):                            # Set all counters to 0.
            if "var counter" in line and " = 0;" in line and "\t" not in line:
                self.graph.script[self.gdiv_id] += line[3:-2] + "{idx};\n\t".format(idx=idx)

        self.graph.script[self.gdiv_id] += "\n\t "                                           # Beautifying

        self.graph.script[self.gdiv_id] += "if (is_paused) { \n\t"                          # Updating to initial frame
        for line in self.graph.script[self.gdiv_id].split("\n"):                            # if animation is paused, by
            if "Plotly.animate(" in line:                                                   # running Plotly.animate
                self.graph.script[self.gdiv_id] += line + "\n\t"                            # function just one.
                break

        self.graph.script[self.gdiv_id] += "} \n"                                            # Closing if conditional
        self.graph.script[self.gdiv_id] += "} \n"                                            # Ending function.

        return self.graph.script[self.gdiv_id]
예제 #2
0
    def add_play_button(self):
        # Making HTML button.
        self.make(id=escape('run'), onclick=escape("onToggleRun()"), value=escape("Play"), type=escape("button"))

        self.graph.script[self.gdiv_id] += "function onToggleRun() { \n\t"                  # Action when button pressed
        self.graph.script[self.gdiv_id] += "is_paused = !is_paused;\n\t"                    # Toggle boolean.

        # If paused, set button text to display Play, else display Pause.
        self.graph.script[self.gdiv_id] += "document.getElementById('run').value = (is_paused) ? 'Play':'Pause';\n\t"
        self.graph.script[self.gdiv_id] += "requestAnimationFrame(update);\n}\n"             # Running animation.

        return self.graph.script[self.gdiv_id]
예제 #3
0
    def finish_plot(self, o_name=None, **kwargs):
        """
        Formats objects as JSON, then updates instance and class dictionaries.
        :param o_name: Name of JS object.
        :param kwargs: Attributes of JS object.
        :return:
        """
        if o_name:
            o_name = str(o_name)

        else:
            o_name = str(self.type[1:-1])

        o_name = "{v_name}".format(v_name=o_name + str(len(self.objects)))
        data = Data(
            **kwargs
        ).json  # Processing keyword arguments and converting to JSON.
        data_js = jsify(data=data,
                        variable_name=o_name)  # Making JS data object.
        o_name = escape(o_name)
        self.objects[o_name] = str(
            data_js)  # Storing current plot's attributes in class' dictionary.
        self.instance_objects[o_name] = str(
            data_js
        )  # Storing current plot's attributes in instance's dictionary.
        return
예제 #4
0
    def show_all(self, **kwargs):
        """
        Writes self.objects dictionary as a JS script, i.e. all plots declared in all instances that inherit from class
        Graph.
        :return:
        """
        plots = "["

        # Setting raw data (lists) to variable names in JS.
        for variable, value in self.objects.items():
            self.script[self.div_id] += "var {variable} = {value}; \n".format(
                variable=var(variable, True), value=value)

        self.script[self.div_id] += "\n"  # Beautifying.

        for v_name, obj in self.objects.items(
        ):  # Writing JS objects (that will be plotted)
            obj = var(obj, decode=True)  # Escaping all references to variables

            self.script[self.div_id] += obj
            plots += v_name[1:-1] + ", "

        plots = plots[:-2]
        plots += "]"

        self.make_layout(**kwargs)

        self.script[self.div_id] += self.layout
        self.script[self.div_id] += "Plotly.newPlot({div_name}, {plots}, layout);"\
            .format(div_name=escape(self.div_id), plots=plots)
        return self.script
예제 #5
0
 def __init__(self, div_id="plot"):
     """
     Sets up HTML
     :param div_id: HTML Id of the div in which graph is placed.
     """
     self.type = escape(type)
     self.instance_objects = {}
     self.data = {
     }  # Dictionary that contains all data objects w/ attributes.
     self.script = dict(
     )  # Variable to store HTML/JS script as and when generated.
     self.div_id = escape(div_id)  # HTML div where graph will be located.
     self.script[self.div_id] = ""
     self.layout = "var layout"  # String to contain layout parameters for graph.
     self.buttons = {}  # Dictionary to store all buttons related to plot
     self.slider = {}
     return
예제 #6
0
    def __init__(self, graph, div_id, **kwargs):
        # Sanity check - is the graph of the correct type?
        if str(graph.__class__.__bases__).find("<class 'translator.statics.Graph'>") == -1:
            raise TypeError("Object graph is not derived from base class 'Graph', but from {bases}"
                            .format(bases=str(graph.__class__.__bases__)))

        self.graph = graph
        self.gdiv_id = self.graph.div_id
        self.div = Tag("div", id=escape(div_id)).html
        self.buttons = []

        if len(kwargs.items()) > 0:
            self.make(**kwargs)
        return
예제 #7
0
    def __init__(self, z=None, div_id="surface_plot", **kwargs):
        """
        Generates script to make PlotlyJS 3D Surface plot.
        :param z: List containing data for all surfaces, each surface in an inner list.
        :param div_id: ID of the HTML div tag this plot will be drawn in.
        :param width: Width of plot
        :param height: Height of plot
        :param kwargs: Attributes of the surface plot.
        """
        Graph.__init__(self, div_id=div_id)
        self.type = escape("surface")

        if z:
            self.plot(z=z, type=self.type, **kwargs)

        return
예제 #8
0
    def __init__(self,
                 x=None,
                 y=None,
                 mode="markers",
                 div_id="scatter_plot",
                 **kwargs):
        """
        :param x: List containing x-axis values
        :param y: List containing y-axis values
        :param mode: Show markers, markers+lines or lines.
        :param div_id: ID of the HTML div tag this plot will be drawn in.
        :param width: Width of plot
        :param height: Height of plot
        :param kwargs: Attributes of the scatter plot.
        """
        Graph.__init__(self, div_id=div_id)
        self.type = escape("scatter")

        if x and y:
            self.plot(x=x, y=y, mode=mode, type=self.type, **kwargs)

        return
예제 #9
0
    def show(self, **kwargs):
        """
        Writes objects in self.data as a JS script, i.e. only the plots that exist in the declared instance.
        :param kwargs: Any attribute-value pair that needs to be written to the layout variable.
        :return:
        """
        plots = "["

        # Setting raw data (lists) to variable names in JS.
        for variable, value in self.data.items():
            self.script[self.div_id] += "var {variable} = {value}; \n".format(
                variable=var(variable, True), value=value)

        traces = list(range(len(self.instance_objects.items())))
        self.script[self.div_id] += "\n"  # Beautifying.

        for v_name, obj in self.instance_objects.items(
        ):  # Writing JS objects (that will be plotted)
            obj = var(obj, decode=True)  # Escaping all references to variables

            self.script[self.div_id] += obj
            plots += v_name[1:-1] + ", "

        plots = plots[:-2]
        plots += "]"

        self.make_layout(**kwargs)

        self.script[self.div_id] += self.layout
        self.script[self.div_id] += "function plot() {{\n\t" \
            "Plotly.newPlot({div_name}, {{data: {plots}, traces: {traces}, layout: layout}}); \n\t " \
            "}} \n\n" \
            "plot();"\
            .format(div_name=escape(self.div_id), traces=traces, plots=plots)

        return self.script