Skip to content

Commit 8b2e0d9

Browse files
moayuisuda林舸
andauthored
feat: support legend render (#363)
Co-authored-by: 林舸 <anhaohui.ahh@antgroup.com>
1 parent 1c03737 commit 8b2e0d9

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

src/ui/legend/category.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component } from '../../core';
2-
import type { Group } from '../../shapes';
2+
import { HTML, type Group } from '../../shapes';
33
import { BBox, select, Selection, splitStyle, subStyleProps } from '../../util';
44
import type { TitleStyleProps } from './title';
55
import { getBBox, Title } from './title';
@@ -9,6 +9,12 @@ import type { CategoryOptions, CategoryStyleProps } from './types';
99

1010
export type { CategoryOptions, CategoryStyleProps };
1111

12+
class HtmlLegend extends HTML {
13+
update(options: any) {
14+
this.attr(options);
15+
}
16+
}
17+
1218
export class Category extends Component<CategoryStyleProps> {
1319
constructor(options: CategoryOptions) {
1420
super(options, CATEGORY_DEFAULT_OPTIONS);
@@ -35,6 +41,26 @@ export class Category extends Component<CategoryStyleProps> {
3541
.update(finalTitleStyle) as Selection<Title>;
3642
}
3743

44+
private renderCustom(container: Selection) {
45+
const { data } = this.attributes;
46+
47+
const style = {
48+
innerHTML: this.attributes.render(data),
49+
pointerEvents: 'auto' as const,
50+
};
51+
52+
container
53+
.maybeAppendByClassName(
54+
CLASS_NAMES.html,
55+
() =>
56+
new HtmlLegend({
57+
className: CLASS_NAMES.html.name,
58+
style,
59+
})
60+
)
61+
.update(style);
62+
}
63+
3864
private renderItems(container: Selection, bbox: DOMRect) {
3965
const { x, y, width, height } = bbox;
4066
const style = subStyleProps(this.attributes, 'title', true);
@@ -84,7 +110,7 @@ export class Category extends Component<CategoryStyleProps> {
84110
}
85111

86112
render(attributes: Required<CategoryStyleProps>, container: Group) {
87-
const { width, height, x = 0, y = 0, classNamePrefix } = this.attributes;
113+
const { width, height, x = 0, y = 0, classNamePrefix, render } = this.attributes;
88114
const ctn = select(container);
89115

90116
// Set root container className
@@ -97,10 +123,14 @@ export class Category extends Component<CategoryStyleProps> {
97123

98124
container.style.transform = `translate(${x}, ${y})`;
99125

100-
this.renderTitle(ctn, width!, height!);
126+
if (render as CategoryStyleProps['render']) {
127+
this.renderCustom(ctn);
128+
} else {
129+
this.renderTitle(ctn, width!, height!);
101130

102-
this.renderItems(ctn, this.availableSpace);
131+
this.renderItems(ctn, this.availableSpace);
103132

104-
this.adjustLayout();
133+
this.adjustLayout();
134+
}
105135
}
106136
}

src/ui/legend/category/items.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export type CategoryItemsStyleProps = GroupStyleProps &
6565
focus?: boolean;
6666
focusMarkerSize?: number;
6767
classNamePrefix?: string;
68+
render?: (data: CategoryItemsDatum[]) => string | HTMLElement;
6869
};
6970

7071
export type CategoryItemsOptions = ComponentOptions<CategoryItemsStyleProps>;

src/ui/legend/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const NAME_VALUE_RATIO = 0.5;
6262
export const CLASS_NAMES = classNames(
6363
{
6464
title: 'title',
65+
html: 'html',
6566
titleGroup: 'title-group',
6667
items: 'items',
6768
itemsGroup: 'items-group',

0 commit comments

Comments
 (0)