Skip to content

Commit

Permalink
Merge pull request #179 from hshoff/harry-add-hier
Browse files Browse the repository at this point in the history
[demo][shape][hierarchy][mock] updates for new hierarchies
  • Loading branch information
hshoff authored Oct 25, 2017
2 parents 30bc545 + d6511f1 commit 3d7e126
Show file tree
Hide file tree
Showing 21 changed files with 7,790 additions and 84 deletions.
70 changes: 63 additions & 7 deletions packages/vx-demo/components/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import BarGroup from '../components/tiles/bargroup';
import BarStack from '../components/tiles/barstack';
import Heatmap from '../components/tiles/heatmap';
import LineRadial from '../components/tiles/lineradial';
import Arcs from '../components/tiles/arc';
import Pies from '../components/tiles/pie';
import Trees from '../components/tiles/tree';
import Cluster from '../components/tiles/dendrogram';
import Voronoi from '../components/tiles/voronoi';
Expand All @@ -28,6 +28,8 @@ import BoxPlot from '../components/tiles/boxplot';
import GeoMercator from '../components/tiles/geo-mercator';
import Network from '../components/tiles/network';
import Streamgraph from '../components/tiles/streamgraph';
import Pack from '../components/tiles/pack';
import Treemap from '../components/tiles/treemap';

const items = [
'#242424',
Expand Down Expand Up @@ -93,6 +95,8 @@ export default class Gallery extends React.Component {
const t18 = this.state.dimensions[17] || [8, 300];
const t19 = this.state.dimensions[18] || [8, 300];
const t20 = this.state.dimensions[19] || [8, 300];
const t21 = this.state.dimensions[20] || [8, 300];
const t22 = this.state.dimensions[21] || [8, 300];

return (
<div>
Expand Down Expand Up @@ -400,19 +404,19 @@ export default class Gallery extends React.Component {
</Link>
</Tilt>
<Tilt className="tilt" options={{ max: 8, scale: 1 }}>
<Link prefetch href="/arcs">
<Link prefetch href="/pies">
<div
className="gallery-item"
style={{ background: '#c94acc' }}
ref={d => this.nodes.add(d)}
>
<div className="image">
<Arcs width={t14[0]} height={t14[1]} />
<Pies width={t14[0]} height={t14[1]} />
</div>
<div className="details" style={{ color: 'white' }}>
<div className="title">Arcs</div>
<div className="title">Pies</div>
<div className="description">
<pre>{`<Shape.Arc />`}</pre>
<pre>{`<Shape.Pie />`}</pre>
</div>
</div>
</div>
Expand Down Expand Up @@ -595,8 +599,60 @@ export default class Gallery extends React.Component {
</div>
</Link>
</Tilt>
<div className="gallery-item placeholder" />
<div className="gallery-item placeholder" />
<Tilt className="tilt" options={{ max: 8, scale: 1 }}>
<Link prefetch href="/pack">
<div
className="gallery-item"
ref={d => this.nodes.add(d)}
style={{
background: '#ffffff',
boxShadow: 'rgba(0, 0, 0, 0.1) 0px 1px 6px',
}}
>
<div className="image">
<Pack width={t21[0]} height={t21[1]} />
</div>
<div
className="details"
style={{
color: '#fd6c6f',
}}
>
<div className="title">Pack</div>
<div className="description">
<pre>{`<Hierarchy.Pack />`}</pre>
</div>
</div>
</div>
</Link>
</Tilt>
<Tilt className="tilt" options={{ max: 8, scale: 1 }}>
<Link prefetch href="/treemap">
<div
className="gallery-item"
ref={d => this.nodes.add(d)}
style={{
background: '#3436b8',
}}
>
<div className="image">
<Treemap width={t22[0]} height={t22[1]} />
</div>
<div
className="details"
style={{
color: '#00ff70',
}}
>
<div className="title">Treemap</div>
<div className="description">
<pre>{`<Hierarchy.Treemap />`}</pre>
</div>
</div>
</div>
</Link>
</Tilt>
{false && <div className="gallery-item placeholder" />}
</div>

<div>
Expand Down
73 changes: 73 additions & 0 deletions packages/vx-demo/components/tiles/pack.js
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>
);
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { Arc } from '@vx/shape';
import { Pie } from '@vx/shape';
import { Group } from '@vx/group';
import { GradientPinkBlue } from '@vx/gradient';
import { letterFrequency, browserUsage } from '@vx/mock-data';

const letters = letterFrequency.slice(0, 4);
const browsers = Object.keys(browserUsage[0])
.filter(k => k !== "date")
.filter(k => k !== 'date')
.map(k => ({ label: k, usage: browserUsage[0][k] }));

function Label({ x, y, children }) {
Expand All @@ -33,7 +33,7 @@ export default ({
left: 20,
right: 20,
bottom: 110,
}
},
}) => {
if (width < 10) return null;
const radius = Math.min(width, height) / 2;
Expand All @@ -49,34 +49,42 @@ export default ({
fill="url('#gradients')"
/>
<Group top={height / 2 - margin.top} left={width / 2}>
<Arc
<Pie
data={browsers}
pieValue={d => d.usage}
outerRadius={radius - 80}
innerRadius={radius - 120}
fill="white"
fillOpacity={d => 1 / (d.index + 2) }
fillOpacity={d => 1 / (d.index + 2)}
cornerRadius={3}
padAngle={0}
centroid={(centroid, arc) => {
const [x, y] = centroid;
const { startAngle, endAngle } = arc;
if (endAngle - startAngle < .1) return null;
return <Label x={x} y={y}>{arc.data.label}</Label>;
if (endAngle - startAngle < 0.1) return null;
return (
<Label x={x} y={y}>
{arc.data.label}
</Label>
);
}}
/>
<Arc
<Pie
data={letters}
pieValue={d => d.frequency}
outerRadius={radius - 135}
fill="black"
fillOpacity={d => 1 / (d.index + 2) }
fillOpacity={d => 1 / (d.index + 2)}
centroid={(centroid, arc) => {
const [x, y] = centroid;
return <Label x={x} y={y}>{arc.data.letter}</Label>;
return (
<Label x={x} y={y}>
{arc.data.letter}
</Label>
);
}}
/>
</Group>
</svg>
);
}
};
98 changes: 98 additions & 0 deletions packages/vx-demo/components/tiles/treemap.js
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>
);
};
9 changes: 5 additions & 4 deletions packages/vx-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,20 @@
"d3-collection": "^1.0.4",
"d3-format": "^1.2.0",
"d3-hierarchy": "^1.1.4",
"d3-interpolate": "^1.1.5",
"d3-scale": "^1.0.6",
"d3-shape": "^1.0.6",
"d3-time-format": "^2.0.5",
"next": "2.2.0",
"next": "^4.1.3",
"nprogress": "^0.2.0",
"prismjs": "^1.6.0",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-ga": "^2.1.2",
"react-github-button": "^0.1.10",
"react-motion": "^0.4.7",
"react-tilt": "^0.1.3",
"recompose": "^0.23.0",
"recompose": "^0.26.0",
"topojson-client": "^3.0.0"
},
"browserify": {
Expand Down
1 change: 0 additions & 1 deletion packages/vx-demo/pages/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ export default () => (
flex-direction: column;
background: white;
padding: 1rem;
border-bottom: 1rem solid #fc2e1c;
}
.home h1 {
Expand Down
Loading

0 comments on commit 3d7e126

Please sign in to comment.