You are here

function chart_build in Google Chart Tools: Image Charts 7

Same name and namespace in other branches
  1. 5 chart.module \chart_build()
  2. 6 chart.module \chart_build()

Build chart query data.

Parameters

$chart: An associative array defining a chart which may contain the following keys.

  • #chart_id: Unique chart indentifier.
  • #type: (cht) The chart type, see chart_types().
  • #data: An array of data.
  • #title: (chtt) (Optional) The chart title.
  • #size: (chs) (Optional) An associative array containing keys: #width and #height.
  • #legends: (chdl) (Optional) An array of legends.
  • #legend_position: (chdlp) (Optional) ...
  • #labels: (chl) (Optional) An array of labels.
  • #adjust_resolution: (Optional) = TRUE to automatically adjust data resolution to the maximum number in all series, or an array containing the key #max or #per_series. #max - number to adjust the data resolution for all series with instead of the actual maximum. #per_series = TRUE to automatically adjust data resolution to the maximum number in EACH series.
  • #line_styles: (chls) (Optional) An array of line styles.
  • #grid_lines: (chg) (Optional) An array of grid line information.
  • #shape_markers: (chm) (Optional) A single or an array of shape markers.
  • #data_colors: (chco) (Optional) An array of data colors.
  • #chart_fill: (chf) (Optional) A single or an array of chart fill colors.
  • #mixed_axis_labels: (chxt) (Optional) An array of mixed axis labels.
  • #mixed_axis_label_styles: (chxs) (Optional) An array of mixed axis label styles.
  • #bar_size: (chbh) (Optional) A string or an array using keys: #size, #spacing, $group_spacing.
  • #countries: (chld) (Optional) An array of countries.
  • #georange: (chtm) (Optional) The geographical scope.

Return value

mixed Associative array of query data, otherwise FALSE.

See also

_chart_append()

3 calls to chart_build()
chart_copy in ./chart.module
Copies rendered chart image.
chart_url in ./chart.module
Returns the chart URL.
theme_chart in ./chart.module
Renders a chart element.

File

./chart.module, line 290
Provides primary Drupal hook implementations.

Code

function chart_build($chart) {
  $charts =& drupal_static(__FUNCTION__, array());
  if (empty($chart['#chart_id'])) {
    trigger_error('Charts must provide a #chart_id.', E_USER_ERROR);
    return FALSE;
  }

  // Hide charts with no data.
  if (empty($chart['#data'])) {
    return FALSE;
  }

  // If the chart has not been built then proceed to build it.
  if (!isset($charts[$chart['#chart_id']])) {

    // Merge optional parameters defaults.
    $chart += array(
      '#title' => '',
      '#size' => array(),
      '#legends' => array(),
      '#legend_position' => '',
      '#labels' => array(),
      '#adjust_resolution' => FALSE,
      '#line_styles' => array(),
      '#grid_lines' => array(),
      '#shape_markers' => array(),
      '#data_colors' => array(),
      '#chart_fill' => array(),
      '#mixed_axis_labels' => array(),
      '#mixed_axis_label_styles' => array(),
      '#bar_size' => '',
      '#countries' => array(),
      '#georange' => '',
      '#margins' => array(),
    );

    // Allow modules to alter a chart after defaults have been added.
    drupal_alter('chart', $chart);

    // Adjust resolution if option is enabled.
    if (!empty($chart['#adjust_resolution'])) {
      if ($chart['#adjust_resolution'] === TRUE) {
        _chart_adjust_resolution($chart['#chart_id'], $chart['#data']);
      }
      elseif (isset($chart['#adjust_resolution']['#max'])) {
        _chart_adjust_resolution($chart['#chart_id'], $chart['#data'], $chart['#adjust_resolution']['#max']);
      }
      elseif (isset($chart['#adjust_resolution']['#per_series'])) {

        // Adjust each series separately, assume valid data.
        foreach ($chart['#data'] as $name => &$series) {
          _chart_adjust_resolution($chart['#chart_id'] . $name, $series);
        }
      }
    }

    //Allow explicitly setting data series count. Useful for creating compound charts.

    //See http://code.google.com/apis/chart/image/docs/gallery/compound_charts.html
    if (!isset($chart['#data_series'])) {
      $chart['#data_series'] = '';
    }
    $data = array();
    $data['chd'] = 't' . $chart['#data_series'] . ':' . _chart_encode_data($chart['#data']);
    _chart_append('cht', $chart['#type'], $data);
    _chart_append('chs', $chart['#size'], $data);
    _chart_append('chtt', $chart['#title'], $data);
    _chart_append('chl', $chart['#labels'], $data);
    _chart_append('chdl', $chart['#legends'], $data);
    _chart_append('chdlp', $chart['#legend_position'], $data);
    _chart_append('chls', $chart['#line_styles'], $data);
    _chart_append('chg', $chart['#grid_lines'], $data);
    _chart_append('chm', $chart['#shape_markers'], $data);
    _chart_append('chco', $chart['#data_colors'], $data);
    _chart_append('chf', $chart['#chart_fill'], $data);
    _chart_append('chxt', $chart['#mixed_axis_labels'], $data);
    _chart_append('chxs', $chart['#mixed_axis_label_styles'], $data);
    _chart_append('chbh', $chart['#bar_size'], $data);
    _chart_append('chld', $chart['#countries'], $data);
    _chart_append('chtm', $chart['#georange'], $data);
    _chart_append('chma', $chart['#margins'], $data);

    // Creates hook_chart_post_build($chart_id, &$chart, &$data)
    foreach (module_implements('chart_post_build') as $module) {
      $function = $module . '_chart_post_build';
      $function($chart['#chart_id'], $chart, $data);
    }
    $charts[$chart['#chart_id']] = $data;
  }
  return $charts[$chart['#chart_id']];
}