|
| 1 | +import React, { useMemo } from 'react' |
| 2 | +import { VisXYContainer, VisTimeline, VisAxis } from '@unovis/react' |
| 3 | + |
| 4 | +import { ExampleViewerDurationProps } from '@src/components/ExampleViewer/index' |
| 5 | +import { Arrangement, TextAlign, TimelineArrow } from '@unovis/ts' |
| 6 | + |
| 7 | +// Icons |
| 8 | +import icon from './icon.svg?raw' |
| 9 | + |
| 10 | +export const title = 'Timeline Arrows' |
| 11 | +export const subTitle = 'Between the lines' |
| 12 | + |
| 13 | +export const component = (props: ExampleViewerDurationProps): JSX.Element => { |
| 14 | + const lineIconSize = 25 |
| 15 | + const data = Array(10).fill(0).map((_, i) => ({ |
| 16 | + timestamp: Date.now() + i * 1000 * 60 * 60 * 24, |
| 17 | + length: 1000 * 60 * 60 * 24, |
| 18 | + id: i.toString(), |
| 19 | + type: `Row ${i}`, |
| 20 | + lineWidth: 5 + Math.random() * 15, |
| 21 | + })) |
| 22 | + |
| 23 | + type Datum = typeof data[number] |
| 24 | + |
| 25 | + const arrows = data.map((d, i) => { |
| 26 | + if (i === data.length - 1) return undefined |
| 27 | + |
| 28 | + return { |
| 29 | + x: d.timestamp + d.length, |
| 30 | + lineSourceId: d.id, |
| 31 | + lineTargetId: data[i + 1].id, |
| 32 | + xOffsetPx: lineIconSize / 2, |
| 33 | + lineSourceMarginPx: (lineIconSize - d.lineWidth) / 2, |
| 34 | + lineTargetMarginPx: 4, |
| 35 | + } |
| 36 | + }).filter(Boolean) as TimelineArrow[] |
| 37 | + |
| 38 | + const svgDefs = useMemo(() => `${icon}`, []) |
| 39 | + return (<> |
| 40 | + <VisXYContainer<Datum> |
| 41 | + data={data} |
| 42 | + height={300} |
| 43 | + svgDefs={svgDefs} |
| 44 | + > |
| 45 | + <VisTimeline |
| 46 | + id={(d) => d.id} |
| 47 | + lineRow={(d: Datum) => d.type as string} |
| 48 | + x={(d: Datum) => d.timestamp} |
| 49 | + rowHeight={40} |
| 50 | + lineWidth={(d) => d.lineWidth} |
| 51 | + lineCap |
| 52 | + showEmptySegments |
| 53 | + showRowLabels |
| 54 | + rowLabelTextAlign={TextAlign.Left} |
| 55 | + duration={props.duration} |
| 56 | + lineEndIcon={'#circle_check_filled'} |
| 57 | + lineEndIconSize={lineIconSize} |
| 58 | + lineStartIconColor={'#fff'} |
| 59 | + lineEndIconColor={'rgb(38, 86, 201)'} |
| 60 | + lineEndIconArrangement={Arrangement.Outside} |
| 61 | + arrows={arrows} |
| 62 | + /> |
| 63 | + <VisAxis |
| 64 | + type='x' |
| 65 | + numTicks={3} |
| 66 | + tickFormat={(x: number) => new Date(x).toDateString()} |
| 67 | + duration={props.duration} |
| 68 | + /> |
| 69 | + </VisXYContainer> |
| 70 | + </> |
| 71 | + ) |
| 72 | +} |
0 commit comments