Skip to content

Commit

Permalink
feat(bar): add ability to customize bar labels colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Aug 5, 2017
1 parent c0aecaa commit 0f63c07
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nivo",
"version": "0.6.0",
"version": "0.7.0",
"licenses": [
{
"type": "MIT",
Expand Down
25 changes: 22 additions & 3 deletions src/components/charts/bar/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { merge } from 'lodash'
import { TransitionMotion, spring } from 'react-motion'
import Nivo, { defaultTheme } from '../../../Nivo'
import { marginPropType, motionPropTypes } from '../../../props'
import { getColorsGenerator } from '../../../lib/colorUtils'
import { getColorsGenerator, getInheritedColorGenerator } from '../../../lib/colorUtils'
import { generateGroupedBars, generateStackedBars } from '../../../lib/charts/bar'
import SvgWrapper from '../SvgWrapper'
import Axes from '../../axes/Axes'
Expand Down Expand Up @@ -49,6 +49,7 @@ export default class Bar extends Component {

// labels
enableLabels: PropTypes.bool.isRequired,
labelsTextColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,

// interactions
onClick: PropTypes.func,
Expand All @@ -66,7 +67,6 @@ export default class Bar extends Component {
margin: Nivo.defaults.margin,
groupMode: 'stacked',
xPadding: 0.1,
enableLabels: true,
axes: {
left: {
enabled: true,
Expand All @@ -78,6 +78,11 @@ export default class Bar extends Component {
enableGridX: false,
enableGridY: true,

// labels
enableLabels: true,
labelsLinkColor: 'theme',
labelsTextColor: 'theme',

// theming
theme: {},
colors: Nivo.defaults.colorRange,
Expand All @@ -100,7 +105,11 @@ export default class Bar extends Component {
axes,
enableGridX,
enableGridY,

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

// theming
theme: _theme,
Expand All @@ -119,6 +128,8 @@ export default class Bar extends Component {

const theme = merge({}, defaultTheme, _theme)
const color = getColorsGenerator(colors, colorBy)
const labelsLinkColor = getInheritedColorGenerator(_labelsLinkColor, 'axis.tickColor')
const labelsTextColor = getInheritedColorGenerator(_labelsTextColor, 'axis.textColor')

const motionProps = {
animate,
Expand Down Expand Up @@ -200,7 +211,15 @@ export default class Bar extends Component {
{...motionProps}
/>
{bars}
{enableLabels && result.bars.map(d => <BarItemLabel {...d} key={d.key} />)}
{enableLabels &&
result.bars.map(d =>
<BarItemLabel
{...d}
key={d.key}
linkColor={labelsLinkColor(d, theme)}
textColor={labelsTextColor(d, theme)}
/>
)}
</SvgWrapper>
)
}
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 @@ -18,13 +18,14 @@ export default class BarItemLabel extends Component {
y: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
color: PropTypes.string.isRequired,
linkColor: PropTypes.string.isRequired,
textColor: PropTypes.string.isRequired,
}

static defaultProps = {}

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

let x = _x
let y = _y
Expand All @@ -35,7 +36,7 @@ export default class BarItemLabel extends Component {
textX = -13
textAnchor = 'end'
y = _y + height / 2
line = <line stroke={color} x1={0} x2={-10} y1={0} y2={0} />
line = <line style={{ stroke: linkColor }} x1={0} x2={-10} y1={0} y2={0} />
} else {
textX = 0
textAnchor = 'middle'
Expand All @@ -46,7 +47,7 @@ export default class BarItemLabel extends Component {
return (
<g transform={`translate(${x},${y})`} className="nivo_bar_legend">
{line}
<text x={textX} textAnchor={textAnchor} dy="0.5em">
<text x={textX} textAnchor={textAnchor} dy="0.5em" style={{ fill: textColor }}>
{value}
</text>
</g>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/colorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const getColorsGenerator = (colors, colorBy) => {
const memoizedColorModifier = _.memoize((method, _amount) => {
const amount = parseFloat(_amount)

return _.memoize(d => rgb(d.color)[method](amount), d => d.color)
return _.memoize(d => rgb(d.color)[method](amount).toString(), d => d.color)
}, (method, amount) => `${method}.${amount}`)

const noneGenerator = () => 'none'
Expand Down

0 comments on commit 0f63c07

Please sign in to comment.