@@ -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 > ;
@@ -227,19 +230,26 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
227230 }
228231
229232 private renderMarker ( container : Selection ) {
230- const { marker } = this . attributes ;
233+ const { marker, classNamePrefix } = this . attributes ;
231234 const style = subStyleProps ( this . attributes , 'marker' ) ;
232235 this . markerGroup = container . maybeAppendByClassName ( CLASS_NAMES . markerGroup , 'g' ) . style ( 'zIndex' , 0 ) ;
233236 ifShow ( ! ! marker , this . markerGroup , ( ) => {
234237 const parent = this . markerGroup . node ( ) ;
235238 const oldMarker = parent . childNodes ?. [ 0 ] as DisplayObject | undefined ;
239+ const markerClassName = getLegendClassName ( CLASS_NAMES . marker . name , CLASSNAME_SUFFIX_MAP . marker , classNamePrefix ) ;
236240 const newMarker =
237- typeof marker === 'string'
238- ? new Marker ( { style : { symbol : marker } , className : CLASS_NAMES . marker . name } )
239- : marker ( ) ;
241+ typeof marker === 'string' ? new Marker ( { style : { symbol : marker } , className : markerClassName } ) : marker ( ) ;
240242
241243 if ( ! oldMarker ) {
242- if ( ! ( newMarker instanceof Marker ) ) select ( newMarker ) . attr ( 'className' , CLASS_NAMES . marker . name ) . styles ( style ) ;
244+ if ( ! ( newMarker instanceof Marker ) ) {
245+ const markerClassName = getLegendClassName (
246+ CLASS_NAMES . marker . name ,
247+ CLASSNAME_SUFFIX_MAP . marker ,
248+ classNamePrefix
249+ ) ;
250+ ( newMarker as any ) . className = markerClassName ;
251+ select ( newMarker ) . styles ( style ) ;
252+ }
243253 parent . appendChild ( newMarker ) ;
244254 } else if ( newMarker . nodeName === oldMarker . nodeName ) {
245255 if ( oldMarker instanceof Marker ) oldMarker . update ( { ...style , symbol : marker } ) ;
@@ -249,7 +259,15 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
249259 }
250260 } else {
251261 oldMarker . remove ( ) ;
252- select ( newMarker ) . attr ( 'className' , CLASS_NAMES . marker . name ) . styles ( style ) ;
262+ if ( ! ( newMarker instanceof Marker ) ) {
263+ const markerClassName = getLegendClassName (
264+ CLASS_NAMES . marker . name ,
265+ CLASSNAME_SUFFIX_MAP . marker ,
266+ classNamePrefix
267+ ) ;
268+ ( newMarker as any ) . className = markerClassName ;
269+ }
270+ select ( newMarker ) . styles ( style ) ;
253271 parent . appendChild ( newMarker ) ;
254272 }
255273
@@ -262,15 +280,23 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
262280
263281 private renderLabel ( container : Selection ) {
264282 const { text : label , ...style } = subStyleProps ( this . attributes , 'label' ) ;
283+ const { classNamePrefix } = this . attributes ;
265284 this . labelGroup = container . maybeAppendByClassName < Group > ( CLASS_NAMES . labelGroup , 'g' ) . style ( 'zIndex' , 0 ) ;
266- this . labelGroup . maybeAppendByClassName ( CLASS_NAMES . label , ( ) => renderExtDo ( label ) ) . styles ( style ) ;
285+ const labelClassName = getLegendClassName ( CLASS_NAMES . label . name , CLASSNAME_SUFFIX_MAP . label , classNamePrefix ) ;
286+ const labelElement = this . labelGroup . maybeAppendByClassName ( CLASS_NAMES . label , ( ) => renderExtDo ( label ) ) ;
287+ labelElement . node ( ) . setAttribute ( 'class' , labelClassName ) ;
288+ labelElement . styles ( style ) ;
267289 }
268290
269291 private renderValue ( container : Selection ) {
270292 const { text : value , ...style } = subStyleProps ( this . attributes , 'value' ) ;
293+ const { classNamePrefix } = this . attributes ;
271294 this . valueGroup = container . maybeAppendByClassName ( CLASS_NAMES . valueGroup , 'g' ) . style ( 'zIndex' , 0 ) ;
272295 ifShow ( this . showValue , this . valueGroup , ( ) => {
273- this . valueGroup . maybeAppendByClassName ( CLASS_NAMES . value , ( ) => renderExtDo ( value ) ) . styles ( style ) ;
296+ const valueClassName = getLegendClassName ( CLASS_NAMES . value . name , CLASSNAME_SUFFIX_MAP . value , classNamePrefix ) ;
297+ const valueElement = this . valueGroup . maybeAppendByClassName ( CLASS_NAMES . value , ( ) => renderExtDo ( value ) ) ;
298+ valueElement . node ( ) . setAttribute ( 'class' , valueClassName ) ;
299+ valueElement . styles ( style ) ;
274300 } ) ;
275301 }
276302
@@ -305,7 +331,7 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
305331 }
306332
307333 private renderFocus ( ctn : Selection ) {
308- const { focus, focusMarkerSize } = this . attributes ;
334+ const { focus, focusMarkerSize, classNamePrefix } = this . attributes ;
309335 const defaultOptions = {
310336 x : 0 ,
311337 y : 0 ,
@@ -320,11 +346,17 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
320346
321347 this . focusGroup = ctn . maybeAppendByClassName < Group > ( CLASS_NAMES . focusGroup , 'g' ) . style ( 'zIndex' , 0 ) ;
322348 ifShow ( focus , this . focusGroup , ( ) => {
349+ const focusClassName = getLegendClassName (
350+ CLASS_NAMES . focus . name ,
351+ CLASSNAME_SUFFIX_MAP . focusIcon ,
352+ classNamePrefix
353+ ) ;
323354 const marker = new Marker ( {
324355 style : {
325356 ...defaultOptions ,
326357 symbol : 'focus' ,
327358 } ,
359+ className : focusClassName ,
328360 } ) ;
329361 const interactiveCircle = new Circle ( {
330362 style : {
@@ -363,7 +395,18 @@ export class CategoryItem extends Component<CategoryItemStyleProps> {
363395 const { width, height } = this . shape ;
364396 const style = subStyleProps ( this . attributes , 'background' ) ;
365397 this . background = container . maybeAppendByClassName ( CLASS_NAMES . backgroundGroup , 'g' ) . style ( 'zIndex' , - 1 ) ;
366- this . background . maybeAppendByClassName ( CLASS_NAMES . background , 'rect' ) . styles ( { width, height, ...style } ) ;
398+ const backgroundElement = this . background . maybeAppendByClassName ( CLASS_NAMES . background , 'rect' ) ;
399+ backgroundElement . styles ( { width, height, ...style } ) ;
400+
401+ const { classNamePrefix = '' } = this . attributes ;
402+ if ( classNamePrefix ) {
403+ const backgroundClassName = getLegendClassName (
404+ CLASS_NAMES . background . name ,
405+ CLASSNAME_SUFFIX_MAP . background ,
406+ classNamePrefix
407+ ) ;
408+ backgroundElement . node ( ) . setAttribute ( 'class' , backgroundClassName ) ;
409+ }
367410 }
368411
369412 private adjustLayout ( ) {
0 commit comments