plotly / plotly/plotly.js

Plot margins do not reset on Plotly.react() if layout.legend.xref or yref are set to "container"

Open
#6,612 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P3
Dominant language
JavaScript
Stars
18.3k
Forks
2k
Avg merge
2d 12h
Merged PRs (30d)
28

Description

I have designed an use case where there is no data to display on the chart: please imagine an user selecting from a list of countries to display some data and the selected country has no data to be displayed. I have all the data available, the page doesn't need to be refreshed (we're trying to keep it dynamic) so the Plot needs to be updated.

Please check this pen: https://codepen.io/bleah1/pen/NWOoZXd where, after 4 seconds, both plots are updated.

As such, I update the Plot using Plotly.react() with no data and a special layout:

const noDataChartLayout = {
  width: 400,
  autosize: true,
  title: null,
  margin: { t: 0, b: 0, pad: 0 },
  // showlegend: false,
  legend: {
    yref: "paper",
    xref: "paper",
    y: 0,
    x: 0
  },
  hovermode: false,
  dragmode: false,
  xaxis: {
    visible: false
  },
  yaxis: {
    visible: false
  },
  annotations: [
    {
      text: "Chart unavailable.<br>No matching data found.",
      xref: "paper",
      yref: "paper",
      showarrow: false,
      font: {
        size: 20
      }
    }
  ]
};

The issue is that when the legend is set with the new layout.legend.xref / yref features to container the margins of the plot do not reset even after updating the layout.

I've tried using or commenting the following layout props:

  // showlegend: false,
  legend: {
    yref: "paper",
    xref: "paper",
    y: 0,
    x: 0
  },

but the annotations still aren't centered when using layout.legend.xref / yref set to container.

With container:

image

With paper:

image

I would like to use the new layout.legend.xref / yref features because with them I don't need to manually calculate legend.x and y positioning and layout.margin for each view size for each chart.

Any thoughts ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior in the linked CodePen, focusing on the Plotly.react() update that changes the legend xref/yref to "container" and then restores the no-data layout. Trace how the container legend affects plot margins and annotation positioning; done means the margins reset and the annotation is centered after the update, matching the paper-reference behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.