Custom function for shape
not working with negative values.
#3919
-
Hello there! TL;DR: In the BarChart, we use a custom function for the shape property within the CleanShot.2023-10-30.at.00.39.08.mp4This is the custom render function that gets passed to const renderShape = (props: any, activeBar: any | undefined, activeLegend: string | undefined) => {
const { x, y, width, height, fillOpacity, name, payload, value } = props;
return (
<rect
x={x}
y={y}
width={width}
height={height}
opacity={
activeBar || (activeLegend && activeLegend !== name)
? deepEqual(activeBar, { ...payload, value })
? fillOpacity
: 0.3
: fillOpacity
}
/>
);
}; I tried to modify the function to take into account the negative values: const renderShape = (
props: any,
activeBar: any | undefined,
layout: string
) => {
const { fillOpacity, payload, value } = props
let { x, width, y, height } = props
+ // Layout fix
+ if (value < 0) {
+ if (layout === 'horizontal') {
+ y += height
+ height = Math.abs(height) // height must be a positive number
+ } else if (layout === 'vertical') {
+ x += width
+ width = Math.abs(width) // width must be a positive number
+ }
+ }
return (
<rect
x={x}
y={y}
width={width}
height={height}
opacity={
activeBar
? deepEqual(activeBar, { ...payload, value })
? fillOpacity
: 0.3
: fillOpacity
}
/>
)
} This works as expected CleanShot.2023-10-30.at.00.44.59.mp4But adding new props such as stacked or relative, is not working for the bar calculation. E.g. CleanShot.2023-10-30.at.00.49.00.mp4I am wondering if I have to add all logic to Reproduction here: https://github.com/severinlandolt/recharts-shape |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
@severinlandolt does this happen before 2.9 as well? |
Beta Was this translation helpful? Give feedback.
-
Related?: #2009 Please correct me if I'm wrong, but at first glance I think this might be expected behavior. According to the SVG spec, a negative value for width/height is "invalid and must be ignored".
In chrome, React throws an error for this in the development build when it attempts to define this attribute on the rect svg element. Since your custom render prop is the one returning the |
Beta Was this translation helpful? Give feedback.
-
I think i got it working, this is the check I had to add to the const renderShape = (
props: any,
activeBar: any | undefined,
layout: string
) => {
const { fillOpacity, payload, value } = props
let { x, width, y, height } = props
if (layout === 'horizontal' && height < 0) {
y += height
height = Math.abs(height) // height must be a positive number
} else if (layout === 'vertical' && width < 0) {
x += width
width = Math.abs(width) // width must be a positive number
}
return (
<rect
x={x}
y={y}
width={width}
height={height}
opacity={
activeBar
? deepEqual(activeBar, { ...payload, value })
? fillOpacity
: 0.3
: fillOpacity
}
/>
) The only thing that makes me think. Should it be possible at all to receive negative height and width by the props? |
Beta Was this translation helpful? Give feedback.
I think i got it working, this is the check I had to add to the
renderShape
function: