You are here

function _google_charts_series in Charts 6

Same name and namespace in other branches
  1. 7 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 145
@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
  $serie_index = 0;
  foreach (element_children($data) as $series) {
    if ($serie_index and $data['#type'] == 'pie3D') {
      break;
    }

    // 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
    $value_index = 0;
    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 the chart type uses data in a way each value has its own attributes
      // (instead the series), put the values there
      if ($options['attributes_per_value'] or $data['#type'] == 'scatter' and $series % 2 == 0) {

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

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

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

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

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

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

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

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

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

  // Insert values labels
  if ($data['#label'] and !empty($value_labels)) {
    foreach ($value_labels as $label) {
      if (!empty($label)) {
        $chart[] = 'chl=' . implode('|', $value_labels);
        break;
      }
    }
  }

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

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