Display php chart in columns

Asked

Viewed 38 times

0

I created a php test that is working properly. But I have a small problem, because the test is composed of 128 questions which are divided into 7 groups. But the number of questions in each group is different.

The complete code:

$sql = 'SELECT * FROM questoes WHERE ativo = 1';
mysqli_query($conn, 'SET NAMES utf8');
$retorno = mysqli_query($conn, $sql);
$totaisChakras = array(1 => 24, 2 => 34, 3 => 24, 4 => 27, 5 => 20, 6 => 16, 7 => 16);
$arrPorcentagens = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0);
// 24   34  24  27  20  16  16
$arrayQuestoesChakras = array(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0);
while($lista = mysqli_fetch_assoc($retorno)){

    foreach($resultados as $idx => $value){
        if($value > 0){
            if($lista['nrPergunta'] == $idx){
                if($lista['ch1']){
                    $arrayQuestoesChakras[1]++;
                }
                if($lista['ch2']){
                    $arrayQuestoesChakras[2]++;
                }
                if($lista['ch3']){
                    $arrayQuestoesChakras[3]++;
                }
                if($lista['ch4']){
                    $arrayQuestoesChakras[4]++;
                }
                if($lista['ch5']){
                    $arrayQuestoesChakras[5]++;
                }
                if($lista['ch6']){
                    $arrayQuestoesChakras[6]++;
                }
                if($lista['ch7']){
                    $arrayQuestoesChakras[7]++;
                }
            }
        }
    }
}

for($x = 1; $x <= 7; $x++){
    if($arrayQuestoesChakras[$x] > 0){
        $arrPorcentagens[$x] = round(($arrayQuestoesChakras[$x] * 100) / $totaisChakras[$x]);
    }else{
        $arrPorcentagens[$x] = 2;
    }
}

In summary, the part that displays the result is this:

<div class="progress-bar progress-bar-raiz active" role="progressbar" aria-valuenow="<?php echo $arrayQuestoesChakras[1] ?>" aria-valuemin="0" aria-valuemax="24" style="height: <?php echo $arrPorcentagens[1] ?>%;">

See that column height I’m determiinando by:

style="height: <?php echo $arrPorcentagens[1] ?>%;">

Although it works very well, the problem is that proportionally the look can be glaring. Since each group has a specific amount of questions.

What better way to solve this ? it is common to use css to determine the height of a graph generated by php ??

  • 1

    Can you edit your question and show a little more about the backend? For example, the $arrPorcentagens formation.

  • finish and update with the rest of the code

  • From what I understand, you want to change height as issues are resolved?

  • yes that’s right. It’s working. The only problem is in relation to the ratio in hroa to display the result. For example, 50% of the second chakra (q has 34 questions = 17 positive questions), compared to 50% of the sixth chakra (has 16 questions = 8 positive questions). The result appears with variation, and should be proportional.

  • can print the result?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.