From 44bff5d30598e340326dc3129311fb87a6dbc19c Mon Sep 17 00:00:00 2001 From: Benedikt Mehl Date: Tue, 17 Dec 2024 15:04:44 +0100 Subject: [PATCH] Use map to make cross-hair not lag #3827 --- .../metricColorRangeDiagram.component.ts | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/visualization/app/codeCharta/ui/ribbonBar/colorSettingsPanel/metricColorRangeDiagram/metricColorRangeDiagram.component.ts b/visualization/app/codeCharta/ui/ribbonBar/colorSettingsPanel/metricColorRangeDiagram/metricColorRangeDiagram.component.ts index 67d089e776..23659f86ec 100644 --- a/visualization/app/codeCharta/ui/ribbonBar/colorSettingsPanel/metricColorRangeDiagram/metricColorRangeDiagram.component.ts +++ b/visualization/app/codeCharta/ui/ribbonBar/colorSettingsPanel/metricColorRangeDiagram/metricColorRangeDiagram.component.ts @@ -38,16 +38,32 @@ export class MetricColorRangeDiagramComponent implements OnChanges { private yLabelXOffset: number private xLabelYOffset: number private percentileRanks: { x: number; y: number }[] + private percentileToMetricValueMap: Map ngOnChanges() { if (this.values.length > 0) { this.percentileRanks = this.isAttributeDirectionInverted ? this.calculateReversedPercentileRanks(this.values) : this.calculatePercentileRanks(this.values) + this.updatePercentileToMetricValueMap() this.renderDiagram() } } + private updatePercentileToMetricValueMap() { + this.percentileToMetricValueMap = new Map() + let currentPercentileRankIndex = 0 + for (let percentile = 0; percentile <= 100; percentile++) { + while ( + percentile > this.percentileRanks[currentPercentileRankIndex]["x"] && + currentPercentileRankIndex < this.percentileRanks.length - 1 + ) { + currentPercentileRankIndex++ + } + this.percentileToMetricValueMap.set(percentile, this.percentileRanks[currentPercentileRankIndex]["y"]) + } + } + private renderDiagram() { this.initializeDiagramDimesions() this.clearDiagramContainer() @@ -343,8 +359,8 @@ export class MetricColorRangeDiagramComponent implements OnChanges { private addOnMouseMoveEvent( rectangle: RectElement, - x: Scale, - y: Scale, + xScale: Scale, + yScale: Scale, tooltip: TextElement, dashedVerticalLine: LineElement, straightVerticalLine: LineElement, @@ -352,11 +368,11 @@ export class MetricColorRangeDiagramComponent implements OnChanges { ) { rectangle.on("mousemove", event => { const mouseX = d3.pointer(event)[0] - let percentile = x.invert(mouseX - this.framePadding) + let percentile = Math.round(xScale.invert(mouseX - this.framePadding)) percentile = Math.max(0, Math.min(percentile, 100)) - const metricValue = this.calculateMetricValueFromPercentile(percentile) + const metricValue = this.percentileToMetricValueMap.get(percentile) - const yLinePosition = this.getYPositionForMetricValue(metricValue, y) + const yLinePosition = yScale(metricValue) + this.framePadding const xTooltipPosition = mouseX < this.frameWidth / 2 ? mouseX + 10 : mouseX - 80 const yTooltipPosition = yLinePosition < this.frameHeight / 2 ? yLinePosition + 20 : yLinePosition - 20 @@ -365,7 +381,7 @@ export class MetricColorRangeDiagramComponent implements OnChanges { .style("display", "block") .attr("x", xTooltipPosition) .attr("y", yTooltipPosition) - .text(`Quantile: ${Math.round(percentile)}`) + .text(`Quantile: ${percentile}`) .append("tspan") .attr("x", xTooltipPosition) .attr("dy", "1.2em") @@ -398,16 +414,4 @@ export class MetricColorRangeDiagramComponent implements OnChanges { horizontalLine.style("display", "none") }) } - - private getYPositionForMetricValue(yValue: number, yScale: Scale): number { - return yScale(yValue) + this.framePadding - } - - private calculateMetricValueFromPercentile(percentile: number) { - for (const percentileRank of this.percentileRanks) { - if (percentileRank["x"] >= percentile) { - return percentileRank["y"] - } - } - } }