Hi guys, I have array that produce from table query, that will used in gvChart (a jQuery Google Chart Plugin).
Array
(
[0] => Array
(
[total_perbulan] => 90000000
[bulan] => 8
)
[1] => Array
(
[total_perbulan] => 0
[bulan] => 8
)
[2] => Array
(
[total_perbulan] => 0
[bulan] => 8
)
[3] => Array
(
[total_perbulan] => 11000000
[bulan] => 8
)
[4] => Array
(
[total_perbulan] => 9900000
[bulan] => 7
)
)
I use array function from php.net to try grouping then sum the group into new key value.
function array_distinct ($array, $group_keys, $sum_keys = NULL, $count_key = NULL){
if (!is_array ($group_keys)) $group_keys = array ($group_keys);
if (!is_array ($sum_keys)) $sum_keys = array ($sum_keys);
$existing_sub_keys = array ();
$output = array ();
foreach ($array as $key => $sub_array){
$puffer = NULL;
#group keys
foreach ($group_keys as $group_key){
$puffer .= $sub_array[$group_key];
}
$puffer = serialize ($puffer);
if (!in_array ($puffer, $existing_sub_keys)){
$existing_sub_keys[$key] = $puffer;
$output[$key] = $sub_array;
}
else{
$puffer = array_search ($puffer, $existing_sub_keys);
#sum keys
foreach ($sum_keys as $sum_key){
if (is_string ($sum_key)) $output[$puffer][$sum_key] += $sub_array[$sum_key];
}
#count grouped keys
if (!array_key_exists ($count_key, $output[$puffer])) $output[$puffer][$count_key] = 1;
if (is_string ($count_key)) $output[$puffer][$count_key]++;
}
}
return $output;
}
function createBarChartValue($arrayNya = array()) {
$dataArray = $this->array_distinct($arrayNya, 'bulan', 'total_perbulan');
for($bulanDigit = 1; $bulanDigit<=12; $bulanDigit++) {
foreach($dataArray as $key => $value) {
//debug($value);
//echo $bulanDigit.' - '.$value['bulan'].'<br>';
//echo '<p>Bulan ke-'.$value['bulan'].' => '.$value['total_perbulan'].'</p>';
if($value['bulan'] == $bulanDigit) {
//$value['bulan'];
//echo '<td><p>Bulan ke-'.$value['bulan'].' => '.$value['total_perbulan'].'</td>';
echo '<td>'.$value['total_perbulan'].'</td>';
} else {
echo '<td></td>';
}
}
}
}
And in HTML View, Google Chart Column created based on createBarChartValue() return value.
<table id='chart' class="resultChart" border="1">
<thead>
<tr>
<th></th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>Mei</th>
<th>Jun</th>
<th>Jul</th>
<th>Agu</th>
<th>Sep</th>
<th>Okt</th>
<th>Nop</th>
<th>Des</th>
</tr>
</thead>
<tbody>
<tr>
<th>Barang</th>
<?php
if(!empty($tmpArray2)) {
$html->createBarChartValue($tmpArray2);
} else {
echo '<td></td>';
}
?>
</tr>
<tr>
<th>Jasa</th>
<?php
if(!empty($tmpArray3)) {
$html->createBarChartValue($tmpArray3);
} else {
echo '<td></td>';
}
?>
</tr>
<tr>
<th>Lainnya</th>
<?php
if(!empty($tmpArray4)) {
$html->createBarChartValue($tmpArray4);
} else {
echo '<td></td>';
}
?>
</tr>
</tbody>
</table>
But, something wrong. Table result is expand in double column, so google chart will not work with this table structures.
The Table Column Result:
<table id="chart" class="resultChart" border="1">
<thead>
<tr>
<th></th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>Mei</th>
<th>Jun</th>
<th>Jul</th>
<th>Agu</th>
<th>Sep</th>
<th>Okt</th>
<th>Nop</th>
<th>Des</th>
</tr>
</thead>
<tbody>
<tr>
<th>Barang</th>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>9900000</td><td>101000000</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<th>Jasa</th>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>409800000</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<th>Lainnya</th>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>0</td><td>0</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
</tbody>
</table>
Please help me to correct the table result, I confused in handling the arrays that work for the 12 month column.
Thank you