# Moving shapes with mouse in plotly.js (+reactjs)

**URL:** <https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457>\
**Category:** plotly.js\
**Created:** [July 3, 2018, 8:08pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457 "2018-07-03T20:08:04Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![fingerpori](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@fingerpori](https://community.plotly.com/u/fingerpori)\
**Post date:** [July 3, 2018, 8:08pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/1 "2018-07-03T20:08:04Z")

</div>

Hi,

A part of my React.js+plotly.js web app is a scatter plot of a time series. I want to have movable vertical lines for controlling some behavior of the app. I can draw the lines using shapes, but don’t know how to move them using the mouse. Is there a way to do this?

You can find an example in [https://codepen.io/fingerpori/pen/GGLWoz](https://codepen.io/fingerpori/pen/GGLWoz). I want to be able to move the red and green vertical lines left or right.

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [July 3, 2018, 8:42pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/2 "2018-07-03T20:42:23Z")

</div>

See [https://plot.ly/javascript/configuration-options/#edit-mode](https://plot.ly/javascript/configuration-options/#edit-mode)

---

<div class="post-metadata">

**Author:** ![fingerpori](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@fingerpori](https://community.plotly.com/u/fingerpori)\
**Post date:** [July 3, 2018, 9:28pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/3 "2018-07-03T21:28:32Z")

</div>

Thanks, now I can move the shapes (the green and red lines). Still two problems: 1) I would like to limit the movement to left and right, no up and down and 2) How can my code react to the movement of the shapes? I did not find any related events in the documentation.

---

<div class="post-metadata">

**Author:** ![etienne](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/etienne/32/17_2.png) [@etienne](https://community.plotly.com/u/etienne)\
**Post date:** [July 3, 2018, 10:01pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/4 "2018-07-03T22:01:34Z")

</div>

> [@fingerpori](#):
>
> 1. I would like to limit the movement to left and right, no up and down

We don’t expose a way to restrict edit-mode motions at the moment

> [@fingerpori](#):
>
> How can my code react to the movement of the shapes? I did not find any related events in the documentation.

Simply listen to `plotly_relayout`

---

<div class="post-metadata">

**Author:** ![fingerpori](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@fingerpori](https://community.plotly.com/u/fingerpori)\
**Post date:** [July 3, 2018, 10:54pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/5 "2018-07-03T22:54:26Z")

</div>

Thank you very much, you saved my day (or actually night)! I cancel the up-down movements in the event listener by forcing the line back to the same vertical position.

---

<div class="post-metadata">

**Author:** ![fjrial](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@fjrial](https://community.plotly.com/u/fjrial)\
**Post date:** [July 23, 2018, 3:09pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/6 "2018-07-23T15:09:50Z")

</div>

Hi @fingerpori, I’m trying to achieve the same as you (move a shape but only to the left-right)… Which event did you use ?? would you mind to share your code ?. Thanks!

Edit: what I’m trying to achieve is to have a vertical line bar that I could move to another position. I was previously using Jqplot, and now migrating all my charts to plotly. In Jqplot moving a line only in one direction vertical/horizontal is straightforward…  
I have code implemented in plotly to move points to a mouse.x mouse.y position (coordinates in pixels related to the chart), but that does not reflect on the data assigned to the plot object.

Example of what I want to achieve (already done in jqplot)  
 ![Peek%202018-07-23%2017-27](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/a/a834c7dacfc9d4c084676494a1e85fe5cbf35004.gif)

---

<div class="post-metadata">

**Author:** ![fjrial](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@fjrial](https://community.plotly.com/u/fjrial)\
**Post date:** [July 24, 2018, 9:42am UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/7 "2018-07-24T09:42:01Z")

</div>

After some work, I figured it out to make something similar…

![Peek%202018-07-24%2011-29](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/8/868058478774670591c625d38d0667aee3cbba62.gif)

Basically:

-A line shape in my chart  
-Disable the dragging events for the boundaries of the shape (in CSS). This only allows dragging the bar vertically/horizontally.  
-Create the vertical line greater than required (use a custom multiplier -10/10 based on min/max values of data). If you adjust it nicely, your vertical bar never gets out of the chart.  
-yaxis range set to min/max values of data. This way, chart is focused on the data, not on the bigger vertical shape we’ve created.  
-Attached a function in the ‘plotly\_afterplot’ to: retrieve the new line position, and redraw it again to adjust its vertical values to its original values (this is just in case, the user moved the shape vertically)

---

<div class="post-metadata">

**Author:** ![fingerpori](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@fingerpori](https://community.plotly.com/u/fingerpori)\
**Post date:** [August 6, 2018, 10:31am UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/8 "2018-08-06T10:31:14Z")

</div>

One more question: I have moving shapes now working by listening to onRelayout as you suggested. Now I want a different activity for just clicking on a shape. I tried to listen onClick, but it does nothing. Is the relayout listener somehow overriding the click listener and what should I do to catch clicks (maybe a different mode in my app for this activity)?

[Edited]  
To experiment, I forked plotly.js and added an attribute ‘editable’ to shapes. If the plot is editable, you can still have some uneditable shapes by setting attribute editable: false in the definition of the shape. This way I can get the behavior I wanted. See [https://github.com/eskonuutila/plotly.js](https://github.com/eskonuutila/plotly.js). The fork does minor changes to three files [src/components/shapes/attributes.js](https://github.com/plotly/plotly.js/compare/master...eskonuutila:master#diff-edae1a36f7ffc67eacfc1842f304c82a), [src/components/shapes/defaults.js](https://github.com/plotly/plotly.js/compare/master...eskonuutila:master#diff-70e989ae64b124dd8a77dcdd0068889a), and [src/components/shapes/draw.js](https://github.com/plotly/plotly.js/compare/master...eskonuutila:master#diff-bbb234d9b0e7cdf3c156bbaf7f915bd0).

This feature could be useful to some other users of plotly.js too.

Esko

---

<div class="post-metadata">

**Author:** ![fjrial](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@fjrial](https://community.plotly.com/u/fjrial)\
**Post date:** [September 26, 2018, 11:28am UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/9 "2018-09-26T11:28:21Z")

</div>

Hi @fingerpori.

Thanks a lot for sharing those files.  
I don’t need that feature right now, but maybe in the future, who knows 😉

About clicking on a shape, my shapes are editable (draggable), so a click event, as far as I know, it’s impossible

Best Regards.  
Javier

---

<div class="post-metadata">

**Author:** ![mayank](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@mayank](https://community.plotly.com/u/mayank)\
**Post date:** [April 1, 2019, 5:18am UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/10 "2019-04-01T05:18:38Z")

</div>

Hi @fjrial can you please tell me how did you do this  
“Disable the dragging events for the boundaries of the shape (in CSS). This only allows dragging the bar vertically/horizontally.”

by setting editable true , its also allowing me to edit labels.

---

<div class="post-metadata">

**Author:** ![joesaad](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/joesaad/32/4873_2.png) [@joesaad](https://community.plotly.com/u/joesaad)\
**Post date:** [July 10, 2019, 5:15pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/11 "2019-07-10T17:15:22Z")

</div>

@fingerpori I think i need to use your fork. My use case is that I want some shapes to editable and others not. After quite a research I was not able to figure out if this level of customization is doable or not. Reading this, I could find that you are allow for some shapes to editable, which means others will be locked.  
If this is the case, what is the best way to use this fork? do i have to replace those files (attributes.js, defaults.js & draw.js) into my npm package? I don’t think this is ideal since other developers might accidentally update ploty-dist npm and this will have your fork changes overwritten. What is the best way to use your fork if this level of customization is available in it?

Thanks,

---

<div class="post-metadata">

**Author:** ![josevera](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@josevera](https://community.plotly.com/u/josevera)\
**Post date:** [August 30, 2019, 3:24pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/12 "2019-08-30T15:24:28Z")

</div>

@fjrial How did you disable the dragging events for the boundaries of the shape (in CSS). ?

---

<div class="post-metadata">

**Author:** ![fjrial](https://avatars.discourse-cdn.com/v4/letter/f/c6cbf5/32.png) [@fjrial](https://community.plotly.com/u/fjrial)\
**Post date:** [September 2, 2019, 9:22am UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/13 "2019-09-02T09:22:56Z")

</div>

Hi @josevera

use this CSS and modify it accordingly (this is for “circle”)

/\*\* restrick pointer events to avoid shapes being dragged using its boundaries \*/  
circle {  
pointer-events: none !important;  
}

---

<div class="post-metadata">

**Author:** ![George.1912](https://avatars.discourse-cdn.com/v4/letter/g/51bf81/32.png) [@George.1912](https://community.plotly.com/u/George.1912)\
**Post date:** [July 9, 2020, 3:26am UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/14 "2020-07-09T03:26:06Z")

</div>

> [@fjrial](#):
>
> circle {  
> pointer-events: none !important;  
> }

Hi Fijral,

If you could please share your codepen, it would be great.

---

<div class="post-metadata">

**Author:** ![PeterC](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@PeterC](https://community.plotly.com/u/PeterC)\
**Post date:** [September 20, 2020, 6:53pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/15 "2020-09-20T18:53:44Z")

</div>

I would also appreciate you sharing the code. I am trying to get a similar behaviour.

---

<div class="post-metadata">

**Author:** ![archmoj](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/archmoj/32/8388_2.png) [@archmoj](https://community.plotly.com/u/archmoj)\
**Post date:** [September 26, 2020, 2:30am UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/16 "2020-09-26T02:30:24Z")

</div>

At the moment, there are two ways to make shapes editable at the moment.

1. Setting `shape.editable: true` in `layout` for a particular shape
2. Setting `config.editable: true` useful for editing all shapes, titles, etc.

---

<div class="post-metadata">

**Author:** ![PeterC](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@PeterC](https://community.plotly.com/u/PeterC)\
**Post date:** [October 8, 2020, 6:02pm UTC](https://community.plotly.com/t/moving-shapes-with-mouse-in-plotly-js-reactjs/11457/17 "2020-10-08T18:02:55Z")

</div>

Though there is no further capability to control the direction of movements etc, right? Any input on how this may be possible?
