PieChart: Update text color and minor changes (#31546)

This commit is contained in:
Torkel Ödegaard 2021-03-01 14:08:48 +01:00 committed by GitHub
parent 5dc000d6b0
commit fcac61203e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 63 deletions

View File

@ -55,14 +55,9 @@
"options": { "options": {
"displayLabels": [ "displayLabels": [
"name" "name"
], ],
"labelOptions": {
"showName": true,
"showPercent": false,
"showValue": false
},
"legend": { "legend": {
"displayColumns": [], "values": [],
"displayMode": "list", "displayMode": "list",
"placement": "right" "placement": "right"
}, },
@ -130,14 +125,9 @@
"displayLabels": [ "displayLabels": [
"name", "name",
"percent" "percent"
], ],
"labelOptions": {
"showName": true,
"showPercent": true,
"showValue": false
},
"legend": { "legend": {
"displayColumns": [ "values": [
"percent" "percent"
], ],
"displayMode": "list", "displayMode": "list",
@ -204,14 +194,9 @@
"options": { "options": {
"displayLabels": [ "displayLabels": [
"name" "name"
], ],
"labelOptions": {
"showName": true,
"showPercent": false,
"showValue": false
},
"legend": { "legend": {
"displayColumns": [], "values": [],
"displayMode": "hidden", "displayMode": "hidden",
"placement": "right" "placement": "right"
}, },
@ -276,14 +261,9 @@
"options": { "options": {
"displayLabels": [ "displayLabels": [
"percent" "percent"
], ],
"labelOptions": {
"showName": false,
"showPercent": true,
"showValue": false
},
"legend": { "legend": {
"displayColumns": [ "values": [
"percent" "percent"
], ],
"displayMode": "table", "displayMode": "table",
@ -350,14 +330,9 @@
"options": { "options": {
"displayLabels": [ "displayLabels": [
"value" "value"
], ],
"labelOptions": {
"showName": false,
"showPercent": false,
"showValue": true
},
"legend": { "legend": {
"displayColumns": [ "values": [
"value" "value"
], ],
"displayMode": "table", "displayMode": "table",
@ -424,14 +399,9 @@
"options": { "options": {
"displayLabels": [ "displayLabels": [
"name" "name"
], ],
"labelOptions": {
"showName": true,
"showPercent": false,
"showValue": false
},
"legend": { "legend": {
"displayColumns": [], "values": [],
"displayMode": "list", "displayMode": "list",
"placement": "bottom" "placement": "bottom"
}, },
@ -494,14 +464,9 @@
}, },
"id": 10, "id": 10,
"options": { "options": {
"displayLabels": [], "displayLabels": [],
"labelOptions": {
"showName": true,
"showPercent": false,
"showValue": false
},
"legend": { "legend": {
"displayColumns": ["percent", "value"], "values": ["percent", "value"],
"displayMode": "table", "displayMode": "table",
"placement": "bottom" "placement": "bottom"
}, },

View File

@ -19,7 +19,7 @@ export enum PieChartLabels {
Percent = 'percent', Percent = 'percent',
} }
export enum LegendColumns { export enum PieChartLegendValues {
Value = 'value', Value = 'value',
Percent = 'percent', Percent = 'percent',
} }
@ -42,14 +42,14 @@ export enum PieChartType {
} }
export interface PieChartLegendOptions extends VizLegendOptions { export interface PieChartLegendOptions extends VizLegendOptions {
displayColumns: LegendColumns[]; values: PieChartLegendValues[];
} }
const defaultLegendOptions: PieChartLegendOptions = { const defaultLegendOptions: PieChartLegendOptions = {
displayMode: LegendDisplayMode.List, displayMode: LegendDisplayMode.List,
placement: 'right', placement: 'right',
calcs: [], calcs: [],
displayColumns: [LegendColumns.Percent], values: [PieChartLegendValues.Percent],
}; };
export const PieChart: FC<Props> = ({ values, legendOptions = defaultLegendOptions, width, height, ...restProps }) => { export const PieChart: FC<Props> = ({ values, legendOptions = defaultLegendOptions, width, height, ...restProps }) => {
@ -65,20 +65,22 @@ export const PieChart: FC<Props> = ({ values, legendOptions = defaultLegendOptio
color: value.color ?? FALLBACK_COLOR, color: value.color ?? FALLBACK_COLOR,
yAxis: 1, yAxis: 1,
getDisplayValues: () => { getDisplayValues: () => {
const valuesToShow = legendOptions.values;
let displayValues = []; let displayValues = [];
if (legendOptions.displayColumns.includes(LegendColumns.Value)) { if (valuesToShow.includes(PieChartLegendValues.Value)) {
displayValues.push({ numeric: value.numeric, text: formattedValueToString(value), title: 'Value' }); displayValues.push({ numeric: value.numeric, text: formattedValueToString(value), title: 'Value' });
} }
if (legendOptions.displayColumns.includes(LegendColumns.Percent)) { if (valuesToShow.includes(PieChartLegendValues.Percent)) {
const fractionOfTotal = value.numeric / total; const fractionOfTotal = value.numeric / total;
const percentOfTotal = fractionOfTotal * 100; const percentOfTotal = fractionOfTotal * 100;
displayValues.push({ displayValues.push({
numeric: fractionOfTotal, numeric: fractionOfTotal,
percent: percentOfTotal, percent: percentOfTotal,
text: percentOfTotal.toFixed(0) + '%', text: percentOfTotal.toFixed(0) + '%',
title: 'Percent', title: valuesToShow.length > 1 ? 'Percent' : undefined,
}); });
} }
@ -192,6 +194,7 @@ export const PieChartSvg: FC<SvgProps> = ({
innerRadius={layout.innerRadius} innerRadius={layout.innerRadius}
displayLabels={displayLabels} displayLabels={displayLabels}
total={total} total={total}
color={theme.colors.text}
/> />
)} )}
</g> </g>
@ -216,7 +219,8 @@ const PieLabel: FC<{
innerRadius: number; innerRadius: number;
displayLabels: PieChartLabels[]; displayLabels: PieChartLabels[];
total: number; total: number;
}> = ({ arc, outerRadius, innerRadius, displayLabels, total }) => { color: string;
}> = ({ arc, outerRadius, innerRadius, displayLabels, total, color }) => {
const labelRadius = innerRadius === 0 ? outerRadius / 6 : innerRadius; const labelRadius = innerRadius === 0 ? outerRadius / 6 : innerRadius;
const [labelX, labelY] = getLabelPos(arc, outerRadius, labelRadius); const [labelX, labelY] = getLabelPos(arc, outerRadius, labelRadius);
const hasSpaceForLabel = arc.endAngle - arc.startAngle >= 0.3; const hasSpaceForLabel = arc.endAngle - arc.startAngle >= 0.3;
@ -232,7 +236,7 @@ const PieLabel: FC<{
return ( return (
<g> <g>
<text <text
fill="white" fill={color}
x={labelX} x={labelX}
y={labelY} y={labelY}
dy=".33em" dy=".33em"

View File

@ -17,7 +17,13 @@ export { LoadingPlaceholder, LoadingPlaceholderProps } from './LoadingPlaceholde
export { ColorPicker, SeriesColorPicker } from './ColorPicker/ColorPicker'; export { ColorPicker, SeriesColorPicker } from './ColorPicker/ColorPicker';
export { SeriesColorPickerPopover, SeriesColorPickerPopoverWithTheme } from './ColorPicker/SeriesColorPickerPopover'; export { SeriesColorPickerPopover, SeriesColorPickerPopoverWithTheme } from './ColorPicker/SeriesColorPickerPopover';
export { EmptySearchResult } from './EmptySearchResult/EmptySearchResult'; export { EmptySearchResult } from './EmptySearchResult/EmptySearchResult';
export { PieChart, PieChartType, PieChartLabels, PieChartLegendOptions } from './PieChart/PieChart'; export {
PieChart,
PieChartType,
PieChartLabels,
PieChartLegendOptions,
PieChartLegendValues,
} from './PieChart/PieChart';
export { UnitPicker } from './UnitPicker/UnitPicker'; export { UnitPicker } from './UnitPicker/UnitPicker';
export { StatsPicker } from './StatsPicker/StatsPicker'; export { StatsPicker } from './StatsPicker/StatsPicker';
export { RefreshPicker, defaultIntervals } from './RefreshPicker/RefreshPicker'; export { RefreshPicker, defaultIntervals } from './RefreshPicker/RefreshPicker';

View File

@ -2,8 +2,7 @@ import { FieldColorModeId, FieldConfigProperty, PanelPlugin } from '@grafana/dat
import { PieChartPanel } from './PieChartPanel'; import { PieChartPanel } from './PieChartPanel';
import { PieChartOptions } from './types'; import { PieChartOptions } from './types';
import { addStandardDataReduceOptions } from '../stat/types'; import { addStandardDataReduceOptions } from '../stat/types';
import { LegendDisplayMode, PieChartType } from '@grafana/ui'; import { LegendDisplayMode, PieChartType, PieChartLabels, PieChartLegendValues } from '@grafana/ui';
import { LegendColumns, PieChartLabels } from '@grafana/ui/src/components/PieChart/PieChart';
export const plugin = new PanelPlugin<PieChartOptions>(PieChartPanel) export const plugin = new PanelPlugin<PieChartOptions>(PieChartPanel)
.useFieldConfig({ .useFieldConfig({
@ -76,11 +75,11 @@ export const plugin = new PanelPlugin<PieChartOptions>(PieChartPanel)
}) })
.addMultiSelect({ .addMultiSelect({
name: 'Legend values', name: 'Legend values',
path: 'legend.displayColumns', path: 'legend.values',
settings: { settings: {
options: [ options: [
{ value: LegendColumns.Percent, label: 'Percent' }, { value: PieChartLegendValues.Percent, label: 'Percent' },
{ value: LegendColumns.Value, label: 'Value' }, { value: PieChartLegendValues.Value, label: 'Value' },
], ],
}, },
showIf: (c) => c.legend.displayMode !== LegendDisplayMode.Hidden, showIf: (c) => c.legend.displayMode !== LegendDisplayMode.Hidden,