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

fix for incorrect final position when dragging legend #6528

Merged
merged 5 commits into from
Mar 17, 2023

Conversation

bmaranville
Copy link
Contributor

Bug fix

When layout.legend.xanchor !== 'left' or layout.legend.yanchor !== 'top', the draggable legend enabled by setting config.editable=true or config.edits.legendPosition=true does not work properly.

During the drag, the position updates correctly, but on release it snaps to a new position. This is because the width and height of the legend are not taken into account in the calculation of dragElement.align(...), with the second argument dv=0 for both xf and yf.

This PR addresses that by including the width and height of the legend in the calculation.

Examples

To reproduce the bug, show either of these figures and drag the legend:

{
  "data":
    [
      {
        "name":"A very long name indeed (though it could be longer)",
        "x":[1,2,3],
        "y":[2,3,4],
        "type":"scatter"
      }    
    ],
  "layout": 
    {
      "showlegend": true,
      "legend": {
        "x": 0.5,
        "y": 0.5,
        "xanchor": "center",
        "yanchor": "middle"
      }
    },
  "config":
    {
      "edits": {"legendPosition": true}
    }
}
{
  "data":
    [
      {
        "name":"A very long name indeed (though it could be longer)",
        "x":[1,2,3],
        "y":[2,3,4],
        "type":"scatter"
      }    
    ],
  "layout": 
    {
      "showlegend": true,
      "legend": {
        "x": 0.5,
        "y": 0.5,
        "xanchor": "left",
        "yanchor": "bottom"
      }
    },
  "config":
    {
      "edits": {"legendPosition": true}
    }
}

legendObj._height and legendObj._width are used for positioning the element at the outset, so it seems appropriate to use them again here
@archmoj
Copy link
Contributor

archmoj commented Mar 17, 2023

Thanks for the PR!
💃

@archmoj archmoj merged commit 7a77f1d into plotly:master Mar 17, 2023
@bmaranville bmaranville deleted the fix-drag-legend branch March 17, 2023 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants