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

Allow skipping on certain period-changes #1925

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 46 additions & 6 deletions src/.internal/charts/axes/DateAxis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,26 @@ export class DateAxis<T extends AxisRenderer = AxisRenderer> extends ValueAxis<T
*/
public periodChangeDateFormats: Dictionary<TimeUnit, string | Intl.DateTimeFormatOptions> = new Dictionary<TimeUnit, string | Intl.DateTimeFormatOptions>();

/**
* When detecting a period change, these units will be skipped in favor of a larger unit.
*
* This allows us to format the next higher unit while zooming, like zooming into:
*
* `22:00 - 23:00 - [b]Jan 1[b] - 01:00 - ...`
*
* Instead of:
*
* `23:00 - 23:30 - [b]00:00[b] - 00:30`
*
* We can have:
*
* `23:00 - 23:30 - [b]Jan 1[b] - 00:30`
*
* This gets overriden by setting `markUnitChange = false`.
*/

public skipPeriodChanges: List<TimeUnit> = new List<TimeUnit>();

/**
* At which intervals grid elements are displayed.
*/
Expand Down Expand Up @@ -1260,6 +1280,16 @@ export class DateAxis<T extends AxisRenderer = AxisRenderer> extends ValueAxis<T
let timeUnit = this._gridInterval.timeUnit;
let intervalCount = this._gridInterval.count;
let prevGridDate = $time.copy(this._gridDate);
let timeUnitAfterSkipping = timeUnit;
if (this.markUnitChange) {
while (this.skipPeriodChanges.contains(timeUnitAfterSkipping)) {
timeUnitAfterSkipping = $time.getNextUnit(timeUnitAfterSkipping);
}
if (timeUnitAfterSkipping === "year") {
timeUnitAfterSkipping = null;
}
}
let nextGridUnit = $time.getNextUnit(timeUnitAfterSkipping);

let dataItemsIterator = this._dataItemsIterator;
this.resetIterators();
Expand All @@ -1274,9 +1304,9 @@ export class DateAxis<T extends AxisRenderer = AxisRenderer> extends ValueAxis<T
let format = this.dateFormats.getKey(timeUnit);

if (this.markUnitChange && prevGridDate) {
if ($time.checkChange(date, prevGridDate, this._nextGridUnit, this._df.utc)) {
if (timeUnit !== "year") {
format = this.periodChangeDateFormats.getKey(timeUnit);
if ($time.checkChange(date, prevGridDate, nextGridUnit, this._df.utc)) {
if (timeUnitAfterSkipping) {
format = this.periodChangeDateFormats.getKey(timeUnitAfterSkipping);
}
}
}
Expand Down Expand Up @@ -1308,6 +1338,16 @@ export class DateAxis<T extends AxisRenderer = AxisRenderer> extends ValueAxis<T
if (axisBreak.breakSize > 0) {
let timeUnit: TimeUnit = axisBreak.gridInterval.timeUnit;
let intervalCount: number = axisBreak.gridInterval.count;
let timeUnitAfterSkipping: TimeUnit = timeUnit;
if (this.markUnitChange) {
while (this.skipPeriodChanges.contains(timeUnitAfterSkipping)) {
timeUnitAfterSkipping = $time.getNextUnit(timeUnitAfterSkipping);
}
if (timeUnitAfterSkipping === "year") {
timeUnitAfterSkipping = null;
}
}
let nextGridUnit: TimeUnit = $time.getNextUnit(timeUnitAfterSkipping);

// only add grid if gap is bigger then minGridDistance
if ($math.getDistance(axisBreak.startPoint, axisBreak.endPoint) > renderer.minGridDistance * 4) {
Expand All @@ -1326,9 +1366,9 @@ export class DateAxis<T extends AxisRenderer = AxisRenderer> extends ValueAxis<T
let format = this.dateFormats.getKey(timeUnit);

if (this.markUnitChange && prevGridDate) {
if ($time.checkChange(date, prevGridDate, this._nextGridUnit, this._df.utc)) {
if (timeUnit !== "year") {
format = this.periodChangeDateFormats.getKey(timeUnit);
if ($time.checkChange(date, prevGridDate, nextGridUnit, this._df.utc)) {
if (timeUnitAfterSkipping) {
format = this.periodChangeDateFormats.getKey(timeUnitAfterSkipping);
}
}
}
Expand Down