@@ -12,15 +12,20 @@ import { ChartConfig } from '@/components/ui/chart';
1212 */
1313function generateSeriesName (
1414 item : InsightsRawData ,
15- groups : Pick < GroupInfo , 'value' > [ ]
15+ groups : Pick < GroupInfo , 'value' > [ ] ,
16+ groupValueFormatter ?: GroupValueFormatter
1617) : string {
1718 // Use alias if available, otherwise use name
1819 let name = item . alias ?? item . name ;
1920
2021 if ( groups . length > 0 ) {
2122 const groupSuffixes = groups
2223 . map ( ( group ) => {
23- return get ( item , group . value ) ;
24+ const value = get ( item , group . value ) ;
25+
26+ return value && groupValueFormatter
27+ ? groupValueFormatter ( String ( value ) , group )
28+ : value ;
2429 } )
2530 . filter ( Boolean ) // Remove null/undefined values
2631 . join ( '-' ) ;
@@ -48,6 +53,11 @@ export interface ProcessedChartData {
4853 [ key : string ] : string | number ;
4954}
5055
56+ export type GroupValueFormatter = (
57+ value : string ,
58+ group : Pick < GroupInfo , 'value' >
59+ ) => string ;
60+
5161export interface UseInsightsDataOptions {
5262 data : InsightsRawData [ ] ;
5363 groups : Pick < GroupInfo , 'value' > [ ] ;
@@ -57,6 +67,7 @@ export interface UseInsightsDataOptions {
5767 unit : DateUnit ;
5868 } ;
5969 valueProcessor ?: ( value : number ) => number ;
70+ groupValueFormatter ?: GroupValueFormatter ;
6071}
6172
6273/**
@@ -73,7 +84,7 @@ export function processInsightsData(options: UseInsightsDataOptions): {
7384 isMultiSeries : boolean ;
7485 seriesCount : number ;
7586} {
76- const { data, groups, time, valueProcessor } = options ;
87+ const { data, groups, time, valueProcessor, groupValueFormatter } = options ;
7788
7889 if ( ! data || data . length === 0 ) {
7990 return {
@@ -117,6 +128,7 @@ export function processInsightsData(options: UseInsightsDataOptions): {
117128
118129 // For complex case (groups or multiple metrics), process full chart data
119130 const res : { date : string } [ ] = [ ] ;
131+ const seriesLabels = new Map < string , string > ( ) ;
120132
121133 // Collect all unique dates from all data series
122134 const allDates = new Set < string > ( ) ;
@@ -131,6 +143,9 @@ export function processInsightsData(options: UseInsightsDataOptions): {
131143 const value = dataPoint ?. value ?? 0 ;
132144 const processedValue = valueProcessor ?.( value ) ?? value ;
133145 const name = generateSeriesName ( item , groups ) ;
146+ const label = generateSeriesName ( item , groups , groupValueFormatter ) ;
147+
148+ seriesLabels . set ( name , label ) ;
134149
135150 res . push ( {
136151 date,
@@ -151,7 +166,7 @@ export function processInsightsData(options: UseInsightsDataOptions): {
151166 return {
152167 ...prev ,
153168 [ curr ] : {
154- label : curr ,
169+ label : seriesLabels . get ( curr ) ?? curr ,
155170 color : pickColorWithNum ( i ) ,
156171 } ,
157172 } ;
@@ -193,6 +208,7 @@ export function useInsightsData(options: UseInsightsDataOptions) {
193208 options . time . endAt ,
194209 options . time . unit ,
195210 options . valueProcessor ,
211+ options . groupValueFormatter ,
196212 ]
197213 ) ;
198214}
0 commit comments