Formidable Forms

Formidable Forms

  • Features
  • Pricing
  • Resources
    • Docs & Support
    • Blog
    • Community
  • Solutions
    • Web Applications
    • Calculators
    • Surveys
    • Directories
    • Payments
    • Contact forms
  • Login
  • Get Formidable Forms

frm_graph_data

Last Updated: April 30, 2018

Knowledge Base → Extend Formidable Forms → Formidable Hooks - for Developers → Display Data → frm_graph_data
Heads up!
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.

Formidable Forms is the best WordPress Form Builder plugin. Get it for free!

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 ); $i<$l; $i++ ) {
			$date = $data[ $i ][0];
			$date = DateTime::createFromFormat( $wp_date_format, $date );
			$data[ $i ][0] = $date->format( $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;
}

Graph field values with stats

Use the code below to display a graph of a field's text values on one axis and the corresponding stats on the other. For example, you could display business locations and the gas they use, employees and the hours they've worked, or bank account holders and their withdrawals.

add_filter( 'frm_graph_data', 'frm_stats_graph', 10, 2 );
function frm_stats_graph( $data, $atts ) {
	if ( isset( $atts['title'] ) && $atts['title'] == 'gas' ) {
		$data[0][1] = "Gas (in gallons)";
		for ( $i = 1, $l = count( $data ); $l > $i; $i++ ) {
			$data[ $i ][1] = FrmProStatisticsController::stats_shortcode( array( 'id'   => 4134, 'type' => 'total', '4135' => $data[$i][0] ) );
		}
	}

	return $data;
}

Notes:

  • The graph should have only one field in the fields param.
  • The graph shortcode needs a title param so it can be identified by the code. Use this title in line 3 in place of 'gas'.'
  • Change "Gas (in gallons)" to the label you want for the stats on the graph. This will be used in the tooltip that shows when a person hovers over a graph value.
  • Change 4134 to the id of the field you want to use for the stat.
  • Change 'total' to the type of stat you want to use.
  • Change 4135 to the other field in the graph, which will be used as a filter for the stat.
  • Add any other params you want to the stat. You could filter by date, for example.

Compare a single entry to the average

With this code example, you can create a graph which shows the value from a single entry and the average, across multiple fields. For example, in an hbar graph, each field will have two bars, one showing the value from the specified entry and the other showing the average value for that field across all entries.

A graph like this can be an easy way to show a person how their answers compare to everyone else's.

Sample graph shortcode:

[frm-graph fields="4250,4251,4252,4253,4254" type=hbar entry=[id] data_type=average title="Your results"]

  1. For the fields param, list the fields you want to show in the graph.
  2. Use the entry param to set the id of the single entry. Or use [id] in a View to use the current entry.
  3. Set the data type to 'average' so the values for the single entry are shown.
  4. The graph shortcode needs a title param so the code will work on the proper graph. The title is added to the code in line 4.
add_filter('frm_graph_data', 'frm_compare_average_graph', 10, 2);

function frm_compare_average_graph( $data, $atts ) {
	if ( ! isset( $atts['title'] ) || $atts['title'] !== 'Score comparison' ) { // Change 'Score comparison' to the title of your graph.
		return $data;
	}
	
	$data[0][1] = 'Your score';  // Change 'Your score' to the label for the values of the single entry
	$data[0][2] = 'Average score';  // Change 'Average score' to the label for the average values 
	
for ( $i = 1, $l = count( $data ); $l > $i; $i ++ ) {
  		if ( ! isset( $atts['fields'][ $i - 1] ) || ! isset( $atts['fields'][ $i - 1]->id ) ) {
			continue;
		}
		
		$total = $data[$i][2] = FrmProStatisticsController::stats_shortcode( array( 'id'   => $atts['fields'][ $i - 1 ]->id,
				                                                                     'type' => 'total'
		) );
		
		$count = $data[$i][2] = FrmProStatisticsController::stats_shortcode( array( 'id'   => $atts['fields'][ $i - 1 ]->id,
				                                                                     'type' => 'count'
		) );
																						 		
		$data[ $i ][2] = ( $total - $data[$i][1] ) / ( $count - 1 );																		 													 
	}

	return $data;
}

Show only percentages

Use this code example to display a graph that shows the percentage by count of each value in a field. The underlying counts will not show, even in the tooltips; just the percentages.

If you like, you can adjust the statistics used to show totals or other values instead of counts.

    Usage

  • A title for the graph is required so the code can identify the correct graph. Set the title in the graph shortcode using the title parameter. Replace "Gas by Percentage" in the code with your title.
  • Change "Percentage of gas used" to the label you want for the stats on the graph. This will be used in the tooltip that shows when a person hovers over a graph value.
  • Replace 4124 in three places with the id of the field whose counts you want to show in the graph.
  • In round( $percent, 1) on line 11, replace 1 with the number of digits you want to show after the decimal point. Use 0 if you want show whole numbers. The numbers will be rounded accordingly.
  • Example: [frm-graph fields=4124 title="Gas by Percentage" type=hbar] where 4124 is the id of the field whose counts you want to show.
add_filter( 'frm_graph_data', 'frm_percentage_graph', 10, 2 );
function frm_percentage_graph( $data, $atts ) {
    if ( isset( $atts['title'] ) && $atts['title'] == 'Gas by Percentage' ) {
        $data[0][1] = "Percentage of gas used"; 
        $total =  FrmProStatisticsController::stats_shortcode( array( 'id' => 4124, 'type' => 'count' ) );
        for ( $i = 1, $l = count( $data ); $l > $i; $i++ ) {
            $graph_value = 0;
            if ( $total !== 0 ){
                $individual_count = FrmProStatisticsController::stats_shortcode( array( 'id'   => 4124, 'type' => 'count', '4124' => $data[$i][0] ) );
                $percent = ( $individual_count/ $total ) * 100;
                $graph_value = round( $percent, 1); 
            }
            $data[ $i ][1] = $graph_value;
        }
    }

    return $data;
}

Filter graphs with y-min parameter

function filter_graph_data_to_match_min_x( $data, $atts ) {
	if ( ! isset( $atts['x_min'] ) ) {
		return $data;
	}

	return array_values(
		array_filter(
			$data,
			function( $row ) use ( $atts ) {
				return $row[1] >= floatval( $atts['x_min'] );
			}
		)
	);
}
add_filter( 'frm_graph_data', 'filter_graph_data_to_match_min_x', 10, 2 );

Change Log

Added in version 2.02.05
  • Usage
  • Parameters
  • Examples
    • Basic Example
    • Change x-axis labels
    • Change date format
    • Generate a cumulative graph
    • Graph field values with stats
    • Compare a single entry to the average
    • Show only percentages
    • Filter graphs with y-min parameter
  • Change Log
Categories
×

Categories

  • Installation & Getting Started
  • Account Management
  • Forms
  • Entries
  • Views
  • Styles
  • Importing & Exporting
  • Add-Ons
  • Extend Formidable Forms

Using WordPress and want to get Formidable Forms for free?

Get Formidable Forms Lite Now

You do not have permission to view this form. Maybe you need to log in?

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

Take on bigger projects Right Now

Get the tools you need to revolutionize your workflow and architect a masterpiece. Build the most advanced WordPress forms and actually use the data you collect in meaningful ways.

Get the most advanced WordPress form plugin and the only form builder with integrated Views.

Get Formidable Forms Now

Resources

  • Community
  • Affiliates
  • Contact
  • Free Online Form Builder

Top Features

  • Application Builder
  • Calculator Forms
  • Surveys & Polls
  • Quiz Maker
  • Form Templates
  • Application Templates
  • Directories
  • Donation Plugin

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • WP Tasty
  • Nutrifox

Copyright © 2023 Strategy11, LLC. Formidable Forms® is a registered trademark Strategy11, LLC.
Privacy Policy | Terms of Service | Sitemap

Join 300,000+ using Formidable Forms to create form-focused solutions fast. Get Started See User Reviews