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

795
Visualizações
How to change font size and color + move datalabels in chart.js with chartjs-plugin-datalabels?

I want to add datalabels to the top of each bar and color them in black and make it in bigger font. Like this: my chart with necessary labels

But all that I tried don't change the chart.

I add chartjs and charts-plugin-datalabels:

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-datalabels/2.0.0/chartjs-plugin-datalabels.js"></script>

Chart creating function:

function bars_chart(data, period) {
        Chart.register(ChartDataLabels)
        document.querySelector('#chartContainer').innerHTML = '<canvas id="bar-chart-horizontal"></canvas>'

        var chartConfig = {
          type: 'bar',
          data: {
              labels: period,
              datasets: data
            },
          options: {
              responsive: true,
              plugins: {
                  dataLabels: {
                      font: {
                          color: "#000000",
                          size: 25
                      },
                      color: "#000000"
                  },
                  legend: {
                      position: 'top',
                  },
                  title: {
                      display: true,
                      text: 'Chart.js Bar Chart'
                  },
              }
          },
        };

        new Chart(document.getElementById("bar-chart-horizontal"), chartConfig);
    }

It looks like there is a simple solution but I can't find it)

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

0

Your namespace is wrong, you putted a capital L in there while it needs to be fully lower case. After that you can set anchor and align to 'end' to get the value on top of the bars:

Chart.register(ChartDataLabels)

const options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: 'pink'
    }]
  },
  options: {
    plugins: {
      datalabels: {
        color: 'black',
        font: {
          size: 40
        },
        anchor: 'end',
        align: 'end'
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-datalabels/2.0.0/chartjs-plugin-datalabels.js"></script>
</body>

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