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 map where the keys are objects from the input argument, and the values are a string or numeric type.
- In TypeScript v1, use
FunctionsMapto return the map. - In TypeScript v2, use
RecordwithObjectSpecifierkeys to return the map.
For example:
```typescript tab="TypeScript v1" import { Function, FunctionsMap, Double } from "@foundry/functions-api"; import { ExampleDataRoute } from "@foundry/ontology-api";
export class DerivedPropertyFunctions {
@Function()
public async flightCancellationPercentage(routes: ExampleDataRoute[]): Promise
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;
}
}
typescript tab="TypeScript v2"
import { ObjectSpecifier, Osdk } from "@osdk/client";
import { Double } from "@osdk/functions";
import { ExampleDataRoute } from "@ontology/sdk";
async function flightCancellationPercentage(routes: Osdk.Instance
:::callout{theme="neutral"} The Map application passes objects to your function in batches, and expects values to be returned for all objects in the batch. All objects in a layer will not be provided in a single function call 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 which objects are 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.

中文翻译¶
基于函数的样式设计¶
函数(Functions)可用于在地图中为对象生成动态值。这些值可以显示在选择(Selection)面板中,并通过基于值的样式设计(value-based styling)应用于地图上的颜色对象。
定义用于样式设计的函数¶
函数必须满足以下条件才能用于地图中的样式设计:
- 必须有一个单一参数,该参数可以是数组或对象集。
- 必须返回一个映射(map),其中键(key)是输入参数中的对象,值(value)是字符串或数值类型。
- 在TypeScript v1中,使用
FunctionsMap返回映射。 - 在TypeScript v2中,使用带有
ObjectSpecifier键的Record返回映射。
例如:
```typescript tab="TypeScript v1" import { Function, FunctionsMap, Double } from "@foundry/functions-api"; import { ExampleDataRoute } from "@foundry/ontology-api";
export class DerivedPropertyFunctions {
@Function()
public async flightCancellationPercentage(routes: ExampleDataRoute[]): Promise
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;
}
}
typescript tab="TypeScript v2"
import { ObjectSpecifier, Osdk } from "@osdk/client";
import { Double } from "@osdk/functions";
import { ExampleDataRoute } from "@ontology/sdk";
async function flightCancellationPercentage(routes: Osdk.Instance
:::callout{theme="neutral"} 地图应用会分批将对象传递给您的函数,并期望为批次中的所有对象返回值。图层中的所有对象不会在第一个参数的单个函数调用中全部提供。无论对象如何分批,您的函数都应产生一致的结果。这意味着对于任何给定对象,无论同一批次中包含哪些对象,函数都应返回相同的值。 :::
向函数传递额外参数¶
在Workshop地图小部件中使用函数进行样式设计时,除了主要对象输入外,您的函数还可以接受其他参数。
处理额外参数时,第一个参数始终指定需要计算并返回值的对象。小部件会自动为第一个参数提供值,但只有额外参数会显示出来。小部件配置允许您通过选择变量来指定额外参数的值。
