From 2e079659f89f5a432029a68bbdc339012115b4f8 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 22 Mar 2019 15:16:04 -0700 Subject: [PATCH] use display value in pie chart --- .../src/components/PieChart/PieChart.tsx | 31 ++++++----- packages/grafana-ui/src/components/index.ts | 2 +- .../plugins/panel/piechart/PieChartPanel.tsx | 52 ++++++------------ .../panel/piechart/PieChartPanelEditor.tsx | 19 +++++-- .../panel/piechart/PieChartValueEditor.tsx | 54 ------------------- public/app/plugins/panel/piechart/module.tsx | 2 + public/app/plugins/panel/piechart/types.ts | 18 +++---- 7 files changed, 56 insertions(+), 122 deletions(-) delete mode 100644 public/app/plugins/panel/piechart/PieChartValueEditor.tsx diff --git a/packages/grafana-ui/src/components/PieChart/PieChart.tsx b/packages/grafana-ui/src/components/PieChart/PieChart.tsx index 3310a967264..f23349b7746 100644 --- a/packages/grafana-ui/src/components/PieChart/PieChart.tsx +++ b/packages/grafana-ui/src/components/PieChart/PieChart.tsx @@ -2,26 +2,20 @@ import React, { PureComponent } from 'react'; import { select, pie, arc, event } from 'd3'; import { sum } from 'lodash'; -import { GrafanaThemeType } from '../../types'; +import { GrafanaThemeType, DisplayValue } from '../../types'; import { Themeable } from '../../index'; +import { colors as grafana_colors } from '../../utils/index'; export enum PieChartType { PIE = 'pie', DONUT = 'donut', } -export interface PieChartDataPoint { - value: number; - name: string; - color: string; -} - export interface Props extends Themeable { height: number; width: number; - datapoints: PieChartDataPoint[]; + values: DisplayValue[]; - unit: string; pieType: PieChartType; strokeWidth: number; } @@ -49,15 +43,20 @@ export class PieChart extends PureComponent { } draw() { - const { datapoints, pieType, strokeWidth } = this.props; + const { values, pieType, strokeWidth } = this.props; - if (datapoints.length === 0) { + if (values.length === 0) { return; } - const data = datapoints.map(datapoint => datapoint.value); - const names = datapoints.map(datapoint => datapoint.name); - const colors = datapoints.map(datapoint => datapoint.color); + const data = values.map(datapoint => datapoint.numeric); + const names = values.map(datapoint => datapoint.text); + const colors = values.map((p, idx) => { + if (p.color) { + return p.color; + } + return grafana_colors[idx % grafana_colors.length]; + }); const total = sum(data) || 1; const percents = data.map((item: number) => (item / total) * 100); @@ -108,9 +107,9 @@ export class PieChart extends PureComponent { } render() { - const { height, width, datapoints } = this.props; + const { height, width, values } = this.props; - if (datapoints.length > 0) { + if (values.length > 0) { return (
{} export class PieChartPanel extends PureComponent { render() { - const { data, width, height, options } = this.props; - const { valueOptions } = options; + const { width, height, options } = this.props; - const datapoints: PieChartDataPoint[] = []; - if (data) { - const vmSeries = processTimeSeries({ - data, - nullValueMode: NullValueMode.Null, - }); - - for (let i = 0; i < vmSeries.length; i++) { - const serie = vmSeries[i]; - if (serie) { - datapoints.push({ - value: serie.stats[valueOptions.stat], - name: serie.label, - color: serie.color, - }); - } - } - } - // TODO: support table data + // TODO -- only process when the data/config changes + const values = getSingleStatValues(this.props); return ( - - {theme => ( - - )} - + ); } } diff --git a/public/app/plugins/panel/piechart/PieChartPanelEditor.tsx b/public/app/plugins/panel/piechart/PieChartPanelEditor.tsx index 66745c6eb43..7a8aae8b7c9 100644 --- a/public/app/plugins/panel/piechart/PieChartPanelEditor.tsx +++ b/public/app/plugins/panel/piechart/PieChartPanelEditor.tsx @@ -1,12 +1,19 @@ import React, { PureComponent } from 'react'; -import { PanelEditorProps, PanelOptionsGrid } from '@grafana/ui'; +import { PanelEditorProps, PanelOptionsGrid, ValueMappingsEditor, ValueMapping } from '@grafana/ui'; -import PieChartValueEditor from './PieChartValueEditor'; import { PieChartOptionsBox } from './PieChartOptionsBox'; -import { PieChartOptions, PieChartValueOptions } from './types'; +import { PieChartOptions } from './types'; +import { SingleStatValueEditor } from '../singlestat2/SingleStatValueEditor'; +import { SingleStatValueOptions } from '../singlestat2/types'; export default class PieChartPanelEditor extends PureComponent> { - onValueOptionsChanged = (valueOptions: PieChartValueOptions) => + onValueMappingsChanged = (valueMappings: ValueMapping[]) => + this.props.onOptionsChange({ + ...this.props.options, + valueMappings, + }); + + onValueOptionsChanged = (valueOptions: SingleStatValueOptions) => this.props.onOptionsChange({ ...this.props.options, valueOptions, @@ -18,9 +25,11 @@ export default class PieChartPanelEditor extends PureComponent - + + + ); } diff --git a/public/app/plugins/panel/piechart/PieChartValueEditor.tsx b/public/app/plugins/panel/piechart/PieChartValueEditor.tsx deleted file mode 100644 index 19d035d13f9..00000000000 --- a/public/app/plugins/panel/piechart/PieChartValueEditor.tsx +++ /dev/null @@ -1,54 +0,0 @@ -import React, { PureComponent } from 'react'; -import { FormLabel, PanelOptionsGroup, Select, UnitPicker } from '@grafana/ui'; -import { PieChartValueOptions } from './types'; - -const statOptions = [ - { value: 'min', label: 'Min' }, - { value: 'max', label: 'Max' }, - { value: 'avg', label: 'Average' }, - { value: 'current', label: 'Current' }, - { value: 'total', label: 'Total' }, -]; - -const labelWidth = 6; - -export interface Props { - options: PieChartValueOptions; - onChange: (valueOptions: PieChartValueOptions) => void; -} - -export default class PieChartValueEditor extends PureComponent { - onUnitChange = unit => - this.props.onChange({ - ...this.props.options, - unit: unit.value, - }); - - onStatChange = stat => - this.props.onChange({ - ...this.props.options, - stat: stat.value, - }); - - render() { - const { stat, unit } = this.props.options; - - return ( - -
- Unit - -
-
- Value -