123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import React, { useMemo } from 'react';
- import { Field, PanelProps } from '@grafana/data';
- import { PanelDataErrorView } from '@grafana/runtime';
- import { TooltipDisplayMode } from '@grafana/schema';
- import { usePanelContext, TimeSeries, TooltipPlugin, ZoomPlugin, KeyboardPlugin } from '@grafana/ui';
- import { config } from 'app/core/config';
- import { getFieldLinksForExplore } from 'app/features/explore/utils/links';
- import { AnnotationEditorPlugin } from './plugins/AnnotationEditorPlugin';
- import { AnnotationsPlugin } from './plugins/AnnotationsPlugin';
- import { ContextMenuPlugin } from './plugins/ContextMenuPlugin';
- import { ExemplarsPlugin } from './plugins/ExemplarsPlugin';
- import { OutsideRangePlugin } from './plugins/OutsideRangePlugin';
- import { ThresholdControlsPlugin } from './plugins/ThresholdControlsPlugin';
- import { TimeSeriesOptions } from './types';
- import { prepareGraphableFields } from './utils';
- interface TimeSeriesPanelProps extends PanelProps<TimeSeriesOptions> {}
- export const TimeSeriesPanel: React.FC<TimeSeriesPanelProps> = ({
- data,
- timeRange,
- timeZone,
- width,
- height,
- options,
- fieldConfig,
- onChangeTimeRange,
- replaceVariables,
- id,
- }) => {
- const { sync, canAddAnnotations, onThresholdsChange, canEditThresholds, onSplitOpen } = usePanelContext();
- const getFieldLinks = (field: Field, rowIndex: number) => {
- return getFieldLinksForExplore({ field, rowIndex, splitOpenFn: onSplitOpen, range: timeRange });
- };
- const frames = useMemo(() => prepareGraphableFields(data.series, config.theme2, timeRange), [data, timeRange]);
- if (!frames) {
- return (
- <PanelDataErrorView
- panelId={id}
- fieldConfig={fieldConfig}
- data={data}
- needsTimeField={true}
- needsNumberField={true}
- />
- );
- }
- const enableAnnotationCreation = Boolean(canAddAnnotations && canAddAnnotations());
- return (
- <TimeSeries
- frames={frames}
- structureRev={data.structureRev}
- timeRange={timeRange}
- timeZone={timeZone}
- width={width}
- height={height}
- legend={options.legend}
- >
- {(config, alignedDataFrame) => {
- return (
- <>
- <KeyboardPlugin config={config} />
- <ZoomPlugin config={config} onZoom={onChangeTimeRange} />
- {options.tooltip.mode === TooltipDisplayMode.None || (
- <TooltipPlugin
- data={alignedDataFrame}
- config={config}
- mode={options.tooltip.mode}
- sortOrder={options.tooltip.sort}
- sync={sync}
- timeZone={timeZone}
- />
- )}
- {/* Renders annotation markers*/}
- {data.annotations && (
- <AnnotationsPlugin annotations={data.annotations} config={config} timeZone={timeZone} />
- )}
- {/* Enables annotations creation*/}
- {enableAnnotationCreation ? (
- <AnnotationEditorPlugin data={alignedDataFrame} timeZone={timeZone} config={config}>
- {({ startAnnotating }) => {
- return (
- <ContextMenuPlugin
- data={alignedDataFrame}
- config={config}
- timeZone={timeZone}
- replaceVariables={replaceVariables}
- defaultItems={[
- {
- items: [
- {
- label: 'Add annotation',
- ariaLabel: 'Add annotation',
- icon: 'comment-alt',
- onClick: (e, p) => {
- if (!p) {
- return;
- }
- startAnnotating({ coords: p.coords });
- },
- },
- ],
- },
- ]}
- />
- );
- }}
- </AnnotationEditorPlugin>
- ) : (
- <ContextMenuPlugin
- data={alignedDataFrame}
- config={config}
- timeZone={timeZone}
- replaceVariables={replaceVariables}
- defaultItems={[]}
- />
- )}
- {data.annotations && (
- <ExemplarsPlugin
- config={config}
- exemplars={data.annotations}
- timeZone={timeZone}
- getFieldLinks={getFieldLinks}
- />
- )}
- {canEditThresholds && onThresholdsChange && (
- <ThresholdControlsPlugin
- config={config}
- fieldConfig={fieldConfig}
- onThresholdsChange={onThresholdsChange}
- />
- )}
- <OutsideRangePlugin config={config} onChangeTimeRange={onChangeTimeRange} />
- </>
- );
- }}
- </TimeSeries>
- );
- };
|