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

Make auto-margin step dependent on axis label span #296

Closed
mnoble01 opened this issue Feb 29, 2016 · 42 comments
Closed

Make auto-margin step dependent on axis label span #296

mnoble01 opened this issue Feb 29, 2016 · 42 comments
Assignees
Labels
feature something new

Comments

@mnoble01
Copy link

Hi there, I have some graphs with particularly long labels/axis string values and I'm wondering if there's a way to get them to fit without changing the layout.margin option because I'm using dynamic data.

Here's CodePen showing a longer label being cut off on both a vertically and horizontally-oriented bar chart - http://codepen.io/mnoble01/pen/zqxZQx.

@etpinard
Copy link
Contributor

etpinard commented Mar 1, 2016

There's no automated way to do that at the moment. Making the auto-margin step dive into the axis labels would be a great addition though.

Renaming this issue accordingly.

@etpinard etpinard added the feature something new label Mar 1, 2016
@etpinard etpinard changed the title Long bar chart axis values are cut off Make auto-margin step dependent on axis label span Mar 1, 2016
@aleburato
Copy link
Contributor

image

Hi guys, adding my +1 to this... Please remember to take Pies into account as well :)

@bextra
Copy link

bextra commented Sep 8, 2016

+1 from me too

@gracecarey
Copy link

+1

4 similar comments
@tilleryj
Copy link

tilleryj commented Nov 4, 2016

+1

@druvisc
Copy link

druvisc commented Nov 30, 2016

+1

@ghost
Copy link

ghost commented Dec 19, 2016

+1

@edwardbetan
Copy link

+1

@donnyv
Copy link

donnyv commented Jan 30, 2017

+1

5 similar comments
@brian428
Copy link

brian428 commented Mar 3, 2017

+1

@hy9be
Copy link
Contributor

hy9be commented Mar 3, 2017

+1

@bpaszcza
Copy link

bpaszcza commented Mar 8, 2017

+1

@mariehooper
Copy link

+1

@aaneitchik
Copy link

+1

@ghost
Copy link

ghost commented Mar 20, 2017

Little workarround:

You can calculate the needed margins for your layout out of the SVG:

 function calculateLegendMargins(containingElement){
      var refBB = $(containingElement).find(".gridlayer")[0].getBBox();
      var legendContainingBB = $(containingElement).find(".plot")[0].getBBox();
      var legendBB = $(containingElement).find(".xaxislayer")[0].getBBox();

      var margin = {};
      margin.b = legendBB.height;
      margin.r = (legendBB.width+legendBB.x+legendContainingBB.x) - (refBB.width+refBB.x);
      return margin;
} 

The Plot will "jump" on pageload, but the legends should be in place.
Only working with Plotly v1.24.2, only tested with single bar plot.

Use the function with:

Plotly.plot(targetDiv,data,layout).then( function() {
  layout.margin = calculateLegendMargins(targetDiv);
  Plotly.relayout(targetDiv,layout);
});

Please also let me know if it worked for you and or your improvement suggestions! Thanks.

@ghost
Copy link

ghost commented Mar 20, 2017

If someone needs it for Plotly 1.17.2:

function calculateLegendMargins(containingElement){
              var widestTick = Math.max.apply(Math,$.map($(containingElement).find(".xtick"),function(elem){
                return elem.getBBox().x + elem.getBBox().width
              }));
              var highestTick = Math.max.apply(Math,$.map($(containingElement).find(".xtick"),function(elem){
                return elem.getBBox().height
              }));

              var refBox = $(containingElement).find(".xtick").last();
              var legendContainingBB = refBox.parent()[0].getBBox();
              var refBB = refBox.parent().parent().find("rect")[0].getBBox();

              var margin = {};
              margin.b = highestTick;
              margin.r = (widestTick+legendContainingBB.x) - (refBB.width);
              return margin;
            }

@cbailiss
Copy link

cbailiss commented Mar 21, 2017

+1. Without a fix for this, a lot of the formatting and styling options are unusable.
PS. Thanks for the workaround, but would prefer a proper fix, i.e. to avoid the double render, to properly work with axis titles, to properly support Plotly for R, etc.

@ghost
Copy link

ghost commented Mar 22, 2017

Workaround is by no means the solution, i agree. I'm just waiting for this already for half a year and used plotly on production site. It's more a desperate try to satisfy my customers and thought, one of you could maybe need it.
I'm still hoping there's a proper fix soon.

@dannycodes
Copy link

+1

2 similar comments
@joepax
Copy link

joepax commented Mar 27, 2017

+1

@brooksryba
Copy link

+1

@hy9be
Copy link
Contributor

hy9be commented Apr 18, 2017

I gave it try to make a fix. My change helped a bit on couple of my simple test cases, for example.

Before:
image

After:
image

The code changes are only a proof of concept: hy9be@5f5ad4f

It would be great if @etpinard or any other admins could take a look and give some feedback to me.

@hy9be
Copy link
Contributor

hy9be commented Apr 18, 2017

And I do not 100% sure about what the 3rd parameters of Plots.autoMargin means. For example for the legend for the case above, this parameter value is {x: 1.02, y: 1, l: 0, r: 88, b: 48, t: 0}. What does the fraction x and y mean here? And also what is the value for l(eft), r(ight), b(ottom), t(top) mean? The desired margins?

@etpinard
Copy link
Contributor

What does the fraction x and y mean here?

That's the starting position in normalized plot coordinates (i.e. the same coordinate system we use to position e.g. legend)

@jackparmer
Copy link
Contributor

Hey @hy9be - do you need any more help, suggestions, or feedback with this one? This looks promising!

@bewaiting4
Copy link

bewaiting4 commented May 24, 2017

Hi @jackparmer I'm following @hy9be 's fix. would you be able to explain more about the "x", "y" here? Could you explain more details about the normalized plot coordinates? I'm asking because I have trouble understanding the algorithm in plots.doAutoMargin. BTW: According to my investigation, this is also a promising fix for #1504

@hy9be
Copy link
Contributor

hy9be commented Jun 13, 2017

@jackparmer I did not get a chance to making further progress as I have similar confusion with @bewaiting4, about what "x" and "y" stands for in the Plots.autoMargin function. @etpinard explained a bit above but I failed to fully understand the whole picture behind even with his help.

Also if any one could add some comments for the core algorithm in plots.doAutoMargin: https://github.com/plotly/plotly.js/blob/master/src/plots/plots.js#L1331-L1364, I should be able to move forward.

@fabiangehring
Copy link

+1

2 similar comments
@aaronryden
Copy link

+1

@roqe
Copy link

roqe commented Aug 23, 2017

+1

@etpinard
Copy link
Contributor

I'm getting a little tired of those _+1_s.

Locking this conversion until we start working on this (which should happen soon).

@nicolaskruchten
Copy link
Contributor

I'm happy to report that this is now fixed, with the addition of the *axis.automargin attribute (see #2243). When set to true it will grow the plot margins so as to make room for both the tick labels and axis title :)

Some caveats: this doesn't interact with the legend's positioning, so tick labels can still conflict there, and for x-axes at the top, can conflict with the plot title, but it's a good start! Here's the mock that shows it in action: https://github.com/plotly/plotly.js/blob/master/test/image/baselines/long_axis_labels.png

@plotly plotly unlocked this conversation Mar 2, 2018
@fcasarramona
Copy link

Nice.
It works for pie charts?

@etpinard
Copy link
Contributor

etpinard commented Mar 5, 2018

Nice.
It works for pie charts?

Nop.

@nicolaskruchten
Copy link
Contributor

Unfortunately it just works for 2d cartesian axes at the moment.

@fcasarramona
Copy link

May I open a new issue for pie charts?

@donnyv
Copy link

donnyv commented Mar 5, 2018

@etpinard @nicolaskruchten I bet you guys thought you were done with this. 😜

@etpinard
Copy link
Contributor

etpinard commented Mar 5, 2018

Yep, we should probably 🔒 this thing again 😛

@nicolaskruchten
Copy link
Contributor

I'm happy to field some comments here for a while :)

@fcasarramona please do create a new issue for whatever else you need... we won't track any additional work on this issue.

@fcasarramona
Copy link

It already exists the issue for pie charts since Dec, 2015, 😛 "Pie charts don't always seem to fit labels to a constrained space" #75

@sorenwacker
Copy link

Long tick labels still getting cut off - Automargin functionality for Python not working.

  • plotly
  • plotly=4.4.1=py_0
  • plotly_express=0.4.1=py_0
    • django-plotly-dash==1.1.3

@nicolaskruchten
Copy link
Contributor

Hi @soerendip ! If you're encountering automargin issues in Python, please create an issue in the Plotly.py repo and we'll take a look :) https://github.com/plotly/plotly.py/issues/new

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

No branches or pull requests