Skip to content

Commit d45faaa

Browse files
arthur-polidorioCSimoesJr
authored andcommitted
feat(chart): adiciona feature toogle
Adiciona feature toogle; Corrige bug sonar
1 parent 9a4dc70 commit d45faaa

File tree

10 files changed

+202
-45
lines changed

10 files changed

+202
-45
lines changed

projects/ui/src/lib/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export * from './po-button-group/index';
88
export * from './po-button/index';
99
export * from './po-calendar/index';
1010
export * from './po-chart/index';
11-
export * from './po-chart-new/index'; /*Deletar esta referência quando não for mais necessário.*/
11+
export * from './po-chart-new/index'; /* @ToDo - Deletar esta referência quando não for mais necessário.*/
1212
export * from './po-container/index';
1313
export * from './po-disclaimer-group/index';
1414
export * from './po-disclaimer/index';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export * from './po-chart-new.component';
22

33
export * from './po-chart-new.module';
44

5-
/* Ao apagar esse arquivo, lembre também de remover a linha "export * from './po-chart-new/index'" no arquivo index.css, que fica em:
5+
/* @ToDo - Ao apagar esse arquivo, lembre também de remover a linha "export * from './po-chart-new/index'" no arquivo index.css, que fica em:
66
po-angular\projects\ui\src\lib\components. */

projects/ui/src/lib/components/po-chart-new/po-chart-grid-utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { EChartsOption } from 'echarts/dist/echarts.esm';
21
import { PoChartNewComponent } from './po-chart-new.component';
32
import { PoChartSerie } from '../po-chart/interfaces/po-chart-serie.interface';
43
import { PoChartType } from '../po-chart/enums/po-chart-type.enum';
@@ -21,7 +20,7 @@ export class PoChartGridUtils {
2120
private isTypeDonut = false;
2221
constructor(private readonly component: PoChartNewComponent) {}
2322

24-
setGridOption(options: EChartsOption) {
23+
setGridOption(options) {
2524
const tokenBorderWidthSm = this.resolvePx('--border-width-sm');
2625
const paddingBottom = this.getPaddingBottomGrid();
2726
const paddingTop = this.getPaddingTopGrid();
@@ -35,7 +34,7 @@ export class PoChartGridUtils {
3534
};
3635
}
3736

38-
setOptionsAxis(options: EChartsOption) {
37+
setOptionsAxis(options) {
3938
const tokenFontSizeGrid = this.resolvePx('--font-size-grid', '.po-chart');
4039
const tokenBorderWidthSm = this.resolvePx('--border-width-sm');
4140

@@ -87,7 +86,7 @@ export class PoChartGridUtils {
8786
}
8887
}
8988

90-
setOptionDataZoom(options: EChartsOption) {
89+
setOptionDataZoom(options) {
9190
options.dataZoom = [
9291
{
9392
show: true,
@@ -105,7 +104,7 @@ export class PoChartGridUtils {
105104
];
106105
}
107106

108-
setShowAxisDetails(options: EChartsOption) {
107+
setShowAxisDetails(options) {
109108
if (this.component.options?.axis?.showAxisDetails) {
110109
options.tooltip = {
111110
trigger: 'none',

projects/ui/src/lib/components/po-chart-new/po-chart-grid.utils.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { PoChartType } from '../po-chart/enums/po-chart-type.enum';
22
import { PoChartOptions } from '../po-chart/interfaces/po-chart-options.interface';
33
import { PoChartGridUtils } from './po-chart-grid-utils';
4-
import { EChartsOption } from 'echarts';
54

65
describe('PoChartGridUtils', () => {
76
let utils: PoChartGridUtils;
@@ -77,7 +76,7 @@ describe('PoChartGridUtils', () => {
7776

7877
describe('setOptionsAxis', () => {
7978
it('should define xAxis and yAxis correctly', () => {
80-
const option: EChartsOption = {};
79+
const option: any = {};
8180

8281
utils.setOptionsAxis(option);
8382

projects/ui/src/lib/components/po-chart-new/po-chart-new.component.ts

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
ElementRef,
66
HostListener,
77
OnChanges,
8+
OnDestroy,
89
OnInit,
910
QueryList,
1011
SimpleChanges,
@@ -23,42 +24,53 @@ import { PoPopupAction } from '../po-popup';
2324
import { PoTableColumn } from '../po-table';
2425
import { PoChartNewBaseComponent } from './po-chart-new-base.component';
2526

26-
import * as echarts from 'echarts/dist/echarts.esm';
27-
import { EChartsOption } from 'echarts/dist/echarts.esm';
2827
import { PoChartType } from '../po-chart/enums/po-chart-type.enum';
2928
import { PoChartGridUtils } from './po-chart-grid-utils';
3029

30+
import * as echarts from 'echarts/core';
31+
import { BarChart, CustomChart, GaugeChart, LineChart, PieChart } from 'echarts/charts';
32+
import { use } from 'echarts/core';
33+
import {
34+
BrushComponent,
35+
DataZoomComponent,
36+
GridComponent,
37+
LegendComponent,
38+
MarkLineComponent,
39+
ToolboxComponent,
40+
TooltipComponent
41+
} from 'echarts/components';
42+
import { CanvasRenderer, SVGRenderer } from 'echarts/renderers';
43+
use([
44+
BarChart,
45+
CustomChart,
46+
GaugeChart,
47+
LineChart,
48+
PieChart,
49+
BrushComponent,
50+
DataZoomComponent,
51+
GridComponent,
52+
LegendComponent,
53+
MarkLineComponent,
54+
ToolboxComponent,
55+
TooltipComponent,
56+
CanvasRenderer,
57+
SVGRenderer
58+
]);
59+
3160
/**
3261
* @docsExtends PoChartBaseComponent
33-
*
34-
* @example
35-
*
36-
* <example name="po-chart-basic" title="PO Chart Basic">
37-
* <file name="sample-po-chart-basic/sample-po-chart-basic.component.html"> </file>
38-
* <file name="sample-po-chart-basic/sample-po-chart-basic.component.ts"> </file>
39-
* </example>
40-
*
41-
* <example name="po-chart-labs" title="PO Chart Labs">
42-
* <file name="sample-po-chart-labs/sample-po-chart-labs.component.html"> </file>
43-
* <file name="sample-po-chart-labs/sample-po-chart-labs.component.ts"> </file>
44-
* </example>
45-
*
46-
* <example name="po-chart-coffee-ranking" title="PO Chart - Coffee Ranking">
47-
* <file name="sample-po-chart-coffee-ranking/sample-po-chart-coffee-ranking.component.html"> </file>
48-
* <file name="sample-po-chart-coffee-ranking/sample-po-chart-coffee-ranking.component.ts"> </file>
49-
* </example>
50-
*
51-
* <example name="po-chart-world-exports" title="PO Chart - World Exports">
52-
* <file name="sample-po-chart-world-exports/sample-po-chart-world-exports.component.html"> </file>
53-
* <file name="sample-po-chart-world-exports/sample-po-chart-world-exports.component.ts"> </file>
54-
* </example>
5562
*/
63+
64+
/* @ToDo - Foi retirado o exemplo do sample pois causava problema no build:package da pipe */
5665
@Component({
5766
selector: 'po-chart-new',
5867
templateUrl: './po-chart-new.component.html',
5968
standalone: false
6069
})
61-
export class PoChartNewComponent extends PoChartNewBaseComponent implements OnInit, AfterViewInit, OnChanges {
70+
export class PoChartNewComponent
71+
extends PoChartNewBaseComponent
72+
implements OnInit, AfterViewInit, OnChanges, OnDestroy
73+
{
6274
@ViewChildren(PoTooltipDirective) poTooltip: QueryList<PoTooltipDirective>;
6375
@ViewChild('targetPopup', { read: ElementRef, static: false }) targetRef: ElementRef;
6476
@ViewChild('modalComponent') modal: PoModalComponent;
@@ -105,6 +117,8 @@ export class PoChartNewComponent extends PoChartNewBaseComponent implements OnIn
105117
];
106118
private chartInstance!: echarts.ECharts;
107119
private currentRenderer: 'svg' | 'canvas';
120+
private intersectionObserver: IntersectionObserver;
121+
private hideDomEchartsDiv = false;
108122

109123
constructor(
110124
private el: ElementRef,
@@ -161,6 +175,11 @@ export class PoChartNewComponent extends PoChartNewBaseComponent implements OnIn
161175

162176
ngAfterViewInit() {
163177
this.initECharts();
178+
this.checkShowCEcharts();
179+
}
180+
181+
ngOnDestroy(): void {
182+
this.intersectionObserver?.disconnect();
164183
}
165184

166185
showTooltipTitle(e: MouseEvent) {
@@ -205,10 +224,30 @@ export class PoChartNewComponent extends PoChartNewBaseComponent implements OnIn
205224
});
206225
}
207226

227+
private checkShowCEcharts() {
228+
const chartElement = this.el.nativeElement.querySelector('#chart-id');
229+
if (!chartElement || !this.hideDomEchartsDiv) return;
230+
231+
this.intersectionObserver = new IntersectionObserver(
232+
entries => {
233+
entries.forEach(entry => {
234+
if (entry.isIntersecting) {
235+
this.initECharts();
236+
this.intersectionObserver.disconnect();
237+
}
238+
});
239+
},
240+
{ threshold: 0.1 }
241+
);
242+
243+
this.intersectionObserver.observe(chartElement);
244+
}
245+
208246
private initECharts() {
209247
this.cdr.detectChanges();
210248
const echartsDiv = this.el.nativeElement.querySelector('#chart-id');
211-
if (!echartsDiv) {
249+
if (!echartsDiv?.clientWidth) {
250+
this.hideDomEchartsDiv = true;
212251
return;
213252
}
214253

@@ -276,15 +315,15 @@ export class PoChartNewComponent extends PoChartNewBaseComponent implements OnIn
276315
}
277316

278317
private setChartsProperties() {
279-
let option: EChartsOption = {};
318+
let option = {};
280319
option = this.setOptions();
281320
this.chartInstance.setOption(option);
282321
}
283322

284323
private setOptions() {
285324
const newSeries = this.setSeries();
286325

287-
const options: EChartsOption = {
326+
const options = {
288327
backgroundColor: this.getCSSVariable('--background-color-grid', '.po-chart'),
289328
series: newSeries as any
290329
};
@@ -305,7 +344,7 @@ export class PoChartNewComponent extends PoChartNewBaseComponent implements OnIn
305344
return options;
306345
}
307346

308-
private formatLabelOption(options: EChartsOption) {
347+
private formatLabelOption(options) {
309348
if (this.options?.axis && Object.keys(this.options.axis).length) {
310349
options.yAxis['splitNumber'] = this.options.axis.gridLines || 5;
311350
options.yAxis['min'] = this.options.axis.minRange;
@@ -319,7 +358,7 @@ export class PoChartNewComponent extends PoChartNewBaseComponent implements OnIn
319358
}
320359
}
321360

322-
private setOptionLegend(options: EChartsOption) {
361+
private setOptionLegend(options) {
323362
options.legend = {
324363
show: true,
325364
orient: 'horizontal',

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export * from './interfaces/po-chart-serie-data-label.interface';
99
export * from './po-chart.component';
1010

1111
export * from './po-chart.module';
12+
13+
export * from './po-chart-switch.component'; /* @ToDo - Deletar esta referência quando não for mais necessário.*/
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* istanbul ignore file */
2+
import {
3+
ChangeDetectorRef,
4+
Component,
5+
ComponentRef,
6+
EventEmitter,
7+
Input,
8+
OnChanges,
9+
OnInit,
10+
Output,
11+
SimpleChanges,
12+
ViewChild,
13+
ViewContainerRef
14+
} from '@angular/core';
15+
16+
import { PoChartComponent } from '.';
17+
import { PoChartNewComponent } from '../po-chart-new/po-chart-new.component';
18+
19+
@Component({
20+
selector: 'po-chart',
21+
template: `<ng-template #container></ng-template>`,
22+
standalone: false
23+
})
24+
export class PoChartSwitchComponent implements OnInit, OnChanges {
25+
@ViewChild('container', { read: ViewContainerRef, static: true }) container!: ViewContainerRef;
26+
27+
@Input('p-title') title: any;
28+
@Input('p-options') options: any;
29+
@Input('p-type') type: any;
30+
@Input('p-series') series: any;
31+
@Input('p-categories') categories: any;
32+
@Input('p-height') height: any;
33+
@Input('p-data-label') dataLabel: any;
34+
@Output('p-series-hover') seriesHover = new EventEmitter<any>();
35+
@Output('p-series-click') seriesClick = new EventEmitter<any>();
36+
37+
componentRef: ComponentRef<any>;
38+
39+
constructor(private readonly changeDetector: ChangeDetectorRef) {}
40+
41+
ngOnChanges(changes: SimpleChanges): void {
42+
if (changes['series']?.firstChange) {
43+
return;
44+
}
45+
46+
this.componentRef.instance.title = this.title;
47+
this.componentRef.instance.options = this.options;
48+
this.componentRef.instance.categories = this.categories;
49+
this.componentRef.instance.series = this.series;
50+
this.componentRef.instance.type = this.type;
51+
this.componentRef.instance.height = this.height;
52+
this.componentRef.instance.dataLabel = this.dataLabel;
53+
54+
this.componentRef.instance.ngOnChanges(changes);
55+
}
56+
57+
ngOnInit() {
58+
const componentesMap = {
59+
old: PoChartComponent,
60+
new: PoChartNewComponent
61+
};
62+
63+
const newChart = localStorage.getItem('newChart') || null;
64+
const tipo = newChart ? 'new' : 'old';
65+
const componente = componentesMap[tipo];
66+
this.componentRef = this.container.createComponent(componente as any);
67+
68+
this.componentRef.instance.title = this.title;
69+
this.componentRef.instance.options = this.options;
70+
this.componentRef.instance.categories = this.categories;
71+
this.componentRef.instance.series = this.series;
72+
this.componentRef.instance.type = this.type;
73+
this.componentRef.instance.height = this.height;
74+
this.componentRef.instance.dataLabel = this.dataLabel;
75+
76+
this.componentRef.instance.seriesHover.subscribe((event: any) => {
77+
this.seriesHover.emit(event);
78+
});
79+
80+
this.componentRef.instance.seriesClick.subscribe((event: any) => {
81+
this.seriesClick.emit(event);
82+
});
83+
84+
const change = {
85+
title: {
86+
currentValue: this.title,
87+
firstChange: true
88+
},
89+
options: {
90+
currentValue: this.options,
91+
firstChange: true
92+
},
93+
categories: {
94+
currentValue: this.categories,
95+
firstChange: true
96+
},
97+
series: {
98+
currentValue: this.series,
99+
firstChange: true
100+
},
101+
type: {
102+
currentValue: this.type,
103+
firstChange: true
104+
},
105+
height: {
106+
currentValue: this.height,
107+
firstChange: true
108+
},
109+
dataLabel: {
110+
currentValue: this.dataLabel,
111+
firstChange: true
112+
}
113+
};
114+
this.componentRef.instance.ngOnChanges(change);
115+
}
116+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import { PoChartMathsService } from './services/po-chart-maths.service';
5252
* </example>
5353
*/
5454
@Component({
55-
selector: 'po-chart',
55+
selector: 'po-chart-old',
5656
templateUrl: './po-chart.component.html',
5757
standalone: false
5858
})

0 commit comments

Comments
 (0)