Skip to content

Commit 473f359

Browse files
authored
fix: ensure temporal field propagates to time expression (#8757)
Fixes #7185
1 parent 044d93d commit 473f359

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

examples/compiled/bar_simple_binned_timeunit_special_chars.vg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"fill": {"value": "#4c78a8"},
6161
"ariaRoleDescription": {"value": "bar"},
6262
"description": {
63-
"signal": "\"b: \" + (format(datum[\"b\"], \"\")) + \"; a.b: \" + (timeFormat(datum[\"a.b\"], timeUnitSpecifier([\"year\",\"month\",\"date\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})))"
63+
"signal": "\"b: \" + (format(datum[\"b\"], \"\")) + \"; a.b: \" + (utcFormat(datum[\"a.b\"], timeUnitSpecifier([\"year\",\"month\",\"date\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})))"
6464
},
6565
"x": {"scale": "x", "field": "b_end"},
6666
"x2": {"scale": "x", "field": "b_start"},

examples/compiled/time_output_utc_timeunit.vg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"update": {
4747
"stroke": {"value": "#4c78a8"},
4848
"description": {
49-
"signal": "\"date (year-month-date-hours-minutes): \" + (timeFormat(datum[\"utcyearmonthdatehoursminutes_date\"], timeUnitSpecifier([\"year\",\"month\",\"date\",\"hours\",\"minutes\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"}))) + \"; price: \" + (format(datum[\"price\"], \"\"))"
49+
"signal": "\"date (year-month-date-hours-minutes): \" + (utcFormat(datum[\"utcyearmonthdatehoursminutes_date\"], timeUnitSpecifier([\"year\",\"month\",\"date\",\"hours\",\"minutes\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"}))) + \"; price: \" + (format(datum[\"price\"], \"\"))"
5050
},
5151
"x": {"scale": "x", "field": "utcyearmonthdatehoursminutes_date"},
5252
"y": {"scale": "y", "field": "price"},

examples/compiled/time_parse_binnedutc.vg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"fill": {"value": "#4c78a8"},
4141
"ariaRoleDescription": {"value": "bar"},
4242
"description": {
43-
"signal": "\"time: \" + (timeFormat(datum[\"date\"], timeUnitSpecifier([\"year\",\"month\",\"date\",\"hours\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})))"
43+
"signal": "\"time: \" + (utcFormat(datum[\"date\"], timeUnitSpecifier([\"year\",\"month\",\"date\",\"hours\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})))"
4444
},
4545
"x": {"field": {"group": "width"}},
4646
"x2": {"value": 0},

examples/compiled/time_parse_utc.vg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"stroke": {"value": "#4c78a8"},
4646
"ariaRoleDescription": {"value": "point"},
4747
"description": {
48-
"signal": "\"time: \" + (timeFormat(datum[\"utchours_date\"], timeUnitSpecifier([\"hours\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})))"
48+
"signal": "\"time: \" + (utcFormat(datum[\"utchours_date\"], timeUnitSpecifier([\"hours\"], {\"year-month\":\"%b %Y \",\"year-month-date\":\"%b %d, %Y \"})))"
4949
},
5050
"x": {"signal": "width", "mult": 0.5},
5151
"y": {"scale": "y", "field": "utchours_date"}

src/compile/format.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,25 @@ export function formatSignalRef({
9595
}
9696
}
9797

98+
function getTimeDef(def: FieldDef<string> | DatumDef<string>) {
99+
if (!isFieldDef(def)) {
100+
return {
101+
unit: undefined,
102+
utc: undefined,
103+
};
104+
}
105+
return normalizeTimeUnit(def.timeUnit) || {};
106+
}
107+
98108
if (isFieldOrDatumDefForTimeFormat(fieldOrDatumDef)) {
109+
const {unit: timeUnit, utc: isUTCUnit} = getTimeDef(fieldOrDatumDef);
99110
const signal = timeFormatExpression({
100111
field,
101-
timeUnit: isFieldDef(fieldOrDatumDef) ? normalizeTimeUnit(fieldOrDatumDef.timeUnit)?.unit : undefined,
112+
timeUnit,
102113
format,
103114
formatType: config.timeFormatType,
104115
rawTimeFormat: config.timeFormat,
105-
isUTCScale: isScaleFieldDef(fieldOrDatumDef) && fieldOrDatumDef.scale?.type === ScaleType.UTC,
116+
isUTCScale: isUTCUnit || (isScaleFieldDef(fieldOrDatumDef) && fieldOrDatumDef.scale?.type === ScaleType.UTC),
106117
});
107118
return signal ? {signal} : undefined;
108119
}

test/compile/format.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ describe('Format', () => {
7070
expect(expression).toBe(`utcFormat(datum["yearmonth_a"], "%Y")`);
7171
});
7272

73+
it('should get the right time expression for utcyearmonth', () => {
74+
const fieldDef = {timeUnit: 'utcyearmonth', field: 'a', type: TEMPORAL} as const;
75+
const expression = timeFormatExpression({
76+
field: vgField(fieldDef, {expr: 'datum'}),
77+
timeUnit: 'month',
78+
format: '%Y',
79+
isUTCScale: true,
80+
});
81+
expect(expression).toBe(`utcFormat(datum["utcyearmonth_a"], "%Y")`);
82+
});
83+
7384
it('should get the right time expression for with a custom timeFormatType', () => {
7485
const fieldDef = {field: 'a', type: TEMPORAL} as const;
7586
const expression = timeFormatExpression({
@@ -157,6 +168,19 @@ describe('Format', () => {
157168
});
158169

159170
describe('formatSignalRef()', () => {
171+
it('should derive the correct temporal unit', () => {
172+
expect(
173+
formatSignalRef({
174+
fieldOrDatumDef: {field: 'foo', type: TEMPORAL, timeUnit: 'utcdate'},
175+
format: undefined,
176+
formatType: undefined,
177+
config: {},
178+
}),
179+
).toEqual({
180+
signal:
181+
'utcFormat(utcdate_foo, timeUnitSpecifier(["date"], {"year-month":"%b %Y ","year-month-date":"%b %d, %Y "}))',
182+
});
183+
});
160184
it('should format ordinal field defs if format is present', () => {
161185
expect(
162186
formatSignalRef({

0 commit comments

Comments
 (0)