Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
¿Es esa una forma de marginar una leyenda colocada en la parte inferior de un gráfico de Donut chartjs?

Estoy trabajando con gráficos de donas usando ChartJS y estoy pasando un mal momento tratando de crear algún tipo de espacio entre el gráfico y la leyenda.

Esa es mi configuración de leyenda:

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

Y ese es el complemento que escribí para agregar más altura a la leyenda:

 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; } } }

Este complemento aumentó la altura de la leyenda debajo del gráfico, pero el texto aún está alineado con la parte superior del cuadro, lo que no soluciona el problema.

¿Puedo de alguna manera alinear el texto con la parte inferior del cuadro o crear algún tipo de margen/relleno entre la leyenda y el gráfico?

Muchas gracias

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Parece que la posición superior de la leyenda se puede controlar mediante la propiedad legend.top . Pero esta propiedad es administrada por el mismo chart.js. Entonces, si lo cambia, se sobrescribirá. Entonces, una forma ingeniosa de evitarlo es usar Object.defineProperty para interceptar llamadas a legend.top de esa manera:

 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; }; }, };

Los valores 30 y 40 aquí son valores de ejemplo que quizás desee ajustar para sus necesidades.

Tenga en cuenta que, dado que esta no es una API pública, podría fallar en versiones futuras, pero funciona para mí en chart.js 3.9.0

Lo ideal es que agreguen soporte para el objeto Padding completo en la opción de configuración options.plugins.legend.labels.padding para que pueda usar padding.top allí. Pero actualmente solo funciona un único valor numérico

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!