-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #778 from hshoff/chris--splitpath-for-real
new(vx-shape): add SplitLinePath
- Loading branch information
Showing
13 changed files
with
413 additions
and
5 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
packages/vx-demo/src/components/Gallery/SplitLinePathTile.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,25 @@ | ||
import React from 'react'; | ||
import SplitLinePath, { | ||
SplitLinePathProps, | ||
backgroundLight, | ||
} from '../../sandboxes/vx-shape-splitlinepath/Example'; | ||
import GalleryTile from '../GalleryTile'; | ||
|
||
export { default as packageJson } from '../../sandboxes/vx-area/package.json'; | ||
|
||
const tileStyles = { background: backgroundLight }; | ||
const detailsStyles = { color: 'white' }; | ||
|
||
export default function SplitLinePathTile() { | ||
return ( | ||
<GalleryTile<SplitLinePathProps> | ||
title="SplitLinePath" | ||
description="<Shape.SplitLinePath />" | ||
exampleRenderer={SplitLinePath} | ||
exampleUrl="/splitlinepath" | ||
tileStyles={tileStyles} | ||
detailsStyles={detailsStyles} | ||
detailsHeight={0} | ||
/> | ||
); | ||
} |
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,17 @@ | ||
import React from 'react'; | ||
import Show from '../components/Show'; | ||
import SplitLinePath from '../sandboxes/vx-shape-splitlinepath/Example'; | ||
import StatsPlotSource from '!!raw-loader!../sandboxes/vx-shape-splitlinepath/Example'; | ||
import packageJson from '../sandboxes/vx-shape-splitlinepath/package.json'; | ||
|
||
export default () => ( | ||
<Show | ||
events | ||
component={SplitLinePath} | ||
title="SplitLinePath" | ||
codeSandboxDirectoryName="vx-shape-splitlinepath" | ||
packageJson={packageJson} | ||
> | ||
{StatsPlotSource} | ||
</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
127 changes: 127 additions & 0 deletions
127
packages/vx-demo/src/sandboxes/vx-shape-splitlinepath/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,127 @@ | ||
import React, { useMemo } from 'react'; | ||
import { scaleLinear } from '@vx/scale'; | ||
import { curveCardinal } from '@vx/curve'; | ||
import { LinePath, SplitLinePath } from '@vx/shape'; | ||
import { LinearGradient } from '@vx/gradient'; | ||
|
||
import generateSinPoints from './generateSinPoints'; | ||
|
||
type Point = { x: number; y: number }; | ||
const getX = (d: Point) => d.x; | ||
const getY = (d: Point) => d.y; | ||
export const background = '#045275'; | ||
export const backgroundLight = '#089099'; | ||
export const foreground = '#b7e6a5'; | ||
|
||
export type SplitLinePathProps = { | ||
width: number; | ||
height: number; | ||
margin?: { top: number; right: number; bottom: number; left: number }; | ||
numberOfWaves?: number; | ||
pointsPerWave?: number; | ||
numberOfSegments?: number; | ||
}; | ||
|
||
export default function SplitPath({ | ||
width, | ||
height, | ||
numberOfWaves = 10, | ||
pointsPerWave = 100, | ||
numberOfSegments = 8, | ||
}: SplitLinePathProps) { | ||
const data = useMemo(() => generateSinPoints({ width, height, numberOfWaves, pointsPerWave }), [ | ||
width, | ||
height, | ||
numberOfWaves, | ||
pointsPerWave, | ||
]); | ||
|
||
const dividedData = useMemo(() => { | ||
const segmentLength = Math.floor(data.length / numberOfSegments); | ||
return new Array(numberOfSegments) | ||
.fill(null) | ||
.map((_, i) => data.slice(i * segmentLength, (i + 1) * segmentLength)); | ||
}, [numberOfSegments, data]); | ||
|
||
const getScaledX = useMemo(() => { | ||
const xScale = scaleLinear({ range: [0, width], domain: [0, width] }); | ||
return (d: Point) => xScale(getX(d)); | ||
}, [width]); | ||
|
||
const getScaledY = useMemo(() => { | ||
const yScale = scaleLinear({ range: [0, height], domain: [height, 0] }); | ||
return (d: Point) => yScale(getY(d)); | ||
}, [height]); | ||
|
||
return width < 10 ? null : ( | ||
<div> | ||
<svg width={width} height={height}> | ||
<LinearGradient | ||
id="vx-shape-splitlinepath-gradient" | ||
from={background} | ||
to={backgroundLight} | ||
fromOpacity={0.8} | ||
toOpacity={0.8} | ||
/> | ||
<rect | ||
x={0} | ||
y={0} | ||
width={width} | ||
height={height} | ||
fill="url(#vx-shape-splitlinepath-gradient)" | ||
rx={14} | ||
/> | ||
|
||
<g transform={`rotate(${0})translate(${-0}, ${-height * 0.5})`}> | ||
<LinePath | ||
data={data} | ||
x={getScaledX} | ||
y={getScaledY} | ||
strokeWidth={8} | ||
stroke="#fff" | ||
strokeOpacity={0.15} | ||
curve={curveCardinal} | ||
/> | ||
|
||
<SplitLinePath | ||
segments={dividedData} | ||
x={getScaledX} | ||
y={getScaledY} | ||
curve={curveCardinal} | ||
styles={[ | ||
{ stroke: foreground, strokeWidth: 3 }, | ||
{ stroke: '#fff', strokeWidth: 2, strokeDasharray: '9,5' }, | ||
{ stroke: background, strokeWidth: 2 }, | ||
]} | ||
> | ||
{({ segment, styles, index }) => | ||
/** overlay circles to a couple of the segments */ | ||
index === numberOfSegments - 1 || index === 2 ? ( | ||
segment.map(({ x, y }, i) => | ||
i % 8 === 0 ? ( | ||
<circle | ||
key={i} | ||
cx={x} | ||
cy={y} | ||
r={10 * (i / segment.length)} | ||
stroke={styles?.stroke} | ||
fill="transparent" | ||
strokeWidth={1} | ||
/> | ||
) : null, | ||
) | ||
) : ( | ||
<LinePath | ||
data={segment} | ||
x={(d: Point) => d.x || 0} | ||
y={(d: Point) => d.y || 0} | ||
{...styles} | ||
/> | ||
) | ||
} | ||
</SplitLinePath> | ||
</g> | ||
</svg> | ||
</div> | ||
); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/vx-demo/src/sandboxes/vx-shape-splitlinepath/generateSinPoints.ts
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,33 @@ | ||
/** generates points along a sin wave, with increasing height toward the center. */ | ||
export default function generateSinPoints({ | ||
width, | ||
height, | ||
numberOfWaves = 10, | ||
pointsPerWave = 10, | ||
}: { | ||
width: number; | ||
height: number; | ||
numberOfWaves?: number; | ||
pointsPerWave?: number; | ||
}) { | ||
const waveLength = width / numberOfWaves; | ||
const distanceBetweenPoints = waveLength / pointsPerWave; | ||
const sinPoints: { x: number; y: number }[] = []; | ||
|
||
for (let waveIndex = 0; waveIndex <= numberOfWaves; waveIndex += 1) { | ||
const waveDistFromStart = waveIndex * waveLength; | ||
|
||
for (let pointIndex = 0; pointIndex <= pointsPerWave; pointIndex += 1) { | ||
const waveXFraction = pointIndex / pointsPerWave; | ||
const waveX = pointIndex * distanceBetweenPoints; | ||
const globalX = waveDistFromStart + waveX; | ||
// scale height based x position | ||
const globalXFraction = (width - globalX) / width; | ||
const waveHeight = Math.min(globalXFraction, 1 - globalXFraction) * height; | ||
|
||
sinPoints.push({ x: globalX, y: waveHeight * Math.sin(waveXFraction * (2 * Math.PI)) }); | ||
} | ||
} | ||
|
||
return sinPoints; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/vx-demo/src/sandboxes/vx-shape-splitlinepath/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,11 @@ | ||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
import ParentSize from '@vx/responsive/lib/components/ParentSize'; | ||
|
||
import Example from './Example'; | ||
import './sandbox-styles.css'; | ||
|
||
render( | ||
<ParentSize>{({ width, height }) => <Example width={width} height={height} />}</ParentSize>, | ||
document.getElementById('root'), | ||
); |
27 changes: 27 additions & 0 deletions
27
packages/vx-demo/src/sandboxes/vx-shape-splitlinepath/package.json
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,27 @@ | ||
{ | ||
"name": "@vx/demo-shape-splitlinepath", | ||
"description": "Standalone vx splitlinepath demo.", | ||
"main": "index.tsx", | ||
"private": true, | ||
"dependencies": { | ||
"@babel/runtime": "^7.8.4", | ||
"@types/react": "^16", | ||
"@types/react-dom": "^16", | ||
"@vx/curve": "latest", | ||
"@vx/gradient": "latest", | ||
"@vx/responsive": "latest", | ||
"@vx/scale": "latest", | ||
"@vx/shape": "latest", | ||
"react": "^16", | ||
"react-dom": "^16", | ||
"react-scripts-ts": "3.1.0", | ||
"typescript": "^3" | ||
}, | ||
"keywords": [ | ||
"visualization", | ||
"d3", | ||
"react", | ||
"vx", | ||
"splitline" | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/vx-demo/src/sandboxes/vx-shape-splitlinepath/sandbox-styles.css
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,8 @@ | ||
html, | ||
body, | ||
#root { | ||
height: 100%; | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, | ||
'Open Sans', 'Helvetica Neue', sans-serif; | ||
line-height: 2em; | ||
} |
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
Oops, something went wrong.