import React, { useCallback, useMemo } from 'react'; import { FieldType, PanelProps, VizOrientation } from '@grafana/data'; import { BarChart, BarChartOptions, GraphNGLegendEvent } from '@grafana/ui'; import { changeSeriesColorConfigFactory } from '../timeseries/overrides/colorSeriesConfigFactory'; import { hideSeriesConfigFactory } from '../timeseries/overrides/hideSeriesConfigFactory'; interface Props extends PanelProps {} /** * @alpha */ export const BarChartPanel: React.FunctionComponent = ({ 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] ); const onSeriesColorChange = useCallback( (label: string, color: string) => { onFieldConfigChange(changeSeriesColorConfigFactory(label, color, fieldConfig)); }, [fieldConfig, onFieldConfigChange] ); if (!data || !data.series?.length) { return (

No data found in response

); } const firstFrame = data.series[0]; if (!firstFrame.fields.some((f) => f.type === FieldType.string)) { return (

Bar charts requires a string field

); } if (!firstFrame.fields.some((f) => f.type === FieldType.number)) { return (

No numeric fields found

); } return ( ); };