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

M Chart has a number of filter/action hooks as well as some Javascript events that you can use to integrate M Chart into your website.

Action Hooks

m_chart_update_post_meta

This is an action that is triggered whenever someone updates a chart's post meta.

The meta array has already been cleaned and validated at the time the action is triggered.

  • Arguments
    • $post_id
      • The WP ID of the chart post
    • $parsed_meta
      • The cleaned and parsed meta array for the chart

m_chart_get_chart_start

This is an action that is triggered at the very start of chart being retrieved.

  • Arguments
    • $post_id
      • The WP ID of the chart post
    • $args
      • An array of base args that are used in the get_chart function

m_chart_get_chart_begin

Available in M Chart v.2.0+

This is an action that is triggered just before the chart template is included.

  • Arguments
    • $post_id
      • The WP ID of the chart post
    • $args
      • An array of base args that are used in the get_chart function

m_chart_get_chart_end

This is an action that is triggered at the very end of a chart being retrieved and the code necessary to display the chart has been built.

  • Arguments
    • $post_id
      • The WP ID of the chart post
    • $args
      • An array of base args that are used in the get_chart function

m_chart_after_chart_args

Available in M Chart v.2.0+

This is an action that is triggered after chart args have been prepared in the front-end chart template, before the chart is initialized.

  • 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

m_chart_after_chartjs_plugins

Available in M Chart v.2.0+

This is an action that is triggered after Chart.js and MChartHelper plugins have been registered in the front-end template.

  • 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

m_chart_admin_scripts

Available in M Chart v.2.0+

This is an action that is triggered when loading admin scripts for a specific library. This is a good place to enqueue additional scripts needed by a library plugin.

  • Arguments
    • $library
      • The active library slug
    • $post_id
      • The WP ID of the chart post

m_chart_settings_admin

Available in M Chart v.2.0+

This is an action that is triggered in the footer of the M Chart settings admin page.

m_chart_admin_footer_javascript

Available in M Chart v.1.1+

This is an action that is triggered in the footer of a chart post edit page.

This can be used to include JavaScript code that affects the chart edit process. Note that in v2.0 the admin interface is React-based — see Javascript events for the wp.hooks API available for admin integration. The canvas_done event used in the v1.x example for this hook has been removed. See Migrating to v2 for details.

m_chart_post_render_javascript

Highcharts only — See Highcharts Action and Filter Hooks for documentation.

Filter Hooks

m_chart_chart_args

This filter hook is triggered after all of the Highcharts/Chart.js chart args for a given chart have been generated.

This hook is useful for changing the behavior of the plugin in regards to how it displays/functions.

You could reformat the tooltips for instance, or you could modify axis title text. You could also add arguments to the chart args that M Chart doesn't use by default like change the colors used to render the chart.

See the Highcharts API and Chart.js Docs references for all the possibilities.

  • Arguments
    • $chart_args
      • The Highcharts/Chart.js chart args for the chart
    • $post
      • The WP post object for the chart
    • $post_meta
      • The WP post_meta for the chart
    • $args
      • The args passed to the get_chart method

Example:

function filter_m_chart_chart_args( $chart_args, $post, $post_meta, $args ) {
	$chart_args['colors'] = array(
		'#2f7ed8',
		'#0d233a',
		'#8bbc21',
		'#910000',
		'#1aadce',
		'#492970',
		'#f28f43',
		'#77a1e5',
		'#c42525',
		'#a6c96a',
	);

	return $chart_args;
}

add_filter( 'm_chart_chart_args', 'filter_m_chart_chart_args', 10, 4 );

m_chart_chart_template

This filter hook is triggered when retreiving the chart template used to build the code needed to display a chart.

  • Arguments
    • $template
      • The template used to build the code needed to display a chart
    • $library
      • The active library
    • $post_id
      • Available in M Chart v.2.0+
      • The WP ID of the chart post

m_chart_share_template

This filter hook is triggered when retreiving the chart template used to display a sharing embed version of a chart.

  • Arguments
    • $template
      • The template used to build the code needed to display a chart
    • $library
      • The active library

m_chart_iframe_scripts

This filter hook is triggered when displaying an iframe embed of a chart and lets you load additional scripts that might be needed to properly display a given chart

  • Arguments
    • $scripts
      • An array of handles that match enqueued scripts that you want loaded in the iframe
    • $post_id
      • The WP ID of the chart post

m_chart_image_support

Available in M Chart v.2.0+

This filter allows a library plugin to declare whether it supports image generation for charts.

  • Arguments
    • $support
      • Default: 'no'
    • $library
      • The active library slug
  • Return value: 'yes' or 'no'

m_chart_instant_preview_support

Available in M Chart v.2.0+

This filter allows a library plugin to declare whether it supports instant previews in the admin UI.

  • Arguments
    • $support
      • Default: 'no'
    • $library
      • The active library slug
  • Return value: 'yes' or 'no'

m_chart_show_image

Available in M Chart v.2.0+

This filter controls whether to render a chart as a static image instead of as an interactive chart.

  • Arguments
    • $show_image
      • Default: false
    • $post_id
      • The WP ID of the chart post
    • $args
      • The args passed to the get_chart method
  • Return value: boolean

m_chart_chartjs_colors

Available in M Chart v.1.8+

This is a filter that is triggered when building a Chart.js chart's args and happens immediately after the theme's colors are pulled and allows you to override the colors.

  • Arguments
    • $colors
      • The array of colors
    • $post
      • The post object of the current chart
function filter_m_chart_chartjs_colors( $colors, $post ) {
	if ( 45 != $post->ID ) {
		return $colors;
	}

	return array(
		'#59a80f',
		'#9ed54c',
		'#c4ed68',
		'#e2ff9e',
		'#f0F2dd',
	);
}

add_action( 'm_chart_chartjs_colors', 'filter_m_chart_chartjs_colors', 10, 2 );

m_chart_chartjs_points

Available in M Chart v.1.8+

This is a filter that is triggered when building a Chart.js chart's args and happens immediately after the theme's points are pulled and allows you to override the points.

See the Chart.js Docs references for all the possibilities.

  • Arguments
    • $points
      • The array of points
    • $post
      • The post object of the current chart
function filter_m_chart_chartjs_points( $points, $post ) {
	if ( 45 != $post->ID ) {
		return $points;
	}

	return array(
		array(
			'point' => array(
				// Circle
				'pointStyle' => 'circle',
			),
		),
		array(
			'point' => array(
				// Square
				'pointStyle' => 'rect',
			),
		),
		array(
			'point' => array(
				// Triangle
				'pointStyle' => 'triangle',
			),
		),
	);
}

add_action( 'm_chart_chartjs_points', 'filter_m_chart_chartjs_points', 10, 2 );

See Highcharts Action and Filter Hooks for Highcharts-specific filter hook documentation.