Highcharts Action and filter hooks - methnen/m-chart GitHub Wiki

M Chart has a number of action and filter hooks specific to the Highcharts library.

Action Hooks

m_chart_post_render_javascript

Available in M Chart Highcharts Library v.1.0+ (Highcharts only)

This is an action that is triggered after a Highcharts chart has finished rendering in the front-end template. It is a good place to add inline JavaScript that runs against the Highcharts chart object — for example, adding a watermark via an SVG renderer call.

  • Arguments
    • $post_id
      • The WP ID of the chart post
    • $args
      • The arguments that were passed to the get_chart method
    • $instance
      • The instance (integer) of the current chart used with the $post_id value to build the ID of the current chart (e.g. '#m-chart-' . $post_id . '-' . $instance)

Example:

function action_m_chart_post_render_javascript( $post_id, $args, $instance ) {
	?>
	chart.renderer.image('https://domain.com/img/logo.svg', 11, 10, 106, 16).add();
	<?php
}

add_action( 'm_chart_post_render_javascript', 'action_m_chart_post_render_javascript', 10, 3 );

Filter Hooks

m_chart_chart_options

This filter hook is triggered after all of the Highcharts chart options have been generated.

Chart options are library wide settings that you may want to change or override. M Chart uses them to set some USA standard formatting and numeric abbreviations.

This hook is useful for changing the behavior of Highcharts.

Most of the options involve localization stuff. For instance you could change the decimal indicator to a comma which is used in some European contries.

See the Highcharts API reference for all the possibilities.

  • Arguments
    • $chart_options
      • The Highcharts options
    • $library
      • The active library

Example:

function filter_m_chart_chart_options( $chart_options, $library ) {
	$chart_options['lang']['decimalPoint'] = ',';

	return $chart_options;
}

add_filter( 'm_chart_chart_options', 'filter_m_chart_chart_options', 10, 2 );

m_chart_enable_highcharts_accessibility

Available in M Chart Highcharts Library v.1.2.3+

The Highcharts Accessibility module is disabled by default. You can use this filter hook to enable it.

This is a filter that is triggered when rendering a Highcharts chart and the return value determines if the accessibility module will be loaded.

  • Arguments
    • $enabled
      • The boolean to determine if the accessibility module will be loaded
    • $post_id
      • The WP ID of the chart post
    • $context
      • Contains 'iframe' when called while rendering a chart embed; Otherwise it is ''.
  • Return value: boolean
function filter_m_chart_enable_highcharts_accessibility( $enabled, $post_id, $context ) {

	// always make the chart accessible
	return true;

}

add_filter( 'm_chart_enable_highcharts_accessibility', 'filter_m_chart_enable_highcharts_accessibility', 10, 3 );

m_chart_enable_highcharts_export

Available in M Chart Highcharts Library v.1.2.3+

The Highcharts Export module is disabled by default. You can use this filter hook to enable it.

This filter is triggered when rendering a Highcharts chart and the return value determines if the export module will be loaded.

  • Arguments
    • $enabled
      • The boolean to determine if the export module will be loaded
    • $post_id
      • The WP ID of the chart post
    • $context
      • Contains 'iframe' when called while rendering a chart embed; Otherwise it is ''.
  • Return value: boolean
function filter_m_chart_enable_highcharts_export( $enabled, $post_id, $context ) {

	// don't show export options when embedded
	if ( $context != 'iframe' ) {
		$enabled = true;
	}

	return $enabled;

}

add_filter( 'm_chart_enable_highcharts_export', 'filter_m_chart_enable_highcharts_export', 10, 3 );

m_chart_highcharts_available_themes

Available in M Chart Highcharts Library v.1.2.3+

This filter is triggered when building the list of themes available for Highcharts charts. Allows one to programmatically add to, remove from or even modify an existing theme in the list of themes. If adding a theme, the file attribute is optional and the slug must be unique between all themes.

  • Arguments
    • $themes
      • The array of themes
      • Each theme is an object with the following attributes:
        • slug => unique label (filename without path/extension)
        • name => display name
        • file => filename without path
        • options => array of options for theme
  • Return value: array of theme objects
function filter_m_chart_highcharts_available_themes( $themes ) {
	foreach ( $themes as $key => $theme ) {
		if ( $theme->slug != 'the-one-and-only-theme' ) {
			unset( $themes[$key] );
		}
	}

	return $themes;
}

add_filter( 'm_chart_highcharts_available_themes', 'filter_m_chart_highcharts_available_themes', 10, 1 );