Skip to content

Commit 2f7419b

Browse files
committed
frontend: improve chart percentage calculation
This commit fixes a regression that was introduced in commit a9a26db that prepended a zero to the data set. With that change the percentage calculation was not correct anymore, leading sometimes to infinite loading skeleton. With this update, the percentage change is correctly computed based on the first non-zero data point. Additionally, this update modifies the rendering of the percentage change. if `hasDifference` is false, it won't be rendered at all. Refactor variable name 'hasDifferenece' to 'hasDifference' to correct a typo.
1 parent b02e97f commit 2f7419b

File tree

1 file changed

+7
-5
lines changed
  • frontends/web/src/routes/account/summary

1 file changed

+7
-5
lines changed

frontends/web/src/routes/account/summary/chart.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,9 @@ class Chart extends Component<Props, State> {
388388
this.setState({ difference: 0, diffSince: '' });
389389
return;
390390
}
391-
const valueFrom = data[rangeFrom].value;
391+
// data should always have at least two data points and when the first
392+
// value is 0 we take the next value as valueFrom to calculate valueDiff
393+
const valueFrom = data[rangeFrom].value === 0 ? data[rangeFrom + 1].value : data[rangeFrom].value;
392394
const valueTo = this.props.data.chartTotal;
393395
const valueDiff = valueTo ? valueTo - valueFrom : 0;
394396
this.setState({
@@ -484,7 +486,7 @@ class Chart extends Component<Props, State> {
484486
toolTipTime,
485487
isMobile
486488
} = this.state;
487-
const hasDifferenece = difference && Number.isFinite(difference);
489+
const hasDifference = difference && Number.isFinite(difference);
488490
const hasData = this.hasData();
489491
const hasHourlyData = this.hasHourlyData();
490492
const disableFilters = !hasData || chartDataMissing;
@@ -516,10 +518,10 @@ class Chart extends Component<Props, State> {
516518
</span>
517519
</div>
518520
{!showMobileTotalValue ?
519-
<span className={!hasDifferenece ? '' : (
521+
<span className={!hasDifference ? '' : (
520522
styles[difference < 0 ? 'down' : 'up']
521523
)} title={diffSince}>
522-
{hasDifferenece ? (
524+
{hasDifference ? (
523525
<>
524526
<span className={styles.arrow}>
525527
{(difference < 0) ? (<ArrowUp />) : (<ArrowDown />)}
@@ -529,7 +531,7 @@ class Chart extends Component<Props, State> {
529531
<span className={styles.diffUnit}>%</span>
530532
</span>
531533
</>
532-
) : chartTotal === 0 ? null : (<Skeleton fontSize="1.125rem" minWidth="125px" />)}
534+
) : null}
533535
</span>
534536
:
535537
<span className={styles.diffValue}>

0 commit comments

Comments
 (0)