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

356
Views
Agregue etiquetas internas al gráfico de barras apiladas horizontales chart.js usando 'chartjs-plugin-labels'

Tengo instalado el siguiente paquete para agregar etiquetas a mis gráficos:

 import * as pluginDataLabels from "chartjs-plugin-labels";

Funciona bien en mis gráficos de líneas, gráficos circulares y gráficos de barras normales. Sin embargo, no muestra nada en mi gráfico de barras apiladas. Aquí está mi código:

 public stackedChartLabels = ["Test 1"]; public stackedChartPlugins = [pluginDataLabels]; public stackedChartData = [ { label: "Low", data: [67.8], backgroundColor: "#D6E9C6", // green stack: "a", }, { label: "Moderate", data: [20.7], backgroundColor: "#FAEBCC", // yellow stack: "a", }, { label: "High", data: [11.4], backgroundColor: "#EBCCD1", // red stack: "a", }, ]; public stackedChartOptions = { responsive: true, maintainAspectRatio: false, plugins: { labels: [ { render: function (args) { return args.value; }, position: "inside", fontColor: "#023d7d", fontSize: 12, }, ], }, scales: { yAxes: [ { ticks: { fontSize: 12, fontColor: "#023d7d", }, }, ], xAxes: [ { ticks: { min: 0, max: 100, fontSize: 12, fontColor: "#023d7d", precision: 0, }, }, ], }, };

Aquí está el código html:

 <canvas baseChart [datasets]="stackedChartData" [labels]="stackedChartLabels" [chartType]="'horizontalBar'" [legend]="false" [plugins]="stackedChartPlugins" [options]="stackedChartOptions" height="150" > </canvas>

¿Alguien sabe cómo puedo hacer que se muestren las etiquetas? Quiero que muestren el valor en cada segmento.

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

0

El siguiente código solucionó esto:

 animation: { duration: 1000, onComplete() { let chartInstance = this.chart; let ctx = chartInstance.ctx; ctx.textAlign = "end"; ctx.textBaseline = "middle"; ctx.fillStyle = "#fff"; ctx.font = "10px sans-serif"; this.data.datasets.forEach(function (dataset, i) { var label = dataset.label; var meta = chartInstance.controller.getDatasetMeta(i); meta.data.forEach(function (bar, index) { var data = dataset.data[index]; if (label.indexOf("%") >= 0) data += "%"; ctx.fillText(data + "%", bar._model.x, bar._model.y); }); }); }, },
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!