From 6a51c32411809ae1e24ec7cbd1819156621341ad Mon Sep 17 00:00:00 2001 From: Nathan Haug <nate@quicksketch.org> Date: Fri, 26 Jul 2013 01:09:36 -0700 Subject: [PATCH] Issue #2050921: Make charts alterable. --- charts.api.php | 70 +++++++++++++++++++ charts.module | 24 +++++++ modules/charts_google/charts_google.inc | 2 +- .../charts_highcharts/charts_highcharts.inc | 2 +- views/charts_plugin_style_chart.inc | 3 + 5 files changed, 99 insertions(+), 2 deletions(-) diff --git a/charts.api.php b/charts.api.php index 257d8ea..83f52f9 100644 --- a/charts.api.php +++ b/charts.api.php @@ -54,6 +54,76 @@ * @see charts_element_info() */ +/** + * Alter an individual chart before it is printed. + * + * @param $chart + * The chart renderable. Passed in by reference. + * @param $chart_id + * The chart identifier, pulled from the $chart['#chart_id'] property (if + * any). Not all charts have a chart identifier. + */ +function hook_chart_alter(&$chart, $chart_id) { + if ($chart_id === 'view_name__display_name') { + // Individual properties may be modified. + $chart['#title_font_size'] = 20; + } +} + +/** + * Alter an individual chart before it's rendered. + * + * Same as hook_chart_alter(), only including the $chart_id in the function + * name instead of being passed in as an argument. + * + * @see hook_chart_alter() + */ +function hook_chart_CHART_ID_alter(&$chart) { +} + +/** + * Alter an individual chart's raw library representation. + * + * This hook is called AFTER hook_chart_alter(), after Charts module has + * converted the renderable into the chart definition that will be used by the + * library. Note that the structure of $definition will differ based on the + * charting library used. Switching charting libraries may cause your code + * to break when using this hook. + * + * Even though this hook may be fragile, it may provide developers with access + * to library-specific functionality. + * + * @param $definition + * The chart definition to be modified. The raw values are passed directly to + * the charting library. + * @param $chart + * The chart renderable. This may be used for reference (or read to add + * support for new properties), but any changes to this variable will not + * have an effect on output. + * @param $chart_id + * The chart ID, derived from the $chart['#chart_id'] property. Note that not + * all charts may have a $chart_id. + */ +function hook_chart_definition_alter(&$definition, $chart, $chart_id) { + if ($chart['#chart_library'] === 'google') { + $definition['options']['titleTextStyle']['fontSize'] = 20; + } + elseif ($chart['#chart_library'] === 'highcharts') { + $definition['title']['style']['fontSize'] = 20; + } +} + +/** + * Alter an individual chart before it's rendered. + * + * Same as hook_chart_definition_alter(), only including the $chart_id in the + * function name instead of being passed in as an argument. + + * @see hook_chart_definition_alter() + */ +function hook_chart_definition_CHART_ID_alter(&$chart) { +} + /** * Provide a new charting library to the system. * diff --git a/charts.module b/charts.module index 3d3392d..ab51961 100644 --- a/charts.module +++ b/charts.module @@ -50,6 +50,7 @@ function charts_element_info() { // Create the elements representing charts themselves. $info['chart'] = array( '#chart_type' => NULL, // Options: pie, bar, column, etc. + '#chart_id' => NULL, // Machine name to alter this chart individually. '#title' => NULL, '#title_color' => '#000', '#title_font_weight' => 'normal', // Options: normal, bold @@ -232,6 +233,13 @@ function charts_pre_render_element($element) { // Generic theme function assuming it will be suitable for most chart types. $element['#theme'] = 'charts_chart'; + // Allow the chart to be altered. + $alter_hooks = array('chart'); + if ($element['#chart_id']) { + $alter_hooks[] = 'chart_' . $element['#chart_id']; + } + drupal_alter($alter_hooks, $element, $element['#chart_id']); + // Include the library specific file and render via their callback. if (isset($chart_library_info['file'])) { $module_path = drupal_get_path('module', $chart_library_info['module']); @@ -239,6 +247,22 @@ function charts_pre_render_element($element) { } $callback = $chart_library_info['render']; $element = $callback($element); + + if (!empty($element['#chart_definition'])) { + $chart_definition = $element['#chart_definition']; + unset($element['#chart_definition']); + + // Allow the chart definition to be altered. + $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']); + + // Set the element #chart_json property as a data-attribute. + $element['#attributes']['data-chart'] = drupal_json_encode($chart_definition); + } + return $element; } diff --git a/modules/charts_google/charts_google.inc b/modules/charts_google/charts_google.inc index 22d6a15..20bffe8 100644 --- a/modules/charts_google/charts_google.inc +++ b/modules/charts_google/charts_google.inc @@ -32,7 +32,7 @@ function _charts_google_render($chart) { $chart['#attached']['library'][] = array('charts_google', 'charts_google'); $chart['#attributes']['class'][] = 'charts-google'; - $chart['#attributes']['data-chart'] = drupal_json_encode($chart_definition); + $chart['#chart_definition'] = $chart_definition; return $chart; } diff --git a/modules/charts_highcharts/charts_highcharts.inc b/modules/charts_highcharts/charts_highcharts.inc index 4caaa8b..035868a 100644 --- a/modules/charts_highcharts/charts_highcharts.inc +++ b/modules/charts_highcharts/charts_highcharts.inc @@ -40,7 +40,7 @@ function _charts_highcharts_render($chart) { $chart['#attached']['library'][] = array('charts_highcharts', 'charts_highcharts'); $chart['#attributes']['class'][] = 'charts-highchart'; - $chart['#attributes']['data-chart'] = drupal_json_encode($chart_definition); + $chart['#chart_definition'] = $chart_definition; return $chart; } diff --git a/views/charts_plugin_style_chart.inc b/views/charts_plugin_style_chart.inc index 1dae418..8af59b3 100644 --- a/views/charts_plugin_style_chart.inc +++ b/views/charts_plugin_style_chart.inc @@ -94,10 +94,13 @@ class charts_plugin_style_chart extends views_plugin_style { $label_field = $field_handlers[$this->options['label_field']]; } + $chart_id = $this->view->name . '__' . $this->view->current_display; $chart = array( '#type' => 'chart', '#chart_type' => $this->options['type'], '#chart_library' => $this->options['library'], + '#chart_id' => $chart_id, + '#id' => drupal_clean_css_identifier('chart_' . $chart_id), '#title' => $this->options['title_position'] ? $this->options['title'] : FALSE, '#title_position' => $this->options['title_position'], '#colors' => $this->options['colors'], -- GitLab