documentation icon indicating copy to clipboard operation
documentation copied to clipboard

(JS) Legend text gets cutoff

Open Jason94 opened this issue 7 years ago • 3 comments

For longer legend labels, the character at the end gets cutoff slightly: cutoff-legend

I noticed that padding one of the lines with spaces seems to fix the problem: fixed-legend

Here is the code that we use to render the plot, with the spaces added (2nd line):

 // Extra space on legend keys is a workaround for plotly cutting off the last character
  const oldBars = bar(getOldData(metrics).reverse(), names, `Submitted before ${cutoff}   `, OldColor);
  const newBars = bar(getNewData(metrics).reverse(), names, `Submitted since ${cutoff}`, NewColor);

const bar = (values, labels, name, color) => ({
  type: 'bar',
  y: labels,
  x: values,
  hoverinfo: 'x',
  orientation: 'h',
  marker: { color },
  name,
});

    <Plot
      data={[newBars, oldBars]}
      layout={{
        autosize: false,
        barmode: 'stack',
        width: 900,
        height: 600,
        margin: { l: 5, r: 0, t: 0, b: 60, pad: 1 },
        xaxis: {
          fixedrange: true,
          showline: false,
          showticklabels: false,
          zeroline: false
        },
        yaxis: {
          fixedrange: true,
          showline: true,
          showgrid: true,
          showticklabels: false,
          zeroline: false
        },
        legend: {
          font: {
            color: 'black',
            family: 'Helvetica',
            size: 16
          },
          traceorder: 'grouped'
        }
      }}
      config={{
        displayModeBar: false,
        scrollZoom: false,
        showAxisDragHandles: false,
        showAxisRangeEntryBoxes: false,
        sendData: false
      }}
      onClick={handleClick}
    />

Jason94 avatar Dec 04 '18 16:12 Jason94

Also having the same issue, using plotly in RShiny. Adding 4-5 spaces after just one of the legend items fixes the issue. Not sure if there are margin or padding parameters for the legend box or the legend items, but it doesn't look like it from the documentation. I looked at it through Firefox's inspector, and after editing a few values couldn't determine which parameter controlled this.

rutabagaist avatar Jul 06 '19 01:07 rutabagaist

Adding four (no fewer!) worked for me. Thanks for the suggestion @rutabagaist

Benjamin-Lee avatar Dec 24 '20 07:12 Benjamin-Lee

Possibly related problem from forums: https://community.plotly.com/t/legend-cut-off-when-plot-wrapped-in-details-html-tag/13232/2

andoks avatar May 06 '21 17:05 andoks