import { Meta, StoryObj } from '@storybook/react'; import cityTemperature, { CityTemperature } from '@visx/mock-data/lib/mocks/cityTemperature'; import React from 'react'; import { ColorTokens } from '../../../foundation/Colors'; import { TokenTypography } from '../../TokenTypography'; import { Typography } from '../../Typography'; import { VisxTimelineChart, VisxTimelineChartProps } from '../VisxTimelineChart'; import { Glyph } from './components/Glyph'; import { TooltipGlyph } from './components/TooltipGlyph'; import { Box, LeftBox, ParentBox, RightBox, TooltipBox } from './Stories.styled'; import { SharedIntegrationProps } from './types'; const cityTemperatures = cityTemperature.slice(); const dataMissingValues = cityTemperatures.map((d, i) => i === 10 || i === 11 ? { ...d, Austin: 'null', 'New York': 'notanumber', 'San Francisco': 'nope' } : d, ); const dataSmall = cityTemperatures.slice(0, 15); const dataSmallMissingValues = dataMissingValues.slice(0, 15); const getDate = (d: CityTemperature) => new Date(d.date).valueOf(); const getSfTemperature = (d: CityTemperature) => Number(d['San Francisco']); const getNegativeSfTemperature = (d: CityTemperature) => -getSfTemperature(d); const getNyTemperature = (d: CityTemperature) => Number(d['New York']); const getAustinTemperature = (d: CityTemperature) => Number(d.Austin); type City = 'San Francisco' | 'New York' | 'Austin'; type VisxTimelineChartIntegrationProps = Omit< VisxTimelineChartProps, 'series' | 'renderTooltip' | 'renderTooltipGlyph' | 'renderGlyph' > & SharedIntegrationProps & { lessData: boolean; manyDecimals: boolean; missingValues: boolean; negativeValues: boolean; }; const VisxTimelineChartIntegration: React.FunctionComponent = ( args, ) => { const glyphComponent = args.glyphComponent; const showTooltipGlyph = args.showTooltipGlyph; const showTooltip = args.showTooltip; const tooltipGlyphComponent = args.tooltipGlyphComponent; const negativeValues = args.negativeValues; const missingValues = args.missingValues; const lessData = args.lessData; const sharedTooltip = args.sharedTooltip; const manyDecimals = args.manyDecimals; const computedData = lessData ? missingValues ? dataSmallMissingValues : dataSmall : missingValues ? dataMissingValues : cityTemperatures; const series: VisxTimelineChartProps['series'] = [ { colorToken: 'primary500' as ColorTokens, data: computedData.map((d) => ({ x: getDate(d), y: negativeValues ? getNegativeSfTemperature(d) : getSfTemperature(d), })), id: 'San Francisco', }, { colorToken: 'secondary500' as ColorTokens, data: computedData.map((d) => ({ x: getDate(d), y: getNyTemperature(d), })), id: 'New York', }, { colorToken: 'warning500' as ColorTokens, data: computedData.map((d) => ({ x: getDate(d), y: getAustinTemperature(d), })), id: 'Austin', }, ].map((s) => manyDecimals ? { ...s, data: s.data.map((d) => ({ ...d, y: d.y / 10000000 })) } : s, ); const renderTooltipGlyph: VisxTimelineChartProps['renderTooltipGlyph'] = showTooltipGlyph ? (props) => : undefined; const renderTooltip: VisxTimelineChartProps['renderTooltip'] = showTooltip ? ({ tooltipData, colorScale }) => { const nearestDatum = tooltipData?.nearestDatum; const datum = nearestDatum?.datum; if (!datum) { return null; } return ( {datum.x ? new Intl.DateTimeFormat(undefined, { day: '2-digit', hour: '2-digit', minute: '2-digit', month: 'short', year: 'numeric', }).format(datum.x) : 'No date'} {( (sharedTooltip ? Object.keys(tooltipData?.datumByKey ?? {}) : [nearestDatum?.key] ).filter((city) => city) as City[] ).map((city) => { const citySerie = series[city === 'San Francisco' ? 0 : city === 'New York' ? 1 : 2]; const temperature = citySerie.data[nearestDatum?.index].y; return ( ); })} ); } : undefined; const renderGlyph: VisxTimelineChartProps['renderGlyph'] = (props) => ( ); return ( Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. value.toString() : undefined} /> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ); }; export default { args: {}, component: VisxTimelineChartIntegration, title: 'Components/VisxTimelineChart', } as Meta; export const Temperature: StoryObj = { argTypes: { chartType: { control: 'select', options: ['glyph', 'line', 'area', 'areastack', 'none'], }, curveType: { control: 'select', options: ['linear', 'cardinal', 'step'], }, glyphComponent: { control: 'select', options: ['star', 'cross', 'circle', '🍍'], }, tooltipGlyphComponent: { control: 'select', options: ['star', 'cross', 'circle', '🍍'], }, xAxisOrientation: { control: 'select', options: ['top', 'bottom'], }, yAxisOrientation: { control: 'select', options: ['left', 'right'], }, }, args: { axisDomainLineColorToken: 'white100', axisTicksTextColorToken: 'white100', axisTypographyToken: 'bodySmallMedium', chartType: 'line', crosshairColorToken: 'primary500', curveType: 'linear', glyphComponent: 'star', lessData: false, manyDecimals: true, missingValues: false, negativeValues: false, sharedTooltip: true, showGridColumns: false, showGridRows: false, showTooltip: true, showTooltipGlyph: true, tickLength: 4, tooltipGlyphComponent: 'circle', tooltipShowHorizontalCrosshair: true, tooltipShowVerticalCrosshair: true, tooltipSnapTooltipToDatumX: true, tooltipSnapTooltipToDatumY: true, xAxisOrientation: 'bottom', yAxisOrientation: 'right', yRangePercentageOffset: 0, } as VisxTimelineChartIntegrationProps, };