PieChart: Add color changing options to pie chart (#31588)

* Allow changing of series color for PieChart

* Use useTheme hook

* Remove duplicate import
This commit is contained in:
Oscar Kilhed 2021-03-02 13:18:03 +01:00 committed by GitHub
parent 6a07a0fe93
commit b36314d03f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 29 deletions

View File

@ -186,6 +186,7 @@ export class PanelOptionsEditorBuilder<TOptions> extends OptionsUIRegistryBuilde
) {
return this.addCustomEditor({
...config,
defaultValue: config.defaultValue ?? [],
id: config.path,
editor: standardEditorsRegistry.get('multi-select').editor as any,
});

View File

@ -31,6 +31,7 @@ interface SvgProps {
pieType: PieChartType;
displayLabels?: PieChartLabels[];
useGradients?: boolean;
onSeriesColorChange?: (label: string, color: string) => void;
}
export interface Props extends SvgProps {
legendOptions?: PieChartLegendOptions;
@ -52,7 +53,14 @@ const defaultLegendOptions: PieChartLegendOptions = {
values: [PieChartLegendValues.Percent],
};
export const PieChart: FC<Props> = ({ values, legendOptions = defaultLegendOptions, width, height, ...restProps }) => {
export const PieChart: FC<Props> = ({
values,
legendOptions = defaultLegendOptions,
onSeriesColorChange,
width,
height,
...restProps
}) => {
const getLegend = (values: DisplayValue[], legendOptions: PieChartLegendOptions) => {
if (legendOptions.displayMode === LegendDisplayMode.Hidden) {
return undefined;
@ -65,7 +73,7 @@ export const PieChart: FC<Props> = ({ values, legendOptions = defaultLegendOptio
color: value.color ?? FALLBACK_COLOR,
yAxis: 1,
getDisplayValues: () => {
const valuesToShow = legendOptions.values;
const valuesToShow = legendOptions.values ?? [];
let displayValues = [];
if (valuesToShow.includes(PieChartLegendValues.Value)) {
@ -90,7 +98,12 @@ export const PieChart: FC<Props> = ({ values, legendOptions = defaultLegendOptio
});
return (
<VizLegend items={legendItems} placement={legendOptions.placement} displayMode={legendOptions.displayMode} />
<VizLegend
items={legendItems}
onSeriesColorChange={onSeriesColorChange}
placement={legendOptions.placement}
displayMode={legendOptions.displayMode}
/>
);
};

View File

@ -1,20 +1,33 @@
import React, { PureComponent } from 'react';
import { config } from 'app/core/config';
import { PieChart } from '@grafana/ui';
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 class PieChartPanel extends PureComponent<Props> {
render() {
const { width, height, options, data, replaceVariables, fieldConfig, timeZone } = this.props;
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: config.theme,
theme: useTheme(),
replaceVariables: replaceVariables,
timeZone,
}).map((v) => v.display);
@ -24,10 +37,10 @@ export class PieChartPanel extends PureComponent<Props> {
width={width}
height={height}
values={values}
onSeriesColorChange={onSeriesColorChange}
pieType={options.pieType}
displayLabels={options.displayLabels}
legendOptions={options.legend}
/>
);
}
}
};