From ba72d344be0c5dabc17a6201ffd9fdd65b19b461 Mon Sep 17 00:00:00 2001 From: Kirill Bobkov Date: Mon, 4 Dec 2023 14:02:28 +0400 Subject: [PATCH] feat: get bounds optimization --- src/chart/bootstrap.ts | 5 +++-- src/chart/components/chart/chart.component.ts | 3 ++- src/chart/components/pane/pane.component.ts | 11 +++++++---- src/chart/model/scaling/viewport.model.ts | 10 ++++++---- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/chart/bootstrap.ts b/src/chart/bootstrap.ts index 9d0e90b7..bce2fa93 100755 --- a/src/chart/bootstrap.ts +++ b/src/chart/bootstrap.ts @@ -257,10 +257,11 @@ export default class ChartBootstrap { const canvasAnimation = new CanvasAnimation(eventBus); this.canvasAnimation = canvasAnimation; + const chartPaneId = CanvasElement.PANE_UUID(CHART_UUID); //#region ScaleModel init const scaleModel = new ScaleModel( config, - () => canvasBoundsContainer.getBounds(CanvasElement.PANE_UUID(CHART_UUID)), + () => canvasBoundsContainer.getBounds(chartPaneId), canvasAnimation, ); this.scaleModel = scaleModel; @@ -482,7 +483,7 @@ export default class ChartBootstrap { 'GRID', drawingManager, () => this.canvasBoundsContainer.getBounds(CanvasElement.ALL_PANES), - () => this.canvasBoundsContainer.getBounds(CanvasElement.PANE_UUID(CHART_UUID)), + () => this.canvasBoundsContainer.getBounds(chartPaneId), () => this.xAxisComponent.xAxisLabelsGenerator.labels, () => this.yAxisComponent.model.baseLabelsModel.labels, () => mainPane.mainExtent.toY(mainPane.mainExtent.getBaseline()), diff --git a/src/chart/components/chart/chart.component.ts b/src/chart/components/chart/chart.component.ts index d126da56..ca41a0f7 100644 --- a/src/chart/components/chart/chart.component.ts +++ b/src/chart/components/chart/chart.component.ts @@ -228,7 +228,8 @@ export class ChartComponent extends ChartBaseElement { new MainChartHistogramDrawer(this.config.components.chart.histogram), ); - const mainChartBoundsProvider = () => this.canvasBoundsContainer.getBounds(CanvasElement.PANE_UUID(CHART_UUID)); + const chartPaneId = CanvasElement.PANE_UUID(CHART_UUID); + const mainChartBoundsProvider = () => this.canvasBoundsContainer.getBounds(chartPaneId); this.registerDataSeriesTypeDrawer('LINEAR', new LinearDrawer()); this.registerDataSeriesTypeDrawer('HISTOGRAM', new HistogramDrawer()); this.registerDataSeriesTypeDrawer('TREND_HISTOGRAM', new TrendHistogramDrawer()); diff --git a/src/chart/components/pane/pane.component.ts b/src/chart/components/pane/pane.component.ts index 6c88b86d..3b7cd31b 100644 --- a/src/chart/components/pane/pane.component.ts +++ b/src/chart/components/pane/pane.component.ts @@ -130,6 +130,7 @@ export class PaneComponent extends ChartBaseElement { yAxisLabelsGenerator: NumericYAxisLabelsGenerator, yAxisState: YAxisConfig, ) { + const chartPaneId = CanvasElement.PANE_UUID(uuid); const gridComponent = new GridComponent( this.mainCanvasModel, scale, @@ -137,8 +138,8 @@ export class PaneComponent extends ChartBaseElement { yAxisState, `PANE_${uuid}_grid_drawer`, this.drawingManager, - () => this.canvasBoundsContainer.getBounds(CanvasElement.PANE_UUID(uuid)), - () => this.canvasBoundsContainer.getBounds(CanvasElement.PANE_UUID(uuid)), + () => this.canvasBoundsContainer.getBounds(chartPaneId), + () => this.canvasBoundsContainer.getBounds(chartPaneId), () => [], () => yAxisLabelsGenerator.generateNumericLabels(), ); @@ -153,9 +154,10 @@ export class PaneComponent extends ChartBaseElement { * @returns {Unsubscriber} */ private createYPanHandler(uuid: string, scale: ScaleModel): [Unsubscriber, DragNDropYComponent] { + const chartPaneId = CanvasElement.PANE_UUID(uuid); const dragNDropComponent = this.chartPanComponent.chartAreaPanHandler.registerChartYPanHandler( scale, - this.canvasBoundsContainer.getBoundsHitTest(CanvasElement.PANE_UUID(uuid)), + this.canvasBoundsContainer.getBoundsHitTest(chartPaneId), ); return [ () => { @@ -174,7 +176,8 @@ export class PaneComponent extends ChartBaseElement { public createExtentComponent(options?: AtLeastOne) { const extentIdx = this.yExtentComponents.length; - const getBounds = () => this.canvasBoundsContainer.getBounds(CanvasElement.PANE_UUID(this.uuid)); + const chartPaneId = CanvasElement.PANE_UUID(this.uuid); + const getBounds = () => this.canvasBoundsContainer.getBounds(chartPaneId); const scaleModel = options?.scale ?? new SyncedByXScaleModel(this.mainScale, this.config, getBounds, this.canvasAnimation); diff --git a/src/chart/model/scaling/viewport.model.ts b/src/chart/model/scaling/viewport.model.ts index faaef78a..1bd88dd4 100644 --- a/src/chart/model/scaling/viewport.model.ts +++ b/src/chart/model/scaling/viewport.model.ts @@ -177,11 +177,12 @@ export abstract class ViewportModel extends ChartBaseElement implements Viewable * @returns {Pixel} - The pixel value of the given unit value in the Y-axis. */ toY(unit: Unit): Pixel { + const bounds = this.getBounds(); if (this.inverseY) { - return this.getBounds().y + unitToPixels(unit - this.yStart, this.zoomY); + return bounds.y + unitToPixels(unit - this.yStart, this.zoomY); } else { // inverse by default because canvas calculation [0,0] point starts from top-left corner - return this.getBounds().y + this.getBounds().height - unitToPixels(unit - this.yStart, this.zoomY); + return bounds.y + bounds.height - unitToPixels(unit - this.yStart, this.zoomY); } } @@ -210,13 +211,14 @@ export abstract class ViewportModel extends ChartBaseElement implements Viewable * @returns {void} */ fromY(px: Pixel): Unit { - const normalizedPx = px - this.getBounds().y; + const bounds = this.getBounds(); + const normalizedPx = px - bounds.y; if (this.inverseY) { return pixelsToUnits(normalizedPx + unitToPixels(this.yStart, this.zoomY), this.zoomY); } else { // inverse by default because canvas calculation [0,0] point starts from top-left corner return pixelsToUnits( - this.getBounds().height - normalizedPx + unitToPixels(this.yStart, this.zoomY), + bounds.height - normalizedPx + unitToPixels(this.yStart, this.zoomY), this.zoomY, ); }