question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Word wrap for long labels

See original GitHub issue

Is there an option to reflow long text labels?

  • For example, a long label without reflow (plotly tries to autocorrect by slanting the labels instead of reflowing the text, but it still doesn’t fit) plotlylonglabels1
  • And a simple manual fix with a few <br> in the code that I’d like to have Plotly be able to do with a simple wrap: true option plotlylonglabels2

JS code for this chart

var data = [{
// first screenshot labels
    //x: ['giraffes', 'orangutans', "Very Long Text Label That Doesn't Fit Properly"],
// second screenshot labels
    x: ['giraffes', 'orangutans', "Very Long<br>Text Label That<br>Doesn't Fit<br>Properly"],
    y: [20, 14, 23],
    type: 'bar'
}]
Plotly.newPlot('PlotlyDivSimple', data, {})

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
punitaojhacommented, Apr 23, 2021

Has the plotly library managed to solve this issue now. Looking for some better alternatives.

3reactions
eugenesvkcommented, Apr 1, 2016

Ok, understood. Would help with a PR if I had a clue how to do that 😃

Pardon my ignorance, but is there no way to introduce a JavaScript function that would be applied to all labels before they’re fed to SVG/WebGL, where it becomes complicated? For example, I’ve google-found this function (it’s not great since there is an issue with long words, but just as an illustration)

function stringDivider(str, width, spaceReplacer) {
    if (str.length>width) {
        var p=width
        for (;p>0 && str[p]!=' ';p--) {
        }
        if (p>0) {
            var left = str.substring(0, p);
            var right = str.substring(p+1);
            return left + spaceReplacer + stringDivider(right, width, spaceReplacer);
        }
    }
    return str;
}

that I can use internally to shorten the labels

var label = "Very Long Text Label That Doesn't Fit Properly"
x: ['giraffes', 'orangutans', stringDivider( label, 20, "<br>") ],

Also, as a side question — is there a way to read the bar width in plotly so I don’t have to adjust the width manually and can make it reflow properly when chart size is adjusted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Word-wrap of Label text in text-area - html - Stack Overflow
I'm trying to do a wrap using a paragraph tag, but it is not working. My code: <label for="qual">This is the format i...
Read more >
Word wrapping and hyphenation for (long) labels
Suppose I want to have a field with 200 character width represented as a label on a map. Suppose I want the label...
Read more >
How to wrap label text ? - MSDN - Microsoft
Set a fixed width and use Word-break:break-all to strictly wrap the content. <asp:Label ...
Read more >
Msc-generator 5.4.0: Word Wrapping and Long Labels
Word wrapping: Use text.wrap=yes (and perhaps a fixed hscale ), in this case the long labels wrap into multiple lines. Automatic scaling: Use...
Read more >
Wrapping and breaking text - CSS: Cascading Style Sheets
In CSS, if you have an unbreakable string such as a very long word, by default it will overflow any container that is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found