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

126
Visualizações
How to add specific label and values from an array into a chart - ChartJS

Hope you have a great time and also hope that you can help me.

I'm trying to code a Doughnut Chart with ChartJS. I have a database in which I have a table called "Signal Quality". In this table I will have five ocurrences: N/A, Excellent, Good, Average, Bad signal. So first, I create an index for each and count how many there are and then store them in an array and for each index will have a label. Here is what I have as an array :

$dataPoints = array(
    array("label" => "Excellent ", "value" => $lqi_excellent),
    array("label" => "Good ",      "value" => $lqi_good),
    array("label" => "Average ",   "value" => $lqi_average),
    array("label" => "Limit ",     "value" => $lqi_limit),
    array("label" => "N/A ",       "value" => $lqi_na)
);

Here is my script code :

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script>
        new Chart(document.getElementById("MyChart"), {
            type: 'doughnut',
            data: {
                labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
                datasets: [
                {
                  backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
                  data: [1,2,3,4,5]
              }
              ]
          },
          options: {
              title: {
                display: true,
                text: 'Predicted world population (millions) in 2050'
            }
        }
    });
</script>

My goal would be to be able to replace the labels and data by the values of the array. How can I do it please?

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

0

use foreach to create two array

   $labelArray = array();
   foreach ($dataPointsas as $key => $val) {
       $labelArray[$key] = $val['label'];
   }
    
   $valueArray = array();
   foreach ($dataPointsas as $key => $val) {
       $valueArray[$key] = $val['value'];
   }

then use

labels: $labelArray

and

data: $valueArray 
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