-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add bar totals layer * fix: insert unique key prop to each total * test: insert tests to totals bar layer * docs: insert recipe on website bar page * fix: update scale functions types * refactor: enable totals by prop instead of directly on layers * style: apply theme configuration on totals * feat: make totals offsets configurable * refactor: centralize compute of totals bar * chore: re-format website docs yml * refactor: use props along with layers to enable totals * fix: remove unnused variable * style: add transitions to bar totals * test: update tests to find totals component * docs: add enable totals docs on website * refactor: use totals computed value through hook on canvas * fix: remove unnused var * fix: add enableTotals prop to default bar props on website * docs(website): add default enableTotals prop to canvas and svg flavors * style: align total label text based on layout mode * feat: add value format to totals labels * refactor: configure totals transition inside its component * refactor: format value in totals compute function * style: prevent overlap on zero totals offset * types: remove optional syntax * feat: animation offset is calculated individually by index * chore: change order of initializing default layers * refactor: add numeric value to bar totals data
- Loading branch information
1 parent
154056b
commit d90a326
Showing
15 changed files
with
512 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { useTheme } from '@nivo/core' | ||
import { AnimationConfig, animated, useTransition } from '@react-spring/web' | ||
import { BarCommonProps, BarDatum } from './types' | ||
import { svgDefaultProps } from './props' | ||
import { BarTotalsData } from './compute/totals' | ||
|
||
interface Props<RawDatum extends BarDatum> { | ||
data: BarTotalsData[] | ||
springConfig: Partial<AnimationConfig> | ||
animate: boolean | ||
layout?: BarCommonProps<RawDatum>['layout'] | ||
} | ||
|
||
export const BarTotals = <RawDatum extends BarDatum>({ | ||
data, | ||
springConfig, | ||
animate, | ||
layout = svgDefaultProps.layout, | ||
}: Props<RawDatum>) => { | ||
const theme = useTheme() | ||
const totalsTransition = useTransition< | ||
BarTotalsData, | ||
{ | ||
x: number | ||
y: number | ||
labelOpacity: number | ||
} | ||
>(data, { | ||
keys: barTotal => barTotal.key, | ||
from: barTotal => ({ | ||
x: layout === 'vertical' ? barTotal.x : barTotal.animationOffset, | ||
y: layout === 'vertical' ? barTotal.animationOffset : barTotal.y, | ||
labelOpacity: 0, | ||
}), | ||
enter: barTotal => ({ | ||
x: barTotal.x, | ||
y: barTotal.y, | ||
labelOpacity: 1, | ||
}), | ||
update: barTotal => ({ | ||
x: barTotal.x, | ||
y: barTotal.y, | ||
labelOpacity: 1, | ||
}), | ||
leave: barTotal => ({ | ||
x: layout === 'vertical' ? barTotal.x : barTotal.animationOffset, | ||
y: layout === 'vertical' ? barTotal.animationOffset : barTotal.y, | ||
labelOpacity: 0, | ||
}), | ||
config: springConfig, | ||
immediate: !animate, | ||
initial: animate ? undefined : null, | ||
}) | ||
|
||
return totalsTransition((style, barTotal) => ( | ||
<animated.text | ||
key={barTotal.key} | ||
x={style.x} | ||
y={style.y} | ||
fillOpacity={style.labelOpacity} | ||
style={{ | ||
...theme.labels.text, | ||
pointerEvents: 'none', | ||
fill: theme.text.fill, | ||
}} | ||
fontWeight="bold" | ||
fontSize={theme.labels.text.fontSize} | ||
fontFamily={theme.labels.text.fontFamily} | ||
textAnchor={layout === 'vertical' ? 'middle' : 'start'} | ||
alignmentBaseline={layout === 'vertical' ? 'alphabetic' : 'middle'} | ||
> | ||
{barTotal.formattedValue} | ||
</animated.text> | ||
)) | ||
} |
Oops, something went wrong.