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.
A function must meet the following conditions to be used for styling in the Map application:
FunctionsMap to return the map.Record with ObjectSpecifier keys to return the map.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 21import { 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 27import { 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.
After you publish your function, you can use its values in the Map application to:
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.
