import type { Meta, StoryObj } from '@storybook/react'; import { useEffect, useState } from 'react'; import '../../ThemeProvider/tailwindcss.css'; import { Button, Menu } from '@mui/material'; import ButtonGroup from '@mui/material/ButtonGroup'; import Switch from '@mui/material/Switch'; import Tooltip from '@mui/material/Tooltip'; import { useLocaleDateTimeFormat } from '@centreon/ui'; import TimePeriod from '../../TimePeriods'; import type { LineChartData } from '../common/models'; import annotationData from '../mockedData/annotationData.json'; import dataCurvesSameColor from '../mockedData/curvesWithSameColor.json'; import exclusionPeriodFirstPeriod from '../mockedData/exclusionPeriodFirstPeriod.json'; import exclusionPeriodSecondPeriod from '../mockedData/exclusionPeriodSecondPeriod.json'; import exclusionPeriodThirdPeriod from '../mockedData/exclusionPeriodThirdPeriod.json'; import dataLastDay from '../mockedData/lastDay.json'; import dataLastDayForword from '../mockedData/lastDayForward.json'; import dataLastDayThreshold from '../mockedData/lastDayThreshold.json'; import dataLastDayWithLotOfUnits from '../mockedData/lastDayWithLotOfUnits.json'; import dataLastMonth from '../mockedData/lastMonth.json'; import dataLastWeek from '../mockedData/lastWeek.json'; import dataPingService from '../mockedData/pingService.json'; import dataPingServiceLinesBars from '../mockedData/pingServiceLinesBars.json'; import dataPingServiceLinesBarsMixed from '../mockedData/pingServiceLinesBarsMixed.json'; import dataPingServiceLinesBarsStacked from '../mockedData/pingServiceLinesBarsStacked.json'; import dataPingServiceLinesStackKeys from '../mockedData/pingServiceWithStackedKeys.json'; import dataZoomPreview from '../mockedData/zoomPreview.json'; import WrapperChart from '.'; import { dateTimeFormat } from './common'; import { argTypes, args as argumentsData, defaultEnd, defaultLast7days, defaultLastMonth, defaultStart, lastDayForwardDate, zoomPreviewDate } from './helpers/doc'; import { type Interval, ThresholdType, type TooltipData } from './models'; const meta: Meta = { component: WrapperChart }; export default meta; type Story = StoryObj; interface Random { max: number; min: number; } const Threshold = (args): JSX.Element => { const [currentFactorMultiplication, setCurrentFactorMultiplication] = useState(2.5); const [countedCircles, setCountedCircles] = useState(); const getRandomInt = ({ min, max }: Random): number => { return Math.floor(Math.random() * (max - min) + min); }; const handleClick = (): void => { setCurrentFactorMultiplication(getRandomInt({ max: 5, min: 1 })); }; const getCountDisplayedCircles = (value: number): void => { setCountedCircles(value); }; return ( <> ); }; const ExternalComponent = (tooltipData): JSX.Element => { const { hideTooltip, data } = tooltipData; const { format } = useLocaleDateTimeFormat(); return ( <> External component

{format({ date: data, formatString: dateTimeFormat })}

); }; const LineChartAndCLS = (args): JSX.Element => { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { setTimeout(() => { setLoading(false); setData(dataLastDayThreshold as unknown as LineChartData); }, 100000); }, []); return ( ); }; interface TimePeriodSwitchProps { getDataSwitch: (value: boolean) => void; } const TimePeriodSwitch = ({ getDataSwitch }: TimePeriodSwitchProps): JSX.Element => { const [checked, setChecked] = useState(false); const handleChange = (event: React.ChangeEvent): void => { setChecked(event.target.checked); }; useEffect(() => { getDataSwitch?.(checked); }, [checked, getDataSwitch]); return ( ); }; const LineChartAndTimePeriod = (args): JSX.Element => { const [currentData, setCurrentData] = useState(); const [start, setStart] = useState(); const [end, setEnd] = useState(); const [displayAnnotation, setDisplayAnnotation] = useState(); const [adjustedTimePeriodInterval, setAdjustedTimePeriodInterval] = useState(); const getParameters = (interval): void => { setStart(interval.start); setEnd(interval.end); }; useEffect(() => { if (!start || !end) { return; } if (start.includes(lastDayForwardDate)) { setCurrentData(dataLastDayForword as unknown as LineChartData); return; } if (start.includes(`${defaultStart.split('T')[0]}`)) { setCurrentData(dataLastDayThreshold as unknown as LineChartData); return; } if (start.includes(defaultLast7days.split('T')[0])) { setCurrentData(dataLastWeek as unknown as LineChartData); return; } if (start.includes(defaultLastMonth.split('T')[0])) { setCurrentData(dataLastMonth as unknown as LineChartData); return; } if (start.includes(zoomPreviewDate)) { setCurrentData(dataZoomPreview as unknown as LineChartData); } }, [start, end]); const getInterval = (interval: Interval): void => { setAdjustedTimePeriodInterval(interval); }; const getDataSwitch = (value): void => { setDisplayAnnotation(value); }; const annotationEventData = displayAnnotation && { data: annotationData.result }; return ( <> } /> ( ) }} zoomPreview={{ enable: true, getInterval }} /> ); }; const LineChartAndExclusionPeriod = (args): JSX.Element => { const [dataExclusionPeriods, setDataExclusionPeriods] = useState< Array >([exclusionPeriodFirstPeriod as unknown as LineChartData]); const handleClick = (data): void => { setDataExclusionPeriods([...dataExclusionPeriods, data]); }; return ( <>
Add exclusion periods:
); }; const Template: Story = { render: (args) => ( ) }; const WithTimePeriod = { render: (args): JSX.Element => }; const LineChartWithExclusionPeriod: Story = { render: (args) => }; const LineChartWithEnvelopVariation: Story = { render: (args) => }; const LineChartWithCLS: Story = { render: (args) => }; export const LineChart: Story = { ...Template, args: argumentsData, argTypes }; export const LineChartWithStepCurve: Story = { ...Template, args: { ...argumentsData, lineStyle: { curve: 'step' } }, argTypes }; export const LineChartWithTimePeriod: Story = { ...WithTimePeriod, args: { end: defaultEnd, height: 500, start: defaultStart }, parameters: { chromatic: { disableSnapshot: true } } }; export const WithEnvelopVariation: Story = { ...LineChartWithEnvelopVariation, args: { end: defaultEnd, height: 500, start: defaultStart } }; export const withExclusionPeriods: Story = { ...LineChartWithExclusionPeriod, args: { end: defaultEnd, height: 500, start: defaultStart } }; export const withCLS: Story = { ...LineChartWithCLS, args: { end: defaultEnd, height: 500, start: defaultStart } }; export const withThresholds: Story = { args: { ...argumentsData, thresholds: { critical: [ { label: 'critical', value: 350 } ], enabled: true, warning: [ { label: 'warning', value: 300 } ] } }, argTypes, render: (args) => ( ) }; export const withThresholdsAndUnit: Story = { args: { ...argumentsData, thresholds: { critical: [ { label: 'critical', value: 79 } ], enabled: true, warning: [ { label: 'warning', value: 65 } ] }, thresholdUnit: '%' }, argTypes, render: (args) => ( ) }; export const thresholdsRange: Story = { args: { ...argumentsData, thresholds: { critical: [ { label: 'critical 1', value: 60 }, { label: 'critical 2', value: 79 } ], enabled: true, warning: [ { label: 'warning 1', value: 20 }, { label: 'warning 2', value: 30 } ] }, thresholdUnit: '%' }, argTypes, render: (args) => ( ) }; export const LineChartWithSameColorCurves: Story = { ...Template, args: argumentsData, argTypes, render: (args) => ( ) }; export const zeroCentered: Story = { args: { ...argumentsData, axis: { isCenteredZero: true } }, argTypes, render: (args) => ( ) }; export const customLines: Story = { args: { ...argumentsData, lineStyle: { areaTransparency: 10, dashLength: 10, dashOffset: 10, lineWidth: 3, showArea: true, showPoints: true } }, argTypes, render: (args) => ( ) }; export const customLinesAndBars: Story = { args: { ...argumentsData, barStyle: { opacity: 0.5, radius: 0.5 }, lineStyle: { curve: 'natural', lineWidth: 2, showPoints: true } }, argTypes, render: (args) => ( ) }; export const multipleUnits: Story = { args: argumentsData, argTypes, render: (args) => ( ) }; export const linesAndBars: Story = { args: { ...argumentsData, lineStyle: { curve: 'natural', lineWidth: 2, showPoints: true } }, argTypes, render: (args) => ( ) }; export const linesAndBarsStacked: Story = { args: { ...argumentsData, lineStyle: { curve: 'natural', lineWidth: 2, showPoints: false } }, argTypes, render: (args) => ( ) }; export const linesAndBarsMixed: Story = { args: { ...argumentsData, lineStyle: { curve: 'natural', lineWidth: 2, showPoints: false } }, argTypes, render: (args) => ( ) }; export const linesAndBarsCenteredZero: Story = { args: { ...argumentsData, axis: { isCenteredZero: true }, lineStyle: { curve: 'natural', lineWidth: 2, showPoints: true } }, argTypes, render: (args) => ( ) }; export const linesAndBarsStackedCenteredZero: Story = { args: { ...argumentsData, axis: { isCenteredZero: true }, lineStyle: { curve: 'natural', lineWidth: 2, showPoints: false } }, argTypes, render: (args) => ( ) }; export const linesAndBarsMixedCenteredZero: Story = { args: { ...argumentsData, axis: { isCenteredZero: true }, lineStyle: { curve: 'natural', lineWidth: 2, showPoints: false } }, argTypes, render: (args) => ( ) }; const CustomYUnits = (props): JSX.Element => { const [leftUnit, setLeftUnit] = useState('B'); const [rightUnit, setRightUnit] = useState('ms'); return ( ); }; export const customYUnits: Story = { args: argumentsData, argTypes, render: (args) => }; export const WithAdditionalLines: Story = { args: { ...argumentsData, additionalLines: [ { color: 'grey', text: 'my text', unit: '%', yValue: 3 } ] }, argTypes, render: (args) => ( ) }; export const linesAndBarsMinMax: Story = { args: { ...argumentsData, lineStyle: { curve: 'natural', lineWidth: 2, showPoints: true }, max: 30, min: 10 }, argTypes, render: (args) => ( ) }; export const linesAndBarsMinMaxForUnit: Story = { args: { ...argumentsData, boundariesUnit: '%', lineStyle: { curve: 'natural', lineWidth: 2, showPoints: true }, max: 30, min: 10 }, argTypes, render: (args) => ( ) }; const LegendSecondaryClick = (args) => { const [anchor, setAnchor] = useState(null); return ( <> setAnchor(element) }} /> setAnchor(null)} open={Boolean(anchor)} > menu ); }; export const withLegendSecondaryClick: Story = { args: argumentsData, argTypes, render: (args) => ( ) }; export const stackedKey: Story = { args: { ...argumentsData, data: dataPingServiceLinesStackKeys }, argTypes }; export const WithControlledCalculations: Story = { ...Template, args: { ...argumentsData, legend: { mode: 'grid', placement: 'bottom', showCalculations: { avg: true, max: true, min: false } }, lineStyle: { curve: 'step' } }, argTypes };