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

feat: add default format to tooltip title #5684 #5748

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions __tests__/plots/tooltip/aapl-line-date-default-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { G2Spec } from '../../../src';
import { seriesTooltipSteps } from './utils';

export function aaplLineDateDefaultFormat(): G2Spec {
return {
type: 'view',
children: [
{
type: 'line',
data: {
type: 'fetch',
value: 'data/aapl.csv',
},
encode: {
x: 'date',
y: 'close',
},
},
],
};
}

aaplLineDateDefaultFormat.steps = seriesTooltipSteps([200, 300]);
1 change: 1 addition & 0 deletions __tests__/plots/tooltip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export { aaplLineSliderFilter } from './appl-line-slider-filter';
export { aaplLineAreaBasicSample } from './aapl-line-area-basic-sample';
export { aaplLineAreaBasicSampleMount } from './aapl-line-area-basic-sample-mount';
export { aaplAreaMissingDataTranspose } from './aapl-area-missing-data-transpose';
export { aaplLineDateDefaultFormat } from './aapl-line-date-default-format';
export { alphabetIntervalBrushTooltip } from './alphabet-interval-brush-tooltip';
export { mockLineFalsy } from './mock-line-falsy';
export { provincesLineGroupName } from './provinces-line-group-name';
Expand Down
9 changes: 8 additions & 1 deletion src/transform/maybeTitle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deepMix } from '@antv/util';
import { isUnset } from '../utils/helper';
import { TransformComponent as TC } from '../runtime';
import { dynamicFormatDateTime } from '../utils/dateFormat';
import { columnOf } from './utils/helper';

export type MaybeTitleOptions = {
Expand All @@ -27,7 +28,13 @@ export const MaybeTitle: TC<MaybeTitleOptions> = (options = {}) => {
if (titles.length === 0) return [I, mark];
const T = [];
for (const i of I) {
T[i] = { value: titles.map((t) => t[i]).join(', ') };
T[i] = {
value: titles
.map((t) =>
t[i] instanceof Date ? dynamicFormatDateTime(t[i] as Date) : t[i],
)
.join(', '),
};
}
return [
I,
Expand Down
22 changes: 22 additions & 0 deletions src/utils/dateFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function fillZero(digit: number) {
if (Math.abs(digit) > 10) return String(digit);
return digit.toString().padStart(2, '0');
}

export function dynamicFormatDateTime(date: Date) {
const year = date.getFullYear();
const month = fillZero(date.getMonth() + 1);
const day = fillZero(date.getDate());

const yyyyMMDD = `${year}-${month}-${day}`;

const hour = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();

if (hour || minutes || seconds)
return `${yyyyMMDD} ${fillZero(hour)}:${fillZero(minutes)}:${fillZero(
seconds,
)}`;
return yyyyMMDD;
}
Loading