Skip to content

Commit

Permalink
feat(chart): adiciona possibilidade de formatação do eixo de valor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Leonardo Fagundes Rocio committed Jan 27, 2025
1 parent 721ca47 commit 49a59e6
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @usedBy PoChartComponent
*
* @description
*
* *Enum* `PoChartLabelFormat` para especificação dos tipos de formatação do eixo de valor no gráfico.
*/
export enum PoChartLabelFormat {
/**
* O eixo de valores exibirá números formatados com duas casas decimais. Equivalente ao formato `'1.2-2'` da [DecimalPipe](https://angular.io/api/common/DecimalPipe).
*/
Number = 'number',

/**
* 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).
*/
Currency = 'currency'
}
1 change: 1 addition & 0 deletions projects/ui/src/lib/components/po-chart/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './interfaces/po-chart-serie.interface';

export * from './enums/po-chart-type.enum';
export * from './enums/po-chart-label-format.enum';
export * from './interfaces/po-chart-axis-options.interface';
export * from './interfaces/po-chart-options.interface';
export * from './interfaces/po-chart-serie-data-label.interface';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PoChartLabelFormat } from '../enums/po-chart-label-format.enum';

/**
* @usedBy PoChartComponent
*
Expand Down Expand Up @@ -36,4 +38,13 @@ export interface PoChartAxisOptions {
* > 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.
*/
minRange?: number;

/**
* @optional
*
* @description
*
* Define o tipo do label e a formatação exibida no eixo de valor.
*/
labelType?: PoChartLabelFormat;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';

import { PoChartAxisLabelComponent } from './po-chart-axis-label.component';
import { PoChartType } from '../../../enums/po-chart-type.enum';
import { PoChartModule } from '../../../po-chart.module';
import { PoChartLabelFormat } from '../../../enums/po-chart-label-format.enum';
import { DEFAULT_CURRENCY_CODE } from '@angular/core';

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

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [PoChartAxisLabelComponent]
imports: [PoChartModule],
declarations: [PoChartAxisLabelComponent],
providers: [{ provide: DEFAULT_CURRENCY_CODE, useValue: 'BRL' }]
}).compileComponents();
});

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

expect(component.trackBy(index)).toBe(expectedValue);
});

describe('formatValueAxis:', () => {
it('shouldn`t apply format to X axis if graphic type is bar', () => {
const value: string = '10000.00';

component.axisOptions = { labelType: PoChartLabelFormat.Number };
component.type = PoChartType.Bar;

expect(component.formatValueAxis(value, 'x')).toBe(value);
});

it('shouldn`t apply format to Y axis if graphic type is not bar', () => {
const value: string = '35000.00';

component.axisOptions = { labelType: PoChartLabelFormat.Number };
component.type = PoChartType.Column;

expect(component.formatValueAxis(value, 'y')).toBe(value);
});

it('should return original value', () => {
const value: string = '27000.00';
expect(component.formatValueAxis(value, 'x')).toBe(value);
});

it('should return formatted currency', () => {
const value = '10000.00';
const expectedValue: string = 'R$10,000.00';

component.axisOptions = { labelType: PoChartLabelFormat.Currency };
component.type = PoChartType.Column;

expect(component.formatValueAxis(value, 'x')).toBe(expectedValue);
});

it('should return formatted number', () => {
const value: string = '1291355450.00';
const expectedValue: string = '1,291,355,450.00';

component.axisOptions = { labelType: PoChartLabelFormat.Number };
component.type = PoChartType.Column;

expect(component.formatValueAxis(value, 'x')).toBe(expectedValue);
});
});
});

describe('Template:', () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Component, Input } from '@angular/core';

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

@Component({
selector: '[po-chart-axis-label]',
Expand All @@ -16,9 +19,33 @@ export class PoChartAxisLabelComponent {

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

constructor() {}
@Input('p-options') axisOptions: PoChartAxisOptions;

constructor(
private decimalPipe: DecimalPipe,
private currencyPipe: CurrencyPipe
) {}

trackBy(index) {
return index;
}

formatValueAxis(label: string, axis: string): string {
const isCategoryAxisValue: boolean =
(this.type === PoChartType.Bar && axis === 'x') || (this.type !== PoChartType.Bar && axis === 'y');

if (isCategoryAxisValue) {
return label;
}

if (this.axisOptions?.labelType === PoChartLabelFormat.Currency) {
return this.currencyPipe.transform(label, null, 'symbol', '1.2-2');
}

if (this.axisOptions?.labelType === PoChartLabelFormat.Number) {
return this.decimalPipe.transform(label, '1.2-2');
}

return label;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions projects/ui/src/lib/components/po-chart/po-chart.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommonModule } from '@angular/common';
import { CommonModule, CurrencyPipe, DecimalPipe } from '@angular/common';
import { NgModule } from '@angular/core';

import { PoTooltipModule } from '../../directives/po-tooltip/po-tooltip.module';
Expand Down Expand Up @@ -51,6 +51,7 @@ import { PoResizeObserverDirective } from './directives/po-resize-observer.direc
PoChartTooltipDirective,
PoResizeObserverDirective
],
exports: [PoChartComponent]
exports: [PoChartComponent],
providers: [DecimalPipe, CurrencyPipe]
})
export class PoChartModule {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from '@angular/core';

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

@Component({
selector: 'sample-po-chart-coffee-ranking',
Expand Down Expand Up @@ -99,7 +99,8 @@ export class SamplePoChartCoffeeRankingComponent {
consumptionPerCapitaOptions: PoChartOptions = {
axis: {
maxRange: 100,
gridLines: 2
gridLines: 2,
labelType: PoChartLabelFormat.Number
}
};

Expand All @@ -114,7 +115,8 @@ export class SamplePoChartCoffeeRankingComponent {
axis: {
minRange: 0,
maxRange: 40,
gridLines: 5
gridLines: 5,
labelType: PoChartLabelFormat.Number
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@
>
</po-number>

<po-select
class="po-md-4"
name="labelType"
[(ngModel)]="options.axis.labelType"
p-label="labelType"
[p-options]="labelTypeOptions"
(p-change)="addOptions()"
>
</po-select>

<po-checkbox-group
class="po-md-4"
name="propertiesOptions"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
PoSelectOption,
PoChartOptions,
PoCheckboxGroupOption,
PoChartDataLabel
PoChartDataLabel,
PoChartLabelFormat
} from '@po-ui/ng-components';

@Component({
Expand All @@ -31,7 +32,8 @@ export class SamplePoChartLabsComponent implements OnInit {
axis: {
minRange: undefined,
maxRange: undefined,
gridLines: undefined
gridLines: undefined,
labelType: undefined
}
};
dataLabel: PoChartDataLabel;
Expand All @@ -49,6 +51,11 @@ export class SamplePoChartLabsComponent implements OnInit {

readonly dataLabelOptions: Array<PoCheckboxGroupOption> = [{ value: 'fixed', label: 'Fixed' }];

readonly labelTypeOptions: Array<PoSelectOption> = [
{ label: 'Number', value: PoChartLabelFormat.Number },
{ label: 'Currency', value: PoChartLabelFormat.Currency }
];

ngOnInit() {
this.restore();
}
Expand Down Expand Up @@ -114,7 +121,8 @@ export class SamplePoChartLabsComponent implements OnInit {
axis: {
minRange: undefined,
maxRange: undefined,
gridLines: undefined
gridLines: undefined,
labelType: undefined
}
};
}
Expand Down

0 comments on commit 49a59e6

Please sign in to comment.