This is an automated email from the ASF dual-hosted git repository. shenyi pushed a commit to branch next in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git
The following commit(s) were added to refs/heads/next by this push: new 41656cf fix(time): fix time format precision in dataZoom label and tooltip 41656cf is described below commit 41656cfd86aded4084adead354bca117909f3266 Author: pissang <bm2736...@gmail.com> AuthorDate: Thu Jul 30 00:23:37 2020 +0800 fix(time): fix time format precision in dataZoom label and tooltip --- src/scale/Time.ts | 66 ++++++++++++++++++++++++++++++++----------------- src/util/time.ts | 74 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 95 insertions(+), 45 deletions(-) diff --git a/src/scale/Time.ts b/src/scale/Time.ts index b7cd2aa..6f7a121 100644 --- a/src/scale/Time.ts +++ b/src/scale/Time.ts @@ -39,20 +39,29 @@ // as its original time, without any time difference. import * as numberUtil from '../util/number'; -import * as timeUtil from '../util/time'; +import { + ONE_SECOND, + ONE_MINUTE, + ONE_HOUR, + ONE_DAY, + ONE_YEAR, + format, + leveledFormat, + PrimaryTimeUnit, + TimeUnit, + getUnitValue, + timeUnits, + fullLeveledFormatter, + getPrimaryTimeUnit, + isPrimaryTimeUnit, + getDefaultFormatPrecisionOfInterval +} from '../util/time'; import * as scaleHelper from './helper'; import IntervalScale from './Interval'; import Scale from './Scale'; import {TimeScaleTick} from '../util/types'; import {TimeAxisLabelFormatterOption} from '../coord/axisCommonTypes'; - -const ONE_SECOND = 1000; -const ONE_MINUTE = ONE_SECOND * 60; -const ONE_HOUR = ONE_MINUTE * 60; -const ONE_DAY = ONE_HOUR * 24; -const ONE_YEAR = ONE_DAY * 365; - // FIXME 公用? const bisect = function ( a: [string, number][], @@ -80,9 +89,20 @@ class TimeScale extends IntervalScale { _approxInterval: number; + _intervalUnit: TimeUnit; + + /** + * Get label is mainly for other components like dataZoom, tooltip. + */ getLabel(tick: TimeScaleTick): string { const useUTC = this.getSetting('useUTC'); - return timeUtil.format(tick.value, timeUtil.defaultLeveledFormatter.none, useUTC); + return format( + tick.value, + fullLeveledFormatter[ + getDefaultFormatPrecisionOfInterval(getPrimaryTimeUnit(this._intervalUnit)) + ] || fullLeveledFormatter.second, + useUTC + ); } getFormattedLabel( @@ -91,7 +111,7 @@ class TimeScale extends IntervalScale { labelFormatter: TimeAxisLabelFormatterOption ): string { const isUTC = this.getSetting('useUTC'); - return timeUtil.leveledFormat(tick, idx, labelFormatter, isUTC); + return leveledFormat(tick, idx, labelFormatter, isUTC); } /** @@ -120,7 +140,7 @@ class TimeScale extends IntervalScale { const intervals = scaleIntervals[Math.min(idx, unitLen - 1)]; const innerTicks = getIntervalTicks( - intervals[0] as timeUtil.PrimaryTimeUnit, + intervals[0] as PrimaryTimeUnit, this._approxInterval, useUTC, extent @@ -180,10 +200,10 @@ class TimeScale extends IntervalScale { const idx = bisect(scaleIntervals, this._approxInterval, 0, scaleIntervalsLen); const intervals = scaleIntervals[Math.min(idx, scaleIntervalsLen - 1)]; - let interval = intervals[1]; // Interval will be used in getTicks - this._interval = interval; + this._interval = intervals[1]; + this._intervalUnit = intervals[0]; } parse(val: number | string | Date): number { @@ -212,7 +232,7 @@ class TimeScale extends IntervalScale { * with some modifications made for this program. * See the license statement at the head of this file. */ -const scaleIntervals: [timeUtil.TimeUnit, number][] = [ +const scaleIntervals: [TimeUnit, number][] = [ // Format interval ['second', ONE_SECOND], // 1s ['minute', ONE_MINUTE], // 1m @@ -229,7 +249,7 @@ const scaleIntervals: [timeUtil.TimeUnit, number][] = [ ]; function isUnitValueSame( - unit: timeUtil.PrimaryTimeUnit, + unit: PrimaryTimeUnit, valueA: number, valueB: number, isUTC: boolean @@ -237,9 +257,9 @@ function isUnitValueSame( const dateA = numberUtil.parseDate(valueA) as any; const dateB = numberUtil.parseDate(valueB) as any; - const isSame = (unit: timeUtil.PrimaryTimeUnit) => { - return timeUtil.getUnitValue(dateA, unit, isUTC) - === timeUtil.getUnitValue(dateB, unit, isUTC); + const isSame = (unit: PrimaryTimeUnit) => { + return getUnitValue(dateA, unit, isUTC) + === getUnitValue(dateB, unit, isUTC); }; const isSameYear = () => isSame('year'); // const isSameHalfYear = () => isSameYear() && isSame('half-year'); @@ -272,7 +292,7 @@ function isUnitValueSame( function getIntervalTicks( - unitName: timeUtil.TimeUnit, + unitName: TimeUnit, approxInterval: number, isUTC: boolean, extent: number[] @@ -280,10 +300,10 @@ function getIntervalTicks( const safeLimit = 10000; const utc = isUTC ? 'UTC' : ''; const ticks: TimeScaleTick[] = []; - const unitNames = timeUtil.timeUnits; + const unitNames = timeUnits; let levelId = 0; for (let i = 0, hasTickInLevel = false; i < unitNames.length && ticks.length < safeLimit; ++i) { - let date = new Date(extent[0]) as any; + const date = new Date(extent[0]) as any; if (unitNames[i] === 'week' || unitNames[i] === 'half-week') { date['set' + utc + 'Hours'](0); @@ -322,7 +342,7 @@ function getIntervalTicks( } } else if (!isUnitValueSame( - timeUtil.getPrimaryTimeUnit(unitNames[i]), + getPrimaryTimeUnit(unitNames[i]), extent[0], extent[1], isUTC )) { // Level value changes within extent @@ -444,7 +464,7 @@ function getIntervalTicks( isFirst = false; } if (hasTickInLevel - && timeUtil.isPrimaryTimeUnit(unitNames[i]) + && isPrimaryTimeUnit(unitNames[i]) ) { ++levelId; } diff --git a/src/util/time.ts b/src/util/time.ts index e8bc46c..bb55987 100644 --- a/src/util/time.ts +++ b/src/util/time.ts @@ -5,6 +5,12 @@ import {pad} from './format'; import lang from '../lang'; import {TimeScaleTick} from './types'; +export const ONE_SECOND = 1000; +export const ONE_MINUTE = ONE_SECOND * 60; +export const ONE_HOUR = ONE_MINUTE * 60; +export const ONE_DAY = ONE_HOUR * 24; +export const ONE_YEAR = ONE_DAY * 365; + export const defaultLeveledFormatter = { year: '{yyyy}', month: '{MMM}', @@ -16,6 +22,18 @@ export const defaultLeveledFormatter = { none: '{yyyy}-{MM}-{dd} {hh}:{mm}:{ss} {SSS}' }; +const fullDayFormatter = '{yyyy}-{MM}-{dd}'; + +export const fullLeveledFormatter = { + year: '{yyyy}', + month: '{yyyy}:{MM}', + day: fullDayFormatter, + hour: fullDayFormatter + defaultLeveledFormatter.hour, + minute: fullDayFormatter + defaultLeveledFormatter.minute, + second: fullDayFormatter + defaultLeveledFormatter.second, + millisecond: defaultLeveledFormatter.none +}; + export type PrimaryTimeUnit = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year'; export type TimeUnit = PrimaryTimeUnit | 'half-year' | 'quarter' | 'week' @@ -49,6 +67,18 @@ export function isPrimaryTimeUnit(timeUnit: TimeUnit): boolean { return timeUnit === getPrimaryTimeUnit(timeUnit); } +export function getDefaultFormatPrecisionOfInterval(timeUnit: PrimaryTimeUnit): PrimaryTimeUnit { + switch (timeUnit) { + case 'year': + case 'month': + return 'day'; + case 'millisecond': + return 'millisecond'; + default: + // Also for day, hour, minute, second + return 'second'; + } +} export function format(time: Date | number, template: string, isUTC?: boolean): string { const date = numberUtil.parseDate(time); @@ -65,28 +95,28 @@ export function format(time: Date | number, template: string, isUTC?: boolean): const S = (date as any)['get' + utc + 'Milliseconds'](); return (template || '') - .replace('{yyyy}', y) - .replace('{yy}', y % 100 + '') - .replace('{Q}', q + '') - .replace('{MMMM}', lang.time.month[M - 1]) - .replace('{MMM}', lang.time.monthAbbr[M - 1]) - .replace('{MM}', pad(M, 2)) - .replace('{M}', M) - .replace('{dd}', pad(d, 2)) - .replace('{d}', d) - .replace('{eeee}', lang.time.dayOfWeek[e]) - .replace('{ee}', lang.time.dayOfWeekAbbr[e]) - .replace('{e}', e) - .replace('{HH}', pad(H, 2)) - .replace('{H}', H) - .replace('{hh}', pad(h + '', 2)) - .replace('{h}', h + '') - .replace('{mm}', pad(m, 2)) - .replace('{m}', m) - .replace('{ss}', pad(s, 2)) - .replace('{s}', s) - .replace('{SSS}', pad(S, 3)) - .replace('{S}', S); + .replace(/{yyyy}/g, y) + .replace(/{yy}/g, y % 100 + '') + .replace(/{Q}/g, q + '') + .replace(/{MMMM}/g, lang.time.month[M - 1]) + .replace(/{MMM}/g, lang.time.monthAbbr[M - 1]) + .replace(/{MM}/g, pad(M, 2)) + .replace(/{M}/g, M) + .replace(/{dd}/g, pad(d, 2)) + .replace(/{d}/g, d) + .replace(/{eeee}/g, lang.time.dayOfWeek[e]) + .replace(/{ee}/g, lang.time.dayOfWeekAbbr[e]) + .replace(/{e}/g, e) + .replace(/{HH}/g, pad(H, 2)) + .replace(/{H}/g, H) + .replace(/{hh}/g, pad(h + '', 2)) + .replace(/{h}/g, h + '') + .replace(/{mm}/g, pad(m, 2)) + .replace(/{m}/g, m) + .replace(/{ss}/g, pad(s, 2)) + .replace(/{s}/g, s) + .replace(/{SSS}/g, pad(S, 3)) + .replace(/{S}/g, S); } export function leveledFormat( --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@echarts.apache.org For additional commands, e-mail: commits-h...@echarts.apache.org