Skip to content

Commit 602c1c3

Browse files
committed
feat(bar): enable labels for canvas flavor
1 parent 3cac1d5 commit 602c1c3

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

packages/bar/src/BarCanvas.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ const InnerBarCanvas = <RawDatum extends BarDatum>({
8282

8383
// barComponent = canvasDefaultProps.barComponent,
8484

85-
// enableLabel = canvasDefaultProps.enableLabel,
86-
// label = canvasDefaultProps.label,
87-
// labelSkipWidth = canvasDefaultProps.labelSkipWidth,
88-
// labelSkipHeight = canvasDefaultProps.labelSkipHeight,
89-
// labelTextColor = canvasDefaultProps.labelTextColor,
85+
enableLabel = canvasDefaultProps.enableLabel,
86+
label = canvasDefaultProps.label,
87+
labelSkipWidth = canvasDefaultProps.labelSkipWidth,
88+
labelSkipHeight = canvasDefaultProps.labelSkipHeight,
89+
labelTextColor = canvasDefaultProps.labelTextColor,
9090

9191
// markers,
9292

@@ -137,8 +137,8 @@ const InnerBarCanvas = <RawDatum extends BarDatum>({
137137
const getBorderColor = useInheritedColor<ComputedBarDatum<RawDatum>>(borderColor, theme)
138138
const getColor = useOrdinalColorScale(colors, colorBy)
139139
const getIndex = usePropertyAccessor(indexBy)
140-
// const getLabel = usePropertyAccessor(label)
141-
// const getLabelColor = useInheritedColor<ComputedBarDatum<RawDatum>>(labelTextColor, theme)
140+
const getLabel = usePropertyAccessor(label)
141+
const getLabelColor = useInheritedColor<ComputedBarDatum<RawDatum>>(labelTextColor, theme)
142142
const getTooltipLabel = usePropertyAccessor(tooltipLabel)
143143

144144
const options = {
@@ -173,6 +173,16 @@ const InnerBarCanvas = <RawDatum extends BarDatum>({
173173
[hiddenIds, keys, result.bars]
174174
)
175175

176+
const shouldRenderLabel = useCallback(
177+
({ width, height }: { height: number; width: number }) => {
178+
if (!enableLabel) return false
179+
if (labelSkipWidth > 0 && width < labelSkipWidth) return false
180+
if (labelSkipHeight > 0 && height < labelSkipHeight) return false
181+
return true
182+
},
183+
[enableLabel, labelSkipHeight, labelSkipWidth]
184+
)
185+
176186
useEffect(() => {
177187
const ctx = canvasEl.current?.getContext('2d')
178188

@@ -266,6 +276,13 @@ const InnerBarCanvas = <RawDatum extends BarDatum>({
266276
if (borderWidth > 0) {
267277
ctx.stroke()
268278
}
279+
280+
if (shouldRenderLabel({ height, width })) {
281+
ctx.textBaseline = 'middle'
282+
ctx.textAlign = 'center'
283+
ctx.fillStyle = getLabelColor(bar)
284+
ctx.fillText(getLabel(bar.data), x + width / 2, y + height / 2)
285+
}
269286
})
270287

271288
ctx.save()
@@ -278,6 +295,8 @@ const InnerBarCanvas = <RawDatum extends BarDatum>({
278295
enableGridX,
279296
enableGridY,
280297
getBorderColor,
298+
getLabel,
299+
getLabelColor,
281300
gridXValues,
282301
gridYValues,
283302
groupMode,
@@ -297,6 +316,7 @@ const InnerBarCanvas = <RawDatum extends BarDatum>({
297316
result.xScale,
298317
result.yScale,
299318
reverse,
319+
shouldRenderLabel,
300320
theme,
301321
width,
302322
])

packages/bar/src/props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const defaultProps = {
3434
labelSkipWidth: 0,
3535
labelSkipHeight: 0,
3636
labelLinkColor: 'theme',
37-
labelTextColor: 'theme',
37+
labelTextColor: { from: 'theme', theme: 'labels.text.fill' },
3838

3939
colorBy: 'id' as const,
4040
colors: { scheme: 'nivo' } as OrdinalColorScaleConfig,

packages/bar/stories/barCanvas.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const commonProps = {
1313
indexBy: 'country',
1414
keys,
1515
padding: 0.2,
16-
labelTextColor: 'inherit:darker(1.4)',
1716
labelSkipWidth: 16,
1817
labelSkipHeight: 16,
1918
}

0 commit comments

Comments
 (0)