-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Component | Annotations: New component
- Loading branch information
Showing
34 changed files
with
687 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/dev/src/examples/auxiliary/annotations/basic-annotations/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React, { useEffect, useRef, useState } from 'react' | ||
import { VisXYContainer, VisLine, VisAxis, VisAnnotations, VisLineRef } from '@unovis/react' | ||
import { AnnotationItem } from '@unovis/ts' | ||
|
||
export const title = 'Basic Annotations' | ||
export const subTitle = 'Dynamic Data Updates' | ||
|
||
export const component = (): JSX.Element => { | ||
const height = 400 | ||
const length = 10 | ||
const min = 3 | ||
const max = 8 | ||
const generateData = (): number[] => Array.from({ length }, () => Math.random() * (max - min) + min) | ||
|
||
const ref = useRef<VisLineRef<number>>(null) | ||
const [data, setData] = useState<number[]>(generateData) | ||
const [annotations, setAnnotations] = useState<AnnotationItem[]>([]) | ||
|
||
useEffect(() => { | ||
if (!ref.current?.component) return | ||
const { xScale, yScale } = ref.current.component | ||
const peaks = data.reduce((acc, y, x) => { | ||
acc.min = Math.min(acc.min, y) | ||
acc.max = Math.max(acc.max, y) | ||
acc.total += y | ||
if (x === data.length - 1) { | ||
acc.items.push({ | ||
x: 20, | ||
y: yScale(min) + 10, | ||
content: [ | ||
{ text: 'Stats', fontSize: 20, fontWeight: 700, color: '#1C72E8' }, | ||
{ text: `Min: ${acc.min}`, fontSize: 14 }, | ||
{ text: `Max: ${acc.max}`, fontSize: 14 }, | ||
{ text: `Range: ~${(acc.max - acc.min).toFixed(2)}`, fontSize: 14 }, | ||
{ text: `Mean: ~${(acc.total / data.length).toFixed(2)}`, fontSize: 14 }, | ||
], | ||
}, { | ||
x: '102%', | ||
y: yScale(acc.min), | ||
content: 'Min', | ||
verticalAlign: 'middle', | ||
textAlign: 'left', | ||
subject: { | ||
x: () => xScale(data.indexOf(acc.min)), | ||
y: () => yScale(acc.min), | ||
connectorLineStrokeDasharray: '2 2', | ||
}, | ||
}) | ||
} else if (y > data[x - 1] && y > data[x + 1]) { | ||
acc.items.push({ | ||
x: `${(x / 9 * 100)}%`, // | ||
y: yScale(y + 1.5), | ||
content: `Peak: [x: ${x}, y: ${y.toFixed(1)}]`, | ||
verticalAlign: 'bottom', | ||
textAlign: 'center', | ||
subject: { | ||
x: () => xScale(x), | ||
y: () => yScale(y), | ||
radius: 6, | ||
}, | ||
}) | ||
} | ||
return acc | ||
}, { min: max, max: min, total: 0, items: new Array<AnnotationItem>() }) | ||
setAnnotations(peaks.items) | ||
}, [data, ref.current?.component]) | ||
|
||
|
||
return ( | ||
<> | ||
<button onClick={() => setData(generateData())}>Update Data</button> | ||
<VisXYContainer data={data} height={height} margin={{ right: 100 }} yDomain={[0, 10]}> | ||
<VisLine ref={ref} x={(_, i) => i as number} y={d => d} /> | ||
<VisAxis type='x' numTicks={5} /> | ||
<VisAxis type='y' numTicks={5} /> | ||
<VisAnnotations items={annotations}/> | ||
</VisXYContainer> | ||
</> | ||
) | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/dev/src/examples/auxiliary/annotations/single-container/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { useCallback, useMemo, useEffect } from 'react' | ||
import { VisAnnotations, VisSingleContainer, VisNestedDonut } from '@unovis/react' | ||
import { Annotations, StringAccessor } from '@unovis/ts' | ||
|
||
export const title = 'Single Container' | ||
export const subTitle = 'Donut with annotation button' | ||
|
||
export const component = (): JSX.Element => { | ||
const data = [ | ||
{ group: 'A', subgroup: 'A0', value: 10 }, | ||
{ group: 'A', subgroup: 'A0', value: 5 }, | ||
{ group: 'A', subgroup: 'A1', value: 2 }, | ||
{ group: 'A', subgroup: 'A1', value: 1 }, | ||
{ group: 'A', subgroup: 'A1', value: 1 }, | ||
{ group: 'A', subgroup: 'A2', value: 15 }, | ||
{ group: 'B', value: 15 }, | ||
{ group: 'C', subgroup: 'C0', value: 3 }, | ||
{ group: 'C', subgroup: 'C1', value: 1 }, | ||
] | ||
type Datum = typeof data[0] | ||
const height = 250 | ||
const [annotations, setAnnotations] = React.useState<AnnotationItem[]>([]) | ||
const [layers, setLayers] = React.useState<StringAccessor<Datum>[]>() | ||
const [expanded, setExpandedState] = React.useState(false) | ||
|
||
useEffect(() => { | ||
setAnnotations([{ | ||
x: '50%', | ||
y: height / 2 + 15 + 8, | ||
cursor: 'pointer', | ||
textAlign: 'center', | ||
content: { text: expanded ? 'Hide' : 'Show', color: '#3355ee', fontSize: 11 }, | ||
}]) | ||
if (expanded) { | ||
setLayers([d => d.group, d => d.subgroup ?? null]) | ||
} else { | ||
setLayers([d => d.group]) | ||
} | ||
}, [expanded]) | ||
|
||
const events = useMemo(() => ({ | ||
[Annotations.selectors.annotationContent]: { | ||
click: () => setExpandedState(!expanded), | ||
}, | ||
}), [expanded]) | ||
return ( | ||
<> | ||
<VisSingleContainer data={data} height={height}> | ||
<VisNestedDonut | ||
showBackground={true} | ||
layers={layers} | ||
value={useCallback((d: Datum) => d.value, [])} | ||
layerSettings={{ width: expanded ? 25 : 50 }} | ||
showSegmentLabels={false} | ||
centralLabel={'Label Text'} centralSubLabel={'Sub-label'}/> | ||
<VisAnnotations events={events} items={annotations}/> | ||
</VisSingleContainer> | ||
</> | ||
) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.