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

Axis automarging does not work #334

Open
MinnML opened this issue Dec 27, 2023 · 6 comments
Open

Axis automarging does not work #334

MinnML opened this issue Dec 27, 2023 · 6 comments

Comments

@MinnML
Copy link

MinnML commented Dec 27, 2023

Hello,
I cannot get the Plotly.js automargin feature to work in React for axis ticks and labels.

Here is an example using official example:

// ...
var data = [
  {
    x: ['Apples', 'Oranges', 'Watermelon', 'Pears'],
    y: [3, 2, 1, 4],
    type: 'bar'
  }
];
var layout = {
  autosize: false,
  width: 500,
  height: 500,
  yaxis: {
    title: 'Y-axis Title',
    ticktext: ['long label','Very long label','3','label'],
    tickvals: [1, 2, 3, 4],
    tickmode: 'array',
    automargin: true,
    titlefont: { size:20 },
  },
};

  return (
    <div className="card-page">
        <Plot data={data} layout={layout}></Plot>
   </div>

What I get is this:
newplot

Am I missing a setting?

@alexcjohnson
Copy link
Collaborator

@saber1440 what plotly.js version do you have? You can verify it by hovering on the plotly logo:
Screenshot 2024-01-04 at 20 02 16

@MinnML
Copy link
Author

MinnML commented Jan 5, 2024

@alexcjohnson, it is the same version as you have in the picture, v2.27.1.

@alexcjohnson
Copy link
Collaborator

hmm ok - the only other thing I can think of is sometimes if you render the plot too soon after page load it doesn't have the font yet and gets the sizing wrong - but that doesn't really look like what's going on here, that's usually only off by a little bit whereas this looks like it's not trying to automargin at all.

I suppose you can check whether plotly.js really got the right settings by looking at _fullLayout in the JS console, ie:

> document.querySelector('.js-plotly-plot')._fullLayout.yaxis.automargin  // assuming there's only one plotly plot on your page
< true

Normally there's no difference between how figures work in react-plotly.js and plain plotly.js, and your figure works fine for me in plain plotly.js https://codepen.io/alexcjohnson/pen/gOErOzO

@MinnML
Copy link
Author

MinnML commented Jan 5, 2024

Thanks for looking at this for me. I ran the following in playcode.io. The result are as expected:

import React from 'react';
import Plot from "react-plotly.js";

export function App(props) {

  var data = [
  {
    x: ['Apples', 'Oranges', 'Watermelon', 'Pears'],
    y: [1, 2, 3, 4],
    z: [[2, 5, 6, 7], [3, 5, 6, 7], [5, 6, 1, 0], [1, 3, 4, 9]],
    type: 'heatmap'
  }
];
var layout = {
  autosize: false,
  width: 500,
  height: 500,
  yaxis: {
    title: 'Y-axis Title',
    ticktext: ['long label','Very long label','3','label'],
    tickvals: [1, 2, 3, 4],
    tickmode: 'array',
    automargin: true,
    titlefont: { size:20 },
  },
};

  return (
    <div>
        <Plot data={data} layout={layout}></Plot>
    </div>
  );
}

But putting the exact code in my source didn't work as expected.
image

As I am new to react, I cannot think of any other settings or configs that could affect this. I even tried removing the whole content of my App.css file.

@MinnML
Copy link
Author

MinnML commented Jan 5, 2024

@alexcjohnson, I figured out what is causing this issue. It is setting the body dir that I have for internationalization.
For example, try this in the example you provided in codepen.io:

<body dir='rtl'>
  <div id="myDiv"></div>
</body>

Any thoughts on how I can fix this?

@alexcjohnson
Copy link
Collaborator

Ah, nice find! I'll bring that back into the plotly.js repo and we can see about finding a fix (and/or a workaround)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants