<?php

include('r_dbc.php');

$ccs = "";
$ccexpenses_tot = "";

$sql = "select r.code_categ,c.desc_categ,sum(r.rab_number) as ccexpenses
		from ccmanager r, categs c
		where r.pid = '".$pid."'
		and r.pid = c.pid
		and r.code_categ = c.code_categ
		and r.mid in (select x.mid from ccmanager_id x where x.pid = '".$pid."' and x.expenses = 1)
		group by r.code_categ
		order by 3 desc
		limit 0,10";
		
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
   // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
		$code_categ 	= $row["code_categ"];
		$desc_categ 	= $row["desc_categ"];		
		$cc				= $desc_categ;
		$ccexpenses 	= $row["ccexpenses"];
		$c_ccexpenses	= number_format($ccexpenses, 0, $septh, $sepdc);

		// $ccwidth = $barwidth  *($ccexpenses / $ccexpenses_max);
		
		$ccs = $ccs."','".$cc;
		$ccexpenses_tot		= $ccexpenses_tot.",".$ccexpenses;		

		}		
    }
 
mysqli_close($conn);

$ccs 			= "[".substr($ccs,2)."']";
$ccexpenses_tot = "[".substr($ccexpenses_tot,1)."]";


?>

<script>
new Chart(document.getElementById("bar-chart-horizontal-categ"), {
    type: 'horizontalBar',
    data: {
      labels: <?php echo $ccs ?>,
      datasets: [
        {
          label: "<?php echo $desc_manager ?>",
          backgroundColor: ["#3e95cd","#8e5ea2","#3cba9f","#e8c3b9","#c45850","#3e95cd","#8e5ea2","#3cba9f","#e8c3b9","#c45850","#3e95cd","#8e5ea2","#3cba9f","#e8c3b9","#c45850","#3e95cd","#8e5ea2","#3cba9f","#e8c3b9","#c45850","#3e95cd","#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
          data: <?php echo $ccexpenses_tot; ?>
        }
      ]
    },
    options: {
      legend: { display: false },
      title: {
        display: true,
        text: '<?php echo $desc_manager." ".$title_chart_bar_categ ?>'
      }
    }
});
</script>