Skip to content
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

Stats: use different Y scales for paid & total lines #100525

Merged
merged 13 commits into from
Feb 27, 2025
27 changes: 26 additions & 1 deletion client/my-sites/stats/components/line-chart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ function StatsLineChart( {
[ chartData ]
);

const yScaleType = useMemo( () => {
if ( chartData.length <= 1 ) {
return 'linear';
}

const maxValues = chartData.map( ( series ) =>
Math.max( ...series.data.map( ( d ) => d.value ) )
);
const [ minMax, maxMax ] = [ Math.min( ...maxValues ), Math.max( ...maxValues ) ];

// Avoid division by zero
if ( minMax === 0 ) {
return 'linear';
}

const scacle = maxMax / minMax;
if ( scacle > 20 && scacle < 200 ) {
return 'sqrt';
} else if ( scacle >= 200 ) {
return 'log';
}

return 'linear';
}, [ chartData ] );

return (
<div className={ clsx( 'stats-line-chart', className ) }>
{ isEmpty && (
Expand All @@ -78,7 +103,7 @@ function StatsLineChart( {
} }
options={ {
yScale: {
type: 'linear',
type: yScaleType,
...( fixedDomain && { domain: [ 0, maxValue ] } ),
zero: zeroBaseline,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export default function SubscribersChartSection( {
chartData={ lineChartData }
height={ 300 }
EmptyState={ () => null }
zeroBaseline={ false }
zeroBaseline={ lineChartData.length > 1 }
formatTimeTick={ formatTimeTick }
/>
) : (
Expand Down