1- import { getAxesSpecForSpecId } from '../store/utils' ;
1+ import { getAxesSpecForSpecId , LastValues } from '../store/utils' ;
22import { identity } from '../../../utils/commons' ;
33import { AxisId , SpecId } from '../../../utils/ids' ;
44import {
55 DataSeriesColorsValues ,
66 findDataSeriesByColorValues ,
77 getSortedDataSeriesColorsValuesMap ,
88} from '../utils/series' ;
9- import { AxisSpec , BasicSeriesSpec } from '../utils/specs' ;
9+ import { AxisSpec , BasicSeriesSpec , Postfixes , isAreaSeriesSpec , isBarSeriesSpec } from '../utils/specs' ;
10+ import { Y0_ACCESSOR_POSTFIX , Y1_ACCESSOR_POSTFIX } from '../tooltip/tooltip' ;
1011
11- export interface LegendItem {
12+ export interface FormatedLastValues {
13+ y0 : number | string | null ;
14+ y1 : number | string | null ;
15+ }
16+
17+ export type LegendItem = Postfixes & {
1218 key : string ;
1319 color : string ;
1420 label : string ;
1521 value : DataSeriesColorsValues ;
1622 isSeriesVisible ?: boolean ;
23+ banded ?: boolean ;
1724 isLegendItemVisible ?: boolean ;
1825 displayValue : {
19- raw : any ;
20- formatted : any ;
26+ raw : LastValues ;
27+ formatted : FormatedLastValues ;
2128 } ;
29+ } ;
30+
31+ export function getPostfix ( spec : BasicSeriesSpec ) : Postfixes {
32+ if ( isAreaSeriesSpec ( spec ) || isBarSeriesSpec ( spec ) ) {
33+ const { y0AccessorFormat = Y0_ACCESSOR_POSTFIX , y1AccessorFormat = Y1_ACCESSOR_POSTFIX } = spec ;
34+ return {
35+ y0AccessorFormat,
36+ y1AccessorFormat,
37+ } ;
38+ }
39+
40+ return { } ;
2241}
2342
2443export function computeLegend (
@@ -33,10 +52,11 @@ export function computeLegend(
3352 const sortedSeriesColors = getSortedDataSeriesColorsValuesMap ( seriesColor ) ;
3453
3554 sortedSeriesColors . forEach ( ( series , key ) => {
36- const spec = specs . get ( series . specId ) ;
55+ const { banded, specId, lastValue, colorValues } = series ;
56+ const spec = specs . get ( specId ) ;
3757 const color = seriesColorMap . get ( key ) || defaultColor ;
3858 const hasSingleSeries = seriesColor . size === 1 ;
39- const label = getSeriesColorLabel ( series . colorValues , hasSingleSeries , spec ) ;
59+ const label = getSeriesColorLabel ( colorValues , hasSingleSeries , spec ) ;
4060 const isSeriesVisible = deselectedDataSeries ? findDataSeriesByColorValues ( deselectedDataSeries , series ) < 0 : true ;
4161
4262 if ( ! label || ! spec ) {
@@ -46,21 +66,30 @@ export function computeLegend(
4666 // Use this to get axis spec w/ tick formatter
4767 const { yAxis } = getAxesSpecForSpecId ( axesSpecs , spec . groupId ) ;
4868 const formatter = yAxis ? yAxis . tickFormat : identity ;
49-
5069 const { hideInLegend } = spec ;
5170
52- legendItems . set ( key , {
71+ const legendItem : LegendItem = {
5372 key,
5473 color,
5574 label,
75+ banded,
5676 value : series ,
5777 isSeriesVisible,
5878 isLegendItemVisible : ! hideInLegend ,
5979 displayValue : {
60- raw : series . lastValue ,
61- formatted : isSeriesVisible ? formatter ( series . lastValue ) : undefined ,
80+ raw : {
81+ y0 : lastValue && lastValue . y0 !== null ? lastValue . y0 : null ,
82+ y1 : lastValue && lastValue . y1 !== null ? lastValue . y1 : null ,
83+ } ,
84+ formatted : {
85+ y0 : isSeriesVisible && lastValue && lastValue . y0 !== null ? formatter ( lastValue . y0 ) : null ,
86+ y1 : isSeriesVisible && lastValue && lastValue . y1 !== null ? formatter ( lastValue . y1 ) : null ,
87+ } ,
6288 } ,
63- } ) ;
89+ ...getPostfix ( spec ) ,
90+ } ;
91+
92+ legendItems . set ( key , legendItem ) ;
6493 } ) ;
6594 return legendItems ;
6695}
0 commit comments