Skip to content

Commit 8709bc6

Browse files
leonardofrociofelipepetuco
authored andcommitted
feat(chart): adiciona possibilidade de formatação do eixo de valor
O componente po-chart não realiza a formatação do milhares eixo de valor do gráfico, dificultando a visualização de grandes valores. Adiciona a propriedade labelType, que indica o tipo de formatação que deve ser aplicada ao eixo de valor do gráfico. Fixes #2358
1 parent 7d5d32a commit 8709bc6

File tree

11 files changed

+157
-13
lines changed

11 files changed

+157
-13
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @usedBy PoChartComponent
3+
*
4+
* @description
5+
*
6+
* *Enum* `PoChartLabelFormat` para especificação dos tipos de formatação do eixo de valor no gráfico.
7+
*/
8+
export enum PoChartLabelFormat {
9+
/**
10+
* Os valores serão exibidos no formato numérico com duas casas decimais. Equivalente ao formato `'1.2-2'` da [DecimalPipe](https://angular.io/api/common/DecimalPipe).
11+
*/
12+
Number = 'number',
13+
14+
/**
15+
* Os valores serão exibidos com o símbolo monetário de acordo com a formatação padrão da aplicação, isto é, o valor do token [DEFAULT_CURRENCY_CODE](https://angular.dev/api/core/DEFAULT_CURRENCY_CODE). Para adequar ao padrão numérico brasileiro, é necessário configurar o [LOCALE_ID](https://angular.dev/api/core/LOCALE_ID) da aplicação. A configuração pode ser feita da seguinte forma:
16+
* ```
17+
* import { LOCALE_ID } from '@angular/core';
18+
* import { registerLocaleData } from '@angular/common';
19+
* import localePt from '@angular/common/locales/pt';
20+
*
21+
* registerLocaleData(localePt);
22+
*
23+
* @NgModule({
24+
* providers: [
25+
* { provide: LOCALE_ID, useValue: 'pt-BR' },
26+
* { provide: DEFAULT_CURRENCY_CODE, useValue: 'BRL' }
27+
* ]
28+
* })
29+
* export class AppModule { }
30+
* ```
31+
*/
32+
Currency = 'currency'
33+
}

projects/ui/src/lib/components/po-chart/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './interfaces/po-chart-serie.interface';
22

33
export * from './enums/po-chart-type.enum';
4+
export * from './enums/po-chart-label-format.enum';
45
export * from './interfaces/po-chart-axis-options.interface';
56
export * from './interfaces/po-chart-options.interface';
67
export * from './interfaces/po-chart-serie-data-label.interface';

projects/ui/src/lib/components/po-chart/interfaces/po-chart-axis-options.interface.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { PoChartLabelFormat } from '../enums/po-chart-label-format.enum';
2+
13
/**
24
* @usedBy PoChartComponent
35
*
@@ -36,4 +38,13 @@ export interface PoChartAxisOptions {
3638
* > Esta definição não deve refletir na plotagem das séries. Os valores máximos e mínimos encontrados nas séries serão as bases para seus alcance.
3739
*/
3840
minRange?: number;
41+
42+
/**
43+
* @optional
44+
*
45+
* @description
46+
*
47+
* Define o tipo do label e a formatação exibida no eixo de valor.
48+
*/
49+
labelType?: PoChartLabelFormat;
3950
}

projects/ui/src/lib/components/po-chart/po-chart-container/po-chart-axis/po-chart-axis-label/po-chart-axis-label.component.spec.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
22

33
import { PoChartAxisLabelComponent } from './po-chart-axis-label.component';
44
import { PoChartType } from '../../../enums/po-chart-type.enum';
5+
import { PoChartModule } from '../../../po-chart.module';
6+
import { PoChartLabelFormat } from '../../../enums/po-chart-label-format.enum';
7+
import { DEFAULT_CURRENCY_CODE } from '@angular/core';
58

69
describe('PoChartAxisXLabelComponent', () => {
710
let component: PoChartAxisLabelComponent;
@@ -10,7 +13,9 @@ describe('PoChartAxisXLabelComponent', () => {
1013

1114
beforeEach(async () => {
1215
await TestBed.configureTestingModule({
13-
declarations: [PoChartAxisLabelComponent]
16+
imports: [PoChartModule],
17+
declarations: [PoChartAxisLabelComponent],
18+
providers: [{ provide: DEFAULT_CURRENCY_CODE, useValue: 'BRL' }]
1419
}).compileComponents();
1520
});
1621

@@ -33,6 +38,51 @@ describe('PoChartAxisXLabelComponent', () => {
3338

3439
expect(component.trackBy(index)).toBe(expectedValue);
3540
});
41+
42+
describe('formatValueAxis:', () => {
43+
it('shouldn`t apply format to X axis if graphic type is bar', () => {
44+
const value: string = '10000.00';
45+
46+
component.axisOptions = { labelType: PoChartLabelFormat.Number };
47+
component.type = PoChartType.Bar;
48+
49+
expect(component.formatValueAxis(value, 'x')).toBe(value);
50+
});
51+
52+
it('shouldn`t apply format to Y axis if graphic type is not bar', () => {
53+
const value: string = '35000.00';
54+
55+
component.axisOptions = { labelType: PoChartLabelFormat.Number };
56+
component.type = PoChartType.Column;
57+
58+
expect(component.formatValueAxis(value, 'y')).toBe(value);
59+
});
60+
61+
it('should return original value', () => {
62+
const value: string = '27000.00';
63+
expect(component.formatValueAxis(value, 'x')).toBe(value);
64+
});
65+
66+
it('should return formatted currency', () => {
67+
const value = '10000.00';
68+
const expectedValue: string = 'R$10,000.00';
69+
70+
component.axisOptions = { labelType: PoChartLabelFormat.Currency };
71+
component.type = PoChartType.Column;
72+
73+
expect(component.formatValueAxis(value, 'x')).toBe(expectedValue);
74+
});
75+
76+
it('should return formatted number', () => {
77+
const value: string = '1291355450.00';
78+
const expectedValue: string = '1,291,355,450.00';
79+
80+
component.axisOptions = { labelType: PoChartLabelFormat.Number };
81+
component.type = PoChartType.Column;
82+
83+
expect(component.formatValueAxis(value, 'x')).toBe(expectedValue);
84+
});
85+
});
3686
});
3787

3888
describe('Template:', () => {

projects/ui/src/lib/components/po-chart/po-chart-container/po-chart-axis/po-chart-axis-label/po-chart-axis-label.component.svg

Lines changed: 3 additions & 3 deletions
Loading

projects/ui/src/lib/components/po-chart/po-chart-container/po-chart-axis/po-chart-axis-label/po-chart-axis-label.component.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { Component, Input } from '@angular/core';
22

33
import { PoChartType } from '../../../enums/po-chart-type.enum';
44
import { PoChartLabelCoordinates } from '../../../interfaces/po-chart-label-coordinates.interface';
5+
import { PoChartAxisOptions } from '../../../interfaces/po-chart-axis-options.interface';
6+
import { CurrencyPipe, DecimalPipe } from '@angular/common';
7+
import { PoChartLabelFormat } from '../../../enums/po-chart-label-format.enum';
58

69
@Component({
710
selector: '[po-chart-axis-label]',
@@ -17,9 +20,33 @@ export class PoChartAxisLabelComponent {
1720

1821
@Input('p-type') type: PoChartType;
1922

20-
constructor() {}
23+
@Input('p-options') axisOptions: PoChartAxisOptions;
24+
25+
constructor(
26+
private decimalPipe: DecimalPipe,
27+
private currencyPipe: CurrencyPipe
28+
) {}
2129

2230
trackBy(index) {
2331
return index;
2432
}
33+
34+
formatValueAxis(label: string, axis: string): string {
35+
const isCategoryAxisValue: boolean =
36+
(this.type === PoChartType.Bar && axis === 'x') || (this.type !== PoChartType.Bar && axis === 'y');
37+
38+
if (isCategoryAxisValue) {
39+
return label;
40+
}
41+
42+
if (this.axisOptions?.labelType === PoChartLabelFormat.Currency) {
43+
return this.currencyPipe.transform(label, null, 'symbol', '1.2-2');
44+
}
45+
46+
if (this.axisOptions?.labelType === PoChartLabelFormat.Number) {
47+
return this.decimalPipe.transform(label, '1.2-2');
48+
}
49+
50+
return label;
51+
}
2552
}

projects/ui/src/lib/components/po-chart/po-chart-container/po-chart-axis/po-chart-axis.component.svg

Lines changed: 1 addition & 0 deletions
Loading

projects/ui/src/lib/components/po-chart/po-chart.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommonModule } from '@angular/common';
1+
import { CommonModule, CurrencyPipe, DecimalPipe } from '@angular/common';
22
import { NgModule } from '@angular/core';
33

44
import { PoTooltipModule } from '../../directives/po-tooltip/po-tooltip.module';
@@ -51,6 +51,7 @@ import { PoResizeObserverDirective } from './directives/po-resize-observer.direc
5151
PoChartTooltipDirective,
5252
PoResizeObserverDirective
5353
],
54-
exports: [PoChartComponent]
54+
exports: [PoChartComponent],
55+
providers: [DecimalPipe, CurrencyPipe]
5556
})
5657
export class PoChartModule {}

projects/ui/src/lib/components/po-chart/samples/sample-po-chart-coffee-ranking/sample-po-chart-coffee-ranking.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component } from '@angular/core';
22

3-
import { PoChartType, PoChartOptions, PoChartSerie, PoDialogService } from '@po-ui/ng-components';
3+
import { PoChartType, PoChartOptions, PoChartSerie, PoDialogService, PoChartLabelFormat } from '@po-ui/ng-components';
44

55
@Component({
66
selector: 'sample-po-chart-coffee-ranking',
@@ -100,7 +100,8 @@ export class SamplePoChartCoffeeRankingComponent {
100100
consumptionPerCapitaOptions: PoChartOptions = {
101101
axis: {
102102
maxRange: 100,
103-
gridLines: 2
103+
gridLines: 2,
104+
labelType: PoChartLabelFormat.Number
104105
}
105106
};
106107

@@ -115,7 +116,8 @@ export class SamplePoChartCoffeeRankingComponent {
115116
axis: {
116117
minRange: 0,
117118
maxRange: 40,
118-
gridLines: 5
119+
gridLines: 5,
120+
labelType: PoChartLabelFormat.Number
119121
}
120122
};
121123

projects/ui/src/lib/components/po-chart/samples/sample-po-chart-labs/sample-po-chart-labs.component.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@
137137
>
138138
</po-number>
139139

140+
<po-select
141+
class="po-md-4"
142+
name="labelType"
143+
[(ngModel)]="options.axis.labelType"
144+
p-label="labelType"
145+
[p-options]="labelTypeOptions"
146+
(p-change)="addOptions()"
147+
>
148+
</po-select>
149+
140150
<po-checkbox-group
141151
class="po-md-4"
142152
name="propertiesOptions"

0 commit comments

Comments
 (0)