-
Notifications
You must be signed in to change notification settings - Fork 862
CHARTS-176: Fix zero-value bars not visible in small chart heights #47477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
e18300e
22f00c8
da9cf39
2962517
792e7c4
44043fd
d1622dd
d96b104
10ac62e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,9 +20,9 @@ export interface UseZeroValueDisplayOptions { | |
| } | ||
kangzj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Minimum pixel height for zero-value bars to ensure visibility. | ||
| * Minimum pixel height/width for bars to ensure visibility. | ||
| */ | ||
| const MIN_PIXEL_HEIGHT = 3; | ||
| const MIN_PIXEL_SIZE = 3; | ||
|
||
|
|
||
| export const useZeroValueDisplay = ( | ||
| data: SeriesData[], | ||
|
|
@@ -33,30 +33,28 @@ export const useZeroValueDisplay = ( | |
| return useMemo( () => { | ||
| if ( ! enabled || ! valueAxisLength || valueAxisLength <= 0 ) return data; | ||
|
|
||
| // Find max and min absolute values | ||
| // Find max absolute value | ||
| let maxAbsoluteValue = 0; | ||
| let minAbsoluteValue = Infinity; | ||
| for ( const series of data ) { | ||
| for ( const point of series.data ) { | ||
| if ( point.value !== null && point.value !== 0 ) { | ||
| const absValue = Math.abs( point.value ); | ||
| maxAbsoluteValue = Math.max( maxAbsoluteValue, absValue ); | ||
| minAbsoluteValue = Math.min( minAbsoluteValue, absValue ); | ||
| maxAbsoluteValue = Math.max( maxAbsoluteValue, Math.abs( point.value ) ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ( maxAbsoluteValue === 0 ) return data; | ||
|
|
||
| // Calculate the value that renders as MIN_PIXEL_HEIGHT pixels, | ||
| // but never exceed the smallest actual value (so zero bars don't appear larger than real data) | ||
| const pixelBasedValue = ( MIN_PIXEL_HEIGHT / valueAxisLength ) * maxAbsoluteValue; | ||
| const minVisibleValue = Math.min( pixelBasedValue, minAbsoluteValue ); | ||
| // Calculate the minimum value that renders as MIN_PIXEL_SIZE pixels | ||
| const minVisibleValue = ( MIN_PIXEL_SIZE / valueAxisLength ) * maxAbsoluteValue; | ||
kangzj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return data.map( series => ( { | ||
| ...series, | ||
| data: series.data.map( ( point: DataPointDate ): EnhancedDataPoint => { | ||
| if ( point.value === 0 ) { | ||
| const absValue = Math.abs( point.value ?? 0 ); | ||
|
|
||
| // Boost any value that would render smaller than MIN_PIXEL_SIZE | ||
| if ( absValue < minVisibleValue ) { | ||
| return { | ||
| ...point, | ||
| visualValue: minVisibleValue, | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new hook tests don’t cover negative near-zero values or
nullvalues. Given BarChart supports negatives andDataPointDate.valueisnumber | null, add cases to ensure boostedvisualValuepreserves sign and thatnullvalues remain untouched (novisualValueadded).