From b3ad6feb21271385d829babb53971a421bcd1861 Mon Sep 17 00:00:00 2001 From: Marco Vettorello Date: Mon, 2 Oct 2023 16:07:40 +0200 Subject: [PATCH] improve example --- .../cartesian/1_domain_edges.story.tsx | 69 +++++++++++++------ 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/storybook/stories/cartesian/1_domain_edges.story.tsx b/storybook/stories/cartesian/1_domain_edges.story.tsx index 614f05934e..871730b8fb 100644 --- a/storybook/stories/cartesian/1_domain_edges.story.tsx +++ b/storybook/stories/cartesian/1_domain_edges.story.tsx @@ -9,7 +9,17 @@ import { boolean, number } from '@storybook/addon-knobs'; import React from 'react'; -import { LineSeries, Axis, BarSeries, Chart, ScaleType, Settings, Position, HistogramBarSeries } from '@elastic/charts'; +import { + LineSeries, + Axis, + BarSeries, + Chart, + ScaleType, + Settings, + Position, + HistogramBarSeries, + niceTimeFormatter, +} from '@elastic/charts'; import { getRandomNumberGenerator } from '@elastic/charts/src/mocks/utils'; import { ChartsStory } from '../../types'; @@ -21,31 +31,36 @@ const interval = 1000 * 60 * 5; const data = Array.from({ length: 20 }, (d, i) => { return [start + interval * i, Math.floor(rng(2, 10))]; }); -console.log( - data - .map((d) => [`"${new Date(d[0]).toISOString()}"`, d[1]]) - .map((d) => d.join(',')) - .join('\n'), -); + export const Example: ChartsStory = (_, { title, description }) => { const customDomain = boolean('custom domain', false); - const rangeSlider = number('time range', 19, { min: 1, max: 25, range: true, step: 0.2 }); + const startTimeSlider = number('start time', 0, { min: 0, max: 20, range: true, step: 0.2 }); + const rangeSlider = number('end time', 19, { min: 1, max: 25, range: true, step: 0.2 }); const subtract = boolean('subtract 1ms', false); const tickFormat = (d: number) => new Date(d).toISOString(); - const xDomain = customDomain ? { min: start, max: start + interval * rangeSlider - (subtract ? 1 : 0) } : undefined; + const xDomain = customDomain + ? { min: start + interval * startTimeSlider, max: start + interval * rangeSlider - (subtract ? 1 : 0) } + : undefined; + const domain: [number, number] = [xDomain?.min ?? data[0][0] ?? 0, xDomain?.max ?? data.at(-1)?.[0] ?? 0]; return ( <>

- {xDomain ? 'configured' : 'data'} domain: {new Date(xDomain ? xDomain.min : data[0][0]).toISOString()} to{' '} - {new Date(xDomain ? xDomain.max : data.at(-1)?.[0] ?? start).toISOString()} + {xDomain ? 'configured' : 'data'} domain: {new Date(domain[0]).toISOString()} to{' '} + {new Date(domain[1]).toISOString()}

{ /> { { /> { { size: 5, }, }} + tickFormat={(d) => niceTimeFormatter(domain)(d, { timeZone: 'UTC' })} />