Skip to content

Commit 79d100e

Browse files
author
林舸
committed
feat: support legend render
1 parent bace973 commit 79d100e

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) as Selection<Title>;
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,14 +110,18 @@ export class Category extends Component<CategoryStyleProps> {
84110
}
85111

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

91-
this.renderTitle(ctn, width!, height!);
117+
if (render as CategoryStyleProps['render']) {
118+
this.renderCustom(ctn);
119+
} else {
120+
this.renderTitle(ctn, width!, height!);
92121

93-
this.renderItems(ctn, this.availableSpace);
122+
this.renderItems(ctn, this.availableSpace);
94123

95-
this.adjustLayout();
124+
this.adjustLayout();
125+
}
96126
}
97127
}

src/ui/legend/category/items.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export type CategoryItemsStyleProps = GroupStyleProps &
6060
focus?: boolean;
6161
focusMarkerSize?: number;
6262
classNamePrefix?: string;
63+
render?: (data: CategoryItemsDatum[]) => string | HTMLElement;
6364
};
6465

6566
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)