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 'sidePadding' property to the treemap #1224

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/treemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ Height of the component.

Type: `number`

The padding between cells the cells of the heatmap in pixels.
The padding between the cells of the heatmap in pixels.

#### sidePadding (optional)

Type: `Object` of `number`

Directions, in which additional side padding is applied.
Possible keys for object are `['left', 'right', 'top', 'bottom']`.

#### data

Expand Down
38 changes: 35 additions & 3 deletions src/treemap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ function _getScaleFns(props) {
};
}

/**
* Adds padding in desired directions to treemapingFunction.
* @param {Function} treemapingFunction function to update.
* @param {Object} sidePadding .
* @returns {Function} treemapingFunction, updated with side padding.
* @private
*/
function _applySidePadding(treemapingFunction, sidePadding) {
if (!sidePadding) {
return treemapingFunction;
}
const directionToProperty = {
'left': 'paddingLeft',
'right': 'paddingRight',
'bottom': 'paddingBottom',
'top': 'paddingTop',
};
Object.entries(sidePadding).forEach(entry => {
const [ direction, padding ] = entry;
const functionProperty = directionToProperty[direction];
treemapingFunction = treemapingFunction[functionProperty](padding);
});
return treemapingFunction;
}

class Treemap extends React.Component {
constructor(props) {
super(props);
Expand All @@ -114,7 +139,7 @@ class Treemap extends React.Component {
*/
_getNodesToRender() {
const {innerWidth, innerHeight} = this.state;
const {data, mode, padding, sortFunction, getSize} = this.props;
const {data, mode, padding, sortFunction, getSize, sidePadding} = this.props;
if (!data) {
return [];
}
Expand Down Expand Up @@ -153,10 +178,10 @@ class Treemap extends React.Component {
}

const tileFn = TREEMAP_TILE_MODES[mode];
const treemapingFunction = treemap(tileFn)
const treemapingFunction = _applySidePadding(treemap(tileFn)
.tile(tileFn)
.size([innerWidth, innerHeight])
.padding(padding);
.padding(padding), sidePadding);
const structuredInput = hierarchy(data)
.sum(getSize)
.sort((a, b) => sortFunction(a, b, getSize));
Expand Down Expand Up @@ -188,6 +213,12 @@ Treemap.propTypes = {
onLeafMouseOut: PropTypes.func,
useCirclePacking: PropTypes.bool,
padding: PropTypes.number.isRequired,
sidePadding: PropTypes.shape({
'left': PropTypes.number,
'right': PropTypes.number,
'top': PropTypes.number,
'bottom': PropTypes.number,
}),
sortFunction: PropTypes.func,
width: PropTypes.number.isRequired,
getSize: PropTypes.func,
Expand All @@ -210,6 +241,7 @@ Treemap.defaultProps = {
opacityType: OPACITY_TYPE,
_opacityValue: DEFAULT_OPACITY,
padding: 1,
sidePadding: null,
sortFunction: (a, b, accessor) => {
if (!accessor) {
return 0;
Expand Down