import React, { useMemo, useState } from 'react'; import { DashboardCursorSync, PanelProps } from '@grafana/data'; import { EventBusPlugin, TooltipDisplayMode, TooltipPlugin2, usePanelContext, useTheme2 } from '@grafana/ui'; import { TimeRange2, TooltipHoverMode } from '@grafana/ui/src/components/uPlot/plugins/TooltipPlugin2'; import { TimelineChart } from 'app/core/components/TimelineChart/TimelineChart'; import { prepareTimelineFields, prepareTimelineLegendItems, TimelineMode, } from 'app/core/components/TimelineChart/utils'; import { StateTimelineTooltip2 } from '../state-timeline/StateTimelineTooltip2'; import { AnnotationsPlugin2 } from '../timeseries/plugins/AnnotationsPlugin2'; import { OutsideRangePlugin } from '../timeseries/plugins/OutsideRangePlugin'; import { getTimezones, isTooltipScrollable } from '../timeseries/utils'; import { Options } from './panelcfg.gen'; interface TimelinePanelProps extends PanelProps {} /** * @alpha */ export const StatusHistoryPanel = ({ data, timeRange, timeZone, options, width, height, replaceVariables, onChangeTimeRange, }: TimelinePanelProps) => { const theme = useTheme2(); // temp range set for adding new annotation set by TooltipPlugin2, consumed by AnnotationPlugin2 const [newAnnotationRange, setNewAnnotationRange] = useState(null); const { sync, eventsScope, canAddAnnotations, dataLinkPostProcessor, eventBus } = usePanelContext(); const cursorSync = sync?.() ?? DashboardCursorSync.Off; const enableAnnotationCreation = Boolean(canAddAnnotations && canAddAnnotations()); const { frames, warn } = useMemo( () => prepareTimelineFields(data.series, false, timeRange, theme), [data.series, timeRange, theme] ); const legendItems = useMemo( () => prepareTimelineLegendItems(frames, options.legend, theme), [frames, options.legend, theme] ); const timezones = useMemo(() => getTimezones(options.timezone, timeZone), [options.timezone, timeZone]); if (!frames || warn) { return (

{warn ?? 'No data found in response'}

); } // Status grid requires some space between values if (frames[0].length > width / 2) { return (

Too many points to visualize properly.
Update the query to return fewer points.
({frames[0].length} points received)

); } return ( {(builder, alignedFrame) => { return ( <> {cursorSync !== DashboardCursorSync.Off && ( )} {options.tooltip.mode !== TooltipDisplayMode.None && ( { if (enableAnnotationCreation && timeRange2 != null) { setNewAnnotationRange(timeRange2); dismiss(); return; } const annotate = () => { let xVal = u.posToVal(u.cursor.left!, 'x'); setNewAnnotationRange({ from: xVal, to: xVal }); dismiss(); }; return ( ); }} maxWidth={options.tooltip.maxWidth} /> )} ); }} ); };