Skip to content

Commit 8a95a60

Browse files
authored
Merge pull request #585 from adobe/clamoureux/axisSecondGranularity
Second granularity for axis
2 parents de540f4 + 96e8e1f commit 8a95a60

File tree

10 files changed

+108
-7
lines changed

10 files changed

+108
-7
lines changed

packages/react-spectrum-charts/src/stories/components/Axis/Axis.story.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ Time.args = {
204204
labelAlign: 'center',
205205
};
206206

207+
const SecondGranularity = bindWithProps(TimeAxisBarStory);
208+
SecondGranularity.args = {
209+
granularity: 'second',
210+
position: 'bottom',
211+
baseline: true,
212+
labelFormat: 'time',
213+
ticks: true,
214+
labelAlign: 'center',
215+
};
216+
217+
const SecondGranularityLine = bindWithProps(TimeAxisStory);
218+
SecondGranularityLine.args = {
219+
granularity: 'second',
220+
position: 'bottom',
221+
baseline: true,
222+
labelFormat: 'time',
223+
ticks: true,
224+
labelAlign: 'center',
225+
};
226+
207227
const SubLabels = bindWithProps(SubLabelStory);
208228
SubLabels.args = {
209229
position: 'bottom',
@@ -317,6 +337,16 @@ CurrencyFormatSpecifier.args = {
317337
title: 'Conversion Rate',
318338
};
319339

340+
const VerticalSecondGranularity = bindWithProps(VerticalTimeAxisStory);
341+
VerticalSecondGranularity.args = {
342+
granularity: 'second',
343+
position: 'left',
344+
baseline: true,
345+
labelFormat: 'time',
346+
ticks: true,
347+
labelAlign: 'center',
348+
};
349+
320350
export {
321351
Basic,
322352
ControlledLabels,
@@ -325,12 +355,15 @@ export {
325355
MultilineTitle,
326356
NonLinearAxis,
327357
NumberFormat,
358+
SecondGranularity,
359+
SecondGranularityLine,
328360
SubLabels,
329361
TickMinStep,
330362
Time,
363+
VerticalSecondGranularity,
364+
VerticalTimeAxis,
331365
YearGranularity,
332366
TruncateLabels,
333-
VerticalTimeAxis,
334367
CurrencyLocale,
335368
CurrencyFormatSpecifier,
336369
};

packages/react-spectrum-charts/src/stories/components/Axis/Axis.test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ import {
1919
MultilineTitle,
2020
NonLinearAxis,
2121
NumberFormat,
22+
SecondGranularity,
23+
SecondGranularityLine,
2224
SubLabels,
2325
TickMinStep,
2426
Time,
27+
VerticalSecondGranularity,
2528
VerticalTimeAxis,
2629
} from './Axis.story';
2730

@@ -65,6 +68,26 @@ describe('Axis', () => {
6568
});
6669

6770
describe('Time axis', () => {
71+
test('Second renders properly with line', async () => {
72+
render(<SecondGranularityLine {...SecondGranularityLine.args} />);
73+
const chart = await findChart();
74+
expect(chart).toBeInTheDocument();
75+
76+
expect(screen.getByText('Dec 2')).toBeInTheDocument();
77+
const timeLabels = screen.getAllByText(/\d+:\d+:\d+ [AP]M/);
78+
expect(timeLabels.length).toBeGreaterThan(0);
79+
});
80+
81+
test('Second renders properly', async () => {
82+
render(<SecondGranularity {...SecondGranularity.args} />);
83+
const chart = await findChart();
84+
expect(chart).toBeInTheDocument();
85+
86+
expect(screen.getByText('Dec 2')).toBeInTheDocument();
87+
const timeLabels = screen.getAllByText(/\d+:\d+:\d+ [AP]M/);
88+
expect(timeLabels.length).toBeGreaterThan(0);
89+
});
90+
6891
test('Minute renders properly', async () => {
6992
render(<Time {...Time.args} granularity="minute" />);
7093
const chart = await findChart();
@@ -134,6 +157,17 @@ describe('Axis', () => {
134157
});
135158

136159
describe('Vertical time axis', () => {
160+
test('Second renders properly', async () => {
161+
render(<VerticalSecondGranularity {...VerticalSecondGranularity.args} />);
162+
const chart = await findChart();
163+
expect(chart).toBeInTheDocument();
164+
165+
// Day and time are in the same label for this configuration.
166+
expect(screen.getByText(/Dec 2.+\d+:\d+:\d+ [AP]M/)).toBeInTheDocument();
167+
const timeLabels = screen.getAllByText(/\d+:\d+:\d+ [AP]M/);
168+
expect(timeLabels.length).toBeGreaterThan(0);
169+
});
170+
137171
test('Minute renders properly', async () => {
138172
render(<VerticalTimeAxis {...VerticalTimeAxis.args} granularity="minute" />);
139173
const chart = await findChart();

packages/react-spectrum-charts/src/stories/components/Axis/timeData.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
{
2+
"second": [
3+
{ "datetime": 1670005800000, "value": 5723, "series": "Downloads" },
4+
{ "datetime": 1670005801000, "value": 6245, "series": "Downloads" },
5+
{ "datetime": 1670005802000, "value": 7102, "series": "Downloads" },
6+
{ "datetime": 1670005803000, "value": 4587, "series": "Downloads" },
7+
{ "datetime": 1670005804000, "value": 3214, "series": "Downloads" },
8+
{ "datetime": 1670005805000, "value": 5698, "series": "Downloads" },
9+
{ "datetime": 1670005806000, "value": 6321, "series": "Downloads" },
10+
{ "datetime": 1670005807000, "value": 4578, "series": "Downloads" },
11+
{ "datetime": 1670005808000, "value": 5214, "series": "Downloads" },
12+
{ "datetime": 1670005809000, "value": 6874, "series": "Downloads" },
13+
{ "datetime": 1670005810000, "value": 5321, "series": "Downloads" },
14+
{ "datetime": 1670005811000, "value": 4562, "series": "Downloads" },
15+
{ "datetime": 1670005812000, "value": 5478, "series": "Downloads" },
16+
{ "datetime": 1670005813000, "value": 6547, "series": "Downloads" },
17+
{ "datetime": 1670005814000, "value": 4578, "series": "Downloads" },
18+
{ "datetime": 1670005815000, "value": 5214, "series": "Downloads" },
19+
{ "datetime": 1670005816000, "value": 6325, "series": "Downloads" },
20+
{ "datetime": 1670005817000, "value": 5421, "series": "Downloads" },
21+
{ "datetime": 1670005818000, "value": 4587, "series": "Downloads" },
22+
{ "datetime": 1670005819000, "value": 5123, "series": "Downloads" },
23+
{ "datetime": 1670005820000, "value": 6421, "series": "Downloads" },
24+
{ "datetime": 1670005821000, "value": 5478, "series": "Downloads" },
25+
{ "datetime": 1670005822000, "value": 4536, "series": "Downloads" },
26+
{ "datetime": 1670005823000, "value": 5874, "series": "Downloads" },
27+
{ "datetime": 1670005824000, "value": 6523, "series": "Downloads" },
28+
{ "datetime": 1670005825000, "value": 5784, "series": "Downloads" },
29+
{ "datetime": 1670005826000, "value": 4563, "series": "Downloads" },
30+
{ "datetime": 1670005827000, "value": 5213, "series": "Downloads" },
31+
{ "datetime": 1670005828000, "value": 6478, "series": "Downloads" },
32+
{ "datetime": 1670005829000, "value": 5123, "series": "Downloads" }
33+
],
234
"minute": [
335
{ "datetime": 1670005860000, "value": 3151, "series": "Downloads" },
436
{ "datetime": 1670005920000, "value": 8618, "series": "Downloads" },

packages/vega-spec-builder/src/area/areaSpecBuilder.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const defaultSpec = initializeSpec({
7171
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
7272
field: DEFAULT_TIME_DIMENSION,
7373
type: 'timeunit',
74-
units: ['year', 'month', 'date', 'hours', 'minutes'],
74+
units: ['year', 'month', 'date', 'hours', 'minutes', 'seconds'],
7575
},
7676
],
7777
values: [],

packages/vega-spec-builder/src/axis/axisLabelUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export const getTimeLabelFormats = (
4747
granularity: Granularity
4848
): { secondaryLabelFormat: string; primaryLabelFormat: string; tickCount: TickCount } => {
4949
switch (granularity) {
50+
case 'second':
51+
return { secondaryLabelFormat: '%-I:%M:%S %p', primaryLabelFormat: '%b %-d', tickCount: 'second' };
5052
case 'minute':
5153
return { secondaryLabelFormat: '%-I:%M %p', primaryLabelFormat: '%b %-d', tickCount: 'minute' };
5254
case 'hour':

packages/vega-spec-builder/src/bar/barSpecBuilder.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ const timeTransform: Transforms[] = [
148148
as: ['datetime0', 'datetime1'],
149149
field: 'browser',
150150
type: 'timeunit',
151-
units: ['year', 'month', 'date', 'hours', 'minutes'],
151+
units: ['year', 'month', 'date', 'hours', 'minutes', 'seconds'],
152152
},
153153
];
154154

packages/vega-spec-builder/src/data/dataUtils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('addTimeTransform()', () => {
2626
{
2727
type: 'timeunit',
2828
field: dimension,
29-
units: ['year', 'month', 'date', 'hours', 'minutes'],
29+
units: ['year', 'month', 'date', 'hours', 'minutes', 'seconds'],
3030
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
3131
},
3232
];

packages/vega-spec-builder/src/data/dataUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const addTimeTransform = produce<Transforms[], [string]>((transforms, dim
3232
transforms.push({
3333
type: 'timeunit',
3434
field: dimension,
35-
units: ['year', 'month', 'date', 'hours', 'minutes'],
35+
units: ['year', 'month', 'date', 'hours', 'minutes', 'seconds'],
3636
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
3737
});
3838
}

packages/vega-spec-builder/src/line/lineSpecBuilder.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const defaultSpec = initializeSpec({
7676
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
7777
field: DEFAULT_TIME_DIMENSION,
7878
type: 'timeunit',
79-
units: ['year', 'month', 'date', 'hours', 'minutes'],
79+
units: ['year', 'month', 'date', 'hours', 'minutes', 'seconds'],
8080
},
8181
],
8282
values: [],

packages/vega-spec-builder/src/types/axis/axisSpec.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { NumberFormat, Orientation, PartiallyRequired, Position } from '../specU
1616
import { AxisAnnotationOptions } from './axisAnnotationSpec.types';
1717
import { ReferenceLineOptions } from './referenceLineSpec.types';
1818

19-
export type Granularity = 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
19+
export type Granularity = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
2020
/**
2121
* `center` will set the align to `center` for horizontal axes and the baseline to `middle` for vertical axes.
2222
*

0 commit comments

Comments
 (0)