Date Range Compare
1 个变体A dashboard date-range bar with presets, a range calendar, and a compare switch that drives a row of KPI cards.
Range and KPIs
Preset tabs, a two-month range calendar, and a compare switch drive a row of KPI cards. Values roll, change pills pop in when compare is on, and each sparkline redraws with a dashed previous-period line.
Visitors
129,483up1%Revenue
$144,673down1%Conversion
4.20%down2%Avg session
1m 56sdown2%
"use client";
import { DateRangeCompare } from "@/components/premium/date-range-compare";
export function DateRangeCompareKpisPreview() {
return (
<div className="w-full px-4 py-10">
<DateRangeCompare />
</div>
);
}
安装
API 参考
除标注为必填的属性外,其余均为可选。数据、文案和颜色默认使用示例内容。
DateRangeCompare12 个属性
kpisKPI cards in display order; four fit the row.
KpiDefinition[]getKpiDataComputes each KPI for a range and its comparison range. Defaults to deterministic sample data.
KpiDataFunctionformattersFormatters for each KPI format; any left out keep their defaults (USD for currency).
Partial<KpiFormatters>valueControlled value: preset, range, compare switch and compare mode.
DateRangeCompareValuedefaultValueStarting value when uncontrolled. Defaults to the 30d preset with compare on.
DateRangeCompareValueonChangeCalled with the whole next value whenever any part changes.
((value: DateRangeCompareValue) => void)presetsQuick ranges shown as tabs, before the Custom tab.
CompareRangePreset[]todayThe day presets end on and the last selectable day, `YYYY-MM-DD`.
string默认值
2026-11-28minEarliest selectable day, `YYYY-MM-DD`.
stringlocaleBCP 47 locale for dates.
stringlabelsCopy overrides; anything left out uses the English defaults.
Partial<DateRangeCompareLabels>classNameExtra classes for the bar.
string
ChangePill3 个属性
from必填Value in the comparison range.
numberto必填Value in the selected range.
numberclassNameExtra classes for the pill.
string
CompareControl9 个属性
compare必填Whether comparison is on.
booleancompareMode必填What the range is compared against.
"previous-period" | "previous-year"range必填The selected range; each comparison option shows the range it would compare against.
CompareDateRangelocale必填BCP 47 locale for the comparison ranges.
stringcurrentYearYear of "today" as `YYYY`; ranges in other years show their year.
stringlabels必填Copy for the switch, menu and range text.
Pick<DateRangeCompareLabels, "compare" | "compareMode" | "previousPeriod" | "previousYear" | "versus">onCompareChange必填Called when the switch flips.
(compare: boolean) => voidonCompareModeChange必填Called when a comparison mode is chosen.
(mode: CompareMode) => voidclassNameExtra classes for the group.
string
DateRangeBar9 个属性
valueControlled value: preset, range, compare switch and compare mode.
DateRangeCompareValuedefaultValueStarting value when uncontrolled. Defaults to the 30d preset with compare on.
DateRangeCompareValueonChangeCalled with the whole next value whenever any part changes.
((value: DateRangeCompareValue) => void)presetsQuick ranges shown as tabs, before the Custom tab.
CompareRangePreset[]todayThe day presets end on and the last selectable day, `YYYY-MM-DD`.
string默认值
2026-11-28minEarliest selectable day, `YYYY-MM-DD`.
stringlocaleBCP 47 locale for dates.
string默认值
en-USlabelsCopy overrides; anything left out uses the English defaults.
Partial<DateRangeCompareLabels>classNameExtra classes for the bar.
string
KpiCard7 个属性
kpi必填The KPI's label, format and colors.
KpiDefinitionsnapshot必填Values and trends for the selected and comparison ranges.
KpiSnapshotcompare必填Whether the change pill and comparison line show.
booleandrawKey必填Changes with the range, so the sparkline draws in again.
stringformatters必填Formatter for each KPI format.
KpiFormattersindex必填Position in the row, for the mount stagger.
numberclassNameExtra classes for the card.
string
KpiSparkline6 个属性
values必填Selected range trend, oldest first.
readonly number[]previousValues必填Comparison range trend on the same x positions as `values`.
readonly number[]showPrevious必填Whether to draw the dashed comparison line behind the trend.
booleandrawKey必填Changes whenever the range changes, so the line draws in again.
stringstrokeClassName必填Stroke class for the trend line, such as `stroke-sky-500`.
stringclassNameExtra classes for the svg; its size comes from here.
string
RangeCalendar7 个属性
range必填Applied range shown on the trigger and selected when the calendar opens.
CompareDateRangemax必填Latest selectable day, usually today, `YYYY-MM-DD`.
stringminEarliest selectable day, `YYYY-MM-DD`.
stringlocale必填BCP 47 locale for dates.
stringlabels必填Copy for the calendar and its buttons.
Pick<DateRangeCompareLabels, "calendar" | "previousMonth" | "nextMonth" | "day" | "days">onRangeChange必填Called with a complete range picked in the calendar.
(range: CompareDateRange) => voidclassNameExtra classes for the trigger.
string
useDateRangeValue3 个属性
valueControlled value.
DateRangeCompareValuedefaultValue必填Starting value when uncontrolled.
DateRangeCompareValueonChangeCalled with the next value whenever any part of it changes.
((value: DateRangeCompareValue) => void)
类型6 个类型
这些属性使用的对象类型及其字段。
CompareRangePreset3 个字段
id必填Stable id stored in the value, such as `30d`. Never `custom`.
stringlabel必填Tab label, such as "30d".
stringdays必填Days covered, ending on `today`. 1 means today only.
number
DateRangeCompareValue4 个字段
preset必填Selected preset id, or `custom` after picking dates in the calendar.
stringrange必填Selected range.
CompareDateRangecompare必填Whether the comparison range is shown.
booleancompareMode必填What the selected range is compared against.
CompareMode
KpiDefinition8 个字段
id必填Stable id.
stringlabel必填Card label, such as "Visitors".
stringformat必填Which formatter shows the value.
KpiFormataggregate必填`sum` adds the days up (visitors, revenue); `average` averages them (rates, durations).
"sum" | "average"base必填Typical value for one day.
numbertrend必填Daily growth as a fraction, such as 0.002 for 0.2% a day.
numbernoise必填How far a day can drift from its trend line, as a fraction of it.
numberstrokeClassName必填Sparkline stroke class, such as `stroke-sky-500`.
string
DateRangeCompareLabels13 个字段
presets必填Accessible name of the preset tabs.
stringcustom必填Tab label for a range picked in the calendar.
stringcalendar必填Accessible name of the calendar.
stringcompare必填Label beside the compare switch.
stringpreviousPeriod必填Option label for the previous period.
stringpreviousYear必填Option label for the same dates a year earlier.
stringcompareMode必填Accessible name of the comparison mode menu.
stringversus必填Prefix for the comparison range text, such as "vs".
stringday必填Unit after a one-day count on the range trigger.
stringdays必填Unit after a count of several days on the range trigger.
stringpreviousMonth必填Previous month button.
stringnextMonth必填Next month button.
stringkpis必填Accessible name of the KPI row.
string
CompareDateRange2 个字段
from必填First day of the range, `YYYY-MM-DD`.
stringto必填Last day of the range, `YYYY-MM-DD`.
string
KpiSnapshot5 个字段
id必填The KPI it belongs to.
stringvalue必填Value for the selected range.
numberprevious必填Value for the comparison range.
numberseries必填Bucketed trend for the selected range, oldest first.
number[]previousSeries必填Bucketed trend for the comparison range, the same length as `series`.
number[]