Avoid Empty Charts
Choose an initial date with measurements to avoid an empty chart
Start with data, not an empty chart. Resolve an initial date from measurement availability and use it as the default date for the shared timeframe.
What you'll build
By the end of this guide, ConsumptionOverview will resolve an initial date with available
measurements before it creates timeframe state and fetches chart data.
This guide builds on Render Usage Charts.
Read stored timeframe state
Create one timeframe storage adapter and use it for both useStoredTimeframe
and useTimeframe. useStoredTimeframe reads persisted state without applying
fallback defaults, so you can tell whether storage already has a date before
resolving an anchor date.
"use client";
import { createLocalStorageAdapter } from "@octopus-energy/measurements/storage/local-storage";
import { useStoredTimeframe, useTimeframe } from "@octopus-energy/measurements/timeframe";
const timeframeStorage = createLocalStorageAdapter();
export function ConsumptionOverview({
accountNumber,
propertyId,
}: {
accountNumber: string;
propertyId: string;
}) {
const storedTimeframe = useStoredTimeframe(timeframeStorage);
// ...resolve market supply points, anchor date, and measurements below
}Resolve the anchor date
Resolve market supply points first. Then resolve an
anchor date only when the
stored timeframe does not already include a date. Pass any stored period,
interval, and the same IANA timezone used by useTimeframe to
anchorDateQueryOptions so the fallback date matches the range and calendar
the user or storage selected.
const marketSupplyPointsQuery = useQuery(
marketSupplyPointsQueryOptions(client, {
accountNumber,
propertyId,
utilityMarkets,
}),
);
const anchorDateQuery = useQuery(
anchorDateQueryOptions(client, {
enabled: !storedTimeframe.date,
interval: storedTimeframe.interval,
marketSupplyPoints: marketSupplyPointsQuery.data,
period: storedTimeframe.period,
propertyId,
timezone: "Europe/London",
}),
);anchorDateQueryOptions is disabled until market supply points are available.
When storage already has a date, the anchor-date query stays disabled because
stored state represents explicit user intent.
Use the date as a default
Only mount the component that calls useTimeframe after you know the initial
date: use the stored date when present, otherwise wait for the anchor-date
query. If neither provides a date, useTimeframe falls back to today.
Resolve the initial date before rendering
When you use anchorDateQueryOptions to resolve a missing date, render a loading state until that
query finishes, then render the overview with the resolved initial date. This keeps the first
visible timeframe and the first measurement query aligned. Gating only the measurement query can
still leave the overview initialized around a temporary fallback date.
const initialDate = storedTimeframe.date ?? anchorDateQuery.data;
const [timeframe, timeframeActions] = useTimeframe({
defaults: {
date: initialDate,
},
storage: timeframeStorage,
timezone: "Europe/London",
});Stored timeframe state still wins over defaults, so returning users keep their previous selection. If storage keeps a period but no date, the anchor-date query uses that period when resolving the missing date.
Fetch measurements for the initial timeframe
Fetch measurements from the component that owns useTimeframe. Because that
component only mounts after the initial date is ready, the measurement query is
keyed to the same timeframe the overview renders.
const measurementsQuery = useQuery(
measurementsQueryOptions(client, {
propertyId,
marketSupplyPoints,
timeframe,
}),
);Implementation checkpoint
At this checkpoint, ConsumptionOverview reads stored timeframe state, resolves
an anchor date only when storage has no date, waits until the initial date is
ready, and then fetches measurements for that resolved timeframe.