4

I'm creating charts using the charts module and its integration with the Views module.

I want to set options for only some charts and found that in charts.api.php:

/**
 * 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;
  }
}

It seems perfect for what I want because it has the if ($chart_id === 'view_name__display_name') { line for specific views but I wonder if I directly add some settings code such as $chart['#title_font_size'] = 10; here (to charts.api.php) or is there any other way to do this?

I mean overriding the charts.api.php inside of the charts module is OK or do I have to write and run these codes anywhere else?

EDIT:

Based on rooby's comments, these are the two ways that I tried to set the properties for each charts (created with views module):

1. Custom Module

I created a custom module called benim.

This is the benim.info file:

name = Benim
description = Override charts properties.
package = Other
core = 7.x

And this is the benim.module file:

<?php

function benim_hook_chart_alter(&$chart, $chart_id) {
  if ($chart_id === 'chartim__block_1') {
    // Individual properties may be modified.
    $chart['#title_font_size'] = 50;
    $chart['subtitle'] = array(
    '#text' => 'Source:',
    );
    $chart['xaxis'] = array(
    '#type' => 'chart_xaxis',
    '#allowDecimals' => 'true',

    );

  }
}



function benim_hook_chart_definition_alter(&$definition, $chart, $chart_id) {
  if ($chart['#chart_library'] === 'highcharts') {
  if ($chart_id === 'chartim__block_1') {
    $definition['title']['style']['fontSize'] = 100;
  }
}
}

2. Template.php

I added the lines below to the theme's template.php.

function mytheme_hook_chart_alter(&$chart, $chart_id) {
  if ($chart_id === 'chartim__page') {
    // Individual properties may be modified.
    $chart['#title_font_size'] = 150;
    $chart['subtitle'] = array(
    '#text' => 'Source:',
    );
    $chart['xaxis'] = array(
    '#type' => 'chart_xaxis',
    '#allowDecimals' => 'false',

    );

    $chart['yaxis'] = array(
    '#type' => 'chart_yaxis',
    '#allowDecimals' => 'false',

    );

  }
}

function mytheme_hook_chart_definition_alter(&$definition, $chart, $chart_id) {
  if ($chart['#chart_library'] === 'highcharts') {
  if ($chart_id === 'chartim__page') {
    $definition['title']['style']['fontSize'] = 100;
  }
}
}

(above mytheme is the name of the actual theme)

Unfortunately, both two ways didn't work for me. I wondered if the $chart_id parameter is correct and checked for different views but the result didn't change.

Note: I also tried clearing caches, and trying inside another theme.

closed as off-topic by kiamlaluno Apr 4 '15 at 9:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Hook implementations don't contain the word hook in their name: Replace hook with the theme/module's short name. – kiamlaluno Apr 4 '15 at 9:08
  • @Pierre.Vriens Your rejected suggested edit was changing the code shown in the question. Since you are not the OP, you cannot know if the code really used benim_hook_chart_alter() or benim_chart_alter(); therefore, you cannot change the question's code. As for code shown by the OP, the full question is based on a typo; that is why the question got closed: "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error." – kiamlaluno Apr 7 '15 at 8:25
2

No, not in the charts module, in a custom module of your own.

It is also possible for you to use drupal alter hooks from your theme's template.php file however you should be careful what you put in your theme. You should only put code relating to visuals in the theme, if you are doing more functional code it should go in a module.

A good rule of thumb is whether or not you would want to keep that functionality if you ever changed themes. If you would want to keep it then that is a possible indicator that it is functional, not visual, and should be in a module.

.api.php files are files that give developers information on the function and hooks that a module makes available to others.

It is basically just documentation so you editing it will do nothing (except for then making that documentation incorrect, which is bad if anyone else wants to use it).

Some reasons why it is generally not a good idea to modify the module itself can be found at Why don't we hack core? (it is talking about drupal core but the same goes for other contrib modules and themes). One of the main reasons we have drupal hooks is so you don't have to modify the original module if you want to make changes.

So you would create a custom drupal module and then in that module implement the hook like this (changing MODULENAME to the name of the module and the chart ID to match the ID of your specific chart):

/**
 * 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 MODULENAME_chart_alter(&$chart, $chart_id) {
  if ($chart_id === 'view_name__display_name') {
    // Individual properties may be modified.
    $chart['#title_font_size'] = 20;
  }
}

There are lots of other drupal module development resources besides drupal.org docs too. There are some good affordable textbooks and some paid training type websites, as well as a scattering of blog posts in random corners of the internet.

Here is a more functional example of using the definition alter hook.

/**
 * 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 THEMENAME_chart_definition_alter(&$definition, $chart, $chart_id) {
  // For the portfolio charts.
  if ($chart_id == 'MY_CHART_ID') {
    $definition['legend']['borderRadius'] = 0;
    $definition['legend']['borderColor'] = NULL;
    $definition['legend']['style']['fontFamily'] = 'Helvetica,Arial,"Nimbus Sans L",sans-serif';
    $definition['legend']['backgroundColor'] = 'transparent';
    $definition['legend']['layout'] = 'vertical';
    $definition['legend']['verticalAlign'] = 'top';
    $definition['legend']['padding'] = 0;
    $definition['legend']['margin'] = 0;
    $definition['legend']['itemStyle']['color'] = '#555555';
    $definition['legend']['itemStyle']['fontSize'] = '12px';
  }
}
  • Thanks for the detailed answer. I tried both custom module and template.php but unfortunately didn't get any result, nothing changed. Try to find the problem and become successful on this. (I've also tried some other functions such as hook_chart_definition_alter). – herci Nov 25 '14 at 11:11
  • 2
    The function to use depends on what you want to alter. Personally I have only used hook_chart_definition_alter() before to modify things based on the highcharts API at api.highcharts.com/highcharts - hook_chart_alter() is more on the drupal end than the highcharts end. One thing to keep an eye out for is when you add a new hook to an already enabled module clear the cache after. Maybe add the code you have tried to your question and it might give some clues as to what is the problem. – rooby Nov 25 '14 at 11:55
  • Actually what I wanted is the same: I want to modify things based on the highcharts API. I added the possible solutions you said to the question. – herci Nov 25 '14 at 12:36
  • 1
    I added a better example, which works for me from my template.php file. You can also run it from a custom module though if that fits your needs better. – rooby Nov 27 '14 at 21:05
  • 1
    That looks like it should be the correct chart ID. Did you clear your cache after adding the function to your template.php? If you install the devel module and use the dpm() function to debug you can see what the chart_id is. You could also try without the chart ID check so it should be affecting all charts to see if your code is running at all. Sounds like your function isn't running. – rooby Nov 27 '14 at 23:22
1

Interesting question, and an even more interesting answer (from Rooby ... again! Thank you!).

Just to be aware, and to take this answer maybe even a little step further): you may want to checkout issue #2347721, titled 'Pass view info to chart (for further processing)', which I recently committed to the most current DEV version. So now you also know which view/display you're actually processing when building the chart. Challenge (for Rooby?): create another sample (variation) that illustrates a good use case for this also?

Disclosure: I'm a co-maintainer of the Charts module.

Not the answer you're looking for? Browse other questions tagged or ask your own question.