Skip to content

Commit

Permalink
feat(bump): add support for function for start/end labels
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Dec 18, 2019
1 parent c6b01ff commit 80c3e92
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/bump/src/bump/Bump.js
Expand Up @@ -101,6 +101,8 @@ const Bump = props => {
activePointBorderWidth,
inactivePointBorderWidth,
pointBorderColor,
startLabel,
endLabel,
isInteractive,
currentSerie,
})
Expand Down Expand Up @@ -158,6 +160,7 @@ const Bump = props => {
<LinesLabels
key="start"
series={series}
getLabel={startLabel}
position="start"
padding={startLabelPadding}
color={startLabelTextColor}
Expand All @@ -169,6 +172,7 @@ const Bump = props => {
<LinesLabels
key="end"
series={series}
getLabel={endLabel}
position="end"
padding={endLabelPadding}
color={endLabelTextColor}
Expand Down
9 changes: 6 additions & 3 deletions packages/bump/src/bump/LinesLabels.js
Expand Up @@ -13,11 +13,12 @@ import { useTheme, useMotionConfig } from '@nivo/core'
import { inheritedColorPropType } from '@nivo/colors'
import { useSeriesLabels } from './hooks'

const LinesLabels = ({ series, position, padding, color }) => {
const LinesLabels = ({ series, getLabel, position, padding, color }) => {
const theme = useTheme()
const { animate, springConfig } = useMotionConfig()
const labels = useSeriesLabels({
series,
getLabel,
position,
padding,
color,
Expand All @@ -38,7 +39,7 @@ const LinesLabels = ({ series, position, padding, color }) => {
fill: label.color,
}}
>
{label.id}
{label.label}
</text>
)
})
Expand Down Expand Up @@ -71,7 +72,7 @@ const LinesLabels = ({ series, position, padding, color }) => {
fill: label.color,
}}
>
{label.id}
{label.label}
</text>
))}
</>
Expand All @@ -92,6 +93,8 @@ LinesLabels.propTypes = {
).isRequired,
})
).isRequired,
getLabel: PropTypes.oneOfType([PropTypes.oneOf([false]), PropTypes.string, PropTypes.func])
.isRequired,
position: PropTypes.oneOf(['start', 'end']).isRequired,
padding: PropTypes.number.isRequired,
color: inheritedColorPropType.isRequired,
Expand Down
8 changes: 7 additions & 1 deletion packages/bump/src/bump/hooks.js
Expand Up @@ -296,7 +296,7 @@ export const useSerieHandlers = ({
return handlers
}

export const useSeriesLabels = ({ series, position, padding, color }) => {
export const useSeriesLabels = ({ series, position, padding, color, getLabel }) => {
const theme = useTheme()
const getColor = useInheritedColor(color, theme)

Expand All @@ -313,6 +313,11 @@ export const useSeriesLabels = ({ series, position, padding, color }) => {

const labels = []
series.forEach(serie => {
let label = serie.id
if (typeof getLabel === 'function') {
label = getLabel(serie)
}

const point =
position === 'start'
? serie.linePoints[0]
Expand All @@ -325,6 +330,7 @@ export const useSeriesLabels = ({ series, position, padding, color }) => {

labels.push({
id: serie.id,
label,
x: point[0] + signedPadding,
y: point[1],
color: getColor(serie),
Expand Down

0 comments on commit 80c3e92

Please sign in to comment.