Skip to content

fix for incorrect final position when dragging legend - #6528

Merged
archmoj merged 5 commits into
plotly:masterfrom
bmaranville:fix-drag-legend
Mar 17, 2023
Merged

fix for incorrect final position when dragging legend#6528
archmoj merged 5 commits into
plotly:masterfrom
bmaranville:fix-drag-legend

Conversation

@bmaranville

Copy link
Copy Markdown
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}
    }
}

@archmoj archmoj added bug something broken community community contribution status: reviewable labels Mar 17, 2023
Comment thread src/components/legend/draw.js Outdated
legendObj._height and legendObj._width are used for positioning the element at the outset, so it seems appropriate to use them again here
Comment thread src/components/legend/draw.js Outdated
@archmoj

archmoj commented Mar 17, 2023

Copy link
Copy Markdown
Contributor

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

Labels

bug something broken community community contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants