My favorites | Sign in
Project Logo
          
Search
for
Updated Mar 05, 2008 by tjholowayhuk
Labels: examples
Examples  

#Charting examples

Simple chart

  $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 chart_render($chart);

Labels, title styling, and chart size

  $chart = array(
      '#chart_id' => 'test_chart',
      '#title' => chart_title(t('Servings'), 'cc0000', 15),
      '#type' => CHART_TYPE_PIE,
      '#size' => chart_size(400, 200),
    );
    
  $chart['#data']['fruits'] = 3;
  $chart['#data']['meats']  = 2;
  $chart['#data']['dairy']  = 5;

  $chart['#labels'][] = t('Fruits');
  $chart['#labels'][] = t('Meats');
  $chart['#labels'][] = t('Dairy');

  echo chart_render($chart);

Custom data colors

Alternatively use the the Color Schemes Hook which can be then associated to multiple charts containing some or all of the same content. You may want to consider chart_unique_color();

  $chart = array(
      '#chart_id' => 'test_chart',
      '#title' => chart_title(t('Servings'), 'cc0000', 15),
      '#type' => CHART_TYPE_PIE,
      '#size' => chart_size(400, 200),
    );
    
  $chart['#data']['fruits'] = 3;
  $chart['#data']['meats']  = 2;
  $chart['#data']['dairy']  = 5;

  $chart['#labels'][] = t('Fruits');
  $chart['#labels'][] = t('Meats');
  $chart['#labels'][] = t('Dairy');
  
  $chart['#data_colors'][] = '00ff00';
  $chart['#data_colors'][] = 'ff0000';
  $chart['#data_colors'][] = '0000ff';

  echo chart_render($chart);

Line chart, legends, resolution adjustment, and label positioning

As you can see in the chart below, the resolution of the encoding type chosen does not reflect well with the data provided. So in the chart below it, we set "'#adjust_resolution' => TRUE,".

 $chart = array(
      '#chart_id' => 'test_chart',
      '#title' => chart_title(t('Servings'), 'cc0000', 15),
      '#type' => CHART_TYPE_LINE,
      '#size' => chart_size(400, 200),
      '#adjust_resolution' => TRUE,
    );
    
  $chart['#data']['fruits'] = array(1, 3, 5, 4, 2);
  $chart['#data']['meats']  = array(2, 2, 4, 3, 1);
  $chart['#data']['dairy']  = array(5, 2, 3, 1, 2);

  $chart['#legends'][] = t('Fruits');
  $chart['#legends'][] = t('Meats');
  $chart['#legends'][] = t('Dairy');

  $chart['#data_colors'][] = '00ff00';
  $chart['#data_colors'][] = 'ff0000';
  $chart['#data_colors'][] = '0000ff';

  $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, 5);
  $chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][1][] = chart_mixed_axis_label(t('Count'), 95);
  
  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t('Mon'));
  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t('Tue'));
  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t('Wed'));
  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t('Thu'));  
  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t('Fri'));
  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][2][] = chart_mixed_axis_label(t('Days of the week'), 50);

  echo chart_render($chart);

Grid lines and chart fill

  $chart = array(
      '#chart_id' => 'test_chart',
      '#title' => chart_title(t('Servings'), 'cc0000', 15),
      '#type' => CHART_TYPE_LINE,
      '#size' => chart_size(400, 200),
      '#chart_fill' => chart_fill('c', 'eeeeee'),          
      '#grid_lines' => chart_grid_lines(20, 20, 1, 5),          
    );
    
  for ($i = 0; $i < 80; $i++){
    $chart['#data'][] = $i + rand(0, $i);
  }

  echo chart_render($chart);

Bar sizing & color mapping

Spacing and sizing of bars can be adjusted using the #bar_size attribute. Colors are mapped to content id's with chart_unique_color(id), no matter how many charts on the page use test_a, test_b, or test_c, they will always have the same corrosponding color.

  $chart = array(
      '#chart_id' => 'test_chart',
      '#title' => chart_title(t('Bar Chart'), '0000ee', 15),
      '#type' => CHART_TYPE_BAR_V_GROUPED,
      '#size' => chart_size(400, 200),
      '#grid_lines' => chart_grid_lines(30, 15),
      '#bar_size' => chart_bar_size(15, 5), 
    );
    
  $chart['#data'][] = array(40, 50, 70);
  $chart['#data'][] = array(60, 50, 30);
  $chart['#data'][] = array(40, 60, 20);
  
  $chart['#data_colors'][] = chart_unique_color('test_a');
  $chart['#data_colors'][] = chart_unique_color('test_b');
  $chart['#data_colors'][] = chart_unique_color('test_c');

  echo chart_render($chart);

Comment by netsperience, Aug 25, 2008

Here's a scatter plot of Drupal Userpoints (anonymous) by member!

Here's the code!

http://snippets.dzone.com/posts/show/5985

Comment by blaze.software, Sep 21, 2008

This examples don't work in drupal. Do I need to install a module, where can I download the module from?

Comment by olli.k.aalto, Nov 12, 2008

You need Drupal module from this project: http://drupal.org/project/chart

Comment by ryanmichaelschmidt, Dec 11, 2008

$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 chart_render($chart);
Comment by tjholowayhuk, Apr 08, 2009

hha "This examples don't work in drupal. Do I need to install a module, where can I download the module from?"

this is why I left Drupal, to many people have no clue what they are doing... PHP makes me barf :P

Comment by MilaDanylova, Apr 18, 2009

The simplest way to use it : 1. Activate into administer -> site building -> Modules -> PHP filter module 2. Create block in administer -> site building -> Blocks -> Add block 3. Change input format for the block into "PHP code" 4. Insert example php code into your block between <?php "example code goes here" ?> 5. Place your block in any egion of your page. 6. Look and feel happy :)

Comment by MilaDanylova, Apr 18, 2009

you can download module from www.drupal.org

Comment by jerry.laster, Apr 24, 2009

Question. Can I use database queries as input?

Comment by empire.schulz, Jul 01 (3 days ago)

great!


Sign in to add a comment
Hosted by Google Code