Skip to content

Commit

Permalink
task/4837 onclick function in legend text (#293)
Browse files Browse the repository at this point in the history
* task/4837 onclick function in legend text

* types

* bump to v0.10.8
  • Loading branch information
johannesleite authored Jan 2, 2025
1 parent 0cc2312 commit a860081
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
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.7",
"version": "0.10.8",
"license": "MIT",
"description": "Visualisation components for wellbore log data",
"repository": "https://github.com/equinor/videx-wellog",
Expand Down
41 changes: 40 additions & 1 deletion src/utils/legend-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export interface LegendBounds {

export type LegendOnUpdateFunction = (elm: Element, bounds: LegendBounds, track: Track) => void;

interface BasicVerticalLinkLabelConfig {
label: string;
abbr: string;
onClick: () => void;
title?: string;
}

export interface LegendConfig {
elementType: string,
getLegendRows(track: Track) : number,
Expand Down Expand Up @@ -48,7 +55,7 @@ export default class LegendHelper {
/**
* Renders a simple rotated text label that is scaled to fit bounds
*/
static renderBasicVerticalSvgLabel(g: D3Selection, bounds: LegendBounds, label: string, abbr: string, horizontal: boolean = false) : void {
static renderBasicVerticalSvgLabel(g: D3Selection, bounds: LegendBounds, label: string, abbr: string, horizontal: boolean = false) : D3Selection {
const { width, height, left = 0, top = 0 } = bounds;

const y = top + height * 0.9;
Expand All @@ -71,6 +78,7 @@ export default class LegendHelper {
if (bbox.width > height * 0.8) {
lbl.text(abbr || label);
}
return lbl;
}

/**
Expand All @@ -91,4 +99,35 @@ export default class LegendHelper {
};
return LegendHelper.basicLegendSvgConfig(() => 3, onLegendUpdate);
}

/**
* Convenience function for creating a legend config object for
* a clickable rotated label legend.
*/
static basicVerticalLinkLabel({ label, abbr, onClick, title = null }:BasicVerticalLinkLabelConfig) : LegendConfig {
const onLegendUpdate: LegendOnUpdateFunction = (elm, bounds, track) => {
const g = select(elm);
g.selectAll('*').remove();
const labelGroup = g.append('g')
.style('fill', '#0000EE')
.style('text-decoration', 'underline')
.style('cursor', 'pointer');

labelGroup.append('title').text(title);

const labelElement = LegendHelper.renderBasicVerticalSvgLabel(
labelGroup,
bounds,
label || track.options.label,
abbr || track.options.abbr,
track.options.horizontal,
);

if (onClick && typeof onClick === 'function') {
labelElement
.on('click', onClick);
}
};
return LegendHelper.basicLegendSvgConfig(() => 3, onLegendUpdate);
}
}

0 comments on commit a860081

Please sign in to comment.