Drupal Charts module and custom Highcharts properties

Drupal Charts is a great charts module for Drupal, it allow use you both Google Charts and Highcharts. You can use same setup for both Google Charts and Highchats but some settings properties not implement yet. Here is tips allow you add custom chart properties

/**
 * Implements hook_chart_definition_alter().
 */
function YOURMODULE_chart_definition_alter(&$definition, $chart, $chart_id) {
  if (!empty($chart['#raw_options'])) {
    $definition = _drupal_array_merge_deep_array(array($definition, $chart['#raw_options']));
  }
}

/**
 * Override drupal_array_merge_deep_array
 *
 * @param $arrays
 *
 * @return array
 */
function _drupal_array_merge_deep_array($arrays) {
  $result = array();

  foreach ($arrays as $array) {
    foreach ($array as $key => $value) {
      // Renumber integer keys as array_merge_recursive() does. Note that PHP
      // automatically converts array keys that are integer strings (e.g., '1')
      // to integers.
      if (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
        $result[$key] = _drupal_array_merge_deep_array(array($result[$key], $value));
      }
      // Otherwise, use the latter value, overriding any previous value.
      else {
        $result[$key] = $value;
      }
    }
  }

  return $result;
}

Now when you create new chart from code you can add custom properties with #raw_options data. Demo code with Highcharts

  $raw_options['plotOptions']['pie']['dataLabels']['distance'] = -15;// change distance
  $raw_options['plotOptions']['pie']['dataLabels']['style']['fontSize'] = '13px';// change font size of labels

  $example['chart'] = array(
    '#type'            => 'chart',
    '#title'           => t('Pie simple'),
    '#chart_type'      => 'pie',
    '#chart_library'   => 'highcharts',
    '#data_labels'     => TRUE,
    '#tooltips'        => TRUE,
    '#height'          => 265,
    '#legend'          => FALSE,
    '#width'           => 265,
    '#raw_options'     => $raw_options,
  );

 

Other posts