Skip to content

Commit

Permalink
Stats: use different Y scales for paid & total lines (#100525)
Browse files Browse the repository at this point in the history
* charts 0.8.3

* remove unnecessary code

* remove unnecessary code

* format tick text for subscribers

* leave tick decision to the lib

* rever unnecessary change

* proper margin for y axis

* proper margin for y axis

* use log y scale when total sub is significantly higher than paid

* add yScaleType to line chart component

* refactoring

* remove unused type
  • Loading branch information
kangzj authored Feb 27, 2025
1 parent 7c1b26a commit 6f8cd18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
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

0 comments on commit 6f8cd18

Please sign in to comment.