You are here

function _google_charts_series in Charts 7

Same name and namespace in other branches
  1. 6 google_charts/google_charts.inc \_google_charts_series()

Convert all Series-level data.

Parameters

&$chart: Array. The array that will be converted into a string for Google Charts

&$data: Array. The raw data.

Return value

String. The string presentation of series data

1 call to _google_charts_series()
_google_charts_render in google_charts/google_charts.inc
Implementation of hook_charts_render().

File

google_charts/google_charts.inc, line 132
@author Bruno Massa http://drupal.org/user/67164

Code

function _google_charts_series(&$chart, &$data) {
  $options = _google_charts($data['#type']);

  // The final output is going to be build
  $chart_data = '';
  $value_labels = array();

  // For each chart value, encode it
  // Note: Underscore represents a missing value
  foreach (element_children($data) as $series) {

    // Include a series separator
    if (!empty($chart_data)) {
      $chart_data .= ',';
    }

    // Get only the numeric values from the series
    $series_data = _charts_series_values($data[$series]);

    // Get the highest value on the series, to be a reference point
    $max = max($series_data);

    // Value labels: temporary array.
    $value_labels_temp = array();

    // For each series of data, scan it
    foreach (array_keys($series_data) as $value) {
      $svalue =& $data[$series][$value];
      $chart_data .= _google_charts_data_codingsimple($series_data[$value], $max);
      $value_labels_temp[] = empty($svalue['#label']) ? NULL : $svalue['#label'];

      // Get the highlight points
      if (!empty($svalue['#highlight']) or $data['#type'] == 'scatter' and $series % 2 == 0) {
        $highlight[] = 't' . $svalue['#label'] . ',' . (empty($svalue['#color']) ? substr($data[$series]['#color'], 1) : substr($svalue['#color'], 1)) . ',' . $series . ',' . $value . ',' . (empty($svalue['#size']) ? 10 : $svalue['#size']);
      }
      if ($options['value_attributes'] or $data['#type'] == 'scatter' and $series % 2 == 0) {

        // Series legends
        $value_labels[] = empty($svalue['#label']) ? NULL : $svalue['#label'];
      }
    }

    // Value labels
    if (!empty($value_labels_temp)) {
      $value_labels += $value_labels_temp;
    }
    if (empty($options['value_attributes'])) {

      // Series legends
      $legends[] = empty($data[$series]['#legend']) ? NULL : $data[$series]['#legend'];

      // Series colors
      $colors[] = empty($data[$series]['#color']) ? NULL : substr($data[$series]['#color'], 1);
    }
  }

  // Return the series of data
  if (empty($chart_data)) {
    return t('No enough data to create a chart.');
  }

  // Insert data
  $chart[] = 'chd=s:' . $chart_data;

  // Insert series color
  if (!empty($colors)) {
    $chart[] = 'chco=' . implode(',', $colors);
  }

  // Insert values labels
  if (!empty($value_labels)) {
    $chart[] = 'chl=' . implode('|', $value_labels);
  }

  // Insert multiple series tag
  if ($options['legend'] and !empty($legends)) {
    $chart[] = 'chdl=' . implode('|', $legends);
  }

  // Insert values labels
  if (!empty($highlight)) {
    $chart[] = 'chm=' . implode('|', $highlight);
  }
  return;
}