Overview
Chart overview transform functions and output types
createMeasurementsOverview
A pure function that transforms MeasurementRecord[] into chart-ready data for
a single utility or supply point. Fetching returns a flat array, so filter or
group records before calling this transformer.
import { groupMeasurements } from "@octopus-energy/measurements";
import { createMeasurementsOverview } from "@octopus-energy/measurements/overview";createMeasurementsOverviewProp
Type
const { electricity } = groupMeasurements(records, {
by: ["utility"],
});
const overview = createMeasurementsOverview(electricity?.measurements, {
timeframe,
preferences,
});For one supply point, match on both utility and marketSupplyPointId:
const [meter] = groupMeasurements(records, {
matching: [{ utility: "electricity", marketSupplyPointId }],
});
const overviewForMeter = createMeasurementsOverview(meter.measurements, {
timeframe,
preferences,
});Cost normalization
The default normalizer converts costs from minor to major currency units using Intl.NumberFormat currency precision. For GBP, minor units are pence and major units are pounds.
Without a currency, the default normalizer divides amounts by 100.
Pass normalizeCost in the optional third argument to override this conversion. normalizeCost receives { amount, currency } and returns a numeric amount in major units without display rounding. amount is numeric; currency is optional.
The transform uses this function for:
- Cost chart points
- Unit rates
- Default cost totals, including standing charges
For APIs that already return major units, return amount unchanged:
const overview = createMeasurementsOverview(
meter.measurements,
{ timeframe, preferences },
{ normalizeCost: ({ amount }) => amount },
);When preferences.tax is true, the transform reads currency from costInclTax.costCurrency. Otherwise, it uses costExclTax.costCurrency. Unit rates always use costExclTax.costCurrency.
Use one currency per overview. For totals, the transform uses the first currency associated with cost data.
With non-empty measurements, default cost aggregation calls normalizeCost even when no costs contribute to a total. In that case, it passes amount: 0 and no currency. Empty or missing measurements return early without calling normalizeCost.
Custom aggregation
Optionally, you can pass in custom aggregate functions.
By default, values are summed as at their original precision and rounded to the requisite decimal places for display in the table at the end.
It is possible to pass in your own functions, allowing you to customise how these values are aggregated:
const [meter] = groupMeasurements(records, {
matching: [{ utility: "electricity", marketSupplyPointId }],
});
const overviewForMeter = createMeasurementsOverview(
meter.measurements,
{
timeframe,
preferences,
},
{
sumMeasurementValues: customMeasurementsAggregateFunction,
sumStatisticsCost: customCostAggregateFunction,
},
);The measurements package provides two alternative functions called sumRoundedMeasurementValues and sumRoundedStatisticsCost out of the box, which round all values to a given number of decimal places first and then sum them.
import {
sumRoundedMeasurementValues,
sumRoundedStatisticsCost,
} from "@octopus-energy/measurements/overview";
const [meter] = groupMeasurements(records, {
matching: [{ utility: "electricity", marketSupplyPointId }],
});
const overviewForMeter = createMeasurementsOverview(
meter.measurements,
{
timeframe,
preferences,
},
{
sumMeasurementValues: sumRoundedMeasurementValues,
sumStatisticsCost: sumRoundedStatisticsCost,
},
);By default, sumRoundedMeasurementValues rounds each measurement to 3 decimal places. sumRoundedStatisticsCost rounds costs to whole minor units before summing, then converts the total with the default normalizer. The cost helper’s decimalPlaces argument sets precision in raw minor units, not display decimal places.
Round before summing when table rows use the same precision and need to add up to the displayed total.
A custom sumStatisticsCost receives raw statistics and the tax preference, and must return a total in major units. The transform does not normalize that result again. sumRoundedStatisticsCost always uses the default normalizer, not an injected normalizeCost dependency.
Configuration and output types
The transform accepts options and dependencies, and returns a MeasurementsOverview.
MeasurementsOverviewOptionsProp
Type
MeasurementsOverviewDependenciesProp
Type
MeasurementsOverviewProp
Type
ChartDataPointProp
Type
Related
- Pipeline: where overview transformation fits in the data flow
- Measurements: producing normalized records
- Market Supply Points: resolving supply points to fetch and filter measurements
- Compat: converting overview and summary output to Coral props