@@ -30,6 +30,8 @@ import {
3030import { Poptip } from '../../poptip' ;
3131import type { PoptipStyleProps } from '../../poptip/types' ;
3232import type { PoptipRender } from './items' ;
33+ import { getLegendClassName } from '../utils/classname' ;
34+ import { CLASSNAME_SUFFIX_MAP } from '../constant' ;
3335
3436type ItemMarkerStyle = { size ?: number } & PathStyleProps ;
3537type ItemTextStyle = Omit < TextStyleProps , 'text' > ;
@@ -53,6 +55,7 @@ export type CategoryItemStyleProps = GroupStyleProps &
5355 poptip ?: PoptipStyleProps & PoptipRender ;
5456 focus ?: boolean ;
5557 focusMarkerSize ?: number ;
58+ classNamePrefix ?: string ;
5659 } ;
5760
5861export type CategoryItemOptions = ComponentOptions < CategoryItemStyleProps > ;
@@ -223,19 +226,26 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
223226 }
224227
225228 private renderMarker ( container : Selection ) {
226- const { marker } = this . attributes ;
229+ const { marker, classNamePrefix } = this . attributes ;
227230 const style = subStyleProps ( this . attributes , 'marker' ) ;
228231 this . markerGroup = container . maybeAppendByClassName ( CLASS_NAMES . markerGroup , 'g' ) . style ( 'zIndex' , 0 ) ;
229232 ifShow ( ! ! marker , this . markerGroup , ( ) => {
230233 const parent = this . markerGroup . node ( ) ;
231234 const oldMarker = parent . childNodes ?. [ 0 ] as DisplayObject | undefined ;
235+ const markerClassName = getLegendClassName ( CLASS_NAMES . marker . name , CLASSNAME_SUFFIX_MAP . marker , classNamePrefix ) ;
232236 const newMarker =
233- typeof marker === 'string'
234- ? new Marker ( { style : { symbol : marker } , className : CLASS_NAMES . marker . name } )
235- : marker ( ) ;
237+ typeof marker === 'string' ? new Marker ( { style : { symbol : marker } , className : markerClassName } ) : marker ( ) ;
236238
237239 if ( ! oldMarker ) {
238- if ( ! ( newMarker instanceof Marker ) ) select ( newMarker ) . attr ( 'className' , CLASS_NAMES . marker . name ) . styles ( style ) ;
240+ if ( ! ( newMarker instanceof Marker ) ) {
241+ const markerClassName = getLegendClassName (
242+ CLASS_NAMES . marker . name ,
243+ CLASSNAME_SUFFIX_MAP . marker ,
244+ classNamePrefix
245+ ) ;
246+ ( newMarker as any ) . className = markerClassName ;
247+ select ( newMarker ) . styles ( style ) ;
248+ }
239249 parent . appendChild ( newMarker ) ;
240250 } else if ( newMarker . nodeName === oldMarker . nodeName ) {
241251 if ( oldMarker instanceof Marker ) oldMarker . update ( { ...style , symbol : marker } ) ;
@@ -245,7 +255,15 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
245255 }
246256 } else {
247257 oldMarker . remove ( ) ;
248- select ( newMarker ) . attr ( 'className' , CLASS_NAMES . marker . name ) . styles ( style ) ;
258+ if ( ! ( newMarker instanceof Marker ) ) {
259+ const markerClassName = getLegendClassName (
260+ CLASS_NAMES . marker . name ,
261+ CLASSNAME_SUFFIX_MAP . marker ,
262+ classNamePrefix
263+ ) ;
264+ ( newMarker as any ) . className = markerClassName ;
265+ }
266+ select ( newMarker ) . styles ( style ) ;
249267 parent . appendChild ( newMarker ) ;
250268 }
251269
@@ -258,15 +276,23 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
258276
259277 private renderLabel ( container : Selection ) {
260278 const { text : label , ...style } = subStyleProps ( this . attributes , 'label' ) ;
279+ const { classNamePrefix } = this . attributes ;
261280 this . labelGroup = container . maybeAppendByClassName < Group > ( CLASS_NAMES . labelGroup , 'g' ) . style ( 'zIndex' , 0 ) ;
262- this . labelGroup . maybeAppendByClassName ( CLASS_NAMES . label , ( ) => renderExtDo ( label ) ) . styles ( style ) ;
281+ const labelClassName = getLegendClassName ( CLASS_NAMES . label . name , CLASSNAME_SUFFIX_MAP . label , classNamePrefix ) ;
282+ const labelElement = this . labelGroup . maybeAppendByClassName ( CLASS_NAMES . label , ( ) => renderExtDo ( label ) ) ;
283+ labelElement . node ( ) . setAttribute ( 'class' , labelClassName ) ;
284+ labelElement . styles ( style ) ;
263285 }
264286
265287 private renderValue ( container : Selection ) {
266288 const { text : value , ...style } = subStyleProps ( this . attributes , 'value' ) ;
289+ const { classNamePrefix } = this . attributes ;
267290 this . valueGroup = container . maybeAppendByClassName ( CLASS_NAMES . valueGroup , 'g' ) . style ( 'zIndex' , 0 ) ;
268291 ifShow ( this . showValue , this . valueGroup , ( ) => {
269- this . valueGroup . maybeAppendByClassName ( CLASS_NAMES . value , ( ) => renderExtDo ( value ) ) . styles ( style ) ;
292+ const valueClassName = getLegendClassName ( CLASS_NAMES . value . name , CLASSNAME_SUFFIX_MAP . value , classNamePrefix ) ;
293+ const valueElement = this . valueGroup . maybeAppendByClassName ( CLASS_NAMES . value , ( ) => renderExtDo ( value ) ) ;
294+ valueElement . node ( ) . setAttribute ( 'class' , valueClassName ) ;
295+ valueElement . styles ( style ) ;
270296 } ) ;
271297 }
272298
@@ -301,7 +327,7 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
301327 }
302328
303329 private renderFocus ( ctn : Selection ) {
304- const { focus, focusMarkerSize } = this . attributes ;
330+ const { focus, focusMarkerSize, classNamePrefix } = this . attributes ;
305331 const defaultOptions = {
306332 x : 0 ,
307333 y : 0 ,
@@ -316,11 +342,17 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
316342
317343 this . focusGroup = ctn . maybeAppendByClassName < Group > ( CLASS_NAMES . focusGroup , 'g' ) . style ( 'zIndex' , 0 ) ;
318344 ifShow ( focus , this . focusGroup , ( ) => {
345+ const focusClassName = getLegendClassName (
346+ CLASS_NAMES . focus . name ,
347+ CLASSNAME_SUFFIX_MAP . focusIcon ,
348+ classNamePrefix
349+ ) ;
319350 const marker = new Marker ( {
320351 style : {
321352 ...defaultOptions ,
322353 symbol : 'focus' ,
323354 } ,
355+ className : focusClassName ,
324356 } ) ;
325357 const interactiveCircle = new Circle ( {
326358 style : {
@@ -359,7 +391,18 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
359391 const { width, height } = this . shape ;
360392 const style = subStyleProps ( this . attributes , 'background' ) ;
361393 this . background = container . maybeAppendByClassName ( CLASS_NAMES . backgroundGroup , 'g' ) . style ( 'zIndex' , - 1 ) ;
362- this . background . maybeAppendByClassName ( CLASS_NAMES . background , 'rect' ) . styles ( { width, height, ...style } ) ;
394+ const backgroundElement = this . background . maybeAppendByClassName ( CLASS_NAMES . background , 'rect' ) ;
395+ backgroundElement . styles ( { width, height, ...style } ) ;
396+
397+ const { classNamePrefix = '' } = this . attributes ;
398+ if ( classNamePrefix ) {
399+ const backgroundClassName = getLegendClassName (
400+ CLASS_NAMES . background . name ,
401+ CLASSNAME_SUFFIX_MAP . background ,
402+ classNamePrefix
403+ ) ;
404+ backgroundElement . node ( ) . setAttribute ( 'class' , backgroundClassName ) ;
405+ }
363406 }
364407
365408 private adjustLayout ( ) {
0 commit comments