From 34228a5f47cfef9f7c2010426e58fdfd4fd3b9e3 Mon Sep 17 00:00:00 2001 From: Denis Travin Date: Mon, 22 Jan 2024 12:46:12 +0300 Subject: [PATCH] fix: added methods to unregister custom labels providers in axis components --- .../components/x_axis/x-axis-labels.model.ts | 41 +++++++++++++++++-- .../components/x_axis/x-axis.component.ts | 10 +++-- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/chart/components/x_axis/x-axis-labels.model.ts b/src/chart/components/x_axis/x-axis-labels.model.ts index 6ad378e8..b140d8e1 100644 --- a/src/chart/components/x_axis/x-axis-labels.model.ts +++ b/src/chart/components/x_axis/x-axis-labels.model.ts @@ -7,6 +7,7 @@ import { ChartBaseElement } from '../../model/chart-base-element'; import EventBus from '../../events/event-bus'; import { LabelAlign } from './x-axis-draw.functions'; import { EVENT_DRAW } from '../../events/events'; +import { uuid } from '../../utils/uuid.utils'; export interface XAxisLabel { text: string; @@ -27,10 +28,15 @@ export interface XAxisLabelsProvider { * Custom labels on X axis. */ export class XAxisLabelsModel extends ChartBaseElement { - public labels: XAxisLabel[] = []; + private _labels: XAxisLabel[] = []; + private labelProviders: Map = new Map(); - constructor(public eventBus: EventBus, readonly labelProviders: XAxisLabelsProvider[]) { + constructor(public eventBus: EventBus) { super(); + } + + protected doActivate(): void { + super.doActivate(); this.initModel(); /** * TODO refactor this, should NOT be recalculated on each DRAW, rather coordinates should be updated in drawer @@ -39,6 +45,33 @@ export class XAxisLabelsModel extends ChartBaseElement { this.addSubscription(this.eventBus.on(EVENT_DRAW, () => this.recalculateLabels())); } + get labels(): XAxisLabel[] { + return this._labels; + } + + /** + * Registers a new label provider. + * @param {XAxisLabelsProvider} provider + * @param {string} id + * @returns {string} + */ + public registerLabelProvider(provider: XAxisLabelsProvider, id: string = uuid()): string { + this.labelProviders.set(id, provider); + this.initModel(); + + return id; + } + + /** + * Unregisters a label provider. + * @param {string} id + * @returns {void} + */ + public unregisterLabelProvider(id: string): void { + this.labelProviders.delete(id); + this.initModel(); + } + /** * Initializes the model by recalculating the labels. */ @@ -51,8 +84,8 @@ export class XAxisLabelsModel extends ChartBaseElement { * @returns {void} */ public recalculateLabels(): void { - this.labels = []; - for (const provider of this.labelProviders) { + this._labels = []; + for (const [, provider] of this.labelProviders) { this.labels.push(...provider.getUnorderedLabels()); } } diff --git a/src/chart/components/x_axis/x-axis.component.ts b/src/chart/components/x_axis/x-axis.component.ts index 96fad6f1..bffae2e1 100644 --- a/src/chart/components/x_axis/x-axis.component.ts +++ b/src/chart/components/x_axis/x-axis.component.ts @@ -66,7 +66,7 @@ export class XAxisComponent extends ChartBaseElement { ); this.xAxisLabelsGenerator = xAxisLabelsGenerator; - this.xAxisLabelsModel = new XAxisLabelsModel(eventBus, []); + this.xAxisLabelsModel = new XAxisLabelsModel(eventBus); const xAxisCompositeDrawer = new CompositeDrawer(); drawingManager.addDrawer(xAxisCompositeDrawer, 'X_AXIS'); @@ -168,8 +168,12 @@ export class XAxisComponent extends ChartBaseElement { * You can add a custom labels provider for additional labels on XAxis (like for drawings) * @param provider */ - public registerXAxisLabelsProvider(provider: XAxisLabelsProvider) { - this.xAxisLabelsModel.labelProviders.push(provider); + public registerXAxisLabelsProvider(provider: XAxisLabelsProvider, id?: string) { + return this.xAxisLabelsModel.registerLabelProvider(provider, id); + } + + public unregisterXAxisLabelsProvider(id: string) { + this.xAxisLabelsModel.unregisterLabelProvider(id); } /**