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

797
Views
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 answers
Answer question

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 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!