Skip to content

Commit

Permalink
Merge pull request #245 from adobe/mif/issue-217-datetime-data
Browse files Browse the repository at this point in the history
Issue-217 Support datetime data properly for Line & Bar & Area charts
  • Loading branch information
marshallpete committed Apr 16, 2024
2 parents 10a1cae + de9699d commit 6e85045
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/specBuilder/area/areaSpecBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ const defaultSpec = initializeSpec({
name: TABLE,
transform: [
{ as: MARK_ID, type: 'identifier' },
{
type: 'formula',
expr: `toDate(datum[\"${DEFAULT_TIME_DIMENSION}\"])`,
as: DEFAULT_TIME_DIMENSION,
},
{
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
field: DEFAULT_TIME_DIMENSION,
Expand Down
23 changes: 21 additions & 2 deletions src/specBuilder/data/dataUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,28 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { TABLE } from '@constants';
import { DEFAULT_TIME_DIMENSION, DEFAULT_TRANSFORMED_TIME_DIMENSION, TABLE } from '@constants';

import { getTableData } from './dataUtils';
import { addTimeTransform, getTableData } from './dataUtils';

describe('addTimeTransform()', () => {
test('should return the time transforms', () => {
const inputTransforms = [];
const dimension = 'datetime';
const outputTransforms = [{
type: 'formula',
expr: `toDate(datum[\"${dimension}\"])`,
as: dimension
},
{
type: 'timeunit',
field: dimension,
units: ['year', 'month', 'date', 'hours', 'minutes'],
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
}];
expect(addTimeTransform(inputTransforms, dimension)).toEqual(outputTransforms);
});
});

describe('getTableData()', () => {
test('should return the table data', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/specBuilder/data/dataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import { Compare, Data, FormulaTransform, SourceData, Transforms, ValuesData } f

export const addTimeTransform = produce<Transforms[], [string]>((transforms, dimension) => {
if (transforms.findIndex((transform) => transform.type === 'timeunit') === -1) {
transforms.push({
type: 'formula',
expr: `toDate(datum["${dimension}"])`,
as: dimension
});
transforms.push({
type: 'timeunit',
field: dimension,
Expand Down
7 changes: 6 additions & 1 deletion src/specBuilder/line/lineSpecBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ const defaultSpec = initializeSpec({
name: TABLE,
transform: [
{ as: MARK_ID, type: 'identifier' },
{
type: 'formula',
expr: `toDate(datum[\"${DEFAULT_TIME_DIMENSION}\"])`,
as: DEFAULT_TIME_DIMENSION,
},
{
as: [DEFAULT_TRANSFORMED_TIME_DIMENSION, `${DEFAULT_TIME_DIMENSION}1`],
field: DEFAULT_TIME_DIMENSION,
Expand Down Expand Up @@ -387,7 +392,7 @@ describe('lineSpecBuilder', () => {
...defaultLineProps,
children: [createElement(Trendline, { method: 'movingAverage-7' })],
})[0].transform
).toHaveLength(2);
).toHaveLength(3);
});

test('adds point data if displayPointMark is not undefined', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/specBuilder/scatter/scatterSpecBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe('addData()', () => {
test('should add time transform is dimensionScaleType === "time"', () => {
const data = addData(initializeSpec().data ?? [], { ...defaultScatterProps, dimensionScaleType: 'time' });
expect(data).toHaveLength(2);
expect(data[0].transform).toHaveLength(2);
expect(data[0].transform?.[1].type).toBe('timeunit');
expect(data[0].transform).toHaveLength(3);
expect(data[0].transform?.[2].type).toBe('timeunit');
});
test('should add additional filteredData if tooltip exists', () => {
const data = addData(initializeSpec().data ?? [], {
Expand Down
22 changes: 22 additions & 0 deletions src/stories/components/Line/Line.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ const LineStory: StoryFn<typeof Line> = (args): ReactElement => {
);
};

const LineStoryWithUTCData: StoryFn<typeof Line> = (args): ReactElement => {
const chartProps = useChartProps({ ...defaultChartProps, data: workspaceTrendsData.map(d => ({ ...d, datetime: new Date(d.datetime).toISOString() })) });
return (
<Chart {...chartProps}>
<Axis position="left" grid title="Users" />
<Axis position="bottom" labelFormat="time" baseline ticks />
<Line {...args} />
<Legend highlight />
</Chart>
);
};

const ComboStory: StoryFn<typeof Line> = (args): ReactElement => {
const chartProps = useChartProps(defaultChartProps);
return (
Expand Down Expand Up @@ -154,6 +166,15 @@ LineWithAxisAndLegend.args = {
scaleType: 'time',
};

const LineWithUTCDatetimeFormat = bindWithProps(LineStoryWithUTCData);
LineWithUTCDatetimeFormat.args = {
color: 'series',
dimension: 'datetime',
metric: 'users',
name: 'line0',
scaleType: 'time',
};

const LineType = bindWithProps(BasicLineStory);
LineType.args = {
color: 'series',
Expand Down Expand Up @@ -251,6 +272,7 @@ WithStaticPointsAndDialogs.args = {
export {
Basic,
LineWithAxisAndLegend,
LineWithUTCDatetimeFormat,
LineType,
Opacity,
TrendScale,
Expand Down
7 changes: 7 additions & 0 deletions src/stories/components/Line/Line.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
HistoricalCompare,
LineType,
LineWithAxisAndLegend,
LineWithUTCDatetimeFormat,
LinearTrendScale,
Opacity,
Tooltip,
Expand Down Expand Up @@ -71,6 +72,12 @@ describe('Line', () => {
expect(lines[0]).toBeInTheDocument();
});

test('Line with UTC datetime format renders', async () => {
render(<LineWithUTCDatetimeFormat {...LineWithAxisAndLegend.args} />);
expect(await screen.findByText('Nov')).toBeInTheDocument();
expect(await screen.findByText('11')).toBeInTheDocument();
});

test('LineType renders', async () => {
render(<LineType {...LineType.args} />);
const chart = await findChart();
Expand Down

0 comments on commit 6e85045

Please sign in to comment.