Function-based styling

Functions can be used in maps to generate dynamic values for objects. These values can be displayed in the Selection panel and applied to color objects on the map through value-based styling.

Define a function for styling

A function must meet the following conditions to be used for styling in maps:

  • It must have a single argument that is either an array or an object set.
  • It must return a FunctionsMap where the keys are objects from the input argument, and the values are a string or numeric type.

For example:

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import { Function, FunctionsMap, Double } from "@foundry/functions-api"; import { ExampleDataRoute } from "@foundry/ontology-api"; export class DerivedPropertyFunctions { @Function() public async flightCancellationPercentage(routes: ExampleDataRoute[]): Promise<FunctionsMap<ExampleDataRoute, Double>> { const routeMap = new FunctionsMap<ExampleDataRoute, Double>(); const allFlights = await Promise.all(routes.map(route => route.flights.allAsync())); for (let i = 0; i < routes.length; i++) { const route = routes[i]; const flights = allFlights[i]; const cancelledFlights = flights.filter(flight => flight.cancelled); const cancellationPercentage = (cancelledFlights.length / flights.length) * 100; routeMap.set(route, cancellationPercentage); } return routeMap; } }

The Map application passes batches of objects to your function and expects you to return values for all objects in the batch. You should not expect all objects in a layer to be provided in a single batch in the first argument. Your function should produce consistent results regardless of how objects are batched. This means that for any given object, the function should return the same value regardless of other objects included in the same batch.

Pass additional arguments to functions

When using a function for styling in the Workshop Map widget, your function can accept arguments in addition to the primary objects input.

When working with additional arguments, the first argument will still always specify the objects for which you need to compute and return values. The widget automatically provides values for this first argument, but only the additional arguments will be shown. The widget configuration allows you to specify values for additional arguments by selecting variables.

Additional arguments to a styling function in the Workshop Map widget.