Calculations in GravityView

Floaty the Astronaut likes to gather information on stars. One day, he hopes to visit them all!

To plan his trip, Floaty wants to know how many days of travel at light speed it will take to get to each star. In the data he collected, he has the distance to each star in light year. In this example, we will show how to convert light years into light days.

Our form collects information on stars

A form in Gravity Forms called Stars

For a star, there's an entry in Gravity Forms

The Entries page in Gravity Forms showing entries containing information for different stars

Create a View to display the Stars

Adding a new View in GravityView

Add a Custom Content field to the layout

Click the "Add Field" button to access the field picker.

The list of fields in GravityView showing the Custom Content field

Save a draft of the View

Once adding the Custom Content field, the View needs to be saved. That way, Merge Tags are available in the next step.

The Save Draft button on the View Editor page

The shortcode is now ready

Don't forget to add scope="entry" inside the [gv_math] shortcode.

The Custom Content text editor with the [gv_math] shortcode inside

Insert the "Distance in Light Years" Merge Tag

To add a Merge Tag, click the icon next to the text area, then click on the field you would like to add.

In this case, the "Distance in Light Years" Merge Tag text will be replaced with the value entered into the form for each star.

The merge tag list on the right side of the Custom Content text editor

Convert the "Light Years" value into days

We want to know how many days it will take to reach a star at light speed, not years. So we multiply the "Distance in Light Years" value by 365.25 days.

The Custom Content text editor

The shortcode is now ready

Don't forget to add scope="entry" inside the [gv_math] shortcode. If you don't add this, it won't work properly.

The Custom Content text editor showing the completed [gv_math] shortcode

Publish the View

The Publish button on the View Editor page

and then click to see the live View

A link that says 'View on website' at the top of the View editor

Nice, our calculations are complete!

We have converted the Light Years stored in Gravity Forms to days, shown on GravityView. But that's not all you can do.

A GravityView Table layout showing data about different stars

Bonus "round"

Seeing the exact number of days makes the distance too daunting for Floaty. Let's round the number of days to the nearest thousand.

Round the days to the nearest thousand

Here we wrap the number in the round() function. This takes whatever is inside it and rounds it.  We set the "precision" of the rounding to-3, which rounds it to the nearest thousand (see the green arrow). Click here to learn more about rounding precision.

The Custom Content text editor

Voilá: "Number of Days" is now rounded

A GravityView Table layout showing data about different stars with the Number of Days column rounded to the nearest thousand

We want to display a summary of the data below the View, so we add a widget

The Add Widget button in GravityView

And then configure the widget

Let's display the average number of light years to each star, and the total light years shown. For that, we use a different scope, called "visible". This calculates values based on only the entries currently visible in GravityView. Note scope="visible" in the screenshot below:

The Custom Content text editor

Showing an average and sum in a Widget

Notice the :avg and :sum modifiers used in the image below (highlighted). When using scope="form" or scope="visible", this is the correct way to get averages and sums. Read more about the modifiers here.

The Custom Content text editor

The summary calculations are now visible

Wow, Floaty the Astronaut has a long trip ahead of him! He better keep working on that FTL spaceship.

Summary calculations below the table showing the average light years and total light years
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.