Skip to content

Commit

Permalink
Intelligently clamp the Y axis max for percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
gggritso committed Mar 5, 2025
1 parent efa66f6 commit f1a3d86
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SizeUnit,
} from 'sentry/utils/discover/fields';

export const Y_AXIS_INTEGER_TOLERANCE = 0.000001;
export const FALLBACK_TYPE = 'number';
export const FALLBACK_UNIT_FOR_FIELD_TYPE = {
number: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ import {Area} from './plottables/area';
import {Bars} from './plottables/bars';
import {Line} from './plottables/line';
import {ReleaseSeries} from './releaseSeries';
import {FALLBACK_TYPE, FALLBACK_UNIT_FOR_FIELD_TYPE} from './settings';
import {
FALLBACK_TYPE,
FALLBACK_UNIT_FOR_FIELD_TYPE,
Y_AXIS_INTEGER_TOLERANCE,
} from './settings';

type VisualizationType = 'area' | 'line' | 'bar';

Expand Down Expand Up @@ -369,6 +373,25 @@ export function TimeSeriesWidgetVisualization(props: TimeSeriesWidgetVisualizati
show: false,
},
},
max: value => {
// Handle a very specific edge case with percentage formatting.
// Percentage charts values usually range from 0 to 1, but JavaScript
// floating math is such that the maximum value at any point in the
// chart might be something like 1.0000000002. This is not enough to
// be visible or significant, but _is_ enough for ECharts to add a
// whole additional axis tick. This makes charts looks stupid, because
// the Y axis will be from 0% to 120%, instead of from 0% to 100%. To
// prevent this case, if the maximum value is _just slightly above 1_,
// force it to be exactly 1. Only for percnetages!
if (
yAxisFieldType === 'percentage' &&
value.max - 1 < Y_AXIS_INTEGER_TOLERANCE
) {
return 1;
}

return value.max;
},
}}
{...chartZoomProps}
{...(props.onZoom ? {onDataZoom: props.onZoom} : {})}
Expand Down

0 comments on commit f1a3d86

Please sign in to comment.