Skip to content

Commit

Permalink
feat(chart): inclusão propriedade p-data-label para chart tipo line
Browse files Browse the repository at this point in the history
Implementação da propriedade 'p-data-label', onde é possível passar a propriedade `fixed`.
Com essa propriedade (fixed=tue), irar alterar o grafico do tipo line.
- O valor da series ficar sendo exibido fixado com uma background transparente.
- Não será mostrado o tooltip ao hover do bullet.
- Será adicionado uma opacidade a todas outras series quando selecionado uma delas.
- O hover da serie ficará ate selecionar outra ou sair do gráfico.

fixes DTHFUI-10134
  • Loading branch information
bruno-severino committed Nov 27, 2024
1 parent 3647b03 commit 7d21f50
Show file tree
Hide file tree
Showing 22 changed files with 551 additions and 27 deletions.
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
Expand Up @@ -3,6 +3,7 @@ export * from './interfaces/po-chart-serie.interface';
export * from './enums/po-chart-type.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';

export * from './po-chart.component';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ export interface PoChartPathCoordinates {

/** O texto de exibição no tooltip. */
tooltipLabel?: string;

/** Indica se é elemento não está em foco. */
isBlur?: boolean;

/** Indica se o valor da série está mostrando fixado na tela. */
isFixed?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ export interface PoChartPointsCoordinates {

/** Coordenada vertical. */
yCoordinate: number;

/** Indica se o valor da série está mostrando fixado na tela. */
isFixed?: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PoChartType } from '../enums/po-chart-type.enum';

/**
* @usedBy PoChartSerie
*
* @description
*
* Interface das propriedade da serie do `po-chart`.
* > Funciona somente para gráfico do tipo `line`.
*/
export interface PoChartDataLabel {
/**
* @optional
*
* @description
*
* Define se o texto que será exibido deverá estar exibindo fixo na série do *chart*.
*
* > Caso seja informado, o *tooltip* não será exibido, e ao hover na série as outras séries ficaram com opacidade
* > Funciona somente para o type *PoChartType.Line*
*/
fixed?: boolean;
}
22 changes: 22 additions & 0 deletions projects/ui/src/lib/components/po-chart/po-chart-base.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PoChartType } from './enums/po-chart-type.enum';
import { PoChartOptions } from './interfaces/po-chart-options.interface';
import { PoChartSerie } from './interfaces/po-chart-serie.interface';
import { PoColorService } from '../../services/po-color/po-color.service';
import { PoChartDataLabel } from './interfaces/po-chart-serie-data-label.interface';

const poChartDefaultHeight = 400;
const poChartMinHeight = 200;
Expand Down Expand Up @@ -210,6 +211,27 @@ export abstract class PoChartBaseComponent implements OnChanges {
return this._options;
}

/**
* @optional
*
* @description
*
* Define as propiedades para a label.
*
* É possível, definir a configuração de exibição dos valores das series, mostrando fixado.
* Ao passar o fixed, o valor ficará visivel e não irar mostrar o tooltip, além da mudança
* visual dos bullets e a adição da opacidade à outras series diferentes no hover.
*
* > Funcionalidade funciona somente para o gráfico do tipo `line`
*
* ```
* dataLabel: PoChartDataLabel = {
* fixed: true,
* };
* ```
*/
@Input('p-data-label') dataLabel?: PoChartDataLabel;

constructor(protected colorService: PoColorService) {}

get isTypeCircular() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[attr.viewBox]="viewBox"
[attr.width]="containerSize.svgWidth"
[attr.height]="containerSize.svgHeight"
(mouseenter)="onChartMouseEnter()"
(mouseleave)="onChartMouseLeave()"
>
<!-- axis -->
<svg:g
Expand Down Expand Up @@ -55,6 +57,8 @@
[p-range]="range"
[p-series]="seriesByType['line']"
[p-container-size]="containerSize"
[p-data-label]="dataLabel"
[p-insideChart]="insideChart"
(p-point-hover)="onSerieHover($event)"
(p-point-click)="onSerieClick($event)"
></svg:g>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,48 @@ describe('PoChartContainerComponent', () => {

expect(fnError).not.toThrow();
});

it('onChartMouseEnter: should set `insideChart` to true', () => {
component.insideChart = false;
component.onChartMouseEnter();

expect(component.insideChart).toBeTrue();
});

it('onChartMouseLeave: should set `insideChart` to false', () => {
component.insideChart = true;
component.onChartMouseLeave();

expect(component.insideChart).toBeFalse();
});

it('onChartMouseEnter and onChartMouseLeave: should toggle `insideChart` between true and false', () => {
component.insideChart = false;

component.onChartMouseEnter();
expect(component.insideChart).toBeTrue();

component.onChartMouseLeave();
expect(component.insideChart).toBeFalse();
});

it('Template: should call `onChartMouseEnter` and `onChartMouseLeave` on mouse enter and leave', () => {
const svgElement = nativeElement.querySelector('svg');
const spyOnChartMouseEnter = spyOn(component, 'onChartMouseEnter').and.callThrough();
const spyOnChartMouseLeave = spyOn(component, 'onChartMouseLeave').and.callThrough();

svgElement.dispatchEvent(new Event('mouseenter'));
fixture.detectChanges();

expect(spyOnChartMouseEnter).toHaveBeenCalled();
expect(component.insideChart).toBeTrue();

svgElement.dispatchEvent(new Event('mouseleave'));
fixture.detectChanges();

expect(spyOnChartMouseLeave).toHaveBeenCalled();
expect(component.insideChart).toBeFalse();
});
});

describe('Properties: ', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PoChartAxisOptions } from '../interfaces/po-chart-axis-options.interfac
import { PoChartMathsService } from '../services/po-chart-maths.service';
import { PoChartMinMaxValues } from '../interfaces/po-chart-min-max-values.interface';
import { PoChartSerie } from '../interfaces/po-chart-serie.interface';
import { PoChartDataLabel } from '../interfaces/po-chart-serie-data-label.interface';

@Component({
selector: 'po-chart-container',
Expand All @@ -19,6 +20,8 @@ export class PoChartContainerComponent implements OnChanges {

@Input('p-container-size') containerSize: PoChartContainerSize;

@Input('p-data-label') dataLabel?: PoChartDataLabel;

@Output('p-serie-click') serieClick = new EventEmitter<any>();

@Output('p-serie-hover') serieHover = new EventEmitter<any>();
Expand All @@ -32,6 +35,7 @@ export class PoChartContainerComponent implements OnChanges {
seriesByType;
svgSpace;
viewBox: string;
insideChart: boolean;

private _options: PoChartOptions;
private _series: Array<PoChartSerie> = [];
Expand Down Expand Up @@ -147,4 +151,12 @@ export class PoChartContainerComponent implements OnChanges {
};
}
}

onChartMouseEnter() {
this.insideChart = true;
}

onChartMouseLeave() {
this.insideChart = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ describe('PoChartLineBaseComponent', () => {
component['seriePathPointsDefinition'](component.containerSize, component.series, range);

const expectedResult = [
{ coordinates: ' M93 27 L136 26 L178 26', color: '#0C6C94', isActive: true },
{ coordinates: ' M93 21 L136 14 L178 8', color: '#29B6C5', isActive: true }
{ coordinates: ' M93 27 L136 26 L178 26', color: '#0C6C94', isActive: true, isFixed: undefined },
{ coordinates: ' M93 21 L136 14 L178 8', color: '#29B6C5', isActive: true, isFixed: undefined }
];

expect(component.seriesPathsCoordinates).toEqual(expectedResult);
Expand All @@ -113,6 +113,7 @@ describe('PoChartLineBaseComponent', () => {

it('should apply apply value to `seriesPointsCoordinates`', () => {
component.series = [{ label: 'Vancouver', data: [5, 10], type: PoChartType.Line, color: 'blue' }];
component.dataLabel = { fixed: true };

component['seriePathPointsDefinition'](component.containerSize, component.series, range);

Expand All @@ -126,7 +127,8 @@ describe('PoChartLineBaseComponent', () => {
data: 5,
xCoordinate: 104,
yCoordinate: 24,
isActive: true
isActive: true,
isFixed: true
},
{
category: undefined,
Expand All @@ -136,7 +138,8 @@ describe('PoChartLineBaseComponent', () => {
data: 10,
xCoordinate: 168,
yCoordinate: 21,
isActive: true
isActive: true,
isFixed: true
}
]
];
Expand All @@ -149,6 +152,7 @@ describe('PoChartLineBaseComponent', () => {
it('should apply apply only data to tooltipLabel if label is undefined', () => {
const minMaxSeriesValues = { minValue: 5, maxValue: 10 };
component.series = [{ label: undefined, data: [5, 10], type: PoChartType.Line, color: 'blue' }];
component.dataLabel = { fixed: true }; // Definir `dataLabel` para incluir `fixed`

component['seriePathPointsDefinition'](component.containerSize, component.series, minMaxSeriesValues);

Expand All @@ -162,7 +166,8 @@ describe('PoChartLineBaseComponent', () => {
data: 5,
xCoordinate: 104,
yCoordinate: 28,
isActive: true
isActive: true,
isFixed: true // Adicione a propriedade esperada
},
{
category: undefined,
Expand All @@ -172,7 +177,8 @@ describe('PoChartLineBaseComponent', () => {
data: 10,
xCoordinate: 168,
yCoordinate: 8,
isActive: true
isActive: true,
isFixed: true // Adicione a propriedade esperada
}
]
];
Expand All @@ -186,8 +192,7 @@ describe('PoChartLineBaseComponent', () => {
const minMaxSeriesValues = { minValue: 5, maxValue: 10 };
component.series = [{ label: 'Vancouver', data: [5, 10], type: PoChartType.Line, color: 'blue' }];
component.categories = ['janeiro', 'fevereiro'];

component['seriePathPointsDefinition'](component.containerSize, component.series, minMaxSeriesValues);
component.dataLabel = { fixed: true }; // Configurar o dataLabel

const expectedResult = [
[
Expand All @@ -199,7 +204,8 @@ describe('PoChartLineBaseComponent', () => {
data: 5,
xCoordinate: 104,
yCoordinate: 28,
isActive: true
isActive: true,
isFixed: true // Adicionar isFixed
},
{
category: 'fevereiro',
Expand All @@ -209,11 +215,14 @@ describe('PoChartLineBaseComponent', () => {
data: 10,
xCoordinate: 168,
yCoordinate: 8,
isActive: true
isActive: true,
isFixed: true // Adicionar isFixed
}
]
];

component['seriePathPointsDefinition'](component.containerSize, component.series, minMaxSeriesValues);

expect(component.seriesPointsCoordinates).toEqual(expectedResult);
expect(component.seriesPointsCoordinates.length).toBe(1);
expect(component.seriesPointsCoordinates[0].length).toBe(2);
Expand All @@ -234,7 +243,8 @@ describe('PoChartLineBaseComponent', () => {
data: 10,
xCoordinate: 93,
yCoordinate: 8,
isActive: true
isActive: true,
isFixed: undefined
},
{
category: 'março',
Expand All @@ -244,7 +254,8 @@ describe('PoChartLineBaseComponent', () => {
data: 12,
xCoordinate: 178,
yCoordinate: 0,
isActive: true
isActive: true,
isFixed: undefined
}
]
];
Expand All @@ -253,7 +264,7 @@ describe('PoChartLineBaseComponent', () => {

expect(component.seriesPointsCoordinates).toEqual(expectedPointsResult);
expect(component.seriesPathsCoordinates).toEqual([
{ coordinates: ' M93 8 L178 0', color: '#29B6C5', isActive: true }
{ coordinates: ' M93 8 L178 0', color: '#29B6C5', isActive: true, isFixed: undefined }
]);
});

Expand All @@ -274,7 +285,7 @@ describe('PoChartLineBaseComponent', () => {

component['seriePathPointsDefinition'](component.containerSize, component.series, minMaxSeriesValues);

const expectedResult = [{ coordinates: ' M136 28', color: '#29B6C5', isActive: true }];
const expectedResult = [{ coordinates: ' M136 28', color: '#29B6C5', isActive: true, isFixed: undefined }];

expect(component.seriesPathsCoordinates).toEqual(expectedResult);
expect(component.seriesPathsCoordinates.length).toBe(1);
Expand Down Expand Up @@ -418,5 +429,19 @@ describe('PoChartLineBaseComponent', () => {

expect(spyseriePathPointsDefinition).not.toHaveBeenCalled();
});

describe('isFixed:', () => {
it('should include `isFixed` property in `pointCoordinates` when `dataLabel?.fixed` is true', () => {
component.dataLabel = { fixed: true };

component.series = [{ label: 'Test', data: [10], type: PoChartType.Line, color: 'blue' }];

component['seriePathPointsDefinition'](component.containerSize, component.series, range);

const point = component.seriesPointsCoordinates[0][0];

expect(point.isFixed).toBeTrue();
});
});
});
});
Loading

0 comments on commit 7d21f50

Please sign in to comment.