Skip to content

Commit

Permalink
feat(bar): add label format support for Bar (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplain90 authored and Raphaël Benitte committed Sep 7, 2017
1 parent b022168 commit c5a63c9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/components/charts/bar/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ const Bar = ({
enableLabels,
getLabelsLinkColor,
getLabelsTextColor,
label,
labelFormat,
getLabel,

// markers
markers,
Expand Down Expand Up @@ -212,6 +215,7 @@ const Bar = ({
result.bars.map(d => (
<BarItemLabel
{...d}
getLabel={getLabel}
textColor={getLabelsTextColor(d, theme)}
linkColor={getLabelsLinkColor(d, theme)}
/>
Expand Down
7 changes: 4 additions & 3 deletions src/components/charts/bar/BarItemLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
import React, { Component } from 'react'
import PropTypes from 'prop-types'

import { getLabelGenerator } from '../../../lib/propertiesConverters'
const safeSize = 20

const labelStyle = {
Expand All @@ -26,12 +26,13 @@ export default class BarItemLabel extends Component {
height: PropTypes.number.isRequired,
linkColor: PropTypes.string.isRequired,
textColor: PropTypes.string.isRequired,
getLabel: PropTypes.func.isRequired,
}

static defaultProps = {}

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

let x = _x
let y = _y
Expand All @@ -54,7 +55,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 }}>
{data.value}
{getLabel(data)}
</text>
</g>
)
Expand Down
6 changes: 5 additions & 1 deletion src/components/charts/bar/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
*d
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand All @@ -14,6 +14,7 @@ import { withTheme, withColors, withDimensions, withMotion } from '../../../hocs
import { getInheritedColorGenerator } from '../../../lib/colors'
import { getAccessorFor } from '../../../lib/propertiesConverters'
import { BarDefaultProps } from './props'
import { getLabelGenerator } from '../../../lib/propertiesConverters'

export default Component =>
compose(
Expand All @@ -31,5 +32,8 @@ export default Component =>
withPropsOnChange(['labelsLinkColor'], ({ labelsLinkColor }) => ({
getLabelsLinkColor: getInheritedColorGenerator(labelsLinkColor, 'axis.tickColor'),
})),
withPropsOnChange(['label', 'labelFormat'], ({ label, labelFormat }) => ({
getLabel: getLabelGenerator(label, labelFormat),
})),
pure
)(Component)
7 changes: 6 additions & 1 deletion src/components/charts/bar/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export const BarPropTypes = {
getLabelsTextColor: PropTypes.func.isRequired, // computed
labelsLinkColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
getLabelsLinkColor: PropTypes.func.isRequired, // computed
label: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
labelFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
getLabel: PropTypes.func,
// interactions
onClick: PropTypes.func,

// theming
getColor: PropTypes.func.isRequired,
Expand Down Expand Up @@ -70,7 +75,7 @@ export const BarDefaultProps = {
enableLabels: true,
labelsLinkColor: 'theme',
labelsTextColor: 'theme',

label: 'value',
// interactivity
isInteractive: true,
onClick: noop,
Expand Down
20 changes: 4 additions & 16 deletions src/lib/propertiesConverters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,14 @@ import _ from 'lodash'
import { format } from 'd3-format'

export const getLabelGenerator = (_label, labelFormat) => {
if (_.isFunction(_label)) {
return _label
}

const label = d => _.get(d, _label)

const getRawLabel = _.isFunction(_label) ? _label : d => _.get(d, _label)
let formatter
if (labelFormat) {
formatter = format(labelFormat)
formatter = _.isFunction(labelFormat) ? labelFormat : format(labelFormat)
}

return data => {
let labelOutput = label(data)

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

return labelOutput
}
if (formatter) return d => formatter(getRawLabel(d))
return getRawLabel
}

export const getAccessorFor = directive => (_.isFunction(directive) ? directive : d => d[directive])
38 changes: 38 additions & 0 deletions test/lib/getLabelGenerator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getLabelGenerator } from '../../src/lib/propertiesConverters'

describe('getLabelGenerator()', () => {
it(`should handle simple value access`, () => {
const getLabel = getLabelGenerator('value')
expect(getLabel({ value: 12 })).toBe(12)
})

it(`should handle nested property access`, () => {
const getLabel = getLabelGenerator('node.value')
expect(getLabel({ node: { value: 13 } })).toBe(13)
})

it(`should handle simple access with d3 formatting`, () => {
const getLabel = getLabelGenerator('value', '.1f')
expect(getLabel({ value: 14 })).toBe('14.0')
})

it(`should handle simple access with d3 formatting`, () => {
const getLabel = getLabelGenerator('value', '.1f')
expect(getLabel({ value: 14 })).toBe('14.0')
})

it(`should handle custom access function`, () => {
const getLabel = getLabelGenerator(d => d.value[0])
expect(getLabel({ value: [15, 16] })).toBe(15)
})

it(`should handle custom formatting function`, () => {
const getLabel = getLabelGenerator('value', v => v + 10)
expect(getLabel({ value: 17 })).toBe(27)
})

it(`should handle custom access & formatting functions`, () => {
const getLabel = getLabelGenerator(d => d.value[1], v => v + 10)
expect(getLabel({ value: [18, 19] })).toBe(29)
})
})

0 comments on commit c5a63c9

Please sign in to comment.