Reputation: 162
im really confuse how to pass data from controller to horizontal bar bcz this is my first time dealing with charts and i hope someone can help me n tell me the best way to fetch data n pass it to chart .
i tried using foreach in view and then echo inside chart script ..when i use Print_R i can see all data .. but when i put it inside the horizontal-bar Chart it only give me 1 record . here is the screenshot of the result
and this is what i did in controller and view :
Controller
public function index()
{
$data['db1'] = $this->m_mds->isiChart();
$data['content'] = 'tempelates/MDS/content';
$data['chart'] = 'tempelates/MDS/chart';
$this->load->view('tempelate',$data);
}
and here is the foreach in view :
<figure class="highcharts-figure">
<div id="container"></div>
</figure>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<?php foreach ($db1->result_array() as $key) {
$nmbrg= $key['NAMA_BRG'];
$oos=$key['OOS'];
$kurang= $key['kurang_4'];
$osa=$key['OSA'];
}?>
<script>
Highcharts.chart('container', {
chart: {
type: 'bar',
options3d: { enabled: true, }
},
title: {
text: 'Stacked bar chart'
},
xAxis: {
categories: [<?php echo json_encode($nmbrg)?> ]
},
yAxis: {
min: 0,
title: {
text: 'Total fruit consumption'
}
},
legend: {
reversed: true
},
plotOptions: {
series: {
stacking: 'normal'
}
},
series: [{
name: 'OOS',
data: [<?php echo json_encode($oos)?>]
}, {
name: '<4',
data: [<?php echo json_encode($kurang)?>]
}, {
name: 'OSA',
data: [<?php echo json_encode($osa)?>]
}]
});
</script>
what i want to do is.. make the output of foreach like this
[{"namabrg":"YOLITE C+100 STRAWBERRY ","oos":1719,"kurang":4264,"osa":40100},{"namabrg":"YOLITE KIDS 70 ML BND 4 ORI ","oos":2,"kurang":3,"osa":162}]
and pass all data comes from foreach to horizontal bar Chart.
Upvotes: 1
Views: 152
Reputation: 1469
You need to change your PHP code block to this: (Put inidvidual values into array and implode array values to lists)
<?php
$nmbrg_values = array();
$oos_values = array();
$kurang_values = array();
$osa_values = array();
foreach ($db1->result_array() as $key) {
$nmbrg_values[] = $key['NAMA_BRG'];
$oos_values[] = $key['OOS'];
$kurang_values[] = $key['kurang_4'];
$osa_values[] = $key['OSA'];
}
$nmbrg = implode(", ", $nmbrg_values);
$oos = implode(", ", $oos_values);
$kurang = implode(", ", $kurang_values);
$osa = implode(", ", $osa_values);
?>
Upvotes: 0