mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
docs(piechart): refactor story to use controls (#33696)
This commit is contained in:
parent
43fd829565
commit
907d7b71dc
@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { select, number, boolean } from '@storybook/addon-knobs';
|
import { Meta, Story } from '@storybook/react';
|
||||||
import { PieChart, PieChartType, TooltipDisplayMode } from '@grafana/ui';
|
import { PieChart, PieChartProps, PieChartType, TooltipDisplayMode } from '@grafana/ui';
|
||||||
import { withCenteredStory } from '../../utils/storybook/withCenteredStory';
|
import { withCenteredStory } from '../../utils/storybook/withCenteredStory';
|
||||||
import {
|
import {
|
||||||
FieldColorModeId,
|
FieldColorModeId,
|
||||||
@ -11,12 +11,8 @@ import {
|
|||||||
ThresholdsMode,
|
ThresholdsMode,
|
||||||
toDataFrame,
|
toDataFrame,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
|
import { LegendDisplayMode, LegendPlacement } from '../VizLegend/models.gen';
|
||||||
export default {
|
import { PieChartLabels, PieChartLegendValues } from './types';
|
||||||
title: 'Visualizations/PieChart',
|
|
||||||
decorators: [withCenteredStory],
|
|
||||||
component: PieChart,
|
|
||||||
};
|
|
||||||
|
|
||||||
const fieldConfig: FieldConfigSource = {
|
const fieldConfig: FieldConfigSource = {
|
||||||
defaults: {
|
defaults: {
|
||||||
@ -30,66 +26,292 @@ const fieldConfig: FieldConfigSource = {
|
|||||||
},
|
},
|
||||||
overrides: [],
|
overrides: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
const reduceOptions: ReduceDataOptions = { calcs: [] };
|
const reduceOptions: ReduceDataOptions = { calcs: [] };
|
||||||
const replaceVariables: InterpolateFunction = (v) => v;
|
const replaceVariables: InterpolateFunction = (v) => v;
|
||||||
const datapoints = [
|
const data = [
|
||||||
toDataFrame({
|
{
|
||||||
fields: [
|
fields: [
|
||||||
{ name: 'time', type: FieldType.time, values: [1618197346845, 1618197346845] },
|
{
|
||||||
{ name: 'Living room', type: FieldType.number, values: [19, 21] },
|
name: 'time',
|
||||||
|
type: FieldType.time,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [1620110299324, 1620110329324],
|
||||||
|
state: {
|
||||||
|
displayName: null,
|
||||||
|
seriesIndex: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Cellar',
|
||||||
|
type: FieldType.number,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [27.008127592438484, 27.287423219155304],
|
||||||
|
state: {
|
||||||
|
displayName: 'Cellar',
|
||||||
|
seriesIndex: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}),
|
},
|
||||||
toDataFrame({
|
{
|
||||||
fields: [
|
fields: [
|
||||||
{ name: 'time', type: FieldType.time, values: [1618197346845, 1618197346845] },
|
{
|
||||||
{ name: 'Cellar', type: FieldType.number, values: [5, 6] },
|
name: 'time',
|
||||||
|
type: FieldType.time,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [1620110299324, 1620110329324],
|
||||||
|
state: {
|
||||||
|
displayName: null,
|
||||||
|
seriesIndex: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Living room',
|
||||||
|
type: FieldType.number,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [65.97347908625065, 65.6588311671438],
|
||||||
|
state: {
|
||||||
|
displayName: 'Living room',
|
||||||
|
seriesIndex: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}),
|
},
|
||||||
|
{
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'time',
|
||||||
|
type: FieldType.time,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [1620110299324, 1620110329324],
|
||||||
|
state: {
|
||||||
|
displayName: null,
|
||||||
|
seriesIndex: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Porch',
|
||||||
|
type: FieldType.number,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [42.01219662436388, 42.48471312850685],
|
||||||
|
state: {
|
||||||
|
displayName: 'Porch',
|
||||||
|
seriesIndex: 3,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'time',
|
||||||
|
type: FieldType.time,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [1620110299324, 1620110329324],
|
||||||
|
state: {
|
||||||
|
displayName: null,
|
||||||
|
seriesIndex: 3,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Bedroom',
|
||||||
|
type: FieldType.number,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [34.28143812581964, 34.37741979130198],
|
||||||
|
state: {
|
||||||
|
displayName: 'Bedroom',
|
||||||
|
seriesIndex: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'time',
|
||||||
|
type: FieldType.time,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
mappings: [],
|
||||||
|
},
|
||||||
|
values: [1620110299324, 1620110329324],
|
||||||
|
state: {
|
||||||
|
displayName: null,
|
||||||
|
seriesIndex: 4,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Guest room',
|
||||||
|
type: FieldType.number,
|
||||||
|
config: {
|
||||||
|
color: {
|
||||||
|
mode: 'palette-classic',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
values: [57.855438763786104, 57.521663794462654],
|
||||||
|
state: {
|
||||||
|
displayName: 'Guest room',
|
||||||
|
seriesIndex: 5,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const getKnobs = () => {
|
export default {
|
||||||
return {
|
title: 'Visualizations/PieChart',
|
||||||
width: number('Width', 500),
|
decorators: [withCenteredStory],
|
||||||
height: number('Height', 500),
|
component: PieChart,
|
||||||
pieType: select('pieType', Object.values(PieChartType), PieChartType.Pie),
|
args: {
|
||||||
showLabelName: boolean('Label.showName', true),
|
width: 500,
|
||||||
showLabelValue: boolean('Label.showValue', false),
|
height: 500,
|
||||||
showLabelPercent: boolean('Label.showPercent', false),
|
fieldConfig,
|
||||||
tooltipMode: select('Tooltip mode', Object.values(TooltipDisplayMode), TooltipDisplayMode.Single),
|
data,
|
||||||
|
},
|
||||||
|
argTypes: {
|
||||||
|
tooltipMode: {
|
||||||
|
control: {
|
||||||
|
type: 'select',
|
||||||
|
options: Object.values(TooltipDisplayMode),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
legendDisplayMode: {
|
||||||
|
control: {
|
||||||
|
type: 'select',
|
||||||
|
options: Object.values(LegendDisplayMode),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
legendPlacement: {
|
||||||
|
control: {
|
||||||
|
type: 'select',
|
||||||
|
options: ['bottom', 'right'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
legendValues: {
|
||||||
|
control: {
|
||||||
|
type: 'check',
|
||||||
|
options: Object.values(PieChartLegendValues),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
displayLabels: {
|
||||||
|
control: {
|
||||||
|
type: 'check',
|
||||||
|
options: Object.values(PieChartLabels),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
height: {
|
||||||
|
control: { type: 'range', min: 300, max: 1000, step: 50 },
|
||||||
|
},
|
||||||
|
width: {
|
||||||
|
control: { type: 'range', min: 300, max: 1000, step: 50 },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
parameters: {
|
||||||
|
controls: {
|
||||||
|
exclude: [
|
||||||
|
'reduceOptions',
|
||||||
|
'replaceVariables',
|
||||||
|
'tooltipOptions',
|
||||||
|
'onSeriesColorChange',
|
||||||
|
'legendOptions',
|
||||||
|
'timeZone',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as Meta;
|
||||||
|
|
||||||
|
interface PieChartStoryProps extends PieChartProps {
|
||||||
|
tooltipMode: TooltipDisplayMode;
|
||||||
|
legendDisplayMode: LegendDisplayMode;
|
||||||
|
legendPlacement: LegendPlacement;
|
||||||
|
legendValues: PieChartLegendValues[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const Template: Story<PieChartStoryProps> = ({
|
||||||
|
tooltipMode,
|
||||||
|
legendDisplayMode,
|
||||||
|
legendPlacement,
|
||||||
|
legendValues,
|
||||||
|
data,
|
||||||
|
...args
|
||||||
|
}) => {
|
||||||
|
const tooltipOpts = {
|
||||||
|
mode: tooltipMode,
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
export const basic = () => {
|
const legendOptions = {
|
||||||
const { pieType, width, height, tooltipMode } = getKnobs();
|
displayMode: legendDisplayMode,
|
||||||
|
placement: legendPlacement,
|
||||||
|
values: legendValues,
|
||||||
|
calcs: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const dataPoints = data.map((d) => toDataFrame(d));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PieChart
|
<PieChart
|
||||||
width={width}
|
{...args}
|
||||||
height={height}
|
data={dataPoints}
|
||||||
|
tooltipOptions={tooltipOpts}
|
||||||
|
legendOptions={legendOptions}
|
||||||
replaceVariables={replaceVariables}
|
replaceVariables={replaceVariables}
|
||||||
reduceOptions={reduceOptions}
|
reduceOptions={reduceOptions}
|
||||||
fieldConfig={fieldConfig}
|
|
||||||
data={datapoints}
|
|
||||||
pieType={pieType}
|
|
||||||
tooltipOptions={{ mode: tooltipMode }}
|
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const donut = () => {
|
export const basic = Template.bind({});
|
||||||
const { width, height, tooltipMode } = getKnobs();
|
|
||||||
|
|
||||||
return (
|
basic.args = {
|
||||||
<PieChart
|
pieType: PieChartType.Pie,
|
||||||
width={width}
|
tooltipMode: TooltipDisplayMode.Single,
|
||||||
height={height}
|
legendDisplayMode: LegendDisplayMode.List,
|
||||||
replaceVariables={replaceVariables}
|
legendPlacement: 'bottom',
|
||||||
reduceOptions={reduceOptions}
|
legendValues: [PieChartLegendValues.Value],
|
||||||
fieldConfig={fieldConfig}
|
displayLabels: [PieChartLabels.Name],
|
||||||
data={datapoints}
|
};
|
||||||
pieType={PieChartType.Donut}
|
|
||||||
tooltipOptions={{ mode: tooltipMode }}
|
export const donut = Template.bind({});
|
||||||
/>
|
|
||||||
);
|
donut.args = {
|
||||||
|
pieType: PieChartType.Donut,
|
||||||
|
tooltipMode: TooltipDisplayMode.Single,
|
||||||
|
legendDisplayMode: LegendDisplayMode.List,
|
||||||
|
legendPlacement: 'bottom',
|
||||||
|
legendValues: [PieChartLegendValues.Value],
|
||||||
|
displayLabels: [PieChartLabels.Name],
|
||||||
};
|
};
|
||||||
|
@ -19,7 +19,13 @@ export { ColorValueEditor, ColorValueEditorProps } from './OptionsUI/color';
|
|||||||
export { SeriesColorPickerPopover, SeriesColorPickerPopoverWithTheme } from './ColorPicker/SeriesColorPickerPopover';
|
export { SeriesColorPickerPopover, SeriesColorPickerPopoverWithTheme } from './ColorPicker/SeriesColorPickerPopover';
|
||||||
export { EmptySearchResult } from './EmptySearchResult/EmptySearchResult';
|
export { EmptySearchResult } from './EmptySearchResult/EmptySearchResult';
|
||||||
export { PieChart } from './PieChart/PieChart';
|
export { PieChart } from './PieChart/PieChart';
|
||||||
export { PieChartType, PieChartLabels, PieChartLegendOptions, PieChartLegendValues } from './PieChart/types';
|
export {
|
||||||
|
PieChartType,
|
||||||
|
PieChartProps,
|
||||||
|
PieChartLabels,
|
||||||
|
PieChartLegendOptions,
|
||||||
|
PieChartLegendValues,
|
||||||
|
} from './PieChart/types';
|
||||||
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';
|
||||||
|
Loading…
Reference in New Issue
Block a user