-
Notifications
You must be signed in to change notification settings - Fork 711
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(demo): add Annotation demo (#909)
* new(demo): add annotation page + sandbox * docs(annotation): add AnnotationTile, add to Gallery, update /docs/annotation * demo(annotation): factor out ExampleControls, add more controls * demo(annotation): update prop names * types(demo/annotation): fix ExampleControls provided types * docs(annotation): add react-annotation + ResizeObserver info * docs(annotation): improve markdown syntax
- Loading branch information
1 parent
5387453
commit 1119398
Showing
13 changed files
with
577 additions
and
5 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
29 changes: 29 additions & 0 deletions
29
packages/visx-demo/src/components/Gallery/AnnotationTile.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,29 @@ | ||
import React from 'react'; | ||
import Annotation, { AnnotationProps, greens } from '../../sandboxes/visx-annotation/Example'; | ||
import GalleryTile from '../GalleryTile'; | ||
|
||
export { default as packageJson } from '../../sandboxes/visx-area/package.json'; | ||
|
||
const tileStyles = { background: greens[0] }; | ||
const detailsStyles: React.CSSProperties = { | ||
color: greens[2], | ||
borderBottomRightRadius: 16, | ||
borderBottomLeftRadius: 16, | ||
}; | ||
const exampleProps = { compact: true }; | ||
const exampleRenderer: React.FC<AnnotationProps> = props => | ||
props.width > 0 && props.height > 0 ? <Annotation {...props} /> : null; | ||
|
||
export default function AnnotationTile() { | ||
return ( | ||
<GalleryTile<AnnotationProps> | ||
title="Annotation" | ||
description="<Annotation />" | ||
exampleRenderer={exampleRenderer} | ||
exampleUrl="/annotation" | ||
detailsStyles={detailsStyles} | ||
tileStyles={tileStyles} | ||
exampleProps={exampleProps} | ||
/> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import Show from '../components/Show'; | ||
import Annotation from '../sandboxes/visx-annotation/Example'; | ||
import AnnotationSource from '!!raw-loader!../sandboxes/visx-annotation/Example'; | ||
import packageJson from '../sandboxes/visx-annotation/package.json'; | ||
|
||
export default () => ( | ||
<Show | ||
component={Annotation} | ||
title="Annotation" | ||
codeSandboxDirectoryName="visx-annotation" | ||
packageJson={packageJson} | ||
> | ||
{AnnotationSource} | ||
</Show> | ||
); |
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 |
---|---|---|
@@ -1,11 +1,34 @@ | ||
import React from 'react'; | ||
import AnnotationReadme from '!!raw-loader!../../../../visx-annotation/Readme.md'; | ||
import LinePathAnnotation from '../../../../visx-annotation/src/deprecated/LinePathAnnotation'; | ||
import Annotation from '../../../../visx-annotation/src/components/Annotation'; | ||
import EditableAnnotation from '../../../../visx-annotation/src/components/EditableAnnotation'; | ||
import CircleSubject from '../../../../visx-annotation/src/components/CircleSubject'; | ||
import LineSubject from '../../../../visx-annotation/src/components/LineSubject'; | ||
import Connector from '../../../../visx-annotation/src/components/Connector'; | ||
import Label from '../../../../visx-annotation/src/components/Label'; | ||
import LinePathAnnotationDeprecated from '../../../../visx-annotation/src/deprecated/LinePathAnnotation'; | ||
import DocPage from '../../components/DocPage'; | ||
import AnnotationTile from '../../components/Gallery/AnnotationTile'; | ||
|
||
const components = [LinePathAnnotation]; | ||
const components = [ | ||
Annotation, | ||
EditableAnnotation, | ||
CircleSubject, | ||
LineSubject, | ||
Connector, | ||
Label, | ||
LinePathAnnotationDeprecated, | ||
]; | ||
|
||
const examples = [AnnotationTile]; | ||
|
||
const AnnotationDocs = () => ( | ||
<DocPage components={components} readme={AnnotationReadme} visxPackage="annotation" /> | ||
<DocPage | ||
examples={examples} | ||
components={components} | ||
readme={AnnotationReadme} | ||
visxPackage="annotation" | ||
/> | ||
); | ||
|
||
export default AnnotationDocs; |
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
107 changes: 107 additions & 0 deletions
107
packages/visx-demo/src/sandboxes/visx-annotation/Example.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,107 @@ | ||
import React from 'react'; | ||
import { Label, Connector, CircleSubject, LineSubject } from '@visx/annotation'; | ||
import { LinePath } from '@visx/shape'; | ||
import ExampleControls from './ExampleControls'; | ||
import findNearestDatum from './findNearestDatum'; | ||
|
||
export type AnnotationProps = { | ||
width: number; | ||
height: number; | ||
compact?: boolean; | ||
}; | ||
|
||
export const orange = '#ff7e67'; | ||
export const greens = ['#ecf4f3', '#68b0ab', '#006a71']; | ||
|
||
export default function Example({ width, height, compact = false }: AnnotationProps) { | ||
return ( | ||
<ExampleControls width={width} height={height} compact={compact}> | ||
{({ | ||
AnnotationComponent, | ||
annotationPosition, | ||
approxTooltipHeight, | ||
connectorType, | ||
data, | ||
getDate, | ||
getStockValue, | ||
horizontalAnchor, | ||
labelWidth, | ||
setAnnotationPosition, | ||
showAnchorLine, | ||
subjectType, | ||
subtitle, | ||
title, | ||
verticalAnchor, | ||
xScale, | ||
yScale, | ||
}) => ( | ||
<svg width={width} height={height}> | ||
<rect width={width} height={height} fill={greens[0]} /> | ||
<LinePath | ||
stroke={greens[2]} | ||
strokeWidth={2} | ||
data={data} | ||
x={d => xScale(getDate(d)) ?? 0} | ||
y={d => yScale(getStockValue(d)) ?? 0} | ||
/> | ||
<AnnotationComponent | ||
width={width} | ||
height={height} | ||
x={annotationPosition.x} | ||
y={annotationPosition.y} | ||
dx={annotationPosition.dx} | ||
dy={annotationPosition.dy} | ||
onDragEnd={({ event, ...nextPosition }) => { | ||
// snap Annotation to the nearest data point | ||
const nearestDatum = findNearestDatum({ | ||
accessor: subjectType === 'horizontal-line' ? getStockValue : getDate, | ||
data, | ||
scale: subjectType === 'horizontal-line' ? yScale : xScale, | ||
value: subjectType === 'horizontal-line' ? nextPosition.y : nextPosition.x, | ||
}); | ||
const x = xScale(getDate(nearestDatum)) ?? 0; | ||
const y = yScale(getStockValue(nearestDatum)) ?? 0; | ||
|
||
// flip label to keep in view | ||
const shouldFlipDx = | ||
(nextPosition.dx > 0 && x + nextPosition.dx + labelWidth > width) || | ||
(nextPosition.dx < 0 && x + nextPosition.dx - labelWidth <= 0); | ||
const shouldFlipDy = // 100 is est. tooltip height | ||
(nextPosition.dy > 0 && height - (y + nextPosition.dy) < approxTooltipHeight) || | ||
(nextPosition.dy < 0 && y + nextPosition.dy - approxTooltipHeight <= 0); | ||
setAnnotationPosition({ | ||
x, | ||
y, | ||
dx: (shouldFlipDx ? -1 : 1) * nextPosition.dx, | ||
dy: (shouldFlipDy ? -1 : 1) * nextPosition.dy, | ||
}); | ||
}} | ||
> | ||
<Connector stroke={orange} type={connectorType} /> | ||
<Label | ||
backgroundFill="white" | ||
showAnchorLine={showAnchorLine} | ||
anchorLineStroke={greens[2]} | ||
backgroundProps={{ stroke: greens[1] }} | ||
fontColor={greens[2]} | ||
horizontalAnchor={horizontalAnchor} | ||
subtitle={subtitle} | ||
title={title} | ||
verticalAnchor={verticalAnchor} | ||
width={labelWidth} | ||
/> | ||
{subjectType === 'circle' && <CircleSubject stroke={orange} />} | ||
{subjectType !== 'circle' && ( | ||
<LineSubject | ||
orientation={subjectType === 'vertical-line' ? 'vertical' : 'horizontal'} | ||
stroke={orange} | ||
min={0} | ||
max={subjectType === 'vertical-line' ? height : width} | ||
/> | ||
)} | ||
</AnnotationComponent> | ||
</svg> | ||
)} | ||
</ExampleControls> | ||
); | ||
} |
Oops, something went wrong.