Granularity select
Day, week, month, quarter or year: the period a time series is grouped by.
Preview
Granularity
Usage
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.
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.
| Prop | Type | Default | Description |
|---|---|---|---|
onChangerequired | ( | Called with the granularity the reader picks. | |
placeholder | string | "Select a granularity" | Text shown while nothing is chosen. Translates like title. |
primaryTimeRange | TimeRange | The 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. | |
granularity | Granularity | The chosen granularity. | |
granularities | Granularity[] | ["hour", | Which granularities to offer. |
clearable | boolean | false | Let the reader clear the value. |
componentName | string | A name for this component, included in the interaction events it dispatches on the DOM, for your analytics. | |
trackingId | string | An id included in the same events, to tell two instances apart. | |
title | string | The title over the field. A "key|fallback" string is looked up in your translations. | |
description | string | Smaller text under the title. Translates like title. | |
tooltip | string | Text for an info icon beside the title. |
A blank default means the prop is unset unless you pass it.
| Type | Definition |
|---|---|
Granularity | "second" | |
TimeRange | { from: Date |What a click’s TimeRange holds: Events. |
Field options
GranularitySelect reads only the common options in a field’s inputs.