Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(vx-demo): convert Pack demo to codesandbox #673

Merged
merged 1 commit into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vx-demo/src/components/Gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Network from './tiles/Network';
import Streamgraph, {
BACKGROUND as streamgraphBackgroundColor,
} from '../docs-v2/examples/vx-streamgraph/Example';
import Pack from './tiles/Pack';
import Pack from '../docs-v2/examples/vx-pack/Example';
import Patterns from '../docs-v2/examples/vx-pattern/Example';
import Treemap from './tiles/Treemap';
import Radar from './tiles/Radar';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Group } from '@vx/group';
import { Pack, hierarchy } from '@vx/hierarchy';
import { scaleQuantize } from '@vx/scale';
import rawData, { Exoplanets as Datum } from '@vx/mock-data/lib/mocks/exoplanets';
import { ShowProvidedProps } from '../../types';

function extent<D>(allData: D[], value: (d: D) => number): [number, number] {
return [Math.min(...allData.map(value)), Math.max(...allData.map(value))];
Expand All @@ -21,48 +20,44 @@ const root = hierarchy<Datum>(pack)
.sum(d => d.radius * d.radius)
.sort(
(a, b) =>
// sort by hierarchy, then distance
(a && a.data ? 1 : -1) - (b && b.data ? 1 : -1) ||
(a.children ? 1 : -1) - (b.children ? 1 : -1) ||
(a.data.distance == null ? -1 : 1) - (b.data.distance == null ? -1 : 1) ||
a.data.distance! - b.data.distance!,
);

export default ({
width,
height,
margin = {
top: 10,
left: 30,
right: 40,
bottom: 80,
},
}: ShowProvidedProps) => {
if (width < 10) return null;
const defaultMargin = { top: 10, left: 30, right: 40, bottom: 80 };

return (
type Props = {
width: number;
height: number;
margin?: { top: number; right: number; bottom: number; left: number };
};

export default function Example({ width, height, margin = defaultMargin }: Props) {
return width < 10 ? null : (
<svg width={width} height={height}>
<rect width={width} height={height} rx={14} fill="#ffffff" />

<Pack<Datum> root={root} size={[width * 2, height * 2]}>
{packData => {
const circles = packData.descendants().slice(2);
const circles = packData.descendants().slice(2); // skip outer hierarchies
return (
<Group top={-height - margin.bottom} left={-width / 2}>
{circles.map((circle, i) => {
return (
<circle
key={`cir-${i}`}
r={circle.r}
cx={circle.x}
cy={circle.y}
fill={colorScale(circle.data.radius)}
/>
);
})}
{circles.map((circle, i) => (
<circle
key={`circle-${i}`}
r={circle.r}
cx={circle.x}
cy={circle.y}
fill={colorScale(circle.data.radius)}
/>
))}
</Group>
);
}}
</Pack>
</svg>
);
};
}
11 changes: 11 additions & 0 deletions packages/vx-demo/src/docs-v2/examples/vx-pack/index.tsx
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 packages/vx-demo/src/docs-v2/examples/vx-pack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@vx/demo-pack",
"description": "Standalone vx hierarchy pack demo.",
"main": "index.tsx",
"dependencies": {
"@babel/runtime": "^7.8.4",
"@types/react": "^16",
"@types/react-dom": "^16",
"@vx/group": "latest",
"@vx/hierarchy": "latest",
"@vx/mock-data": "latest",
"@vx/responsive": "latest",
"@vx/scale": "latest",
"react": "^16",
"react-dom": "^16",
"react-scripts-ts": "3.1.0",
"typescript": "^3"
},
"keywords": [
"visualization",
"d3",
"react",
"vx",
"hierarchy",
"pack"
]
}
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;
}
16 changes: 7 additions & 9 deletions packages/vx-demo/src/pages/Pack.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React from 'react';
import Show from '../components/Show';
import Pack from '../components/tiles/Pack';
import PackSource from '!!raw-loader!../components/tiles/Pack';
import Pack from '../docs-v2/examples/vx-pack/Example';
import PackSource from '!!raw-loader!../docs-v2/examples/vx-pack/Example';

export default () => {
return (
<Show component={Pack} title="Pack">
{PackSource}
</Show>
);
};
export default () => (
<Show component={Pack} title="Pack" codeSandboxDirectoryName="vx-pack">
{PackSource}
</Show>
);