Skip to Content
Octopus Energy Packages

Render Usage Charts

Group measurements into reusable Coral cards with scoped preferences

After fetching measurements, render one card per resolved market supply point. The parent component owns page-level concerns like timeframe, fetch state, grouping, and storage adapter choice. The card owns one visual unit: preferences, transformation, date summary, and rendering for a single market supply point.

What you'll build

By the end of this guide, your energy overview page will render one reusable ConsumptionCard per market supply point, with shared timeframe state and preference storage.

Share timeframe, scope preferences

Use the shared timeframe from the parent. Scope preference storage when each card needs to remember its own display settings.

Prepare preference storage

The parent knows which cards can render, so it owns the storage adapter choice. Use localStorage and create a small factory that receives the card identity.

_components/consumption-overview.tsx
import { createLocalStorageAdapter } from "@octopus-energy/measurements/storage/local-storage";

function getPreferenceStorage(marketSupplyPointId: string) {
  return createLocalStorageAdapter({ scope: marketSupplyPointId });
}

This keeps the card storage-agnostic. A later improvement swaps this factory for URL-backed preference storage.

Group and loop over supply points

Create card groups from the resolved supply point descriptors, then loop over those groups. matching starts from the descriptor list, so card identity and ordering come from the resolved market supply points rather than from whichever measurements happened to come back for the selected range.

_components/consumption-overview.tsx
import { groupMeasurements } from "@octopus-energy/measurements";

const groups = groupMeasurements(measurements, {
  matching: marketSupplyPoints,
});

return groups.map(({ filters, key, measurements }) => (
  <ConsumptionCard
    getPreferenceStorage={getPreferenceStorage}
    key={key}
    marketSupplyPointId={filters.marketSupplyPointId}
    measurements={measurements}
    timeframe={timeframe}
    utility={filters.utility}
  />
));

Define the reusable card

Use the storage factory with the card's identity, then transform the grouped measurements into the prop shape expected by Coral. The card performs two transformations: createMeasurementsOverview turns raw measurements into chart-ready overview data, and getCardProps adapts that overview data plus preference state to EnergyOverviewCard props.

_components/consumption-card.tsx
import { Card } from "@octopus-energy/coral/atoms";
import {
  EnergyOverviewCard,
  EnergyOverviewDataSection,
  EnergyOverviewDateDisplay,
  EnergyOverviewEnergyUsage,
  EnergyOverviewHeader,
  EnergyOverviewSettingsAndDisplay,
  EnergyOverviewSettingsControls,
  EnergyOverviewStandingCharge,
  EnergyOverviewTimeframeSummary,
  EnergyOverviewTitle,
  EnergyOverviewTotalValue,
  EnergyOverviewUnitToggle,
  EnergyOverviewUnitToggleOption,
  EnergyOverviewViewType,
} from "@octopus-energy/coral/molecules";
import type { MeasurementRecord, Utility } from "@octopus-energy/measurements";
import { getCardProps, getDateRangeDisplay } from "@octopus-energy/measurements/compat";
import { createMeasurementsOverview } from "@octopus-energy/measurements/overview";
import { usePreferences } from "@octopus-energy/measurements/preferences";
import type { StorageAdapter } from "@octopus-energy/measurements/storage";
import type { Timeframe } from "@octopus-energy/measurements/timeframe";
import { enGB } from "date-fns/locale/en-GB";

type Props = {
  getPreferenceStorage(marketSupplyPointId: string): StorageAdapter;
  marketSupplyPointId: string;
  measurements: MeasurementRecord[] | undefined;
  timeframe: Timeframe;
  utility: Utility;
};

export function ConsumptionCard({
  getPreferenceStorage,
  marketSupplyPointId,
  measurements,
  timeframe,
  utility,
}: Props) {
  const [preferences, preferencesActions] = usePreferences({
    storage: getPreferenceStorage(marketSupplyPointId),
  });

  const overview = createMeasurementsOverview(measurements, {
    preferences,
    timeframe,
  });

  const cardProps = getCardProps({
    overview,
    preferences,
    preferencesActions,
    utility,
  });

  const label = utility === "electricity" ? "Electricity" : "Gas";

  return (
    <Card borderless theme="base500">
      <EnergyOverviewCard
        {...cardProps}
        currency="GBP"
        dateLocale={enGB}
        locale="en-GB"
        timezone={timeframe.timezone}
      >
        <EnergyOverviewHeader>
          <EnergyOverviewTitle>
            {label} {marketSupplyPointId}
          </EnergyOverviewTitle>
          <EnergyOverviewViewType />
        </EnergyOverviewHeader>

        <EnergyOverviewDataSection>
          <EnergyOverviewTimeframeSummary>
            <EnergyOverviewDateDisplay>
              {getDateRangeDisplay(timeframe, enGB)}
            </EnergyOverviewDateDisplay>
            <EnergyOverviewTotalValue />
          </EnergyOverviewTimeframeSummary>

          <EnergyOverviewSettingsAndDisplay>
            <EnergyOverviewSettingsControls>
              <EnergyOverviewStandingCharge />
              <EnergyOverviewUnitToggle>
                <EnergyOverviewUnitToggleOption value="kWh">kWh</EnergyOverviewUnitToggleOption>
                <EnergyOverviewUnitToggleOption value="cost">£</EnergyOverviewUnitToggleOption>
              </EnergyOverviewUnitToggle>
            </EnergyOverviewSettingsControls>

            <EnergyOverviewEnergyUsage>
              {label} usage {marketSupplyPointId}
            </EnergyOverviewEnergyUsage>
          </EnergyOverviewSettingsAndDisplay>
        </EnergyOverviewDataSection>
      </EnergyOverviewCard>
    </Card>
  );
}

Implementation checkpoint

At this checkpoint, the parent groups measurements by market supply point and renders a reusable ConsumptionCard for each group. Each card owns preferences, overview transforms, date summary, and Coral card rendering.

FAQ

On this page