You are here

function hook_chart_alter in Charts 8

Same name and namespace in other branches
  1. 8.4 charts.api.php \hook_chart_alter()
  2. 7.2 charts.api.php \hook_chart_alter()
  3. 5.0.x charts.api.php \hook_chart_alter()

@file Documentation on hooks provided by the Charts module.

Charts module provides 4 element types that can be used to construct a chart. In its most basic form, a chart may be created by specifying an element with the #type property "chart".

$chart = array(
  '#type' => 'chart',
  '#chart_type' => 'pie',
  '#data' => array(
    array(
      'Male',
      10,
    ),
    array(
      'Female',
    ),
  ),
);

On charts that have multiple axes, you'll need to add individual sub-elements for each series of data. If you desire, you may also customize the axes by providing an axis element too.

$chart = array(
  '#type' => 'chart',
  '#chart_type' => 'column',
);
$chart['male'] = array(
  '#type' => 'chart_data',
  '#title' => t('Male'),
  '#data' => array(
    10,
    20,
    30,
  ),
);
$chart['xaxis'] = array(
  '#type' => 'chart_xaxis',
  '#title' => t('Month'),
  '#labels' => array(
    t('Jan'),
    t('Feb'),
    t('Mar'),
  ),
);

Once you have generated a chart object, you can run drupal_render() on it to turn it into HTML:

$output = drupal_render($chart);

There are many, many properties available for the four chart types (chart, chart_data, chart_xaxis, and chart_yaxis). For a full list, see the charts_element_info() function.

Alter an individual chart before it is printed.

Parameters

$chart: The chart renderable. Passed in by reference.

$chart_id: The chart identifier, pulled from the $chart['#chart_id'] property (if any). Not all charts have a chart identifier.

See also

charts_element_info()

File

./charts.api.php, line 63
Documentation on hooks provided by the Charts module.

Code

function hook_chart_alter(&$chart, $chart_id) {
  if ($chart_id === 'view_name__display_name') {

    // Individual properties may be modified.
    $chart['#title_font_size'] = 20;
  }
}