Skip to content

Commit

Permalink
Merge pull request #1542 from rodekruis/feat.compact-numbers
Browse files Browse the repository at this point in the history
feat: compact numbers
  • Loading branch information
gulfaraz authored Aug 26, 2024
2 parents f089d68 + 47e4811 commit 25449fe
Show file tree
Hide file tree
Showing 31 changed files with 205 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
style="background: var(--ion-color-ibf-secondary)"
data-test="action-title"
>
<ion-col class="ion-no-padding">
<ion-col class="ion-no-padding" size-lg="11" size-md="11" size-xs="11">
<ion-item
lines="none"
class="component-title-item ion-no-margin"
Expand Down Expand Up @@ -34,6 +34,19 @@
</ion-col>
</ion-item>
</ion-col>
<ion-col size-lg="1" size-md="1" size-xs="1">
<ion-item
class="aggregate-item-information-icon ion-no-padding"
data-test="icon-row"
style="--inner-padding-end: 0px"
color="transparent"
lines="none"
>
<ion-label>
<app-disclaimer-approximate></app-disclaimer-approximate>
</ion-label>
</ion-item>
</ion-col>
</ion-row>
<div data-test="aggregate-list">
@for (indicator of indicators; track indicator.name) {
Expand Down Expand Up @@ -62,47 +75,15 @@
class="ion-text-right"
data-test="aggregate-number"
>
@switch (indicator.numberFormatAggregate) {
@case ('decimal2') {
<span>
{{
getAggregate(
indicator.name,
indicator.weightedAvg,
indicator.numberFormatAggregate
)
| number: '.2-2'
| translate
}}
</span>
}
@case ('decimal0') {
<span>
{{
getAggregate(
indicator.name,
indicator.weightedAvg,
indicator.numberFormatAggregate
)
| number: '.0-0'
| translate
}}
</span>
}
@case ('perc') {
<span>
{{
getAggregate(
indicator.name,
indicator.weightedAvg,
indicator.numberFormatAggregate
)
| percent: '.0-0'
| translate
}}
</span>
}
}
{{
getAggregate(
indicator.name,
indicator.weightedAvg,
indicator.numberFormatAggregate
)
| compact: indicator.numberFormatAggregate
| translate
}}
{{ indicator.aggregateUnit }}
</ion-label>
} @else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,7 @@
"
></span>
<strong>
@switch (actionIndicatorNumberFormat) {
@case ('decimal0') {
<span>{{ area.actionsValue | number: '.0-0' }}</span>
}
@case ('perc') {
<span>{{ area.actionsValue | percent: '.0-0' }}</span>
}
}
{{ area.actionsValue | compact: actionIndicatorNumberFormat }}
</strong>
</ion-note>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { AdminLevelService } from '../../services/admin-level.service';
import { AggregatesService } from '../../services/aggregates.service';
import { TimelineService } from '../../services/timeline.service';
import { AdminLevel, AdminLevelType } from '../../types/admin-level';
import { Indicator } from '../../types/indicator-group';
import { Indicator, NumberFormat } from '../../types/indicator-group';
import { LeadTimeTriggerKey, LeadTimeUnit } from '../../types/lead-time';
import { TriggeredArea } from '../../types/triggered-area';
import { ActionResultPopoverComponent } from '../action-result-popover/action-result-popover.component';
Expand Down Expand Up @@ -72,7 +72,7 @@ export class ChatComponent implements OnInit, OnDestroy {
public disasterTypeLabel: string;
public disasterTypeName: string;
public actionIndicatorLabel: string;
public actionIndicatorNumberFormat: string;
public actionIndicatorNumberFormat: NumberFormat;
public forecastInfo: string[];
public country: Country;
public disasterType: DisasterType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<img src="assets/icons/source-info.svg" id="disclaimer-approximate-trigger" />
<ion-popover
side="right"
trigger="disclaimer-approximate-trigger"
triggerAction="click"
showBackdrop="false"
>
<ng-template>
<ion-content class="ion-padding font-size-12">
{{ 'disclaimer-approximate-component.message' | translate }}
</ion-content>
</ng-template>
</ion-popover>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ion-popover {
--offset-x: 8px;
--offset-y: -2px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { TranslateModule } from '@ngx-translate/core';

import { DisclaimerApproximateComponent } from './disclaimer-approximate.component';

describe('DisclaimerApproximateComponent', () => {
let component: DisclaimerApproximateComponent;
let fixture: ComponentFixture<DisclaimerApproximateComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [DisclaimerApproximateComponent],
imports: [IonicModule, TranslateModule.forRoot()],
}).compileComponents();

fixture = TestBed.createComponent(DisclaimerApproximateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component } from '@angular/core';

@Component({
selector: 'app-disclaimer-approximate',
templateUrl: './disclaimer-approximate.component.html',
styleUrls: ['./disclaimer-approximate.component.scss'],
})
export class DisclaimerApproximateComponent {}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
'(' +
(actionIndicatorLabel | titlecase) +
' ' +
(event.actionsValueSum | number: '.0-0') +
(event.actionsValueSum | compact) +
')'
}}</span>
} @else {
Expand Down Expand Up @@ -220,14 +220,9 @@
}
@if (area.actionsValue && area.actionsValue > 0) {
{{ ' - ' }}
@switch (actionIndicatorNumberFormat) {
@case ('decimal0') {
<span>{{ area.actionsValue | number: '.0-0' }}</span>
}
@case ('perc') {
<span>{{ area.actionsValue | percent: '.0-0' }}</span>
}
}
<span>{{
area.actionsValue | compact: actionIndicatorNumberFormat
}}</span>
}
</p>
</li>
Expand Down Expand Up @@ -281,14 +276,9 @@
<span>({{ area.nameParent }})</span>
}
-
@switch (actionIndicatorNumberFormat) {
@case ('decimal0') {
<span>{{ area.actionsValue | number: '.0-0' }}</span>
}
@case ('perc') {
<span>{{ area.actionsValue | percent: '.0-0' }}</span>
}
}
<span>{{
area.actionsValue | compact: actionIndicatorNumberFormat
}}</span>
</p>
</li>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TimelineService } from '../../services/timeline.service';
import { DisasterTypeKey } from '../../types/disaster-type-key';
import { LeadTime, LeadTimeTriggerKey } from '../../types/lead-time';
import { TriggeredArea } from '../../types/triggered-area';
import { NumberFormat } from '../../types/indicator-group';

@Component({
selector: 'app-event-speech-bubble',
Expand Down Expand Up @@ -46,7 +47,7 @@ export class EventSpeechBubbleComponent implements AfterViewChecked, OnDestroy {
@Input()
public actionIndicatorLabel: string;
@Input()
public actionIndicatorNumberFormat: string;
public actionIndicatorNumberFormat: NumberFormat;

public typhoonLandfallText: string;
public displayName: string;
Expand Down
23 changes: 0 additions & 23 deletions interfaces/IBF-dashboard/src/app/config.backup.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
>
<ion-content>
<ion-grid
class="ibf-dashboard-right-column-left-sub-column-grid ion-justify-content-between ion-no-padding"
class="ibf-dashboard-right-column-left-sub-column-grid ion-no-padding"
>
<ion-row>
<ion-col>
Expand All @@ -149,7 +149,7 @@
<app-aggregates areaStatus="stopped"></app-aggregates>
</ion-col>
</ion-row>
<ion-row>
<ion-row class="margin-top-auto">
<ion-col>
<app-areas-of-focus-summary></app-areas-of-focus-summary>
</ion-col>
Expand Down
8 changes: 8 additions & 0 deletions interfaces/IBF-dashboard/src/app/pipes/compact.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CompactPipe } from './compact.pipe';

describe('CompactPipe', () => {
it('create an instance', () => {
const pipe = new CompactPipe('en-US');
expect(pipe).toBeTruthy();
});
});
23 changes: 23 additions & 0 deletions interfaces/IBF-dashboard/src/app/pipes/compact.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Inject, LOCALE_ID, Pipe, PipeTransform } from '@angular/core';
import { NumberFormat } from '../types/indicator-group';

@Pipe({
standalone: true,
name: 'compact',
})
export class CompactPipe implements PipeTransform {
constructor(@Inject(LOCALE_ID) private locale: string) {}

transform(value: number, format: NumberFormat = NumberFormat.decimal0) {
const style = format === NumberFormat.perc ? 'percent' : 'decimal';
const min = format === NumberFormat.perc ? 0.1 : 10;

value = value > 0 ? Math.max(min, value) : 0;

return new Intl.NumberFormat(this.locale, {
maximumSignificantDigits: 1,
style,
notation: 'compact',
}).format(value);
}
}
6 changes: 6 additions & 0 deletions interfaces/IBF-dashboard/src/app/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ import { TooltipPopoverComponent } from './components/tooltip-popover/tooltip-po
import { TooltipComponent } from './components/tooltip/tooltip.component';
import { UserStateComponent } from './components/user-state/user-state.component';
import { BackendMockScenarioComponent } from './mocks/backend-mock-scenario-component/backend-mock-scenario.component';
import { CompactPipe } from './pipes/compact.pipe';
import { DisclaimerApproximateComponent } from './components/disclaimer-approximate/disclaimer-approximate.component';

@NgModule({
imports: [
AnalyticsModule,
Expand All @@ -57,6 +60,7 @@ import { BackendMockScenarioComponent } from './mocks/backend-mock-scenario-comp
LeafletMarkerClusterModule,
LeafletModule,
TranslateModule,
CompactPipe,
],
declarations: [
AboutBtnComponent,
Expand Down Expand Up @@ -101,6 +105,7 @@ import { BackendMockScenarioComponent } from './mocks/backend-mock-scenario-comp
DynamicPointPopupComponent,
TyphoonTrackpointPopupContentComponent,
GlofasStationPopupContentComponent,
DisclaimerApproximateComponent,
],
exports: [
AboutBtnComponent,
Expand Down Expand Up @@ -146,6 +151,7 @@ import { BackendMockScenarioComponent } from './mocks/backend-mock-scenario-comp
DynamicPointPopupComponent,
TyphoonTrackpointPopupContentComponent,
GlofasStationPopupContentComponent,
DisclaimerApproximateComponent,
],
})
export class SharedModule {}
3 changes: 3 additions & 0 deletions interfaces/IBF-dashboard/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
"plural-suffix": "(s)",
"not-applicable": "N/A"
},
"disclaimer-approximate-component": {
"message": "All numbers are approximate and meant to be used as guidance."
},
"areas-of-focus-summary-component": {
"title": "Actions Summary",
"aof-subtitle": "Sector",
Expand Down
8 changes: 8 additions & 0 deletions interfaces/IBF-dashboard/src/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,11 @@ app-tooltip {
}
}
}

.margin-top-auto {
margin-top: auto;
}

.font-size-12 {
font-size: 12px;
}
15 changes: 2 additions & 13 deletions interfaces/IBF-dashboard/src/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
// sort array ascending
const asc = (arr) => arr.sort((a, b) => a - b);
const asc = (arr: number[]) => arr.sort((a: number, b: number) => a - b);

// const sum = (arr) => arr.reduce((a, b) => a + b, 0);

// const mean = (arr) => sum(arr) / arr.length;

// sample standard deviation
// const std = (arr) => {
// const mu = mean(arr);
// const diffArr = arr.map((a) => (a - mu) ** 2);
// return Math.sqrt(sum(diffArr) / (arr.length - 1));
// };

export const quantile = (arr, q) => {
export const quantile = (arr: number[], q: number) => {
const sorted = asc(arr);
const pos = (sorted.length - 1) * q;
const base = Math.floor(pos);
Expand Down
Loading

0 comments on commit 25449fe

Please sign in to comment.