You are here

function _charts_google_populate_chart_data in Charts 8

Same name and namespace in other branches
  1. 7.2 modules/charts_google/charts_google.inc \_charts_google_populate_chart_data()

Utility to populate chart data.

1 call to _charts_google_populate_chart_data()
_charts_google_render in modules/charts_google/charts_google.inc
Chart render callback; Convert all chart-level data.

File

modules/charts_google/charts_google.inc, line 164
Callbacks and utility functions for rendering a Google Chart.

Code

function _charts_google_populate_chart_data(&$chart, $chart_definition) {
  $chart_definition['options']['series'] = array();
  $chart_type_info = charts_get_type($chart['#chart_type']);
  $series_number = 0;
  foreach (element_children($chart) as $key) {
    if ($chart[$key]['#type'] === 'chart_data') {
      $series = array();

      // Make sure defaults are loaded.
      if (empty($chart[$key]['#defaults_loaded'])) {
        $chart[$key] += element_info($chart[$key]['#type']);
      }

      // Convert target named axis keys to integers.
      $axis_index = 0;
      if (isset($chart[$key]['#target_axis'])) {
        $axis_name = $chart[$key]['#target_axis'];
        foreach (element_children($chart) as $axis_key) {
          $multi_axis_type = $chart_type_info['axis_inverted'] ? 'chart_xaxis' : 'chart_yaxis';
          if ($chart[$axis_key]['#type'] === $multi_axis_type) {
            if ($axis_key === $axis_name) {
              break;
            }
            $axis_index++;
          }
        }
        $series['targetAxisIndex'] = $axis_index;
      }

      // Allow data to provide the labels. This will override the axis settings.
      if ($chart[$key]['#labels']) {
        foreach ($chart[$key]['#labels'] as $label_index => $label) {
          $chart_definition['data'][$label_index + 1][0] = $label;
        }
      }
      if ($chart[$key]['#title']) {
        $chart_definition['data'][0][$series_number + 1] = $chart[$key]['#title'];
      }
      foreach ($chart[$key]['#data'] as $index => $data_value) {

        // Nested array values typically used for scatter charts. This weird
        // approach leaves columns empty in order to make arbitrary pairings.
        // See https://developers.google.com/chart/interactive/docs/gallery/scatterchart#Data_Format
        if (is_array($data_value)) {
          $chart_definition['data'][] = array(
            0 => $data_value[0],
            $series_number + 1 => $data_value[1],
          );
        }
        else {
          $chart_definition['data'][$index + 1][$series_number + 1] = $data_value;
        }
      }
      $series['color'] = $chart[$key]['#color'];
      $series['pointSize'] = $chart[$key]['#marker_radius'];
      $series['visibleInLegend'] = $chart[$key]['#show_in_legend'];

      // Labels only supported on pies.
      $series['pieSliceText'] = $chart[$key]['#show_labels'] ? 'label' : 'none';

      // These properties are not real Google Charts properties. They are
      // utilized by the formatter in charts_google.js.
      $decimal_count = $chart[$key]['#decimal_count'] ? '.' . str_repeat('0', $chart[$key]['#decimal_count']) : '';
      $prefix = _charts_google_escape_icu_characters($chart[$key]['#prefix']);
      $suffix = _charts_google_escape_icu_characters($chart[$key]['#suffix']);
      $format = $prefix . '#' . $decimal_count . $suffix;
      $series['_format']['format'] = $format;

      // TODO: Convert this from PHP's date format to ICU format.
      // See https://developers.google.com/chart/interactive/docs/reference#dateformatter.

      //$series['_format']['dateFormat'] = $chart[$key]['#date_format'];

      // Conveniently only the axis that supports multiple axes is the one that
      // can receive formatting, so we know that the key will always be plural.
      $axis_type = $chart_type_info['axis_inverted'] ? 'hAxes' : 'vAxes';
      $chart_definition['options'][$axis_type][$axis_index]['format'] = $format;

      // Convert to a ComboChart if mixing types.
      // See https://developers.google.com/chart/interactive/docs/gallery/combochart?hl=en.
      if ($chart[$key]['#chart_type']) {

        // Oddly Google calls a "column" chart a "bars" series. Using actual bar
        // charts is not supported in combo charts with Google.
        $main_chart_type = $chart['#chart_type'] === 'column' ? 'bars' : $chart['#chart_type'];
        $chart_definition['visualization'] = 'ComboChart';
        $chart_definition['options']['seriesType'] = $main_chart_type;
        $data_chart_type = $chart[$key]['#chart_type'] === 'column' ? 'bars' : $chart[$key]['#chart_type'];
        $series['type'] = $data_chart_type;
      }

      // Add the series to the main chart definition.
      charts_trim_array($series);
      $chart_definition['options']['series'][$series_number] = $series;

      // Merge in any point-specific data points.
      foreach (element_children($chart[$key]) as $sub_key) {
        if ($chart[$key][$sub_key]['#type'] === 'chart_data_item') {

          // Make sure defaults are loaded.
          if (empty($chart[$key][$sub_key]['#defaults_loaded'])) {
            $chart[$key][$sub_key] += element_info($chart[$key][$sub_key]['#type']);
          }
          $data_item = $chart[$key][$sub_key];
          if ($data_item['#data']) {
            $chart_definition['data'][$sub_key + 1][$series_number + 1] = $data_item['#data'];
          }

          // These data properties are manually applied to cells in JS.
          // Color role not yet supported. See https://code.google.com/p/google-visualization-api-issues/issues/detail?id=1267
          $chart_definition['_data'][$sub_key + 1][$series_number + 1]['color'] = $data_item['#color'];
          $chart_definition['_data'][$sub_key + 1][$series_number + 1]['tooltip'] = $data_item['#title'];
          charts_trim_array($chart_definition['_data'][$sub_key + 1][$series_number + 1]);
        }
      }
      $series_number++;
    }
  }

  // Once complete, normalize the chart data to ensure a full 2D structure.
  $data = $chart_definition['data'];

  // Stub out corner value.
  $data[0][0] = isset($data[0][0]) ? $data[0][0] : 'x';

  // Ensure consistent column count.
  $column_count = count($data[0]);
  foreach ($data as $row => $values) {
    for ($n = 0; $n < $column_count; $n++) {
      $data[$row][$n] = isset($data[$row][$n]) ? $data[$row][$n] : NULL;
    }
    ksort($data[$row]);
  }
  ksort($data);
  $chart_definition['data'] = $data;
  return $chart_definition;
}