This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.
This hook will allow you to manipulate all the data in a graph, including the labels, rows, and columns.
Usage
add_filter( 'frm_graph_data', 'change_my_graph', 10, 2 ); function my_custom_graph_data( $data, $atts )
Parameters
- $data (array)
- $atts (array)
Examples
Basic Example
This example will add a row of data to a graph. Replace 'My graph' with your graph title. Please note that you must include the title parameter in your graph shortcode.
add_filter('frm_graph_data', 'my_custom_graph_data', 10, 2);
function my_custom_graph_data( $data, $atts ) {
if ( isset( $atts['title'] ) && $atts['title'] == 'My graph' ) {
$data[] = array( 'X-axis label', 10, 20, 30 );
}
return $data;
}
Change x-axis labels
Change the x-axis labels in your graph. Change First Label, Second Label, and Third Label to your new x-axis graph labels. Replace 'My graph' with your graph title. Please note that you must include the title parameter in your graph shortcode.
add_filter( 'frm_graph_data', 'change_my_graph_labels', 10, 2 );
function change_my_graph_labels( $data, $atts ) {
if ( isset( $atts['title'] ) && $atts['title'] == 'My graph' ) {
$new_labels = array( 'First label', 'Second label', 'Third label' );
foreach ( $new_labels as $key => $label ) {
$row_key = $key + 1;
if ( isset( $data[ $row_key ] ) ) {
$data[ $row_key ][0] = $label;
}
}
}
return $data;
}
Change date format
Use the code below to change the format for dates on the x-axis of a graph. Replace 'created_at' with the value or date field you are setting for your x-axis. Replace 'F d' with the format you would like to use. You may use any of the date formats described here.
add_filter('frm_graph_data', 'change_my_graph_date_format', 10, 2);
function change_my_graph_date_format( $data, $atts ) {
if ( isset( $atts['x_axis'] ) && $atts['x_axis'] == 'created_at' ) {
$wp_date_format = get_option('date_format');
$new_format = 'F d';
for ( $i = 1, $l = count( $data ); $iformat( $new_format );
}
}
return $data;
}
Generate a cumulative graph
Use the code below to convert a line graph of numbers to a cumulative graph. In order for this code to work, your graph shortcode must be graphing one field and an x_axis parameter must be set. It must also include data_type="total" and title="Your title". You can add the code below to the Code Snippets plugin or a child theme's functions.php file. Replace 'Cumulative graph' with the title you have given your graph.
add_filter('frm_graph_data', 'generate_cumulative_graph', 10, 2);
function generate_cumulative_graph( $data, $atts ) {
if ( isset( $atts['title'] ) && $atts['title'] == 'Cumulative graph' ) {
for ( $i=2, $l=count($data); $i<$l; $i++ ) {
$data[ $i ][1]+= $data[ $i-1 ][1];
}
}
return $data;
}
Change Log
Added in version 2.02.05