Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
Is that a way to marginTop a legend positioned on bottom of a Doughnut chartjs graph?

I'm working with doughnut graphs using ChartJS and I'm having a bad time trying to create some kind of space between the graph and the legend.

That's my legend config:

plugins: {
  legend: {
    display: true,
    align: 'start',
    position: 'bottom',
  }
}

And that's the plugin I wrote to add more height to the legend:

const LegendMargin = {
  id: 'LegendMargin',
  beforeInit(chart, legend, options) {
    const fitValue = chart.legend.fit;

    chart.legend.fit = function fit() {
      fitValue.bind(chart.legend)();

      this.height += 40;
    }
  }
}

This plugin increased the height of the legend below the graph, but the text is still aligned to the top of the box, which doesn't fix the problem.

Can I somehow align the text to the bottom of the box or create some sort of margin/padding between the legend and the graph?

Thank you so much

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems like the legend top position can be controlled using the legend.top property. But this property is managed by chart.js itself. So if you change it, is gonna be overwritten. So one hacky way to get around it is to use Object.defineProperty to intercept calls to legend.top like that:

const LegendMarginPlugin = {
  id: 'LegendMarginPlugin',
  beforeInit(chart, legend, options) {
    const fitValue = chart.legend.fit;

    chart.legend.fit = function fit() {
      fitValue.bind(chart.legend)();

      let top;
      const marginTop = 30;

      Object.defineProperty(this, 'top', {
        get() {
          return top + marginTop;
        },
        set(v) {
          top = v;
        },
      });

      this.height += 40;
    };
  },
};

The value 30 and 40 here are example values that you might want to adjust for you needs.

Keep in mind that since this is not a public API it might break in future releases but it works for me on chart.js 3.9.0

The ideal is that they add support for the full Padding object on options.plugins.legend.labels.padding config option so that you could use padding.top there. But currently only a single numeric value works

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda