Skip to content

Instantly share code, notes, and snippets.

@cjsaylor cjsaylor/old.php
Created Jul 10, 2013

Embed
What would you like to do?
Google Visualization CakePHP refactor
<?php
protected function loadDataAndLabels($data, $graph_type) {
$o = '';
foreach($data['labels'] as $label) {
foreach($label as $type => $label_name) {
$o.= "data.addColumn('$type', '$label_name');\n";
}
}
$data_count = count($data['data']);
$label_count = count($data['labels']);
$o.= "data.addRows($data_count);\n";
for($i = 0; $i < $data_count; $i++) {
for($j=0; $j < $label_count; $j++) {
$value = $data['data'][$i][$j];
$type = key($data['labels'][$j]);
if($type == 'string') {
$o.= "data.{$this->chart_types[$graph_type]['data_method']}($i, $j, '$value');\n";
} else {
$o.= "data.{$this->chart_types[$graph_type]['data_method']}($i, $j, $value);\n";
}
}
}
return $o;
}
<?php
protected function loadDataAndLabels($data) {
$formatted = array(
'cols' => array(),
'rows' => array()
);
foreach ($data['labels'] as $labels) {
foreach ($labels as $type => $label) {
$formatted['cols'][] = compact('label', 'type');
}
}
foreach ($data['data'] as $datum) {
$entry = array_map(function($entry) {
return array('v' => $entry);
}, $datum);
$formatted['rows'][] = array(
'c' => $entry
);
}
return json_encode($formatted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.