Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable_move for Lines #532

Closed
sigbjorngrini opened this issue Aug 21, 2017 · 6 comments
Closed

enable_move for Lines #532

sigbjorngrini opened this issue Aug 21, 2017 · 6 comments

Comments

@sigbjorngrini
Copy link

Hi

Is it possible to move entire lines as one does with enable_move for a scatter point?

Regards,
Sigbjorn

@dmadeka
Copy link
Contributor

dmadeka commented Aug 22, 2017

@sigbjorngrini What would the behavior you expect be? I guess you could see the HandDraw as "moving" a line. We have an issue open for moving the markers of a line to change the data - is that what you mean?

@sigbjorngrini
Copy link
Author

@dmadeka The thought is to be able to click and hold on a line and move it in x or y direction. I. e. that you add the same value to all x or y points for each point in the line. This would in effect change the data yes.

@dmadeka
Copy link
Contributor

dmadeka commented Aug 22, 2017

@sigbjorngrini This might be better done with the markers. For now, the workaround would be to put a Scatter on the points of the line (or one point) and do something like:

def observe_scatter_x_y(change):
    if change['name'] == 'x':
        line.x = line.x + change['new'] - change['old']
    else:
        line.y = line.y + change['new'] - change['old']

Does that work for you?

@sigbjorngrini
Copy link
Author

sigbjorngrini commented Aug 23, 2017

@dmadeka Thank you for the suggestion. Since I want to move all points relative to the one I physically move, the observe function reruns for every change of scatter.x and scatter.y and my kernel crashes. I found out that I could use the drag on_drag events. Something like this works as intended.

import bqplot as bq
import numpy as np

xs = bq.LinearScale()
ys = bq.LinearScale()
x = np.arange(-10, 11)
x2 = np.arange(-10, 11)
y = -x ** 3
y2 = -x2 ** 3 + 100

line = bq.Lines(x=x, y=y, scales={'x': xs, 'y': ys}, colors=['red'])
line2 = bq.Lines(x=x2, y=y2, scales={'x': xs, 'y': ys}, colors=['blue'])
scatter = bq.Scatter(x=x, y=y, scales={'x': xs, 'y': ys}, colors=['red'], enable_move=True)
scatter2 = bq.Scatter(x=x2, y=y2, scales={'x': xs, 'y': ys}, colors=['blue'], enable_move=False, marker='cross')
xax = bq.Axis(scale=xs, label='x', grid_lines='solid')
yax = bq.Axis(scale=ys, orientation='vertical', tick_format='0.2f', label='y', grid_lines='solid')

fig = bq.Figure(marks=[line, line2, scatter, scatter2], axes=[xax, yax])
        
def drag_end(s, changes):
    line.x = line.x + changes['point']['x'] - line.x[changes['index']]
    line.y = line.y + changes['point']['y'] - line.y[changes['index']]
    scatter.x = line.x
    scatter.y = line.y
    line.opacities = [1]
    scatter.visible = True
    
def drag_start(s, changes):
    scatter.visible = False
    line.opacities = [0.3]
    
def drag(s, changes):
    line.x = line.x + changes['point']['x'] - line.x[changes['index']]
    line.y = line.y + changes['point']['y'] - line.y[changes['index']]
    scatter.x = line.x
    scatter.y = line.y

scatter.on_drag_end(drag_end)
scatter.on_drag(drag)
scatter.on_drag_start(drag_start)

fig

Now, you can move around the red curve. I could now make a general function for this and make all lines with scatter move so this works for me. Ideally I wouldn't need the markers though.

@dmadeka
Copy link
Contributor

dmadeka commented Aug 23, 2017

@sigbjorngrini You could also move the line with one point right? Or is that too clunky? It would have less updating overhead. Or you could efficiently put them along your line? Maybe at every diadic partition?

@sigbjorngrini
Copy link
Author

@dmadeka Yes that would work too. I'll see what I choose. That solved my problem. Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants