Skip to content

Commit

Permalink
feat(bar): add support for horizontal layout & change data format
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Aug 17, 2017
1 parent 1bf5956 commit 29a4b35
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 145 deletions.
5 changes: 1 addition & 4 deletions src/components/axes/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,7 @@ const Axis = ({
}

return (
<g
className={`nivo__axis nivo__axis--${_position} nivo__axis--orient-${orient}`}
transform={`translate(${x},${y})`}
>
<g transform={`translate(${x},${y})`}>
{legend}
{tickElements}
</g>
Expand Down
57 changes: 35 additions & 22 deletions src/components/charts/bar/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { merge } from 'lodash'
import { TransitionMotion, spring } from 'react-motion'
import compose from 'recompose/compose'
import defaultProps from 'recompose/defaultProps'
import withPropsOnChange from 'recompose/withPropsOnChange'
import pure from 'recompose/pure'
import { withTheme, withColors, withDimensions, withMotion } from '../../../hocs'
import { getInheritedColorGenerator } from '../../../lib/colorUtils'
import { generateGroupedBars, generateStackedBars } from '../../../lib/charts/bar'
import { getAccessorFor } from '../../../lib/propertiesConverters'
import Container from '../Container'
import SvgWrapper from '../SvgWrapper'
import Axes from '../../axes/Axes'
Expand All @@ -25,7 +27,11 @@ import BarItemLabel from './BarItemLabel'

const Bar = ({
data,
getIndex,
keys,

groupMode,
layout,

margin,
width,
Expand All @@ -44,8 +50,8 @@ const Bar = ({

// labels
enableLabels,
labelsLinkColor: _labelsLinkColor,
labelsTextColor: _labelsTextColor,
getLabelsLinkColor,
getLabelsTextColor,

// theming
theme,
Expand All @@ -59,9 +65,6 @@ const Bar = ({
// interactivity
isInteractive,
}) => {
const labelsLinkColor = getInheritedColorGenerator(_labelsLinkColor, 'axis.tickColor')
const labelsTextColor = getInheritedColorGenerator(_labelsTextColor, 'axis.textColor')

const motionProps = {
animate,
motionDamping,
Expand All @@ -70,11 +73,11 @@ const Bar = ({

let result
if (groupMode === 'grouped') {
result = generateGroupedBars(data, width, height, getColor, {
result = generateGroupedBars(layout, data, getIndex, keys, width, height, getColor, {
xPadding,
})
} else if (groupMode === 'stacked') {
result = generateStackedBars(data, width, height, getColor, {
result = generateStackedBars(layout, data, getIndex, keys, width, height, getColor, {
xPadding,
})
}
Expand Down Expand Up @@ -151,9 +154,8 @@ const Bar = ({
result.bars.map(d =>
<BarItemLabel
{...d}
key={d.key}
linkColor={labelsLinkColor(d, theme)}
textColor={labelsTextColor(d, theme)}
textColor={getLabelsTextColor(d, theme)}
linkColor={getLabelsLinkColor(d, theme)}
/>
)}
</SvgWrapper>
Expand All @@ -165,19 +167,13 @@ const Bar = ({

Bar.propTypes = {
// data
data: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
data: PropTypes.arrayOf(
PropTypes.shape({
x: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
y: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
})
).isRequired,
})
).isRequired,
data: PropTypes.arrayOf(PropTypes.object).isRequired,
indexBy: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
getIndex: PropTypes.func.isRequired, // computed
keys: PropTypes.arrayOf(PropTypes.string).isRequired,

groupMode: PropTypes.oneOf(['stacked', 'grouped']).isRequired,
layout: PropTypes.oneOf(['horizontal', 'vertical']).isRequired,

xPadding: PropTypes.number.isRequired,

Expand All @@ -192,6 +188,9 @@ Bar.propTypes = {
// labels
enableLabels: PropTypes.bool.isRequired,
labelsTextColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
getLabelsTextColor: PropTypes.func.isRequired, // computed
labelsLinkColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
getLabelsLinkColor: PropTypes.func.isRequired, // computed

// interactions
onClick: PropTypes.func,
Expand All @@ -204,7 +203,12 @@ Bar.propTypes = {
}

export const BarDefaultProps = {
indexBy: 'id',
keys: ['value'],

groupMode: 'stacked',
layout: 'vertical',

xPadding: 0.1,

// axes & grid
Expand All @@ -227,9 +231,18 @@ Bar.defaultProps = BarDefaultProps
const enhance = compose(
defaultProps(BarDefaultProps),
withTheme(),
withColors({ defaultColorBy: 'serie.id' }),
withColors(),
withDimensions(),
withMotion(),
withPropsOnChange(['indexBy'], ({ indexBy }) => ({
getIndex: getAccessorFor(indexBy),
})),
withPropsOnChange(['labelsTextColor'], ({ labelsTextColor }) => ({
getLabelsTextColor: getInheritedColorGenerator(labelsTextColor, 'axis.textColor'),
})),
withPropsOnChange(['labelsLinkColor'], ({ labelsLinkColor }) => ({
getLabelsLinkColor: getInheritedColorGenerator(labelsLinkColor, 'axis.tickColor'),
})),
pure
)

Expand Down
23 changes: 8 additions & 15 deletions src/components/charts/bar/BarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,12 @@ import PropTypes from 'prop-types'
import pure from 'recompose/pure'
import BasicTooltip from '../../tooltip/BasicTooltip'

const BarItem = ({
serie,
xValue,
yValue,
x,
y,
width,
height,
color,
showTooltip,
hideTooltip,
}) => {
const BarItem = ({ data, x, y, width, height, color, showTooltip, hideTooltip }) => {
const handleTooltip = e =>
showTooltip(
<BasicTooltip
id={`${serie.id} - ${xValue}`}
value={yValue}
id={`${data.id} - ${data.indexValue}`}
value={data.value}
enableChip={true}
color={color}
/>,
Expand All @@ -52,12 +41,16 @@ const BarItem = ({
}

BarItem.propTypes = {
//value: PropTypes.number.isRequired,
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
color: PropTypes.string.isRequired,
data: PropTypes.shape({
id: PropTypes.string.isRequired,
value: PropTypes.number.isRequired,
indexValue: PropTypes.string.isRequired,
}).isRequired,

showTooltip: PropTypes.func.isRequired,
hideTooltip: PropTypes.func.isRequired,
Expand Down
9 changes: 5 additions & 4 deletions src/components/charts/bar/BarItemLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const labelStyle = {

export default class BarItemLabel extends Component {
static propTypes = {
xValue: PropTypes.any.isRequired,
yValue: PropTypes.number.isRequired,
data: PropTypes.shape({
value: PropTypes.number.isRequired,
}).isRequired,
x: PropTypes.number.isRequired,
y: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
Expand All @@ -30,7 +31,7 @@ export default class BarItemLabel extends Component {
static defaultProps = {}

render() {
const { x: _x, y: _y, width, height, linkColor, textColor, yValue } = this.props
const { x: _x, y: _y, width, height, linkColor, textColor, data } = this.props

let x = _x
let y = _y
Expand All @@ -53,7 +54,7 @@ export default class BarItemLabel extends Component {
<g transform={`translate(${x},${y})`} className="nivo_bar_legend" style={labelStyle}>
{line}
<text x={textX} textAnchor={textAnchor} dy="0.5em" style={{ fill: textColor }}>
{yValue}
{data.value}
</text>
</g>
)
Expand Down
Loading

0 comments on commit 29a4b35

Please sign in to comment.