The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/plugins/panel/graph3/GraphPanel.tsx

83 lines
2.3 KiB

import React, { useMemo } from 'react';
import {
ContextMenuPlugin,
TooltipPlugin,
UPlotChart,
ZoomPlugin,
LegendPlugin,
Canvas,
LegendDisplayMode,
} from '@grafana/ui';
import { PanelProps } from '@grafana/data';
import { Options } from './types';
import { alignAndSortDataFramesByFieldName } from './utils';
import { VizLayout } from './VizLayout';
interface GraphPanelProps extends PanelProps<Options> {}
const TIME_FIELD_NAME = 'Time';
export const GraphPanel: React.FC<GraphPanelProps> = ({
data,
timeRange,
timeZone,
width,
height,
options,
onChangeTimeRange,
}) => {
const alignedData = useMemo(() => {
if (!data || !data.series?.length) {
return null;
}
return alignAndSortDataFramesByFieldName(data.series, TIME_FIELD_NAME);
}, [data]);
if (!alignedData) {
return (
<div className="panel-empty">
<p>No data found in response</p>
</div>
);
}
return (
<VizLayout width={width} height={height}>
{({ builder, getLayout }) => {
const layout = getLayout();
// when all layout slots are ready we can calculate the canvas(actual viz) size
const canvasSize = layout.isReady
? {
width: width - (layout.left.width + layout.right.width),
height: height - (layout.top.height + layout.bottom.height),
}
: { width: 0, height: 0 };
if (options.legend.isVisible) {
builder.addSlot(
options.legend.placement,
<LegendPlugin
placement={options.legend.placement}
displayMode={options.legend.asTable ? LegendDisplayMode.Table : LegendDisplayMode.List}
/>
);
} else {
builder.clearSlot(options.legend.placement);
}
return (
<UPlotChart data={alignedData} timeRange={timeRange} timeZone={timeZone} {...canvasSize}>
{builder.addSlot('canvas', <Canvas />).render()}
<TooltipPlugin mode={options.tooltipOptions.mode as any} timeZone={timeZone} />
<ZoomPlugin onZoom={onChangeTimeRange} />
<ContextMenuPlugin />
{/* TODO: */}
{/*<AnnotationsEditorPlugin />*/}
</UPlotChart>
);
}}
</VizLayout>
);
};