Function-based styling

Use functions in the Map application to calculate values for objects. You can display these values in the Selection panel or use them to style objects through value-based styling.

To create shapes and define their appearance in code, use a rendering function instead. To compare the approaches, review Use functions to customize maps.

Define a function for styling

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

  • Its first argument must be an array or object set containing the objects to style. In Workshop, the function can also accept additional arguments.
  • It must return a key-value map where each key is an object from the input and each value is a string or number.

The following function calculates the percentage of canceled flights on each route:

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; } }
Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import { ObjectSpecifier, Osdk } from "@osdk/client"; import { Double } from "@osdk/functions"; import { ExampleDataRoute } from "@ontology/sdk"; async function flightCancellationPercentage(routes: Osdk.Instance<ExampleDataRoute>[]): Promise<Record<ObjectSpecifier<ExampleDataRoute>, Double>> { const routeMap: Record<ObjectSpecifier<ExampleDataRoute>, Double> = {}; await Promise.all(routes.map(async route => { let totalFlights = 0; let cancelledFlights = 0; for await (const flight of route.$link.flights.asyncIter()) { totalFlights++; if (flight.cancelled) { cancelledFlights++; } } routeMap[route.$objectSpecifier] = totalFlights === 0 ? 0 : (cancelledFlights / totalFlights) * 100; })); return routeMap; } export default flightCancellationPercentage;

The TypeScript v2 example uses link traversal to read all linked flights, including results across multiple pages. Routes with no linked flights return zero.

The Map application passes objects to your function in batches, not all at once, and expects a value for every object in each batch. Your function must return the same value for an object regardless of which other objects are in the batch.

Apply a function to a map

After you publish your function, you can use its values in the Map application to:

  • Style a display: In a display attribute, such as color, select Function as the value source, then choose your function. Learn more about value-based styling.
  • Show values in the Selection panel: On the Properties tab of the Selection panel, select the plus icon, then choose your function.
  • Add values to labels: Include function values in a layer's label content.

Pass additional arguments in Workshop

In the Workshop Map widget, a styling function can accept other arguments in addition to the object input. The widget always provides the objects as the first argument and shows only the additional arguments in its configuration, where you can set each one to a variable.

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