Chart API Examples

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

Below are some examples to help you create your own charts using the API provided by the Chart module. Note that the examples differ according to the Drupal version being used.

If you have other examples that should be added, please go ahead and add them to this page.

Examples for Drupal 7

Example using theme()

Here is a Chart API example to create a chart using the theme() function:

  $chart = array(
    '#chart_id' => 'test_chart',
    '#title' => t('Servings'),
    '#type' => CHART_TYPE_PIE_3D,
  );

  $chart['#data']['fruits'] = 3;
  $chart['#data']['meats']  = 2;
  $chart['#data']['dairy']  = 5;

  echo theme('chart', array('chart' => $chart));

Example using renderable arrays

Here is a Chart API example to create a chart using Render Arrays:

  $page['chart'] = array(
    '#theme' => 'chart',
    '#chart_id' => 'test_chart',
    '#title' => t('Servings'),
    '#type' => CHART_TYPE_PIE_3D,
    '#data' => array(
      'fruits' => 3,
      'meats' => 2,
      'dairy' => 5,
    ),
  );

Example (Drupal 6)

Here is a Chart API example to create a chart using the 6.x version:

  $chart = array(
    '#chart_id' => 'someid', //required
    '#type'     => CHART_TYPE_PIE_3D, // Display a 3D pie chart
    '#title'    => t('Example'), // Chart title
    '#data'     => array( //required
      0 => 60, 
      1 => 40),
    '#labels'     => array(
      0 => '60%', 
      1 => '40%'),      
    '#size'   => array('#width' => 300, '#height' => 150),
  );
  
  return chart_render($chart);

Help improve this page

Page status: Not set

You can: