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

211
Visualizações
Chart JS : Display the percentage of labels without getContext('2d')

I'm using the library chartJs to display the data that i get from my MySQL database. In the fucntion below, AddValuesToNavigatorChart takes the browsers data and the percentage data and displays them on the pie chart. What i need to know, is how to add '%' in my labels ? I found few answer with getContent('2d').. using the old way, but it's not working. This is the function :

 function AddValuesToNavigatorChart(browsers, percentage) {
            checkNavigatorChart()
            const data = {
                labels: browsers,
                datasets: [{
                    data: percentage,
                    backgroundColor: [
                        'rgb(192,192,192)',
                        'rgb(255,0,0)',
                        'rgb(0,0,0)',
                        'rgb(105,105,105)'
                    ],
                    hoverOffset: 4
                }]
            };
            const config = {
                tooltips: {
                    enabled: false
                },
                type: 'pie',
                data: data,
                plugins: [ChartDataLabels],
                options: {
                    showTooltips: false,
                    plugins: {
                        datalabels: {
                            color: 'rgb(255,255,255)',
                            borderWidth: '2',
                            align: 'top'
                        },
                        legend: {
                            position: 'bottom',
                            align: 'center',
                            labels: {
                                boxWidth: 12,
                                padding: 15,
                                usePointStyle: true,
                                pointStyle: 'circle'
                            }
                        },
                        title: {
                            text: 'Navigator Chart',
                            display: true,
                            color: 'rgb(0,0,0)',
                            position: 'top'
                        }
                    }
                }
            };
            const NavigatorChart = new Chart(
                document.getElementById('NavigatorChart'),
                config
            );
        }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Please provide the version of chartjs that you are using.

Did you mean you want to show the % in the label of the tooltip?

For version v3.8.0, here is a working code I have just made, Link

As in option, you can edit the label of tooltip as you want:

options: {
    plugins: {
      tooltip: {
        callbacks: {
          label: (tooltipItem) => {
              //tooltipItem is for the slice of the pie you are hovering now
              return ' ' + tooltipItem.parsed + '%';
            },
        }
      }
    }
  }
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