@ -3,6 +3,10 @@ import { XYFieldMatchers } from '@grafana/ui/src/components/GraphNG/types';
import {
ArrayVector ,
DataFrame ,
DashboardCursorSync ,
DataHoverPayload ,
DataHoverEvent ,
DataHoverClearEvent ,
FALLBACK_COLOR ,
Field ,
FieldColorModeId ,
@ -58,6 +62,8 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<TimelineOptions> = ({
timeZone ,
getTimeRange ,
mode ,
eventBus ,
sync ,
rowHeight ,
colWidth ,
showValue ,
@ -65,6 +71,9 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<TimelineOptions> = ({
} ) = > {
const builder = new UPlotConfigBuilder ( timeZone ) ;
const xScaleUnit = 'time' ;
const xScaleKey = 'x' ;
const isDiscrete = ( field : Field ) = > {
const mode = field . config ? . color ? . mode ;
return ! ( mode && field . display && mode . startsWith ( 'continuous-' ) ) ;
@ -116,6 +125,13 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<TimelineOptions> = ({
let hoveredDataIdx : number | null = null ;
const coreConfig = getConfig ( opts ) ;
const payload : DataHoverPayload = {
point : {
[ xScaleUnit ] : null ,
[ FIXED_UNIT ] : null ,
} ,
data : frame ,
} ;
builder . addHook ( 'init' , coreConfig . init ) ;
builder . addHook ( 'drawClear' , coreConfig . drawClear ) ;
@ -148,7 +164,7 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<TimelineOptions> = ({
builder . setCursor ( coreConfig . cursor ) ;
builder . addScale ( {
scaleKey : 'x' ,
scaleKey : xScaleKey ,
isTime : true ,
orientation : ScaleOrientation.Horizontal ,
direction : ScaleDirection.Right ,
@ -164,7 +180,7 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<TimelineOptions> = ({
} ) ;
builder . addAxis ( {
scaleKey : 'x' ,
scaleKey : xScaleKey ,
isTime : true ,
splits : coreConfig.xSplits ! ,
placement : AxisPlacement.Bottom ,
@ -219,6 +235,34 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<TimelineOptions> = ({
} ) ;
}
if ( sync !== DashboardCursorSync . Off ) {
let cursor : Partial < uPlot.Cursor > = { } ;
cursor . sync = {
key : '__global_' ,
filters : {
pub : ( type : string , src : uPlot , x : number , y : number , w : number , h : number , dataIdx : number ) = > {
payload . rowIndex = dataIdx ;
if ( x < 0 && y < 0 ) {
payload . point [ xScaleUnit ] = null ;
payload . point [ FIXED_UNIT ] = null ;
eventBus . publish ( new DataHoverClearEvent ( ) ) ;
} else {
payload . point [ xScaleUnit ] = src . posToVal ( x , xScaleKey ) ;
payload . point . panelRelY = y > 0 ? y / h : 1 ; // used for old graph panel to position tooltip
payload . down = undefined ;
eventBus . publish ( new DataHoverEvent ( payload ) ) ;
}
return true ;
} ,
} ,
//TODO: remove any once https://github.com/leeoniya/uPlot/pull/611 got merged or the typing is fixed
scales : [ xScaleKey , null as any ] ,
} ;
builder . setSync ( ) ;
builder . setCursor ( cursor ) ;
}
return builder ;
} ;