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/barchart/BarChartPanel.tsx

69 lines
1.8 KiB

import React, { useCallback, useMemo } from 'react';
import { FieldType, PanelProps, VizOrientation } from '@grafana/data';
import { BarChart, BarChartOptions, GraphNGLegendEvent } from '@grafana/ui';
import { hideSeriesConfigFactory } from '../timeseries/overrides/hideSeriesConfigFactory';
interface Props extends PanelProps<BarChartOptions> {}
/**
* @alpha
*/
export const BarChartPanel: React.FunctionComponent<Props> = ({
data,
options,
width,
height,
fieldConfig,
onFieldConfigChange,
}) => {
const orientation = useMemo(() => {
if (!options.orientation || options.orientation === VizOrientation.Auto) {
return width < height ? VizOrientation.Horizontal : VizOrientation.Vertical;
}
return options.orientation;
}, [width, height, options.orientation]);
const onLegendClick = useCallback(
(event: GraphNGLegendEvent) => {
onFieldConfigChange(hideSeriesConfigFactory(event, fieldConfig, data.series));
},
[fieldConfig, onFieldConfigChange, data.series]
);
if (!data || !data.series?.length) {
return (
<div className="panel-empty">
<p>No data found in response</p>
</div>
);
}
const firstFrame = data.series[0];
if (!firstFrame.fields.some((f) => f.type === FieldType.string)) {
return (
<div className="panel-empty">
<p>Bar charts requires a string field</p>
</div>
);
}
if (!firstFrame.fields.some((f) => f.type === FieldType.number)) {
return (
<div className="panel-empty">
<p>No numeric fields found</p>
</div>
);
}
return (
<BarChart
data={data.series}
structureRev={data.structureRev}
width={width}
height={height}
onLegendClick={onLegendClick}
{...options}
orientation={orientation}
/>
);
};