mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
6a07a0fe93
commit
b36314d03f
@ -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,
|
||||
});
|
||||
|
@ -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}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -1,33 +1,46 @@
|
||||
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,
|
||||
replaceVariables: replaceVariables,
|
||||
timeZone,
|
||||
}).map((v) => v.display);
|
||||
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}
|
||||
pieType={options.pieType}
|
||||
displayLabels={options.displayLabels}
|
||||
legendOptions={options.legend}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<PieChart
|
||||
width={width}
|
||||
height={height}
|
||||
values={values}
|
||||
onSeriesColorChange={onSeriesColorChange}
|
||||
pieType={options.pieType}
|
||||
displayLabels={options.displayLabels}
|
||||
legendOptions={options.legend}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user