-
Notifications
You must be signed in to change notification settings - Fork 720
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 #179 from hshoff/harry-add-hier
[demo][shape][hierarchy][mock] updates for new hierarchies
- Loading branch information
Showing
21 changed files
with
7,790 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react'; | ||
import { Group } from '@vx/group'; | ||
import { Pack } from '@vx/hierarchy'; | ||
import { hierarchy } from 'd3-hierarchy'; | ||
import { LinearGradient } from '@vx/gradient'; | ||
import { scaleQuantize } from '@vx/scale'; | ||
import { exoplanets as data } from '@vx/mock-data'; | ||
import { extent } from 'd3-array'; | ||
|
||
const exoplanets = data.filter(d => d.distance === 0); | ||
const planets = data.filter(d => d.distance !== 0); | ||
const raw = { children: [{ children: planets }].concat(exoplanets) }; | ||
|
||
const color = scaleQuantize({ | ||
range: [ | ||
'#49f4e7', | ||
'#11d2f9', | ||
'#855af2', | ||
'#fd6d6f', | ||
'#ffc10e', | ||
'#ffe108', | ||
].reverse(), | ||
domain: extent(data, d => +d.radius), | ||
}); | ||
|
||
export default ({ | ||
width, | ||
height, | ||
events = false, | ||
margin = { | ||
top: 10, | ||
left: 30, | ||
right: 40, | ||
bottom: 80, | ||
}, | ||
}) => { | ||
if (width < 10) return null; | ||
const data = hierarchy(raw) | ||
.sum(d => d.radius * d.radius) | ||
.sort((a, b) => { | ||
return ( | ||
!a.children - !b.children || | ||
isNaN(a.data.distance) - isNaN(b.data.distance) || | ||
a.data.distance - b.data.distance | ||
); | ||
}); | ||
return ( | ||
<svg width={width} height={height}> | ||
<rect width={width} height={height} rx={14} fill="#ffffff" /> | ||
<Pack | ||
top={-height - margin.bottom} | ||
left={-width / 2} | ||
root={data} | ||
size={[width * 2, height * 2]} | ||
> | ||
{({ data }) => { | ||
const desc = data.descendants().slice(2); | ||
return desc.map((d, i) => { | ||
return ( | ||
<circle | ||
key={`cir-${i}`} | ||
r={d.r} | ||
cx={d.x} | ||
cy={d.y} | ||
fill={color(+d.data.radius)} | ||
/> | ||
); | ||
}); | ||
}} | ||
</Pack> | ||
</svg> | ||
); | ||
}; |
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,98 @@ | ||
import React from 'react'; | ||
import { Group } from '@vx/group'; | ||
import { Treemap } from '@vx/hierarchy'; | ||
import { hierarchy, stratify } from 'd3-hierarchy'; | ||
import { shakespeare } from '@vx/mock-data'; | ||
import { treemapSquarify } from 'd3-hierarchy'; | ||
import { scaleLinear } from '@vx/scale'; | ||
import { interpolateRgb } from 'd3-interpolate'; | ||
|
||
export default ({ | ||
width, | ||
height, | ||
events = false, | ||
margin = { | ||
top: 0, | ||
left: 30, | ||
right: 40, | ||
bottom: 80, | ||
}, | ||
}) => { | ||
if (width < 10) return null; | ||
const color = scaleLinear({ | ||
domain: [0, Math.max(...shakespeare.map(d => d.size || 0))], | ||
range: ['#0373d9', '#00ff70'], | ||
}); | ||
const nodes = stratify() | ||
.id(d => d.id) | ||
.parentId(d => d.parent)(shakespeare).sum(d => d.size || 0); | ||
const data = hierarchy(nodes).sort((a, b) => b.value - a.value); | ||
return ( | ||
<svg width={width} height={height}> | ||
<rect width={width} height={height} rx={14} fill="#3436b8" /> | ||
<Treemap | ||
top={margin.top} | ||
root={data} | ||
size={[width, height - margin.top - margin.bottom]} | ||
tile={treemapSquarify} | ||
round={true} | ||
> | ||
{({ data }) => { | ||
return ( | ||
<Group> | ||
{data | ||
.descendants() | ||
.reverse() | ||
.map((node, i) => ( | ||
<Group | ||
top={node.y0} | ||
left={node.x0} | ||
key={`node-${i}`} | ||
> | ||
{node.depth == 1 && ( | ||
<rect | ||
id={`rect-${i}`} | ||
width={node.x1 - node.x0} | ||
height={node.y1 - node.y0} | ||
fill={'transparent'} | ||
stroke={'#3436b8'} | ||
strokeWidth={4} | ||
/> | ||
)} | ||
{node.depth > 2 && ( | ||
<rect | ||
id={`rect-${i}`} | ||
width={node.x1 - node.x0} | ||
height={node.y1 - node.y0} | ||
fill={color(node.value)} | ||
stroke={'#3436b8'} | ||
/> | ||
)} | ||
|
||
<clipPath id={`clip-${i}`}> | ||
<use xlinkHref={`#rect-${i}`} /> | ||
</clipPath> | ||
|
||
{node.depth === 1 && ( | ||
<text | ||
x={(node.x1 - node.x0) / 2} | ||
y={(node.y1 - node.y0) / 2} | ||
dx={-30} | ||
fill={'white'} | ||
clipPath={`url(#clip-${i})`} | ||
style={{ | ||
font: '18px sans-serif', | ||
}} | ||
> | ||
{node.data.id} | ||
</text> | ||
)} | ||
</Group> | ||
))} | ||
</Group> | ||
); | ||
}} | ||
</Treemap> | ||
</svg> | ||
); | ||
}; |
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.