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

Added logic to limit amount of entries being shown in the legend of distribution tracks #283

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 11 additions & 6 deletions src/tracks/distribution/distribution-legend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -91,7 +95,7 @@ function renderDistributionPlotLegend(g: D3Selection, bounds: LegendBounds, opti
},
horizontal,
).attr('fill', 'white');
});
}
}

function onUpdateLegend(elm: HTMLElement, bounds: LegendBounds, track: DistributionTrack): void {
Expand All @@ -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);
3 changes: 3 additions & 0 deletions src/tracks/distribution/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
Loading