Skip to content

Commit

Permalink
task/4837 onclick function in legend text
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesleite committed Dec 15, 2024
1 parent fd5293a commit e3e5ea0
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/utils/legend-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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 +71,7 @@ export default class LegendHelper {
if (bbox.width > height * 0.8) {
lbl.text(abbr || label);
}
return lbl;
}

/**
Expand All @@ -91,4 +92,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 }) : 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 e3e5ea0

Please sign in to comment.