View source
<?php
define('CHARTS_SINGLE_AXIS', 'y_only');
define('CHARTS_DUAL_AXIS', 'xy');
function charts_menu() {
$items['admin/config/content/charts'] = array(
'title' => 'Default chart configuration',
'description' => 'Set the default library, behavior, and style of charts.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'charts_default_settings_form',
),
'access arguments' => array(
'administer charts',
),
'file' => 'includes/charts.pages.inc',
);
$items['charts/examples'] = array(
'title' => 'Charts examples',
'page callback' => 'charts_examples',
'access arguments' => array(
'access example charts',
),
'file' => 'includes/charts.examples.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['charts/examples/built-in'] = array(
'title' => 'Built-in examples',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
function charts_element_info() {
$info['chart'] = array(
'#chart_type' => NULL,
'#chart_id' => NULL,
'#title' => NULL,
'#title_color' => '#000',
'#title_font_weight' => 'normal',
'#title_font_style' => 'normal',
'#title_font_size' => 14,
'#title_position' => 'out',
'#colors' => charts_default_colors(),
'#font' => 'Arial',
'#font_size' => 12,
'#background' => 'transparent',
'#stacking' => NULL,
'#pre_render' => array(
'charts_pre_render_element',
),
'#tooltips' => TRUE,
'#tooltips_use_html' => FALSE,
'#data_labels' => FALSE,
'#legend' => TRUE,
'#legend_title' => NULL,
'#legend_title_font_weight' => 'bold',
'#legend_title_font_style' => 'normal',
'#legend_title_font_size' => NULL,
'#legend_position' => 'right',
'#legend_font_weight' => 'normal',
'#legend_font_style' => 'normal',
'#legend_font_size' => NULL,
'#width' => NULL,
'#height' => NULL,
'#attributes' => array(),
'#chart_library' => NULL,
'#raw_options' => array(),
);
$axis_properties = array(
'#axis_type' => 'linear',
'#title' => '',
'#title_color' => '#000',
'#title_font_weight' => 'normal',
'#title_font_style' => 'normal',
'#title_font_size' => 12,
'#labels' => NULL,
'#labels_color' => '#000',
'#labels_font_weight' => 'normal',
'#labels_font_style' => 'normal',
'#labels_font_size' => NULL,
'#labels_rotation' => NULL,
'#grid_line_color' => '#ccc',
'#base_line_color' => '#ccc',
'#minor_grid_line_color' => '#e0e0e0',
'#max' => NULL,
'#min' => NULL,
'#opposite' => FALSE,
);
$info['chart_xaxis'] = array() + $axis_properties;
$info['chart_yaxis'] = array() + $axis_properties;
$info['chart_data'] = array(
'#title' => NULL,
'#labels' => NULL,
'#data' => array(),
'#color' => NULL,
'#show_in_legend' => TRUE,
'#show_labels' => FALSE,
'#chart_type' => NULL,
'#line_width' => 1,
'#marker_radius' => 3,
'#target_axis' => NULL,
'#decimal_count' => NULL,
'#date_format' => NULL,
'#prefix' => NULL,
'#suffix' => NULL,
);
$info['chart_data_item'] = array(
'#data' => NULL,
'#color' => NULL,
'#title' => NULL,
);
return $info;
}
function charts_library() {
$library['charts.admin'] = array(
'title' => t('Charts admin integration'),
'version' => '1.0',
'js' => array(
array(
'data' => drupal_get_path('module', 'charts') . '/js/charts.admin.js',
'type' => 'file',
'weight' => 10,
),
),
);
return $library;
}
function charts_permission() {
return array(
'administer charts' => array(
'title' => t('Administer Charts'),
'description' => t('Visit the <a href="!url">Default chart configuration</a> (used for Charts administration)', array(
'!url' => url('admin/config/content/charts'),
)),
),
'access example charts' => array(
'title' => t('Access example charts'),
'description' => t('Visit the <a href="!url">Charts examples</a> (used for Charts testing and demo)', array(
'!url' => url('charts/examples'),
)),
),
);
}
function charts_theme() {
return array(
'charts_settings_fields' => array(
'render element' => 'element',
),
'charts_chart' => array(
'render element' => 'element',
),
);
}
function charts_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'charts') . '/views',
);
}
function charts_pre_render_element($element) {
$charts_info = charts_info();
$chart_library = isset($element['#chart_library']) ? $element['#chart_library'] : NULL;
if (isset($chart_library) && isset($charts_info[$chart_library])) {
$chart_library_info = $charts_info[$chart_library];
}
else {
$chart_library = key($charts_info);
$chart_library_info = $charts_info[$chart_library];
}
if (!isset($chart_library_info)) {
$element['#type'] = 'markup';
$element['#markup'] = t('No charting library found. Enable a charting module such as Google Charts or Highcharts.');
return $element;
}
$chart_type = chart_get_type($element['#chart_type']);
if ($chart_type['axis'] === CHARTS_DUAL_AXIS) {
foreach (element_children($element) as $key) {
$children_types[] = $element[$key]['#type'];
}
if (!in_array('chart_xaxis', $children_types)) {
$element['xaxis'] = array(
'#type' => 'chart_xaxis',
);
}
if (!in_array('chart_yaxis', $children_types)) {
$element['yaxis'] = array(
'#type' => 'chart_yaxis',
);
}
}
charts_cast_element_integer_values($element);
$element['#theme'] = 'charts_chart';
$alter_hooks = array(
'chart',
);
if ($element['#chart_id']) {
$alter_hooks[] = 'chart_' . $element['#chart_id'];
}
drupal_alter($alter_hooks, $element, $element['#chart_id']);
if (isset($chart_library_info['file'])) {
$module_path = drupal_get_path('module', $chart_library_info['module']);
include_once $module_path . '/' . $chart_library_info['file'];
}
$callback = $chart_library_info['render'];
$element = $callback($element);
if (!empty($element['#chart_definition'])) {
$chart_definition = $element['#chart_definition'];
unset($element['#chart_definition']);
$alter_hooks = array(
'chart_definition',
);
if ($element['#chart_id']) {
$alter_hooks[] = 'chart_definition_' . $element['#chart_id'];
}
drupal_alter($alter_hooks, $chart_definition, $element, $element['#chart_id']);
$element['#attributes']['data-chart'] = drupal_json_encode($chart_definition);
}
return $element;
}
function charts_info() {
$charts_info = array();
foreach (module_implements('charts_info') as $module) {
$module_charts_info = module_invoke($module, 'charts_info');
foreach ($module_charts_info as $chart_library => $chart_library_info) {
$module_charts_info[$chart_library]['module'] = $module;
}
$charts_info = array_merge($charts_info, $module_charts_info);
}
drupal_alter('charts_info', $charts_info);
return $charts_info;
}
function chart_get_library($library) {
$info = charts_info();
return $info[$library] ? $info[$library] : FALSE;
}
function charts_type_info() {
$charts_type_info = module_invoke_all('charts_type_info');
foreach ($charts_type_info as $chart_type => $chart_type_info) {
$charts_type_info[$chart_type] += array(
'label' => '',
'axis' => CHARTS_DUAL_AXIS,
'axis_inverted' => FALSE,
'stacking' => FALSE,
);
}
drupal_alter('charts_type_info', $charts_type_info);
return $charts_type_info;
}
function chart_get_type($chart_type) {
$types = charts_type_info();
return $types[$chart_type] ? $types[$chart_type] : FALSE;
}
function charts_charts_type_info() {
$chart_types['pie'] = array(
'label' => t('Pie'),
'axis' => CHARTS_SINGLE_AXIS,
);
$chart_types['bar'] = array(
'label' => t('Bar'),
'axis' => CHARTS_DUAL_AXIS,
'axis_inverted' => TRUE,
'stacking' => TRUE,
);
$chart_types['column'] = array(
'label' => t('Column'),
'axis' => CHARTS_DUAL_AXIS,
'stacking' => TRUE,
);
$chart_types['line'] = array(
'label' => t('Line'),
'axis' => CHARTS_DUAL_AXIS,
);
$chart_types['area'] = array(
'label' => t('Area'),
'axis' => CHARTS_DUAL_AXIS,
'stacking' => TRUE,
);
$chart_types['scatter'] = array(
'label' => t('Scatter'),
'axis' => CHARTS_DUAL_AXIS,
);
return $chart_types;
}
function charts_default_colors() {
$form = variable_get('charts_default_settings');
if (isset($form['colors'])) {
return $form['colors'];
}
else {
return array(
'#2f7ed8',
'#0d233a',
'#8bbc21',
'#910000',
'#1aadce',
'#492970',
'#f28f43',
'#77a1e5',
'#c42525',
'#a6c96a',
);
}
}
function charts_trim_array(&$array) {
foreach ($array as $key => &$value) {
if (is_array($value)) {
charts_trim_array($value);
}
elseif (is_null($value) || is_array($value) && count($value) === 0) {
unset($array[$key]);
}
}
}
function charts_cast_element_integer_values(&$element) {
$integer_options = array(
'#title_font_size',
'#font_size',
'#legend_title_font_size',
'#legend_font_size',
'#width',
'#height',
'#title_font_size',
'#labels_font_size',
'#labels_rotation',
'#max',
'#min',
'#decimal_count',
);
foreach ($element as $property_name => $value) {
if (is_array($element[$property_name])) {
charts_cast_element_integer_values($element[$property_name]);
}
elseif ($property_name && in_array($property_name, $integer_options)) {
$element[$property_name] = is_null($element[$property_name]) || strlen($element[$property_name]) === 0 ? NULL : (int) $element[$property_name];
}
}
}
function theme_charts_chart($variables) {
$element = $variables['element'];
$attributes = $element['#attributes'];
$attributes['id'] = $element['#id'];
$attributes['class'][] = 'chart';
return '<div ' . drupal_attributes($attributes) . '>' . (isset($element['#chart']) ? $element['#chart'] : '') . '</div>';
}