|
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 colorsAlternatively 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 positioningAs 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 mappingSpacing 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);
|
Sign in to add a comment

Creating a Map
It is now possible to create a map using the following code:
<?php $chart = array( '#chart_id' => 'test_chart', '#type' => CHART_TYPE_MAP, //Map Type '#countries' => array(MG,KE,TN), //Array of ISO country codes (2 letter codes) '#georange' => africa, //Geographic Range of the Map - continent name or 'world' '#data' => array(10,50,100), //Value affects intensity of colours which will be in the range defined by #data_colours second and third elements '#data_colors' => array('ffffff', 'edf0d4', '13390a') //3 values for the colour (all other countries, start colour and end colour) ); echo chart_render($chart); ?>This results in:
Here's a scatter plot of Drupal Userpoints (anonymous) by member!
Here's the code!
http://snippets.dzone.com/posts/show/5985
This examples don't work in drupal. Do I need to install a module, where can I download the module from?
You need Drupal module from this project: http://drupal.org/project/chart
$chart = array(