-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata-labels.ts
More file actions
32 lines (28 loc) · 996 Bytes
/
data-labels.ts
File metadata and controls
32 lines (28 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import type {Selection} from 'd3-selection';
import type {BaseTextStyle} from '../types/chart/base';
type RenderableLabelData = {
text: string;
x: number;
y: number;
textAnchor: 'start' | 'end' | 'middle';
style: BaseTextStyle;
};
export function renderDataLabels<T extends RenderableLabelData>(args: {
container: Selection<SVGGElement, unknown, null, undefined>;
data: T[];
className: string;
}): Selection<SVGTextElement, T, SVGGElement, unknown> {
const {container, data, className} = args;
return container
.selectAll<SVGTextElement, T>('text')
.data(data)
.join('text')
.html((d) => d.text)
.attr('class', className)
.attr('x', (d) => d.x)
.attr('y', (d) => d.y)
.attr('text-anchor', (d) => d.textAnchor)
.style('font-size', (d) => d.style.fontSize)
.style('font-weight', (d) => d.style.fontWeight || null)
.style('fill', (d) => d.style.fontColor || null);
}