Formidable Forms

The Most Advanced WordPress Forms Plugin and Form Builder

  • Features
  • Pricing
  • Blog
  • Support
  • 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;
}

Change Log

Added in version 2.02.05

  • Usage
  • Parameters
  • Examples
    • Basic Example
    • Change x-axis labels
    • Change date format
    • Generate a cumulative graph
  • Change Log
Categories
×

Categories

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

Using WordPress and want to get Formidable Forms for free?

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

Top Features

  • Front End Editor
  • Repeating Fields
  • Views from Entries
  • Calculator Forms
  • Conditional Logic
  • Visual Form Styler
  • Form Templates
  • User Submitted Posts
  • File Upload Forms
  • Spam Protection
  • Multi Page Forms
  • Surveys & Polls
  • Form Graphs & Charts
  • Save and Continue
  • Mobile Forms
  • Stripe Forms
  • PayPal Forms
  • WooCommerce Forms
  • MailChimp Forms
  • User Registration
  • Signature Forms
  • Bootstrap Forms
  • Quiz Maker
  • Zapier Forms
  • Salesforce Forms
  • HubSpot Forms
  • API Webhooks
  • Multilingual Forms
  • Directories

Company

  • About Us
  • Giving Back
  • Careers
  • Newsletter
  • Blog

Copyright © 2021 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