Consider using the Vega Chart widget if the Chart XY widget does not enable desired functionality or formatting.
The Chart XY widget is used to visualize Objects as interactive charts. Module builders configuring a Chart XY widget can:
The below screenshot shows an example of three configured Chart XY widgets displaying Flight Alerts
data:
In the image below, to the left of the blue arrow you can see a newly added (but not yet configured) Chart XY widget, alongside its initial configuration panel. To the right of the blue arrow in the image below, you can see an individual Layer configuration panel with the backing object set of Flight Alerts
already populated:
Configuring a Layer is required to add data to the Chart XY widget. The following configuration options are available for a Layer:
In addition to the configuration options for a layer described above, the main Chart XY configuration panel contains a number of chart-wide configuration options:
Configuring a function-backed layer requires writing a function that returns either a TwoDimensionalAggregation
or ThreeDimensionalAggregation
.
Below is a full example that returns a TwoDimensionalAggregation
in order to chart one time series divided by another time series:
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 28 29 30 31 32 33
import { Function, TwoDimensionalAggregation, ThreeDimensionalAggregation, IRange, Timestamp } from "@foundry/functions-api"; import { ObjectSet, MyObjectType } from "@foundry/ontology-api" export class TimeseriesAggregations { @Function() public async percentOfTotal(objects:ObjectSet<MyObjectType>): Promise<TwoDimensionalAggregation<IRange<Timestamp>>> { const numerators = await objects.groupBy(e => e.date.byDays()) .sum(e => e.value); const denominators = await objects.groupBy(e => e.date.byDays()) .sum(e => e.total); return this.divide(numerators, denominators); } private divide(numerators:TwoDimensionalAggregation<IRange<Timestamp>>, denominators: TwoDimensionalAggregation<IRange<Timestamp>>): TwoDimensionalAggregation<IRange<Timestamp>> { const percentage = numerators.buckets.map((bucket, i) => { const numerator = bucket.value; const denominator = denominators.buckets[i].value; if (denominator == 0) { return { key: bucket.key, value: 0 }; } return { key: bucket.key, value: numerator / denominator } }); return { buckets: percentage }; } }
Below is a full example that returns a ThreeDimensionalAggregation
which will chart a separate series for each value returned by segmentBy()
:
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 28 29 30 31 32 33 34
import { Function, TwoDimensionalAggregation, ThreeDimensionalAggregation, IRange, Timestamp } from "@foundry/functions-api"; import { ObjectSet, MyObjectType } from "@foundry/ontology-api" export class TimeseriesAggregations { @Function() public async percentOfTotalSegmented(objects:ObjectSet<MyObjectType>): Promise<ThreeDimensionalAggregation<IRange<Timestamp>, string>> { const numerators = await objects.groupBy(e => e.date.byDays()) .segmentBy(e => e.groupId.topValues()) .sum(e => e.value); const denominators = await objects.groupBy(e => e.date.byDays()) .segmentBy(e => e.groupId.topValues()) .sum(e => e.total); return this.divideThreeDimensional(numerators, denominators); } private divideThreeDimensional(numerators:ThreeDimensionalAggregation<IRange<Timestamp>, string>, denominators: ThreeDimensionalAggregation<IRange<Timestamp>, string>): ThreeDimensionalAggregation<IRange<Timestamp>, string> { var percentage = numerators.buckets; //copy for (let i = 0; i < numerators.buckets.length; i++) { for (let j = 0; j < numerators.buckets[i].value.length; j++) { percentage[i].value[j].value = numerators.buckets[i].value[j].value / denominators.buckets[i].value[j].value; } } return { buckets: percentage }; } }
For more examples, see the Functions documentation on object set aggregations and creating custom aggregations.