From 6b8f54a77b14d50bb3fba436e3d8f4c13a427e2c Mon Sep 17 00:00:00 2001 From: Tom Kristian Tjemsland Date: Thu, 3 Oct 2024 15:54:47 +0200 Subject: [PATCH] Added logic to limit amount of entries being shown in the legend of distribution tracks (#283) --- package-lock.json | 4 ++-- package.json | 2 +- src/tracks/distribution/distribution-legend.ts | 17 +++++++++++------ src/tracks/distribution/interfaces.ts | 3 +++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 99da7e7..451780e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@equinor/videx-wellog", - "version": "0.10.5", + "version": "0.10.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@equinor/videx-wellog", - "version": "0.10.5", + "version": "0.10.6", "license": "MIT", "dependencies": { "@equinor/videx-math": "^1.1.0", diff --git a/package.json b/package.json index fe8eb10..fe6bbd1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": false, "name": "@equinor/videx-wellog", - "version": "0.10.5", + "version": "0.10.6", "license": "MIT", "description": "Visualisation components for wellbore log data", "repository": "https://github.com/equinor/videx-wellog", diff --git a/src/tracks/distribution/distribution-legend.ts b/src/tracks/distribution/distribution-legend.ts index de625db..2b43f5f 100644 --- a/src/tracks/distribution/distribution-legend.ts +++ b/src/tracks/distribution/distribution-legend.ts @@ -15,14 +15,16 @@ const applyRectDimensions = (node: D3Selection, { x, y, width, height }, isHoriz function renderDistributionPlotLegend(g: D3Selection, bounds: LegendBounds, options: DistributionTrackOptions) : void { const { width, height, left = 0, top = 0 } = bounds; - const { horizontal = false } = options; + const { horizontal = false, legendEntries } = options; // Get components in distribution, return if missing const components = Object.entries(options.components ?? {}); if (components.length === 0) return; - // Get available height per component - const componentStride = height / components.length; + const entries = Math.min(components.length, legendEntries ?? components.length); + + // Get available height per entry + const componentStride = height / entries; // Margin is used around colored rect, padding around text const margin = componentStride * 0.1; @@ -34,7 +36,9 @@ function renderDistributionPlotLegend(g: D3Selection, bounds: LegendBounds, opti const textX = left + width / 2; - components.forEach(([label, component], index) => { + for (let index = 0; index < entries; index++) { + const [label, component] = components[index]; + const color = component.color; const textColor = component.textColor ?? color; const y = top + index * componentStride + margin / 2; @@ -91,7 +95,7 @@ function renderDistributionPlotLegend(g: D3Selection, bounds: LegendBounds, opti }, horizontal, ).attr('fill', 'white'); - }); + } } function onUpdateLegend(elm: HTMLElement, bounds: LegendBounds, track: DistributionTrack): void { @@ -105,7 +109,8 @@ function onUpdateLegend(elm: HTMLElement, bounds: LegendBounds, track: Distribut } function getGraphTrackLegendRows(track: DistributionTrack): number { - return Object.keys(track.options?.components ?? {}).length || 3; + const componentCount = Object.keys(track.options?.components ?? {}).length || 3; + return track.options?.legendEntries ?? componentCount; } export default LegendHelper.basicLegendSvgConfig(getGraphTrackLegendRows, onUpdateLegend); diff --git a/src/tracks/distribution/interfaces.ts b/src/tracks/distribution/interfaces.ts index 90de2f2..0318431 100644 --- a/src/tracks/distribution/interfaces.ts +++ b/src/tracks/distribution/interfaces.ts @@ -20,6 +20,9 @@ export interface DistributionTrackOptions extends TrackOptions { /** Specifies whether the graph should interpolate between the points. */ interpolate?: boolean, + /** Number of legend entries to display in the track. */ + legendEntries?: number, + /** List of distribution components. */ components?: DistributionComponents, }