Chart.js does not appear

Asked

Viewed 514 times

3

UPDATE: Now appear the Labels and graph displaying the quantity, but all in gray with the names as undefined.

I’m trying to create a chart with the Chart js., but he doesn’t show up at div, She is blank. I had already done one in this same project, but as I eliminated a module and respectively the BD related to it, I had to create another graph with other data, and now it is blank.

I have the Chart.min.js latest installed.

The function for data generation is this:

public function getTypeList($id_company){
    $array = array('0'=>0, '1'=>0, '2'=>0, '3'=>0, '4'=>0);
      $sql = "SELECT COUNT(id) as total, type FROM inventory WHERE "
            . "id_company = :id_company "
            . "GROUP BY type ORDER BY type ASC";
    $sql = $this->db->prepare($sql);
    $sql->bindValue(':id_company', $id_company);
    $sql->execute();

   if($sql->rowCount() > 0){
        $rows = $sql->fetchAll();

        foreach ($rows as $sale_item){
            $array[$sale_item['type']] = $sale_item['total'];
        }
    }
    return $array;
}

The archive js of the graph:

var rel2 = new Chart(document.getElementById("rel2"), {
type:'pie',
data:{
    labels: status_name_list,
    datasets: [{
        data: status_list,
        backgroundColor:['#0000FF','#32CD32','#FF0000', '#FFFFFF', '#000000']
    }]
  }
});  

The call on the screen:

<div class="db-row row2">
  <div class="grid-1">
    <div class="db-info">
      <div class="db-info-title">Tipos de imóveis disponíveis para venda</div>
      <div class="db-info-body" style="height: 300px">
        <canvas id="rel2"></canvas>
      </div>
    </div>
  </div>
</div>

And finally the call in the controller:

$data['status_list'] = $i->getTypeList($user->getCompany());

Is not returning mistakes, the div where the graph should appear appears blank. When I gave a var_dump($rows) returned this data, but I can not check, I’m beginner. Actually appeared the name of the fields I need, (apartment, etc.) but I do not know if is returning the quantity correctly, or the reason not appear on the screen.

array(7) {
  [0]=> array(4) {
    ["total"]=> string(1) "4"
    [0]=> string(1) "4"
    ["type"]=> string(11) "Apartamento"
    [1]=> string(11) "Apartamento"
  }
  [1]=> array(4) {
    ["total"]=> string(2) "25"
    [0]=> string(2) "25"
    ["type"]=> string(4) "Casa"
    [1]=> string(4) "Casa"
  }
  [2]=> array(4) {
    ["total"]=> string(1) "1"
    [0]=> string(1) "1"
    ["type"]=> string(19) "Casa em Condomínio"
    [1]=> string(19) "Casa em Condomínio"
  }
  [3]=> array(4) {
    ["total"]=> string(1) "2"
    [0]=> string(1) "2"
    ["type"]=> string(9) "Cobertura"
    [1]=> string(9) "Cobertura"
  }
  [4]=> array(4) {
    ["total"]=> string(1) "1"
    [0]=> string(1) "1"
    ["type"]=> string(4) "cond"
    [1]=> string(4) "cond"
  }
  [5]=> array(4) {
    ["total"]=> string(1) "1"
    [0]=> string(1) "1"
    ["type"]=> string(11) "Condomínio"
    [1]=> string(11) "Condomínio"
  }
  [6]=> array(4) {
    ["total"]=> string(1) "3"
    [0]=> string(1) "3"
    ["type"]=> string(4) "Loja"
    [1]=> string(4) "Loja"
  }
}

1 answer

1

As far as I can see, the problem is status_name_list, you have not posted how is being filled this array, appears undefined when Chart does not find the caption name for that part of the chart.

Then the label would have something like:

labels: ['Apartamento', 'Casa', 'Casa em Condomínio']

and then on the date would have the values:

data: [5,10,15]

So the grid brings you that for apartment legend has 5 and so on!

Browser other questions tagged

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