Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for formatType: "utc" #9265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/channeldef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,10 @@ export interface DatumDef<

export interface FormatMixins {
/**
* When used with the default `"number"` and `"time"` format type, the text formatting pattern for labels of guides (axes, legends, headers) and text marks.
* When used with the default `"number"`, `"time"`, or `"utc"` format type, the text formatting pattern for labels of guides (axes, legends, headers) and text marks.
*
* - If the format type is `"number"` (e.g., for quantitative fields), this is D3's [number format pattern](https://github.com/d3/d3-format#locale_format).
* - If the format type is `"time"` (e.g., for temporal fields), this is D3's [time format pattern](https://github.com/d3/d3-time-format#locale_format).
* - If the format type is `"time"` or `"utc"` (e.g., for temporal fields), this is D3's [time format pattern](https://github.com/d3/d3-time-format#locale_format).
*
* See the [format documentation](https://vega.github.io/vega-lite/docs/format.html) for more examples.
*
Expand All @@ -414,13 +414,13 @@ export interface FormatMixins {
format?: string | Dict<unknown>;

/**
* The format type for labels. One of `"number"`, `"time"`, or a [registered custom format type](https://vega.github.io/vega-lite/docs/config.html#custom-format-type).
* The format type for labels. One of `"number"`, `"time"`, `"utc"`, or a [registered custom format type](https://vega.github.io/vega-lite/docs/config.html#custom-format-type).
*
* __Default value:__
* - `"time"` for temporal fields and ordinal and nominal fields with `timeUnit`.
* - `"number"` for quantitative fields as well as ordinal and nominal fields without `timeUnit`.
*/
formatType?: 'number' | 'time' | string;
formatType?: 'number' | 'time' | 'utc' | string;
}

export type StringDatumDef<F extends Field = string> = DatumDef<F> & FormatMixins;
Expand Down Expand Up @@ -1338,7 +1338,7 @@ export function channelCompatibility(
*/
export function isFieldOrDatumDefForTimeFormat(fieldOrDatumDef: FieldDef<string> | DatumDef): boolean {
const {formatType} = getFormatMixins(fieldOrDatumDef);
return formatType === 'time' || (!formatType && isTimeFieldDef(fieldOrDatumDef));
return formatType === 'time' || formatType === 'utc' || (!formatType && isTimeFieldDef(fieldOrDatumDef));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use formatType === 'time' || formatType === 'utc' quite often. Can we pull it out into a reusable method?

}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/compile/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {TimeUnit} from './../timeunit';
import {datumDefToExpr} from './mark/encode/valueref';

export function isCustomFormatType(formatType: string) {
return formatType && formatType !== 'number' && formatType !== 'time';
return formatType && formatType !== 'number' && formatType !== 'time' && formatType !== 'utc';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use it here.

}

function customFormatExpr(formatType: string, field: string, format: string | Dict<unknown>) {
Expand Down Expand Up @@ -227,7 +227,10 @@ export function guideFormatType(
fieldOrDatumDef: FieldDef<string> | DatumDef<string>,
scaleType: ScaleType
) {
if (formatType && (isSignalRef(formatType) || formatType === 'number' || formatType === 'time')) {
if (
formatType &&
(isSignalRef(formatType) || formatType === 'number' || formatType === 'time' || formatType === 'utc')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here (possible other places as well).

) {
return formatType;
}
if (isFieldOrDatumDefForTimeFormat(fieldOrDatumDef) && scaleType !== 'time' && scaleType !== 'utc') {
Expand Down
1 change: 1 addition & 0 deletions test/compile/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ describe('Format', () => {
it('should return existing format type', () => {
expect(guideFormatType('number', {field: ' foo', type: 'quantitative'}, 'ordinal')).toBe('number');
expect(guideFormatType('time', {field: ' foo', type: 'quantitative'}, 'ordinal')).toBe('time');
expect(guideFormatType('utc', {field: ' foo', type: 'quantitative'}, 'ordinal')).toBe('utc');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test the new code? I thought it's for nominal.

});

it('should return utc for utc time units', () => {
Expand Down