Skip to content

Commit 6e85045

Browse files
authored
Merge pull request #245 from adobe/mif/issue-217-datetime-data
Issue-217 Support datetime data properly for Line & Bar & Area charts
2 parents 10a1cae + de9699d commit 6e85045

File tree

7 files changed

+68
-5
lines changed

7 files changed

+68
-5
lines changed

src/specBuilder/area/areaSpecBuilder.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ const defaultSpec = initializeSpec({
5757
name: TABLE,
5858
transform: [
5959
{ as: MARK_ID, type: 'identifier' },
60+
{
61+
type: 'formula',
62+
expr: `toDate(datum[\"${DEFAULT_TIME_DIMENSION}\"])`,
63+
as: DEFAULT_TIME_DIMENSION,
64+
},
6065
{
6166
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
6267
field: DEFAULT_TIME_DIMENSION,

src/specBuilder/data/dataUtils.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,28 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
import { TABLE } from '@constants';
12+
import { DEFAULT_TIME_DIMENSION, DEFAULT_TRANSFORMED_TIME_DIMENSION, TABLE } from '@constants';
1313

14-
import { getTableData } from './dataUtils';
14+
import { addTimeTransform, getTableData } from './dataUtils';
15+
16+
describe('addTimeTransform()', () => {
17+
test('should return the time transforms', () => {
18+
const inputTransforms = [];
19+
const dimension = 'datetime';
20+
const outputTransforms = [{
21+
type: 'formula',
22+
expr: `toDate(datum[\"${dimension}\"])`,
23+
as: dimension
24+
},
25+
{
26+
type: 'timeunit',
27+
field: dimension,
28+
units: ['year', 'month', 'date', 'hours', 'minutes'],
29+
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
30+
}];
31+
expect(addTimeTransform(inputTransforms, dimension)).toEqual(outputTransforms);
32+
});
33+
});
1534

1635
describe('getTableData()', () => {
1736
test('should return the table data', () => {

src/specBuilder/data/dataUtils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import { Compare, Data, FormulaTransform, SourceData, Transforms, ValuesData } f
2323

2424
export const addTimeTransform = produce<Transforms[], [string]>((transforms, dimension) => {
2525
if (transforms.findIndex((transform) => transform.type === 'timeunit') === -1) {
26+
transforms.push({
27+
type: 'formula',
28+
expr: `toDate(datum["${dimension}"])`,
29+
as: dimension
30+
});
2631
transforms.push({
2732
type: 'timeunit',
2833
field: dimension,

src/specBuilder/line/lineSpecBuilder.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ const defaultSpec = initializeSpec({
7070
name: TABLE,
7171
transform: [
7272
{ as: MARK_ID, type: 'identifier' },
73+
{
74+
type: 'formula',
75+
expr: `toDate(datum[\"${DEFAULT_TIME_DIMENSION}\"])`,
76+
as: DEFAULT_TIME_DIMENSION,
77+
},
7378
{
7479
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
7580
field: DEFAULT_TIME_DIMENSION,
@@ -387,7 +392,7 @@ describe('lineSpecBuilder', () => {
387392
...defaultLineProps,
388393
children: [createElement(Trendline, { method: 'movingAverage-7' })],
389394
})[0].transform
390-
).toHaveLength(2);
395+
).toHaveLength(3);
391396
});
392397

393398
test('adds point data if displayPointMark is not undefined', () => {

src/specBuilder/scatter/scatterSpecBuilder.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe('addData()', () => {
3434
test('should add time transform is dimensionScaleType === "time"', () => {
3535
const data = addData(initializeSpec().data ?? [], { ...defaultScatterProps, dimensionScaleType: 'time' });
3636
expect(data).toHaveLength(2);
37-
expect(data[0].transform).toHaveLength(2);
38-
expect(data[0].transform?.[1].type).toBe('timeunit');
37+
expect(data[0].transform).toHaveLength(3);
38+
expect(data[0].transform?.[2].type).toBe('timeunit');
3939
});
4040
test('should add additional filteredData if tooltip exists', () => {
4141
const data = addData(initializeSpec().data ?? [], {

src/stories/components/Line/Line.story.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ const LineStory: StoryFn<typeof Line> = (args): ReactElement => {
9393
);
9494
};
9595

96+
const LineStoryWithUTCData: StoryFn<typeof Line> = (args): ReactElement => {
97+
const chartProps = useChartProps({ ...defaultChartProps, data: workspaceTrendsData.map(d => ({ ...d, datetime: new Date(d.datetime).toISOString() })) });
98+
return (
99+
<Chart {...chartProps}>
100+
<Axis position="left" grid title="Users" />
101+
<Axis position="bottom" labelFormat="time" baseline ticks />
102+
<Line {...args} />
103+
<Legend highlight />
104+
</Chart>
105+
);
106+
};
107+
96108
const ComboStory: StoryFn<typeof Line> = (args): ReactElement => {
97109
const chartProps = useChartProps(defaultChartProps);
98110
return (
@@ -154,6 +166,15 @@ LineWithAxisAndLegend.args = {
154166
scaleType: 'time',
155167
};
156168

169+
const LineWithUTCDatetimeFormat = bindWithProps(LineStoryWithUTCData);
170+
LineWithUTCDatetimeFormat.args = {
171+
color: 'series',
172+
dimension: 'datetime',
173+
metric: 'users',
174+
name: 'line0',
175+
scaleType: 'time',
176+
};
177+
157178
const LineType = bindWithProps(BasicLineStory);
158179
LineType.args = {
159180
color: 'series',
@@ -251,6 +272,7 @@ WithStaticPointsAndDialogs.args = {
251272
export {
252273
Basic,
253274
LineWithAxisAndLegend,
275+
LineWithUTCDatetimeFormat,
254276
LineType,
255277
Opacity,
256278
TrendScale,

src/stories/components/Line/Line.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
HistoricalCompare,
3535
LineType,
3636
LineWithAxisAndLegend,
37+
LineWithUTCDatetimeFormat,
3738
LinearTrendScale,
3839
Opacity,
3940
Tooltip,
@@ -71,6 +72,12 @@ describe('Line', () => {
7172
expect(lines[0]).toBeInTheDocument();
7273
});
7374

75+
test('Line with UTC datetime format renders', async () => {
76+
render(<LineWithUTCDatetimeFormat {...LineWithAxisAndLegend.args} />);
77+
expect(await screen.findByText('Nov')).toBeInTheDocument();
78+
expect(await screen.findByText('11')).toBeInTheDocument();
79+
});
80+
7481
test('LineType renders', async () => {
7582
render(<LineType {...LineType.args} />);
7683
const chart = await findChart();

0 commit comments

Comments
 (0)