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

fix: added methods to unregister custom labels providers in axis comp… #139

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
41 changes: 37 additions & 4 deletions src/chart/components/x_axis/x-axis-labels.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string, XAxisLabelsProvider> = 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
Expand All @@ -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.
*/
Expand All @@ -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());
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/chart/components/x_axis/x-axis.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

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

/**
Expand Down
Loading