import numeral from 'numeral' ; import omit from 'lodash/omit' ; import isArray from 'lodash/isArray' ; import isString from 'lodash/isString' ; import isFunction from 'lodash/isFunction' ; import isUndefined from 'lodash/isUndefined' ; import warning from './_util/warning' ; import { ChartOption , EChartOption , ChartTooltip } from './interface' ; /**
 * var string = numeral(1000).format('0,0');
 * '1,000'
 */ export type NormalizedFormat = { type ? : string ; label ? : string | ( ( name : string ) => string ) ; title ? : string ; unit ? : string ; } ; export type Format = string | NormalizedFormat ; type FormatterParam = any ; const typeValue = { line ( param , format , isAxis ) { const { marker , name , seriesName , value } = param ; return `<div>${ marker }${ getFormatedLabel ( isAxis ? seriesName : name , format ) }: ${ getFormatedValue ( value , format ) }</div>` ; } , bar ( param , format , isAxis ) { const { marker , name , seriesName , value } = param ; return `<div>${ marker }${ getFormatedLabel ( isAxis ? seriesName : name , format ) }: ${ getFormatedValue ( value , format ) }</div>` ; } , heatmap ( param , format , isAxis ) { const { marker , name , seriesName , value , dimensionNames } = param ; return `<div>${ marker }${ getFormatedLabel ( isAxis ? seriesName : name , format ) }: ${ getFormatedValue ( value [ dimensionNames . length - 1 ] , format ) }</div>` ; } , radar ( param , format , _ , option ) { const { indicator } = option . radar as { indicator : { name : string ; } [ ] ; } ; const { value } = param ; return value . map ( ( val , index ) => { return `<div> ${ getFormatedLabel ( indicator [ index ] . name , format ) }: ${ getFormatedValue ( val , format ) }</div>` ; } ) . join ( '' ) ; } , scatter ( param , format ) { const { marker , value , dimensionNames } = param ; return `<div>${ marker }${ getFormatedValue ( value [ dimensionNames . length - 1 ] , format ) }</div>` ; } , pie ( param , format ) { const { marker , value , name } = param ; return `<div>${ marker }${ getFormatedLabel ( name , format ) }: ${ getFormatedValue ( value , format ) }</div>` ; } , map ( param , format ) { const { value , name } = param ; return `<div>${ getFormatedLabel ( name , format ) }: ${ getFormatedValue ( value , format ) }</div>` ; } , } ; const typeTitle = { radar ( param ) { return param . name ; } , } ; function getFormatedValue ( value , format ) { const { type , unit } = format ; return type ? numeral ( value ) . format ( type ) : value + ( unit || '' ) ; } function getFormatedLabel ( name , format ) { const { label } = format ; if ( label ) { if ( isString ( label ) ) { return label ; } else if ( isFunction ( label ) ) { return label ( name ) ; } } return name ; } const formatter = ( option ) => { const { tooltip } = option ; if ( ! tooltip || ! tooltip . formatter || ! isArray ( tooltip . formatter ) ) { return option ; } const finalOption = omit ( option , [ 'tooltip' ] ) as EChartOption ; const formatter = tooltip . formatter ; const finalTooltip = omit ( tooltip , [ 'formatter' ] ) as ChartTooltip ; finalTooltip . formatter = ( params ) => { return tooltipFormatter ( params , formatter , option ) ; } ; finalOption . tooltip = finalTooltip ; return finalOption ; } ; function normalizeFormat ( format ) { if ( isString ( format ) ) { return { type : format , } ; } return format ; } function tooltipFormatter ( params , formatter , option ) { const finalParams = isArray ( params ) ? params : [ params ] ; const finalFormates = formatter . map ( ( format ) => normalizeFormat ( format ) ) as NormalizedFormat [ ] ; warning ( finalParams . length === formatter . length , 'the length of the series must be equal to the length of the formatter' ) ; const firstFormat = finalFormates [ 0 ] ; let title = firstFormat . title ; const isAxis = finalParams . length > 1 ; const values = finalParams . map ( ( param , index ) => { const { name , seriesName , seriesType } = param ; if ( isUndefined ( title ) ) { title = typeTitle [ seriesType ] ? typeTitle [ seriesType ] ( param ) : isAxis ? name : seriesName ; } const typeValueFn = typeValue [ seriesType ] ; warning ( typeValueFn , `formatter of ${ seriesType } is not supported temporarily` ) ; return typeValueFn ( param , finalFormates [ index ] , isAxis , option ) ; } ) ; const labelValues = values . join ( '' ) ; if ( title ) { return `<div><div>${ title }</div>${ labelValues }</div>` ; } return `<div>${ labelValues }</div>` ; } export default formatter ; export { numeral } ;