Components · Controls

Granularity select

Day, week, month, quarter or year: the period a time series is grouped by.

Open in playground

Preview

Granularity

Usage

tsx
import { useState } from 'react';
import { ChartsProvider, GranularitySelect } from 'em-kit';
import type { Granularity } from 'em-kit';

export const Example = () => {
  const [granularity, setGranularity] = useState<Granularity>('month');
  return (
    <ChartsProvider>
      <div style={{ width: 320 }}>
        <GranularitySelect
          title="Granularity"
          granularity={granularity}
          onChange={setGranularity}
        />
      </div>
    </ChartsProvider>
  );
};
  • Pass the value to your time dimension as granularity, and query rows at that granularity.

Install with npm install em-kit. Needs <ChartsProvider> once, and your own state for its value: Quick start.

Examples

Fewer options

Only the granularities your data supports.

tsxDiffers from Usage
Show full code (19 lines)
import { useState } from 'react';
import { ChartsProvider, GranularitySelect } from 'em-kit';
import type { Granularity } from 'em-kit';

export const Example = () => {
  const [granularity, setGranularity] = useState<Granularity>('month');
  return (
    <ChartsProvider>
      <div style={{ width: 320 }}>
        <GranularitySelect
          title="Granularity"
          granularity={granularity}
          onChange={setGranularity}
          granularities={['week', 'month', 'quarter']}
        />
      </div>
    </ChartsProvider>
  );
};
<GranularitySelect
  title="Granularity"
  granularity={granularity}
  onChange={setGranularity}
  granularities={['week', 'month', 'quarter']}
/>

Props

Keyboard and screen readers: Accessibility.

PropTypeDefaultDescription
onChangerequired(newGranularity: Granularity) => voidCalled with the granularity the reader picks.
placeholderstring"Select a granularity"Text shown while nothing is chosen. Translates like title.
primaryTimeRangeTimeRangeThe dates being charted: { from: Date, to: Date }, with an optional relativeTimeString naming a preset. With it, only the granularities that suit the range are offered, and a choice that no longer suits is replaced through onChange.
granularityGranularityThe chosen granularity.
granularitiesGranularity[]["hour", "day", "week", "month", "quarter", "year"]Which granularities to offer.
clearablebooleanfalseLet the reader clear the value.
componentNamestringA name for this component, included in the interaction events it dispatches on the DOM, for your analytics.
trackingIdstringAn id included in the same events, to tell two instances apart.
titlestringThe title over the field. A "key|fallback" string is looked up in your translations.
descriptionstringSmaller text under the title. Translates like title.
tooltipstringText for an info icon beside the title.

A blank default means the prop is unset unless you pass it.

TypeDefinition
Granularity"second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year"
TimeRange{ from: Date | undefined; to: Date | undefined; relativeTimeString?: string; } | undefinedWhat a click’s TimeRange holds: Events.

Field options

GranularitySelect reads only the common options in a field’s inputs.