Skip to content

Commit

Permalink
feat(treemap): add SVG based TreeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Benitte committed Oct 3, 2016
1 parent ae77d43 commit de08c6b
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 86 deletions.
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ it's built on top of d3.
- composable
- highly customizable
- motion/transitions
- [component playground](https://plouc.github.io/nivo/)
- [exhaustive documentation](https://plouc.github.io/nivo/)
- [component playground](http://nivo.rocks)
- [exhaustive documentation](http://nivo.rocks)
- isomorphic rendering
- support for SVG and HTML
- placeholder components for advanced customization
Expand All @@ -24,25 +24,30 @@ it's built on top of d3.
## Components

- Bars
- [`<Bars />`](https://plouc.github.io/nivo/#/bars/react)
- [`<ResponsiveBars />`](https://plouc.github.io/nivo/#/bars/react)
- [`<Bars />`](http://nivo.rocks/#/bars/react)
- [`<ResponsiveBars />`](http://nivo.rocks/#/bars/react)
- Line
- [`<Line />`](https://plouc.github.io/nivo/#/line/react)
- [`<ResponsiveLine />`](https://plouc.github.io/nivo/#/line/react)
- [`<Line />`](http://nivo.rocks/#/line/react)
- [`<ResponsiveLine />`](http://nivo.rocks/#/line/react)
- Bubble
- [`<Bubble />`](https://plouc.github.io/nivo/#/bubble/react)
- [`<ResponsiveBubble />`](https://plouc.github.io/nivo/#/bubble/react)
- [`<BubblePlaceholders />`](https://plouc.github.io/nivo/#/bubble/placeholders)
- [`<ResponsiveBubblePlaceholders />`](https://plouc.github.io/nivo/#/bubble/placeholders)
- [`<Bubble />`](http://nivo.rocks/#/bubble/react)
- [`<ResponsiveBubble />`](http://nivo.rocks/#/bubble/react)
- [`<BubblePlaceholders />`](http://nivo.rocks/#/bubble/placeholders)
- [`<ResponsiveBubblePlaceholders />`](http://nivo.rocks/#/bubble/placeholders)
- TreeMap
- [`<TreeMap />`](https://plouc.github.io/nivo/#/treemap/react)
- [`<ResponsiveTreeMap />`](https://plouc.github.io/nivo/#/treemap/react)
- [`<TreeMapPlaceholders />`](https://plouc.github.io/nivo/#/treemap/placeholders)
- [`<ResponsiveTreeMapPlaceholders />`](https://plouc.github.io/nivo/#/treemap/placeholders)
- [`<TreeMap />`](http://nivo.rocks/#/treemap)
- [`<ResponsiveTreeMap />`](http://nivo.rocks/#/treemap)
- [`<TreeMapHTML />`](http://nivo.rocks/#/treemap/html)
- [`<ResponsiveTreeMapHTML />`](http://nivo.rocks/#/treemap/html)
- [`<TreeMapPlaceholders />`](http://nivo.rocks/#/treemap/placeholders)
- [`<ResponsiveTreeMapPlaceholders />`](http://nivo.rocks/#/treemap/placeholders)
- Chord
- [`<Chord />`](https://plouc.github.io/nivo/#/chord)
- [`<ResponsiveChord />`](https://plouc.github.io/nivo/#/chord)
- [`<Chord />`](http://nivo.rocks/#/chord)
- [`<ResponsiveChord />`](http://nivo.rocks/#/chord)

## Guides

- [colors](http://nivo.rocks/#/guides/colors)

## Repositories

Expand Down
31 changes: 31 additions & 0 deletions src/components/charts/treemap/ResponsiveTreeMapHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of the nivo project.
*
* (c) Raphaël Benitte
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
'use strict'

import React, { Component, PropTypes } from 'react'
import TreeMapHTML from './TreeMapHTML'
import Dimensions from 'react-dimensions'


class ResponsiveTreeMapHTML extends Component {
render() {
const { containerWidth, containerHeight } = this.props

return (
<TreeMapHTML
width={containerWidth}
height={containerHeight}
{...this.props}
/>
)
}
}


export default Dimensions()(ResponsiveTreeMapHTML)
58 changes: 30 additions & 28 deletions src/components/charts/treemap/TreeMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,35 @@ const createNodes = ({
node.style.height > node.style.width

renderedNodes.push(
<div
<g
key={node.key}
className="nivo_treemap_node"
style={{
boxSizing: 'border-box',
position: 'absolute',
top: node.style.y,
left: node.style.x,
width: node.style.width,
height: node.style.height,
background: node.style.color,
overflow: 'hidden',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
borderWidth: borderWidth,
borderStyle: 'solid',
borderColor: borderColorFn(node.data),
}}
transform={`translate(${node.style.x},${node.style.y})`}
>
<rect
width={node.style.width}
height={node.style.height}
fill={node.style.color}
stroke={borderColorFn(node.data)}
strokeWidth={borderWidth}
/>
{shouldRenderLabel && (
<span
className="nivo_treemap_node_label"
style={{
color: textColorFn(node.data),
transform: `rotate(${rotate ? '-90' : '0'}deg)`
}}
<g
transform={`translate(${node.style.width / 2},${node.style.height / 2}) rotate(${rotate ? '-90' : '0'})`}
>
{label(node.data.data)}
</span>
<text
className="nivo_treemap_node_label"
textAnchor="middle"
dy="0.5em"
style={{
fill: textColorFn(node.data),
}}
>
{label(node.data.data)}
</text>
</g>
)}
</div>
</g>
)
})

Expand All @@ -97,8 +94,13 @@ class TreeMap extends Component {
}
}

TreeMap.propTypes = _.omit(treeMapPropTypes, ['children', 'stiffness', 'damping'])
TreeMap.defaultProps = _.omit(treeMapDefaultProps, ['stiffness', 'damping'])
TreeMap.propTypes = _.omit(treeMapPropTypes, [
'children',
'namespace',
])

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


export default TreeMap
109 changes: 109 additions & 0 deletions src/components/charts/treemap/TreeMapHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* This file is part of the nivo project.
*
* (c) 2016 Raphaël Benitte
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
'use strict'

import React, { Component } from 'react'
import { findDOMNode } from 'react-dom'
import _ from 'lodash'
import { convertLabel } from '../../../lib/propertiesConverters'
import { treeMapPropTypes, treeMapDefaultProps } from './TreeMapProps'
import TreeMapPlaceholders from './TreeMapPlaceholders'
import { getColorGenerator } from '../../../ColorUtils'


const createNodes = ({
borderWidth,
borderColor,
enableLabels,
label: _label,
labelFormat,
orientLabels,
labelSkipSize,
labelTextColor,
}) => {
const label = convertLabel(_label, labelFormat)
const borderColorFn = getColorGenerator(borderColor)
const textColorFn = getColorGenerator(labelTextColor)

return nodes => {
const renderedNodes = []

nodes.forEach(node => {
const shouldRenderLabel = enableLabels &&
node.data.height === 0 &&
(labelSkipSize === 0 || Math.min(node.style.width, node.style.height) > labelSkipSize)

const rotate = shouldRenderLabel &&
orientLabels &&
node.style.height > node.style.width

renderedNodes.push(
<div
key={node.key}
className="nivo_treemap_node"
style={{
boxSizing: 'border-box',
position: 'absolute',
top: node.style.y,
left: node.style.x,
width: node.style.width,
height: node.style.height,
background: node.style.color,
overflow: 'hidden',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
borderWidth: borderWidth,
borderStyle: 'solid',
borderColor: borderColorFn(node.data),
}}
>
{shouldRenderLabel && (
<span
className="nivo_treemap_node_label"
style={{
color: textColorFn(node.data),
transform: `rotate(${rotate ? '-90' : '0'}deg)`
}}
>
{label(node.data.data)}
</span>
)}
</div>
)
})

return renderedNodes
}
}


class TreeMapHTML extends Component {
render() {
return (
<TreeMapPlaceholders
{...this.props}
namespace="html"
>
{createNodes(this.props)}
</TreeMapPlaceholders>
)
}
}

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

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


export default TreeMapHTML

0 comments on commit de08c6b

Please sign in to comment.