7
7
import { formatPercentage } from '../../../utils' ;
8
8
import type { SeriesData , DataPointDate , DataPointPercentage } from '../../../types' ;
9
9
import type { BaseLegendItem } from '../types' ;
10
- import type { GlyphProps , LineStyles } from '@visx/xychart' ;
11
- import type { ReactNode , CSSProperties } from 'react' ;
10
+ import type { LegendShape } from '@visx/legend/lib/types' ;
11
+ import type { GlyphProps } from '@visx/xychart' ;
12
+ import type { ReactNode } from 'react' ;
12
13
13
14
export type LegendValueDisplay = 'percentage' | 'value' | 'valueDisplay' | 'none' ;
14
15
@@ -18,6 +19,7 @@ export interface ChartLegendOptions {
18
19
renderGlyph ?: < Datum extends object > ( props : GlyphProps < Datum > ) => ReactNode ;
19
20
showValues ?: boolean ;
20
21
legendValueDisplay ?: LegendValueDisplay ;
22
+ legendShape ?: LegendShape < SeriesData [ ] , number > ;
21
23
}
22
24
23
25
/**
@@ -67,6 +69,7 @@ function formatPointValue(
67
69
* @param withGlyph - Whether to include glyph rendering
68
70
* @param glyphSize - Size of the glyph
69
71
* @param renderGlyph - Component to render the glyph
72
+ * @param legendShape - The shape to use for the item
70
73
* @return Array of processed legend items
71
74
*/
72
75
function processSeriesData (
@@ -75,19 +78,21 @@ function processSeriesData(
75
78
showValues : boolean ,
76
79
withGlyph : boolean ,
77
80
glyphSize : number ,
78
- renderGlyph ?: < Datum extends object > ( props : GlyphProps < Datum > ) => ReactNode
81
+ renderGlyph ?: < Datum extends object > ( props : GlyphProps < Datum > ) => ReactNode ,
82
+ legendShape ?: LegendShape < SeriesData [ ] , number >
79
83
) : BaseLegendItem [ ] {
80
84
const mapper = ( series : SeriesData , index : number ) => {
81
- const { color, lineStyles , glyph } = getElementStyles ( {
85
+ const { color, glyph , shapeStyles } = getElementStyles ( {
82
86
data : series ,
83
87
index,
88
+ legendShape,
84
89
} ) ;
85
90
86
91
const baseItem : BaseLegendItem = {
87
92
label : series . label ,
88
93
value : showValues ? series . data ?. length ?. toString ( ) || '0' : '' ,
89
94
color,
90
- shapeStyle : lineStyles as CSSProperties & LineStyles ,
95
+ shapeStyles ,
91
96
} ;
92
97
93
98
if ( withGlyph ) {
@@ -116,6 +121,7 @@ function processSeriesData(
116
121
* @param withGlyph - Whether to include glyph rendering
117
122
* @param glyphSize - Size of the glyph
118
123
* @param renderGlyph - Component to render the glyph
124
+ * @param legendShape - The shape to use for the item
119
125
* @return Array of processed legend items
120
126
*/
121
127
function processPointData (
@@ -125,19 +131,21 @@ function processPointData(
125
131
legendValueDisplay : LegendValueDisplay ,
126
132
withGlyph : boolean ,
127
133
glyphSize : number ,
128
- renderGlyph ?: < Datum extends object > ( props : GlyphProps < Datum > ) => ReactNode
134
+ renderGlyph ?: < Datum extends object > ( props : GlyphProps < Datum > ) => ReactNode ,
135
+ legendShape ?: LegendShape < SeriesData [ ] , number >
129
136
) : BaseLegendItem [ ] {
130
137
const mapper = ( point : DataPointDate | DataPointPercentage , index : number ) => {
131
- const { color, lineStyles , glyph } = getElementStyles ( {
138
+ const { color, glyph , shapeStyles } = getElementStyles ( {
132
139
data : point as DataPointPercentage ,
133
140
index,
141
+ legendShape,
134
142
} ) ;
135
143
136
144
const baseItem : BaseLegendItem = {
137
145
label : point . label ,
138
146
value : formatPointValue ( point , showValues , legendValueDisplay ) ,
139
147
color,
140
- shapeStyle : lineStyles as CSSProperties & LineStyles ,
148
+ shapeStyles ,
141
149
} ;
142
150
143
151
if ( withGlyph ) {
@@ -159,13 +167,18 @@ function processPointData(
159
167
160
168
/**
161
169
* Hook to transform chart data into legend items
162
- * @param data - The chart data to transform
163
- * @param options - Configuration options for legend generation
170
+ * @param data - The chart data to transform
171
+ * @param options - Configuration options for legend generation
172
+ * @param legendShape - The shape to use for the item
164
173
* @return Array of legend items ready for display
165
174
*/
166
175
export function useChartLegendItems <
167
176
T extends SeriesData [ ] | DataPointDate [ ] | DataPointPercentage [ ] ,
168
- > ( data : T , options : ChartLegendOptions = { } ) : BaseLegendItem [ ] {
177
+ > (
178
+ data : T ,
179
+ options : ChartLegendOptions = { } ,
180
+ legendShape ?: LegendShape < SeriesData [ ] , number >
181
+ ) : BaseLegendItem [ ] {
169
182
const {
170
183
showValues = false ,
171
184
legendValueDisplay = 'percentage' ,
@@ -188,7 +201,8 @@ export function useChartLegendItems<
188
201
showValues ,
189
202
withGlyph ,
190
203
glyphSize ,
191
- renderGlyph
204
+ renderGlyph ,
205
+ legendShape
192
206
) ;
193
207
}
194
208
@@ -200,7 +214,8 @@ export function useChartLegendItems<
200
214
legendValueDisplay ,
201
215
withGlyph ,
202
216
glyphSize ,
203
- renderGlyph
217
+ renderGlyph ,
218
+ legendShape
204
219
) ;
205
220
} , [
206
221
data ,
@@ -210,5 +225,6 @@ export function useChartLegendItems<
210
225
withGlyph ,
211
226
glyphSize ,
212
227
renderGlyph ,
228
+ legendShape ,
213
229
] ) ;
214
230
}
0 commit comments