diff --git a/includes/plugins/google_visualization_api.inc b/includes/plugins/google_visualization_api.inc index c23f109..a3c3464 100644 --- a/includes/plugins/google_visualization_api.inc +++ b/includes/plugins/google_visualization_api.inc @@ -13,16 +13,26 @@ class GoogleVisualizationAPIHandler implements VisualizationHandlerInterface { * Renders a chart using the Google Visualization API. */ public function render($chart_id, $data, $options) { - // Chart options. + // Default chart options. $chart_options = array( - 'title' => $options['title'], - 'width' => !empty($options['width']) ? $options['width'] : '100%', - 'height' => !empty($options['height']) ? $options['height'] : '100%', + //'title' => $options['title'], + //'width' => !empty($options['width']) ? $options['width'] : '100%', + //'height' => !empty($options['height']) ? $options['height'] : '100%', + 'width' => '100%', + 'height' => '100%', 'vAxis' => array( 'title' => !empty($options['yAxis']['title']) ? $options['yAxis']['title'] : '', ), ); + // Allow override of defaults. + if (isset($options['chart_options'])) { + $chart_options = drupal_array_merge_deep($chart_options, $options['chart_options']); + } + + // Set title. + $chart_options['title'] = $options['title']; + switch ($options['type']) { case 'map': $chart_options['dataMode'] = !empty($options['dataMode']) ? $options['dataMode'] : 'regions'; diff --git a/includes/plugins/highcharts.inc b/includes/plugins/highcharts.inc index ca5e70a..1934576 100644 --- a/includes/plugins/highcharts.inc +++ b/includes/plugins/highcharts.inc @@ -29,16 +29,42 @@ class HighchartsHandler implements VisualizationHandlerInterface { $highchart = new stdClass(); // Chart. - $highchart->chart = (object) array( - 'plotBackgroundColor' => NULL, - 'plotBorderWidth' => NULL, - 'plotShadow' => FALSE, + // Default options. + $chart_options = array( + 'chart' => array( + 'plotBackgroundColor' => NULL, + 'plotBorderWidth' => NULL, + 'plotShadow' => FALSE, + ), + 'plotOptions' => array( + 'pie' => array( + 'allowPointSelect' => TRUE, + 'cursor' => 'pointer', + 'dataLabels' => array( + 'enabled' => TRUE, + ), + 'showInLegend' => TRUE, + ), + 'bar' => array( + 'dataLabels' => array( + 'enabled' => TRUE, + ), + ), + ), + 'credits' => array( + 'enabled' => FALSE, + ), ); + // Allow override of defaults. + if (isset($options['chart_options'])) { + $chart_options = drupal_array_merge_deep($chart_options, $options['chart_options']); + } + // Set title. - $highchart->title = new stdClass(); - $highchart->title->text = $options['title']; + $chart_options['title']['text'] = $options['title']; + // Axis settings. $x_axis = array(); if (!empty($options['xAxis']['labelField'])) { foreach ($data as $row) { @@ -47,70 +73,51 @@ class HighchartsHandler implements VisualizationHandlerInterface { } if (!empty($x_axis)) { - $highchart->xAxis = (object) array( - 'categories' => $x_axis, - ); + $chart_options['xAxis']['categories'] = $x_axis; } if (!empty($options['yAxis']['title'])) { - $highchart->yAxis = (object) array( - 'title' => (object) array( - 'text' => $options['yAxis']['title'], - ), - ); + $chart_options['yAxis']['title']['text'] = $options['yAxis']['title']; } - // Series. - $highchart->series = array(); + // Series settings. + $chart_options['series'] = array(); foreach ($options['fields'] as $name => $column) { if (!empty($column['enabled'])) { - $serie = new stdClass(); - $serie->name = $column['label']; - $serie->type = $options['type']; + $serie = array( + 'name' => $column['label'], + 'type' => $options['type'], + 'data' => array(), + ); - $serie->data = array(); foreach ($data as $row) { $value = (int) $row[$name]; if (!empty($column['enabled'])) { - $serie->data[] = (object) array('name' => (string) html_entity_decode($row[$options['xAxis']['labelField']], ENT_QUOTES), 'y' => $value); + $serie['data'][] = array( + 'name' => (string) html_entity_decode($row[$options['xAxis']['labelField']], ENT_QUOTES), + 'y' => $value, + ); } else { - $serie->data[] = $value; + $serie['data'][] = $value; } } - $highchart->series[] = $serie; + $chart_options['series'][] = $serie; } } - $highchart->plotOptions = new stdClass(); - $highchart->plotOptions->pie = (object) array( - 'allowPointSelect' => TRUE, - 'cursor' => 'pointer', - 'dataLabels' => (object) array( - 'enabled' => TRUE, - ), - 'showInLegend' => TRUE, - ); - $highchart->plotOptions->bar = (object) array( - 'dataLabels' => (object) array( - 'enabled' => TRUE, - ), - ); - - $highchart->credits = new stdClass(); - $highchart->credits->enabled = FALSE; - - // Rendering. - $highchart->chart->renderTo = $chart_id; + // This can't be overridden. + $chart_options['chart']['renderTo'] = $chart_id; + // Add Drupal.settings for this chart. $information = array( 'library' => 'highcharts', - 'options' => $highchart, + 'options' => $chart_options, ); - // Add Drupal.settings for this chart. + // This will run through json_enconde() so will be converted to objects. drupal_add_js(array('visualization' => array($chart_id => $information)), array('type' => 'setting')); } diff --git a/theme/visualization.theme.inc b/theme/visualization.theme.inc index d93d0ee..4ab3e46 100644 --- a/theme/visualization.theme.inc +++ b/theme/visualization.theme.inc @@ -9,7 +9,7 @@ * * Used by both regular calls to theme() and the visualization Views handler. */ -function template_preprocess_visualization(&$vars) { +function template_process_visualization(&$vars) { $options = $vars['options']; drupal_add_library('visualization', 'visualization'); if (!empty($vars['view'])) { diff --git a/visualization.module b/visualization.module index 7667a4b..ff84e20 100644 --- a/visualization.module +++ b/visualization.module @@ -36,6 +36,7 @@ function visualization_theme() { 'fields' => array(), 'type' => 'line', 'data' => array(), + 'chart_options' => array(), ), ), 'template' => 'visualization',