@@ -341,31 +341,40 @@ export const LineChart: React.FunctionComponent<LineChartProps> = React.forwardR
341341 function _createLegends ( data : LineChartDataWithIndex [ ] ) : JSXElement {
342342 const { legendProps, allowMultipleShapesForPoints = false } = props ;
343343 const isLegendMultiSelectEnabled = ! ! ( legendProps && ! ! legendProps . canSelectMultipleLegends ) ;
344- const legendDataItems = data . map ( ( point : LineChartDataWithIndex ) => {
345- const color : string = point . color ! ;
344+ const mapLegendToPoints : Record < string , LineChartDataWithIndex [ ] > = { } ;
345+ data . forEach ( ( point : LineChartDataWithIndex ) => {
346+ if ( point . legend ) {
347+ if ( ! mapLegendToPoints [ point . legend ] ) {
348+ mapLegendToPoints [ point . legend ] = [ ] ;
349+ }
350+ mapLegendToPoints [ point . legend ] . push ( point ) ;
351+ }
352+ } ) ;
353+ const legendDataItems : Legend [ ] = Object . entries ( mapLegendToPoints ) . map ( ( [ legendTitle , points ] ) => {
354+ const representativePoint = points [ 0 ] ;
346355 // mapping data to the format Legends component needs
347356 const legend : Legend = {
348- title : point . legend ! ,
349- color,
357+ title : legendTitle ,
358+ color : representativePoint . color ! ,
350359 action : ( ) => {
351360 if ( isLegendMultiSelectEnabled ) {
352- _handleMultipleLineLegendSelectionAction ( point ) ;
361+ points . forEach ( p => _handleMultipleLineLegendSelectionAction ( p ) ) ;
353362 } else {
354- _handleSingleLegendSelectionAction ( point ) ;
363+ points . forEach ( p => _handleSingleLegendSelectionAction ( p ) ) ;
355364 }
356365 } ,
357366 onMouseOutAction : ( ) => {
358367 setActiveLegend ( '' ) ;
359368 } ,
360369 hoverAction : ( ) => {
361370 _handleChartMouseLeave ( ) ;
362- setActiveLegend ( point . legend ) ;
371+ setActiveLegend ( legendTitle ) ;
363372 } ,
364- ...( point . legendShape && {
365- shape : point . legendShape ,
373+ ...( representativePoint . legendShape && {
374+ shape : representativePoint . legendShape ,
366375 } ) ,
367376 ...( allowMultipleShapesForPoints && {
368- shape : Points [ point . index % Object . keys ( pointTypes ) . length ] as Legend [ 'shape' ] ,
377+ shape : Points [ representativePoint . index % Object . keys ( pointTypes ) . length ] as Legend [ 'shape' ] ,
369378 } ) ,
370379 } ;
371380 return legend ;
0 commit comments