You are here

function chart_build in Google Chart Tools: Image Charts 6

Same name and namespace in other branches
  1. 5 chart.module \chart_build()
  2. 7 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 if the resolution of data should be automatically adjusted, an associative array containing #adjust which should be equal to previous description and #max to adjust data by.
  • #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 Query string or FALSE on failure.

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

File

./chart.module, line 254
Provides Google chart API integration.

Code

function chart_build($chart) {
  static $charts;

  // In the odd event that the same chart may appear twice
  // we will statically cache the results below
  if (isset($charts[$chart['#chart_id']])) {
    return $charts[$chart['#chart_id']];
  }

  // Merge with optional paramter defaults.
  $chart += array(
    '#title' => '',
    '#size' => '',
    '#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(),
  );
  $data = array();

  // Must have a chart id
  if (!strlen(trim($chart['#chart_id']))) {
    return FALSE;
  }

  // We need data :)
  if (!isset($chart['#data']) || !count($chart['#data']) || !is_array($chart['#data'])) {
    return FALSE;
  }
  drupal_alter('chart', $chart);

  // Adjust resolution
  if (isset($chart['#adjust_resolution'])) {
    if ($chart['#adjust_resolution'] === TRUE) {
      _chart_adjust_resolution($chart['#chart_id'], $chart['#data']);
    }
    elseif ($chart['#adjust_resolution']['#adjust'] == TRUE) {
      _chart_adjust_resolution($chart['#chart_id'], $chart['#data'], $chart['#adjust_resolution']['#max']);
    }
  }
  $data['chd'] = 't:' . _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']] = drupal_query_string_encode($data);
  return $charts[$chart['#chart_id']];
}