[英]How to plot a line in plotly from origin to the mouse coordinates of the user, using plotly/Python/Dash
I want to plot a line from origin (0,0) to the mouse coordinates (x,y) of the user. 我想绘制一条从原点(0,0)到用户的鼠标坐标(x,y)的线。 My plan is to use the mouse coordinates of the user and to use the @app.callback method to update the graph.
我的计划是使用用户的鼠标坐标,并使用@ app.callback方法更新图形。 My main problem is in getting the mouse coordinates of the user.
我的主要问题是获取用户的鼠标坐标。 I'm using plotly, dash and python.
我正在使用plotly,破折号和python。
I found a method to get the mouse coords on the plotly community ( https://community.plot.ly/t/how-to-make-shape-track-mouse-motion/1604 ), code posted by etienne at https://codepen.io/etpinard/pen/EyydEj . 我找到了一种在密谋社区上获取鼠标坐标的方法( https://community.plot.ly/t/how-to-make-shape-track-mouse-motion/1604 ),此代码由etienne发布于https: //codepen.io/etpinard/pen/EyydEj 。 The main function seems to be to the p2c part
主要功能似乎在p2c部分
gd.addEventListener('mousemove', function(evt) {
var xInDataCoord = xaxis.p2c(evt.x - l);
var yInDataCoord = yaxis.p2c(evt.y - t);
However, this is in JavaScript and I want to implement this using python and I can not find a similar method in Python. 但是,这是在JavaScript中,我想使用python来实现,而我在Python中找不到类似的方法。
Any solution? 有什么办法吗?
An useful package to get the actual mouse position within dash is the package mydcc 可以在破折号内获得鼠标实际位置的有用软件包是mydcc软件包
At first you need to import it within your script: 首先,您需要将其导入脚本中:
import mydcc
With that you can define listeners: 这样,您可以定义侦听器:
app.layout = html.Div([
mydcc.Listener( id = "listener", aim = 'graph' ),
dcc.Graph(id='graph')
])
The callback can be handled eg by this: 回调可以通过以下方式处理:
@app.callback(Output('graph', 'figure'),
[Input('uuu', 'data')])
def update_graph(mouseData):
if mouseData:
x_data = [0, mouseData['x']]
y_data = [0, mouseData['y']]
else:
x_data = [0, 10]
y_data = [0,20]
#... some code for your figure
return {'data': data, 'layout': layout}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.