Plot Actions

Plots can be configured to run code or other cells when the user clicks on or types into them.


In [ ]:
abc = 0; // test variable

In [ ]:
def random = new Random()
def p = new Plot(showLegend: true, useToolTip: false);
p << new Line(x: [1, 2, 3], y: [2, 3, 4], width: 10, displayName: "line 1")
         .onClick({info -> info.graphics.displayName = "new name"})


p << new Line(x: [1, 2, 3], y: [5, 6, 7,], width: 10, displayName: "line 2")
         .onClick({info -> info.graphics.y[1] = random.nextInt(10)})

In [ ]:
def p = new Plot(showLegend: true, useToolTip: false);

p << new Line(x: [1, 2, 3], y: [2, 3, 4], width: 10, displayName: "line 1")
         .onClick({
            abc++
            beakerx.runByTag("on_click_any_action")
          })

In [ ]:
println abc

In [ ]:
plot = new Plot(useToolTip: false);
plot << new Points(x: (1..5), y: (1..5), size: 12.0, color: Color.orange, outlineColor: Color.black, displayName: "orange").onClick("run_tag")
plot << new Points(x: (1..5), y: (3..8), size: 12.0, color: Color.green, outlineColor: Color.black, displayName: "green").onClick("run_tag")

In [ ]:
def details = plot.details
def item = details.graphics
def index = details.index
def key = details.key
def tag = details.tag
def action = details.actionType
println ("You clicked on " + item.displayName + " " + item.class.simpleName + " (element with coordinates [" + item.x[index] + "," + item.y[index] + "])")
println "Key pressed = " + key + " Tag = " + tag  + " Action = " + action

In [ ]:
barsPlot = new Plot(useToolTip: false);
barsPlot << new Bars(x: (1..5), y: [5, 2, 4, 3, 7], color: Color.green, outlineColor: Color.black, width: 0.3)
        
        //Also buttons like KeyboardCodes.UP_ARROW is handled by jupyter notebook
        .onKey(KeyboardCodes.SPACE, {info -> info.graphics.y[info.index]++})

        //Also buttons like KeyboardCodes.DOWN_ARROW is handled by jupyter notebook
        .onKey(KeyboardCodes.CAPS_LOCK, {info -> info.graphics.y[info.index]--})
        
        //Tag events working
        .onKey("T", "run_tag2")

In [ ]:
def details = barsPlot.details
def item = details.graphics
def index = details.index
def key = details.key
def tag = details.tag
def action = details.actionType
println ("Key action on " + item.class.simpleName + " (element with coordinates [" + item.x[index] + "," + item.y[index] + "])")
println "Key pressed = " + key + " Tag = " + tag  + " Action = " + action

In [ ]:
barsPlot = new Plot(useToolTip: false);
barsPlot << new Bars(x: (1..5), y: [5, 2, 4, 3, 7], color: Color.green, outlineColor: Color.black, width: 0.3)
        
        //Buttons like KeyboardCodes.UP_ARROW is handled by jupyter notebook
        .onKey(KeyboardCodes.SPACE, {
            abc++
            beakerx.runByTag("run_tag3")
        })

In [ ]:
println abc

In [ ]: