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

Add Pack, Partition, and Treemap hierarchical layouts. Closes #172 #173

Merged
merged 2 commits into from
Oct 12, 2017
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
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@
"scripts": {
"test": "lerna exec npm install && jest",
"docs": "node ./scripts/docs/index.js",
"prepare-release":
"git checkout master && git pull --rebase origin master && npm run docs && lerna updated",
"prepare-release": "git checkout master && git pull --rebase origin master && npm run docs && lerna updated",
"release": "npm run prepare-release && lerna publish --exact"
},
"jest": {
"projects": ["<rootDir>/packages/*"],
"projects": [
"<rootDir>/packages/*"
],
"collectCoverage": true,
"coverageDirectory": "<rootDir>/coverage",
"coveragePathIgnorePatterns": ["/node_modules/"],
"coverageReporters": ["text", "lcov"]
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"coverageReporters": [
"text",
"lcov"
]
},
"keywords": ["react", "d3", "visualization", "vx", "charts"],
"keywords": [
"react",
"d3",
"visualization",
"vx",
"charts"
],
"author": "@hshoff",
"license": "MIT",
"devDependencies": {
Expand Down
9 changes: 4 additions & 5 deletions packages/vx-hierarchy/src/hierarchies/Cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ export default function Cluster({
if (size) cluster.size(size);
if (nodeSize) cluster.nodeSize(nodeSize);
if (separation) cluster.separation(separation);

const data = cluster(root);
const links = data.links();
const descendants = root.descendants();

if (!!children) {
return (
Expand All @@ -39,7 +38,7 @@ export default function Cluster({
left={left}
className={cx('vx-cluster', className)}
>
{children({ data, links, root, descendants })}
{children({ data })}
</Group>
);
}
Expand All @@ -51,15 +50,15 @@ export default function Cluster({
className={cx('vx-cluster', className)}
>
{linkComponent &&
links.map((link, i) => {
data.links().map((link, i) => {
return (
<Group key={`cluster-link-${i}`}>
{React.createElement(linkComponent, { link })}
</Group>
);
})}
{nodeComponent &&
descendants.map((node, i) => {
data.descendants().map((node, i) => {
return (
<Group key={`cluster-node-${i}`}>
{React.createElement(nodeComponent, { node })}
Expand Down
52 changes: 52 additions & 0 deletions packages/vx-hierarchy/src/hierarchies/Pack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { Group } from '@vx/group';
import { pack as d3pack } from 'd3-hierarchy';
import DefaultNode from '../HierarchyDefaultNode';

Pack.propTypes = {
root: PropTypes.object.isRequired,
children: PropTypes.func
};

export default function Pack({
top,
left,
className,
root,
radius,
size,
padding,
children,
nodeComponent = DefaultNode,
...restProps
}) {
const pack = d3pack();
if (size) pack.size(size);
if (radius) pack.radius(radius);
if (padding) pack.padding(padding);

const data = pack(root);

if (!!children) {
return (
<Group top={top} left={left} className={cx('vx-pack', className)}>
{children({ data })}
</Group>
);
}

return (
<Group top={top} left={left} className={cx('vx-pack', className)}>
{nodeComponent &&
data.descendants().map((node, i) => {
return (
<Group key={`pack-node-${i}`}>
{React.createElement(nodeComponent, { node })}
</Group>
);
})}
</Group>
);
}
52 changes: 52 additions & 0 deletions packages/vx-hierarchy/src/hierarchies/Partition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { Group } from '@vx/group';
import { partition as d3partition } from 'd3-hierarchy';
import DefaultNode from '../HierarchyDefaultNode';

Partition.propTypes = {
root: PropTypes.object.isRequired,
children: PropTypes.func
};

export default function Partition({
top,
left,
className,
root,
size,
round,
padding,
children,
nodeComponent = DefaultNode,
...restProps
}) {
const partition = d3partition();
if (size) partition.size(size);
if (round) partition.round(round);
if (padding) partition.padding(padding);

const data = partition(root);

if (!!children) {
return (
<Group top={top} left={left} className={cx('vx-pack', className)}>
{children({ data })}
</Group>
);
}

return (
<Group top={top} left={left} className={cx('vx-pack', className)}>
{nodeComponent &&
data.descendants().map((node, i) => {
return (
<Group key={`pack-node-${i}`}>
{React.createElement(nodeComponent, { node })}
</Group>
);
})}
</Group>
);
}
8 changes: 3 additions & 5 deletions packages/vx-hierarchy/src/hierarchies/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export default function Tree({
if (separation) tree.separation(separation);

const data = tree(root);
const links = data.links();
const descendants = root.descendants();

if (!!children) {
return (
Expand All @@ -40,23 +38,23 @@ export default function Tree({
left={left}
className={cx('vx-tree', className)}
>
{children({ data, links, root, descendants })}
{children({ data })}
</Group>
);
}

return (
<Group top={top} left={left} className={cx('vx-tree', className)}>
{linkComponent &&
links.map((link, i) => {
data.links().map((link, i) => {
return (
<Group key={`tree-link-${i}`}>
{React.createElement(linkComponent, { link })}
</Group>
);
})}
{nodeComponent &&
descendants.map((node, i) => {
data.descendants().map((node, i) => {
return (
<Group key={`tree-node-${i}`}>
{React.createElement(nodeComponent, { node })}
Expand Down
66 changes: 66 additions & 0 deletions packages/vx-hierarchy/src/hierarchies/Treemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { Group } from '@vx/group';
import { treemap as d3treemap } from 'd3-hierarchy';
import DefaultNode from '../HierarchyDefaultNode';

Treemap.propTypes = {
root: PropTypes.object.isRequired,
children: PropTypes.func
};

export default function Treemap({
top,
left,
className,
root,
tile,
size,
round,
padding,
paddingInner,
paddingOuter,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
children,
nodeComponent = DefaultNode,
...restProps
}) {
const treemap = d3treemap();
if (tile) treemap.tile(tile);
if (size) treemap.size(size);
if (round) treemap.round(round);
if (padding) treemap.padding(padding);
if (paddingInner) treemap.paddingInner(paddingInner);
if (paddingOuter) treemap.paddingOuter(paddingOuter);
if (paddingTop) treemap.paddingTop(paddingTop);
if (paddingRight) treemap.paddingRight(paddingRight);
if (paddingBottom) treemap.paddingBottom(paddingBottom);
if (paddingLeft) treemap.paddingLeft(paddingLeft);

const data = treemap(root);

if (!!children) {
return (
<Group top={top} left={left} className={cx('vx-treemap', className)}>
{children({ data })}
</Group>
);
}

return (
<Group top={top} left={left} className={cx('vx-treemap', className)}>
{nodeComponent &&
data.descendants().map((node, i) => {
return (
<Group key={`treemap-node-${i}`}>
{React.createElement(nodeComponent, { node })}
</Group>
);
})}
</Group>
);
}