Skip to content

Commit

Permalink
Add granularity label handling for timeline offset calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
adiletelf committed Jan 28, 2025
1 parent f2ec81f commit 6ad93ec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
43 changes: 43 additions & 0 deletions src/granularity/granularityLabel.ts
Original file line number Diff line number Diff line change
@@ -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, GranularityLabel[]> = {
[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],
};
28 changes: 5 additions & 23 deletions src/timeLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6ad93ec

Please sign in to comment.