A common need in reporting is the ability to display different messages for different outcomes. For example, you perform a query to see if a sales territory under-performing. To you, the maintainer of the data, the meaning of the resulting gaggle of numbers might be obvious. However, when you're creating maps for colleagues, clients or stakeholders to perform these analyses, how do you ensure the results are easily understood?
Mango lets you compose conditional (if/then) statements that provide context to each query, helping the audience understand the results.
For example, you have a dataset that shows sales results and KPIs across all territories and you want to summarise the results in a query report:
Condition: is Sales Volume in selected territories >= 5,000
if True, display "Achieved"
if False, display "Under performing"
In our query report, we can check if the aggregate sales volume is greater than or equal to the KPI value and return the appropriate message in an equation wrapper:
#{ '{sales:total}' >= '{kpi}' ? 'Achieved' : 'Under performing' }#
Conditionals can be composed in the custom popup editor and in custom reports for the query and proximity tools.
- Syntax and operators
- Using conditionals in Feature popups
- Using conditionals in Query and Proximity Reports
- Nested conditions
Syntax and operators
Build conditional statements using a ternary operator with the following syntax:
#{ 'condition' ? 'value_if_true' : 'value_if_false' }#
The operator must be wrapped in #{ }#
, and each value in the arguments should be wrapped in single quotes '
or double quotes "
to escape any content that might invalidate the statement.
You can use the following comparative operators to define your condition:
==
is equal to!=
is not equal to>
is greater than>=
is greater than or equal to>
is less than<=
is less than or equal to
For example, this will return True if the value of attribute
is greater than or equal to 5.
#{ '{attribute}' >= '5' ? 'True' : 'False' }#
The Logical AND ( &&
) operator can be used to combine conditions.
For example, this will return True if the value of attribute_a
is greater or equal to 5 AND the value of attribute_b
is equal to 10.
#{ '{attribute_a}' >= '5' && '{attribute_b}' == '10' ? 'True' : 'False' }#
Basic math within conditionals
Given the conditional statements are composed in equation wrappers, it's possible to do some basic math as part of both the condition and the true/false statements.
For the sake of readability, each equation function here is wrapped in parenthesis:
#{ ( '{attribute x}' / '{attribute y}' ) == '1' ? ( '{attribute z}' * '10' ) : ( '{attribute a}' / '10' ) }#
Using conditionals in Feature popups
When composing a custom popup, you can add conditionals via the equation editor, or simply type in your conditional in the popup editor, wrapped in #{ }#
.
Popup examples
Hiding an empty attribute
Let's assume you have an owner2
field which only contains a value for a small number of records. Using the following condition will remove the line from your popup when no owner2
value exists for a selected feature.
#{ '{owner2}' != '0' ? 'Owner: {owner2}' : '' }#
- Where
owner2
has no value, we should expect no result. - Where
owner2
isTom Jones
, we should expectOwner: Tom Jones
returned in the popup.
Now let's assume your territory management map displays the name of franchisees. If a territory is yet to be sold, we'd usually end up with a Current franchisee:
, left blank. Using a condition, we can hide this instead.
#{ '{franchisee}' == '0' ? '' : 'Current franchisee: {franchisee}' }#
- Where
franchisee
isTom Jones
, the popup will displayCurrent franchisee: Tom Jones
- Where
franchisee
has no value, nothing will be displayed.
Adding a classifier word and displaying a value when a value exists
Assuming we have a map of addresses where "Unit n" is sometimes part of the address but the data table only contains the unit number. Using the following will include "Unit" when there is a value in {propunit}
, and will not display "Unit" when no value exists
Address: #{ '{propunit}' != '0' ? 'Unit {propunit}, ' : '' }#{propadd} {propstreet}, {town}
- Where a value exists for
{propunit}
, the popup will displayAddress: Unit 5, 25 High street, Melbourne
- Where
{propunit}
has no value, the popup will displayAddress: 27 High street, Melbourne
Using HTML within a conditional
Let's say you have data where a link exists in some records, but not in others. Rather than displaying an empty link that will confuse your users, you can conditionally display the link only when a value exists in the URL field for the selected record.
#{ '{url}' == '' ? '' : '<a href="{url}">Link</a>' }#
- Where
{url}
is empty, nothing will be displayed - Where
{url}
is not empty, a link will be rendered in the popup using the selected feature's{url}
value as the link's URL.
Note that some reserved characters used to write a link will "break" the equation. In order to use links, or any other HTML within a conditional statement, we first must encode the HTML as hexadecimal entities. You can easily do this with an online encoding tool.
For example, <a href="{url}">Link</a>
encoded as HTML entities will become <a href="{url}">Link</a>
.
We can insert this into the conditional, and our equation will be valid.
#{ '{url}' == '' ? '' : '<a href="{url}">Link</a>' }#
The HTML entities will be rendered in their correct form when the popup is viewed.
Using conditionals in Query and Proximity Reports
When composing a custom report in the Query or Proximity tools, you can also use summary values of the query or proximity results for any numeric attributes in the dataset to create conditional statements.
Summary types
There are 6 types of summary values available: total, mean, min, max, median and mean. The format to display a summary value is {attribute:type}
.
For example, given the field {population}
exists, the results summary values can be considered as:
{population:total}
sum of all results{population:min}
minimum value of all results{population:max}
maximum value of all results{population:median}
median value of all results{population:mean}
mean value of all results
One can also return the count of records using the :count variable. For example, {population:count}
returns a count of all features in the query selection where the {population}
field contains a value. NULL values are not counted.
The syntax in the report is the same as in the custom popup:
#{ 'condition' ? 'value_if_true' : 'value_if_false' }#
When using summary variables in query or proximity reports, simply add the type:
#{ '{population:total}' >= '50000' ? "Population is greater than or equal to 50K" : "Population is less than 50K" }#
In this example, if the query selection results for {population:total} is greater than or equal to 50,000, the report will display "Population is greater than or equal to 50K"
Query and Proximity report examples
#{ '{attribute:count}' >= '5' ? 'value_if_true' : 'value_if_false' }#
will return True if the count of attribute
returned by the query or proximity search is greater than or equal to 5.
#{ '{attribute:total}' != '0' ? 'value_if_true' : 'value_if_false' }#
will return True if the resulting total sum of attribute
is not 0.
#{ '{attribute:max}' >= '4000' ? 'value_if_true' : 'value_if_false' }#
will return True if the maximum value of attribute
exceeds 4000.
#{ '{attribute_x:total}' > '{attribute_y:total}' ? 'value_if_true' : 'value_if_false' }#
will return True if the sum total of attribute_x
greater than the sum total of {attribute_y}
.
Nested conditions
You can nest statements within any part of the statement.
#{ 'condition' ? ( 'condition' ? 'value_if_true' : 'value_if_false' ) : ( 'condition' ? 'value_if_true' : 'value_if_false' ) }#
Wrapping each statement in parenthesis will help keep things tidy, but is not required.
For example, this will return either >=75, >=50, >=20, or <25, based on the value of {attribute:count}
in the query report.
#{ '{attribute:count}' >= '75' ? '>=75' : ( '{attribute:count}' >= '50' ? '>=50' : ( '{attribute:count}' >= '25' ? '>=25' : '<25' ) ) }#