11import { Component } from '../../core' ;
2- import type { Group } from '../../shapes' ;
2+ import { HTML , type Group } from '../../shapes' ;
33import { BBox , select , Selection , splitStyle , subStyleProps } from '../../util' ;
44import type { TitleStyleProps } from './title' ;
55import { getBBox , Title } from './title' ;
@@ -9,6 +9,12 @@ import type { CategoryOptions, CategoryStyleProps } from './types';
99
1010export type { CategoryOptions , CategoryStyleProps } ;
1111
12+ class HtmlLegend extends HTML {
13+ update ( options : any ) {
14+ this . attr ( options ) ;
15+ }
16+ }
17+
1218export 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}
0 commit comments