-
Notifications
You must be signed in to change notification settings - Fork 715
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 #173 from techniq/master
Add Pack, Partition, and Treemap hierarchical layouts. Closes #172
- Loading branch information
Showing
6 changed files
with
195 additions
and
16 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
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,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> | ||
); | ||
} |
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,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> | ||
); | ||
} |
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,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> | ||
); | ||
} |