grafana/public/app/plugins/panel/piechart/PieChartPanel.tsx
Oscar Kilhed b36314d03f
PieChart: Add color changing options to pie chart (#31588)
* Allow changing of series color for PieChart

* Use useTheme hook

* Remove duplicate import
2021-03-02 13:18:03 +01:00

47 lines
1.2 KiB
TypeScript

import React, { useCallback } from 'react';
import { PieChart, useTheme } from '@grafana/ui';
import { PieChartOptions } from './types';
import { getFieldDisplayValues, PanelProps } from '@grafana/data';
import { changeSeriesColorConfigFactory } from '../timeseries/overrides/colorSeriesConfigFactory';
interface Props extends PanelProps<PieChartOptions> {}
export const PieChartPanel: React.FC<Props> = ({
width,
height,
options,
data,
onFieldConfigChange,
replaceVariables,
fieldConfig,
timeZone,
}) => {
const onSeriesColorChange = useCallback(
(label: string, color: string) => {
onFieldConfigChange(changeSeriesColorConfigFactory(label, color, fieldConfig));
},
[fieldConfig, onFieldConfigChange]
);
const values = getFieldDisplayValues({
fieldConfig,
reduceOptions: options.reduceOptions,
data: data.series,
theme: useTheme(),
replaceVariables: replaceVariables,
timeZone,
}).map((v) => v.display);
return (
<PieChart
width={width}
height={height}
values={values}
onSeriesColorChange={onSeriesColorChange}
pieType={options.pieType}
displayLabels={options.displayLabels}
legendOptions={options.legend}
/>
);
};