Skip to content
Snippets Groups Projects
Commit 601264c8 authored by Bruno Massa's avatar Bruno Massa
Browse files

New features:

* _google_charts_chart() will set all chart-level data
* _google_charts_series() will set all series-level data
* _google_charts_values() will set all value-level data

Misc:
* Google Chart now uses the new chart data schema
parent df07a658
No related branches found
No related tags found
No related merge requests found
......@@ -18,40 +18,93 @@
*
* @param $data
* Array. A series of numeric data values
* @return
* String. The string presentation of series data
*/
function _google_charts_data_codingsimple($data) {
function _google_charts_data_codingsimple($value, $max) {
// Set the list of characters and the size of the list
$simple = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$simple_len = strlen($simple) - 1;
if ($value >= 0) {
return $simple[round($simple_len * $value / $max)];
}
else {
return '_';
}
}
function _google_charts_chart(&$chart, &$data) {
// Convert the chat TYPE into the Google Chart way.
// Since its a requirement to build the chart on Google, if the value
// was not found, return nothing and stop the execution.
$options = array(
'line2D' => 'lc',
'hbar2D' => 'bhg',
'vbar2D' => 'bvg',
'pie2D' => 'p',
'pie3D' => 'p3',
'venn' => 'v',
'scatter' => 's',
);
if (empty($options[$data['#type']])) {
return FALSE;
}
$chart[] = 'cht='. $options[$data['#type']];
// Convert the chat SIZE into the Google Chart way.
// Since its a requirement to build the chart on Google, if the value
// was not found, return nothing and stop the execution.
if (empty($data['#width']) or empty($data['#height'])) {
return FALSE;
}
$chart[] = 'chs='. $data['#width'] .'x'. $data['#height'];
// Add Title and Description to the chart
if (!empty($data['#title'])) {
$chart[] = 'chtt='. $data['#title'];
}
// Chart background color. Since the default color
// is white (#ffffff), only different colors are considered
if (!empty($data['#color']) and $data['#color'] != 'ffffff') {
$chart[] = 'chf=bg,s,'. $data['#color'];
}
return TRUE;
}
function _google_charts_series(&$chart, &$data) {
// The final output is going to be build
$chart_data = '';
// For each chart value, encode it
// Note: Underscore represents a missing value
foreach ($data as $series) {
foreach (element_children($data) as $series) {
// Include a series separator
if (!empty($chart_data)) {
$chart_data .= '|';
$chart_data .= ',';
}
// Get only the numeric values from the series
$series = _charts_series_values($data[$series]);
// Get the highest value on the series, to be a reference point
$max = max($series);
// For each series of data, scan it
foreach ($series as $value) {
if ($value >= 0) {
$chart_data .= $simple[round($simple_len * $value / $max)];
}
else {
$chart_data .= '_';
}
foreach (element_children($series) as $value) {
$chart_data .= _google_charts_data_codingsimple($series[$value], $max);
}
}
// Return the series of data
return 's:'. $chart_data;
if (empty($chart_data)) {
return FALSE;
}
$chart[] = 'chd=s:'. $chart_data;
return TRUE;
}
function _google_charts_data_codingextended() {
function _google_charts_values() {
}
......@@ -45,40 +45,17 @@ function google_charts_chartsapi(&$data) {
// Include the specific Google Chart API helper functions
include_once drupal_get_path('module', 'google_charts') .'/google_charts.inc';
// Convert the chat TYPE into the Google Chart way.
// Since its a requirement to build the chart on Google, if the value
// was not found, return nothing and stop the execution.
$options = array(
'line2D' => 'lc',
'hbar2D' => 'bhg',
'vbar2D' => 'bvg',
'pie2D' => 'p',
'pie3D' => 'p3',
'venn' => 'v',
'scatter' => 's',
);
if (empty($options[$data['basic']['charttype']])) {
$chart = array();
if (!_google_charts_chart($chart, $data)) {
return '';
}
$chart[] = 'cht='. $options[$data['basic']['charttype']];
// Convert the chat SIZE into the Google Chart way.
// Since its a requirement to build the chart on Google, if the value
// was not found, return nothing and stop the execution.
if (empty($data['layout']['width']) or empty($data['layout']['height'])) {
return '';
}
$chart[] = 'chs='. $data['layout']['width'] .'x'. $data['layout']['height'];
// Convert the chat DATA into the Google Chart way.
// Since its a requirement to build the chart on Google, if the value
// was not found, return nothing and stop the execution.
if (empty($data['data'])) {
if (!_google_charts_series($chart, $data)) {
return '';
}
$chart[] = 'chd='. _google_charts_data_codingsimple($data['data']);
// If its all ok, build the HTML img tag
return '<img src="http://chart.apis.google.com/chart?'. implode('&amp;', $chart) .'" />';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment