Skip to content

Instantly share code, notes, and snippets.

@cjsaylor
Created July 10, 2013 02:09
Show Gist options
  • Save cjsaylor/5962926 to your computer and use it in GitHub Desktop.
Save cjsaylor/5962926 to your computer and use it in GitHub Desktop.
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