grafana/public/app/plugins/panel/trend/TrendPanel.tsx
sam boyer 33fd83f7e3
kindsys: Adapt to new PanelCfg schema interface (#65297)
* kindsys: Adapt to new PanelCfg schema interface

* building locally

* Remove Panel prefix in cue files

* Regenerate

* Update imports

* fixup! Merge branch 'remove-panel-prefix' into sdboyer/redundant-panelcfg-prefixes

* Fix formatting

---------

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
Co-authored-by: Tania B <yalyna.ts@gmail.com>
2023-05-15 23:07:54 -04:00

130 lines
3.7 KiB
TypeScript

import React, { useMemo } from 'react';
import { FieldType, PanelProps } from '@grafana/data';
import { isLikelyAscendingVector } from '@grafana/data/src/transformations/transformers/joinDataFrames';
import { config, PanelDataErrorView } from '@grafana/runtime';
import { KeyboardPlugin, TimeSeries, TooltipDisplayMode, TooltipPlugin, usePanelContext } from '@grafana/ui';
import { findFieldIndex } from 'app/features/dimensions';
import { ContextMenuPlugin } from '../timeseries/plugins/ContextMenuPlugin';
import { prepareGraphableFields, regenerateLinksSupplier } from '../timeseries/utils';
import { Options } from './panelcfg.gen';
export const TrendPanel = ({
data,
timeRange,
timeZone,
width,
height,
options,
fieldConfig,
replaceVariables,
id,
}: PanelProps<Options>) => {
const { sync } = usePanelContext();
const info = useMemo(() => {
if (data.series.length > 1) {
return {
warning: 'Only one frame is supported, consider adding a join transformation',
frames: data.series,
};
}
let frames = data.series;
let xFieldIdx: number | undefined;
if (options.xField) {
xFieldIdx = findFieldIndex(frames[0], options.xField);
if (xFieldIdx == null) {
return {
warning: 'Unable to find field: ' + options.xField,
frames: data.series,
};
}
} else {
// first number field
// Perhaps we can/should support any ordinal rather than an error here
xFieldIdx = frames[0].fields.findIndex((f) => f.type === FieldType.number);
if (xFieldIdx === -1) {
return {
warning: 'No numeric fields found for X axis',
frames,
};
}
}
// Make sure values are ascending
if (xFieldIdx != null) {
const field = frames[0].fields[xFieldIdx];
if (field.type === FieldType.number && !isLikelyAscendingVector(field.values)) {
return {
warning: `Values must be in ascending order`,
frames,
};
}
}
return { frames: prepareGraphableFields(frames, config.theme2, undefined, xFieldIdx) };
}, [data.series, options.xField]);
if (info.warning || !info.frames) {
return (
<PanelDataErrorView
panelId={id}
fieldConfig={fieldConfig}
data={data}
message={info.warning}
needsNumberField={true}
/>
);
}
return (
<TimeSeries // Name change!
frames={info.frames}
structureRev={data.structureRev}
timeRange={timeRange}
timeZone={timeZone}
width={width}
height={height}
legend={options.legend}
options={options}
>
{(config, alignedDataFrame) => {
if (
alignedDataFrame.fields.filter((f) => f.config.links !== undefined && f.config.links.length > 0).length > 0
) {
alignedDataFrame = regenerateLinksSupplier(alignedDataFrame, info.frames!, replaceVariables, timeZone);
}
return (
<>
<KeyboardPlugin config={config} />
{options.tooltip.mode === TooltipDisplayMode.None || (
<TooltipPlugin
frames={info.frames!}
data={alignedDataFrame}
config={config}
mode={options.tooltip.mode}
sortOrder={options.tooltip.sort}
sync={sync}
timeZone={timeZone}
/>
)}
<ContextMenuPlugin
data={alignedDataFrame}
frames={info.frames!}
config={config}
timeZone={timeZone}
replaceVariables={replaceVariables}
defaultItems={[]}
/>
</>
);
}}
</TimeSeries>
);
};