Files
grafana/packages/grafana-ui/src/components/Graph/GraphWithLegend.story.tsx
Torkel Ödegaard 5b022a1c75 Legends: Refactoring and rewrites of legend components to simplify components & reuse (#30165)
* Legends: Refactoring and rewrites of legend components to simplify components & reuse

* Removed onSeriesAxisToggle

* More removal of onSeriesAxisToggle and storybook improvements

* Added story with legend values

* Move table legend styles from inline to defined in stylesFactory

* Update styles

* Change to circle

* Updated style to fat line

* Rename to VizLegend

* More renamed and fixes / polish

* Removed imports

* Minor change

* Updates

* Updates
2021-01-12 15:58:46 +01:00

143 lines
3.2 KiB
TypeScript

import React from 'react';
import { select, text } from '@storybook/addon-knobs';
import { withCenteredStory } from '../../utils/storybook/withCenteredStory';
import { GraphWithLegend, GraphWithLegendProps } from './GraphWithLegend';
import { LegendPlacement, LegendDisplayMode } from '../VizLegend/types';
import { GraphSeriesXY, FieldType, ArrayVector, dateTime, FieldColorModeId } from '@grafana/data';
export default {
title: 'Visualizations/Graph/GraphWithLegend',
component: GraphWithLegend,
decorator: [withCenteredStory],
};
const series: GraphSeriesXY[] = [
{
data: [
[1546372800000, 10],
[1546376400000, 20],
[1546380000000, 10],
],
color: 'red',
isVisible: true,
label: 'A-series',
seriesIndex: 0,
timeField: {
type: FieldType.time,
name: 'time',
values: new ArrayVector([1546372800000, 1546376400000, 1546380000000]),
config: {},
},
valueField: {
type: FieldType.number,
name: 'a-series',
values: new ArrayVector([10, 20, 10]),
config: {
color: {
mode: FieldColorModeId.Fixed,
fixedColor: 'red',
},
},
},
timeStep: 3600000,
yAxis: {
index: 1,
},
},
{
data: [
[1546372800000, 20],
[1546376400000, 30],
[1546380000000, 40],
],
color: 'blue',
isVisible: true,
label: 'B-series',
seriesIndex: 1,
timeField: {
type: FieldType.time,
name: 'time',
values: new ArrayVector([1546372800000, 1546376400000, 1546380000000]),
config: {},
},
valueField: {
type: FieldType.number,
name: 'b-series',
values: new ArrayVector([20, 30, 40]),
config: {
color: {
mode: FieldColorModeId.Fixed,
fixedColor: 'blue',
},
},
},
timeStep: 3600000,
yAxis: {
index: 1,
},
},
];
const getStoriesKnobs = () => {
const rightAxisSeries = text('Right y-axis series, i.e. A,C', '');
const legendPlacement = select<LegendPlacement>(
'Legend placement',
{
bottom: 'under',
right: 'right',
},
'bottom'
);
const renderLegendAsTable = select<any>(
'Render legend as',
{
list: false,
table: true,
},
false
);
return {
rightAxisSeries,
legendPlacement,
renderLegendAsTable,
};
};
export const graphWithLegend = () => {
const { legendPlacement, rightAxisSeries, renderLegendAsTable } = getStoriesKnobs();
const props: GraphWithLegendProps = {
series: series.map(s => {
if (
rightAxisSeries
.split(',')
.map(s => s.trim())
.indexOf(s.label.split('-')[0]) > -1
) {
s.yAxis = { index: 2 };
} else {
s.yAxis = { index: 1 };
}
return s;
}),
legendDisplayMode: renderLegendAsTable ? LegendDisplayMode.Table : LegendDisplayMode.List,
onToggleSort: () => {},
timeRange: {
from: dateTime(1546372800000),
to: dateTime(1546380000000),
raw: {
from: dateTime(1546372800000),
to: dateTime(1546380000000),
},
},
timeZone: 'browser',
width: 600,
height: 300,
placement: legendPlacement,
};
return <GraphWithLegend {...props} />;
};