Skip to content

Commit

Permalink
Allow default 8 character max to be overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
edew committed Aug 8, 2023
1 parent babfa82 commit 9f55db3
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 24 deletions.
36 changes: 12 additions & 24 deletions src/model/time-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
} from './time-data';
import { TimeScaleVisibleRange } from './time-scale-visible-range';

const defaultTickMarkMaxCharacterLength = 8;

const enum Constants {
DefaultAnimationDuration = 400,
// make sure that this (1 / MinVisibleBarsCount) >= coeff in max bar spacing
Expand Down Expand Up @@ -204,6 +206,13 @@ export interface TimeScaleOptions {
* @defaultValue `false`
*/
ticksVisible: boolean;

/**
* Maximum tick mark label length. Used to override the default 8 character maximum length.
*
* @defaultValue `undefined`
*/
tickMarkMaxCharacterLength?: number;
}

export class TimeScale {
Expand Down Expand Up @@ -234,7 +243,6 @@ export class TimeScale {
private _timeMarksCache: TimeMark[] | null = null;

private _labels: TimeMark[] = [];
private _labelLengthEstimate: number = 0;

public constructor(model: ChartModel, options: TimeScaleOptions, localizationOptions: LocalizationOptions) {
this._options = options;
Expand All @@ -244,7 +252,6 @@ export class TimeScale {
this._model = model;

this._updateDateTimeFormatter();
this._updateLabelLengthEstimate();
}

public options(): Readonly<TimeScaleOptions> {
Expand All @@ -256,7 +263,6 @@ export class TimeScale {

this._invalidateTickMarks();
this._updateDateTimeFormatter();
this._updateLabelLengthEstimate();
}

public applyOptions(options: DeepPartial<TimeScaleOptions>, localizationOptions?: DeepPartial<LocalizationOptions>): void {
Expand Down Expand Up @@ -288,8 +294,6 @@ export class TimeScale {

this._invalidateTickMarks();
this._updateDateTimeFormatter();
this._updateLabelLengthEstimate();

this._optionsApplied.fire();
}

Expand Down Expand Up @@ -489,7 +493,9 @@ export class TimeScale {
const spacing = this._barSpacing;
const fontSize = this._model.options().layout.fontSize;

const maxLabelWidth = Math.max((fontSize + 4) * 5, this._labelLengthEstimate * fontSize);
const pixelsPer8Characters = (fontSize + 4) * 5;
const pixelsPerCharacter = pixelsPer8Characters / defaultTickMarkMaxCharacterLength;
const maxLabelWidth = pixelsPerCharacter * (this._options.tickMarkMaxCharacterLength || defaultTickMarkMaxCharacterLength);
const indexPerLabel = Math.round(maxLabelWidth / spacing);

const visibleBars = ensureNotNull(this.visibleStrictRange());
Expand Down Expand Up @@ -997,24 +1003,6 @@ export class TimeScale {

this._correctBarSpacing();
}

private _updateLabelLengthEstimate(): void {
const formatter = this._options.tickMarkFormatter;

if (formatter === undefined) {
this._labelLengthEstimate = 0;
return;
}

const formattedLabel = formatter(0 as UTCTimestamp, TickMarkType.TimeWithSeconds, this._localizationOptions.locale);

if (typeof formattedLabel !== 'string') {
this._labelLengthEstimate = 0;
return;
}

this._labelLengthEstimate = formattedLabel.length;
}
}

// eslint-disable-next-line complexity
Expand Down
Loading

0 comments on commit 9f55db3

Please sign in to comment.