From 6ad93ece4d01233f78589cad81134b76e00bd371 Mon Sep 17 00:00:00 2001 From: Adilet Soronov <74559101+adiletelf@users.noreply.github.com> Date: Tue, 28 Jan 2025 18:52:01 +0600 Subject: [PATCH] Add granularity label handling for timeline offset calculation --- src/granularity/granularityLabel.ts | 43 +++++++++++++++++++++++++++++ src/timeLine.ts | 28 ++++--------------- 2 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 src/granularity/granularityLabel.ts diff --git a/src/granularity/granularityLabel.ts b/src/granularity/granularityLabel.ts new file mode 100644 index 0000000..331c9f5 --- /dev/null +++ b/src/granularity/granularityLabel.ts @@ -0,0 +1,43 @@ +/* + * Power BI Visualizations + * + * Copyright (c) Microsoft Corporation + * All rights reserved. + * MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the ""Software""), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +import { GranularityType } from "./granularityType"; + +export enum GranularityLabel { + displayYears = 'displayYears', + displayQuarters = 'displayQuarters', + displayMonths = 'displayMonths', + displayWeeks = 'displayWeeks', + displayDays = 'displayDays', +} + +export const granularityLevels: Record = { + [GranularityType.year]: [], + [GranularityType.quarter]: [GranularityLabel.displayYears], + [GranularityType.month]: [GranularityLabel.displayYears, GranularityLabel.displayQuarters], + [GranularityType.week]: [GranularityLabel.displayYears, GranularityLabel.displayQuarters, GranularityLabel.displayMonths], + [GranularityType.day]: [GranularityLabel.displayYears, GranularityLabel.displayQuarters, GranularityLabel.displayMonths, GranularityLabel.displayWeeks], +}; diff --git a/src/timeLine.ts b/src/timeLine.ts index d4e5fba..4f1b48e 100644 --- a/src/timeLine.ts +++ b/src/timeLine.ts @@ -60,6 +60,7 @@ import { import {GranularityData} from "./granularity/granularityData"; import {GranularityNames} from "./granularity/granularityNames"; import {GranularityType} from "./granularity/granularityType"; +import {GranularityLabel, granularityLevels} from "./granularity/granularityLabel"; import {ITimelineDatePeriod, ITimelineDatePeriodBase,} from "./datePeriod/datePeriod"; @@ -445,29 +446,10 @@ export class Timeline implements powerbiVisualsApi.extensibility.visual.IVisual if (this.visualSettings.labels.displayAll.value) { granularityOffset += granularityType; } else { - // compute the offset depending on the enabled labels - switch (granularityType) { - case GranularityType.quarter: - granularityOffset += this.visualSettings.labels.displayYears.value ? 1 : 0; - break; - case GranularityType.month: - granularityOffset += this.visualSettings.labels.displayYears.value ? 1 : 0; - granularityOffset += this.visualSettings.labels.displayQuarters.value ? 1 : 0; - break; - case GranularityType.week: - granularityOffset += this.visualSettings.labels.displayYears.value ? 1 : 0; - granularityOffset += this.visualSettings.labels.displayQuarters.value ? 1 : 0; - granularityOffset += this.visualSettings.labels.displayMonths.value ? 1 : 0; - break; - case GranularityType.day: - granularityOffset += this.visualSettings.labels.displayYears.value ? 1 : 0; - granularityOffset += this.visualSettings.labels.displayQuarters.value ? 1 : 0; - granularityOffset += this.visualSettings.labels.displayMonths.value ? 1 : 0; - granularityOffset += this.visualSettings.labels.displayWeeks.value ? 1 : 0; - break; - default: - break; - } + const labelsToCheck: GranularityLabel[] = granularityLevels[granularityType] || []; + granularityOffset += labelsToCheck.reduce((offset, label) => { + return offset + (this.visualSettings.labels[label].value ? 1 : 0) + }, 0); } this.timelineProperties.cellsYPosition += labelSize