import { colors } from '@grafana/ui' ; import { getFlotPairs , getDisplayProcessor , NullValueMode , reduceField , FieldType , DisplayValue , GraphSeriesXY , getTimeField , DataFrame , FieldDisplayOptions , getSeriesTimeStep , TimeZone , hasMsResolution , MS_DATE_TIME_FORMAT , DEFAULT_DATE_TIME_FORMAT , FieldColor , FieldColorMode , } from '@grafana/data' ; import { SeriesOptions , GraphOptions } from './types' ; import { GraphLegendEditorLegendOptions } from './GraphLegendEditor' ; export const getGraphSeriesModel = ( dataFrames , timeZone , seriesOptions , graphOptions , legendOptions , fieldOptions ? ) => { const graphs = [ ] ; const displayProcessor = getDisplayProcessor ( { field : { config : { unit : fieldOptions ?. defaults ?. unit , decimals : legendOptions . decimals , } , } , } ) ; let fieldColumnIndex = - 1 ; for ( const series of dataFrames ) { const { timeField } = getTimeField ( series ) ; if ( ! timeField ) { continue ; } for ( const field of series . fields ) { if ( field . type !== FieldType . number ) { continue ; } fieldColumnIndex ++ ; const points = getFlotPairs ( { xField : timeField , yField : field , nullValueMode : NullValueMode . Null , } ) ; if ( points . length > 0 ) { const seriesStats = reduceField ( { field , reducers : legendOptions . stats } ) ; let statsDisplayValues ; if ( legendOptions . stats ) { statsDisplayValues = legendOptions . stats . map < DisplayValue > ( stat => { const statDisplayValue = displayProcessor ( seriesStats [ stat ] ) ; return { ... statDisplayValue , title : stat , } ; } ) ; } let color ; if ( seriesOptions [ field . name ] && seriesOptions [ field . name ] . color ) { color = { mode : FieldColorMode . Fixed , fixedColor : seriesOptions [ field . name ] . color , } ; } else if ( field . config && field . config . color ) { color = field . config . color ; } else { color = { mode : FieldColorMode . Fixed , fixedColor : colors [ graphs . length % colors . length ] , } ; } field . config = fieldOptions ? { ... field . config , unit : fieldOptions . defaults . unit , decimals : fieldOptions . defaults . decimals , color , } : { ... field . config , color } ; field . display = getDisplayProcessor ( { field } ) ; const timeStep = getSeriesTimeStep ( timeField ) ; const useMsDateFormat = hasMsResolution ( timeField ) ; timeField . display = getDisplayProcessor ( { timeZone , field : { ... timeField , type : timeField . type , config : { unit : `time:${ useMsDateFormat ? MS_DATE_TIME_FORMAT : DEFAULT_DATE_TIME_FORMAT }` , } , } , } ) ; graphs . push ( { label : field . name , data : points , color : field . config . color ?. fixedColor , info : statsDisplayValues , isVisible : true , yAxis : { index : ( seriesOptions [ field . name ] && seriesOptions [ field . name ] . yAxis ) || 1 , } , seriesIndex : fieldColumnIndex , timeField : { ... timeField } , valueField : { ... field } , timeStep , } ) ; } } } return graphs ; } ;