Skip to content

Commit

Permalink
feat(treemap): get rid of data nesting when using animated treemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Aug 7, 2017
1 parent b687ec8 commit 507dcb6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/components/charts/treemap/TreeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const createNodes = ({
width={node.style.width}
height={node.style.height}
fill={node.style.color}
stroke={borderColor(node.data)}
stroke={borderColor({ ...node.data, color: node.style.color })}
strokeWidth={borderWidth}
/>
{shouldRenderLabel &&
Expand All @@ -63,7 +63,7 @@ const createNodes = ({
textAnchor="middle"
dy="0.5em"
style={{
fill: textColor(node.data),
fill: textColor({ ...node.data, color: node.style.color }),
}}
>
{label(node.data)}
Expand Down
18 changes: 8 additions & 10 deletions src/components/charts/treemap/TreeMapHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ const createNodes = ({
justifyContent: 'center',
borderWidth: borderWidth,
borderStyle: 'solid',
borderColor: borderColorFn(node.data),
borderColor: borderColorFn({ ...node.data, color: node.style.color }),
}}
>
{shouldRenderLabel &&
<span
className="nivo_treemap_node_label"
style={{
color: textColorFn(node.data),
color: textColorFn({ ...node.data, color: node.style.color }),
transform: `rotate(${rotate ? '-90' : '0'}deg)`,
}}
>
{label(node.data.data)}
{label(node.data)}
</span>}
</div>
)
Expand All @@ -79,7 +79,11 @@ const createNodes = ({
}
}

class TreeMapHTML extends Component {
export default class TreeMapHTML extends Component {
static propTypes = _.omit(treeMapPropTypes, ['children', 'namespace'])

static defaultProps = _.omit(treeMapDefaultProps, [])

render() {
return (
<TreeMapPlaceholders {...this.props} namespace="html">
Expand All @@ -88,9 +92,3 @@ class TreeMapHTML extends Component {
)
}
}

TreeMapHTML.propTypes = _.omit(treeMapPropTypes, ['children', 'namespace'])

TreeMapHTML.defaultProps = _.omit(treeMapDefaultProps, [])

export default TreeMapHTML
46 changes: 20 additions & 26 deletions src/components/charts/treemap/TreeMapPlaceholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@ import { findDOMNode } from 'react-dom'
import _ from 'lodash'
import { TransitionMotion, spring } from 'react-motion'
import Nivo from '../../../Nivo'
import TreeMapHelper from '../../../lib/charts/treemap/TreeMapHelper'
import { computeTreeMap } from '../../../lib/charts/treemap/TreeMapHelper'
import { convertGetter } from '../../../lib/propertiesConverters'
import { treeMapPropTypes, treeMapDefaultProps } from './TreeMapProps'
import { getColorsGenerator, extractRGB } from '../../../lib/colorUtils'

class TreeMapPlaceholders extends Component {
componentWillMount() {
this.treemap = TreeMapHelper()
}

willEnter({ data: node }) {
export default class TreeMapPlaceholders extends Component {
static propTypes = _.omit(treeMapPropTypes, [
'orientLabels',
'skipVMin',
'transitionDuration',
'transitionEasing',
])

static defaultProps = _.omit(treeMapDefaultProps, [
'orientLabels',
'skipVMin',
'transitionDuration',
'transitionEasing',
])

nodeWillEnter({ data: node }) {
const width = node.x1 - node.x0
const height = node.y1 - node.y0

Expand Down Expand Up @@ -92,7 +102,7 @@ class TreeMapPlaceholders extends Component {
}
}

const nodes = this.treemap.compute({
const nodes = computeTreeMap({
width,
height,
root,
Expand Down Expand Up @@ -141,11 +151,11 @@ class TreeMapPlaceholders extends Component {
wrapperTag,
wrapperProps,
<TransitionMotion
willEnter={this.willEnter}
willEnter={this.nodeWillEnter}
styles={nodes.map(node => {
return {
key: node.data.key,
data: node,
data: node.data,
style: {
x: spring(node.x0, springConfig),
y: spring(node.y0, springConfig),
Expand Down Expand Up @@ -184,19 +194,3 @@ class TreeMapPlaceholders extends Component {
)
}
}

TreeMapPlaceholders.propTypes = _.omit(treeMapPropTypes, [
'orientLabels',
'skipVMin',
'transitionDuration',
'transitionEasing',
])

TreeMapPlaceholders.defaultProps = _.omit(treeMapDefaultProps, [
'orientLabels',
'skipVMin',
'transitionDuration',
'transitionEasing',
])

export default TreeMapPlaceholders
84 changes: 35 additions & 49 deletions src/lib/charts/treemap/TreeMapHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import _ from 'lodash'
import {
treemap as Treemap,
hierarchy,
Expand All @@ -28,60 +29,45 @@ export const tilingMethods = {
}

/**
* This wrapper is responsible for computing treemap chart positions.
* It's used for all TreeMap related chart components.
*
* @returns {{ compute: (function) }}
* @constructor
* @param {number} width
* @param {number} height
* @param {object} _root
* @param {boolean} leavesOnly
* @param {string} tile
* @param {number} innerPadding
* @param {number} outerPadding
* @param {function} identity
* @param {function} value
* @param {function} color
*/
const TreeMapHelper = () => {
export const computeTreeMap = ({
width,
height,
root: _root,
leavesOnly,
tile,
innerPadding,
outerPadding,
identity,
value,
color,
}) => {
const treemap = Treemap()
.size([width, height])
.tile(tilingMethods[tile])
.round(true)
.paddingInner(innerPadding)
.paddingOuter(outerPadding)

return {
/**
* @param {number} width
* @param {number} height
* @param {object} _root
* @param {boolean} leavesOnly
* @param {string} tile
* @param {number} innerPadding
* @param {number} outerPadding
* @param {function} identity
* @param {function} value
* @param {function} color
*/
compute({
width,
height,
root: _root,
leavesOnly,
tile,
innerPadding,
outerPadding,
identity,
value,
color,
}) {
treemap
.size([width, height])
.tile(tilingMethods[tile])
.round(true)
.paddingInner(innerPadding)
.paddingOuter(outerPadding)

const root = treemap(hierarchy(_root).sum(value))
const root = treemap(hierarchy(_root).sum(value))

const nodes = leavesOnly ? root.leaves() : root.descendants()
const nodes = leavesOnly ? root.leaves() : root.descendants()

return nodes.map(d => {
d.color = color({ ...d.data, depth: d.depth })
return nodes.map(d => {
d.color = color({ ...d.data, depth: d.depth })

d.data.key = d.ancestors().map(a => identity(a.data)).join('.')
d.data.key = d.ancestors().map(a => identity(a.data)).join('.')

return d
})
},
}
return d
})
}

export default TreeMapHelper
2 changes: 0 additions & 2 deletions src/lib/propertiesConverters.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export const convertLabel = (_label, labelFormat) => {
return data => {
let labelOutput = label(data)

console.log('labelOutput', labelOutput)

if (formatter) {
labelOutput = formatter(labelOutput)
}
Expand Down

0 comments on commit 507dcb6

Please sign in to comment.