Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

286
Vistas
Chart JS Tooltip Currency Problem - Stacked Bar Chart

I use ChatJS and i want to add an € Symbol after every number in the Tooltip. On my most charts it works perfectly but the Stacked Bar Chart makes me problems.

Here is an example Code like mine (Just a basic Example)

var barChart = new Chart(ctx_barChart, {
  type: 'bar',
  data: {
    labels: ["Fruits"],
    datasets: [{
        label: [
          ["Apple"]
        ],
        data: [12],
        backgroundColor: 'blue',
        barThickness: 80,
      },
      {
        label: ["Banana"],
        data: [15],
        backgroundColor: 'red',
        barThickness: 80,
      },
    ]
  },
  options: {
    maintainAspectRatio: false,
    indexAxis: 'y',
    responsive: true,
    scales: {
      x: {
        stacked: true,
        display: false,
      },
      y: {
        stacked: true,
        display: false,
        mirror: true,
      }
    },
    plugins: {
      tooltip: {
        callbacks: {
          label: function(context) {
            let label = context.dataset.label || '';

            if (label) {
              label += ': ';
            }
            if (context.parsed.y !== null) {
              label += new Intl.NumberFormat('en-US', {
                style: 'currency',
                currency: 'EUR'
              }).format(context.parsed.y);
            }
            return label;
          }
        }
      },
      legend: {
        display: false
      },
      title: {
        display: false,
      },
    },
  },
});

The tooltips both show 0,00$, but not my Values 12 and 15. Anyone have an suggestiont?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should use context.parsed.x instead of context.parsed.y since it's a horizontal bar chart.

Please take a look at your amended code and see how it works.

new Chart('barChart', {
  type: 'bar',
  data: {
    labels: ["Fruits"],
    datasets: [{
        label: "Apple",
        data: [12],
        backgroundColor: 'blue',
        barThickness: 80,
      },
      {
        label: "Banana",
        data: [15],
        backgroundColor: 'red',
        barThickness: 80,
      },
    ]
  },
  options: {
    maintainAspectRatio: false,
    indexAxis: 'y',
    responsive: true,
    scales: {
      x: {
        stacked: true,
        display: false,
      },
      y: {
        stacked: true,
        display: false,
        mirror: true,
      }
    },
    plugins: {
      tooltip: {
        callbacks: {
          label: ctx => ctx.dataset.label + ': ' + new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(ctx.parsed.x)
        }
      },
      legend: {
        display: false
      },
      title: {
        display: false,
      },
    },
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
<canvas id="barChart" height="100"></canvas>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda