Skip to content

Commit 5f6cd68

Browse files
fabiana-monteiroarthur-polidorio
authored andcommitted
feat(chart): implementa compatibilidade com variáveis css
Na interface PoChartSerie foi implementada a compatibilidade com variáveis css, possibilitando o desenvolvedor final tratar as cores dinamicamente. FIXES DTHFUI-11683
1 parent 2658b6f commit 5f6cd68

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ export interface PoChartSerie {
1212
* @description
1313
*
1414
* Determina a cor da série. As maneiras de customizar o *preset* padrão de cores são:
15-
* * Hexadeximal, por exemplo `#c64840`;
16-
* * RGB, como `rgb(0, 0, 165)`
15+
* * Hexadecimal, por exemplo `#c64840`;
16+
* * RGB, por exemplo `rgb(0, 0, 165)`
1717
* * O nome da cor, por exemplo `blue`;
18+
* * Variáveis CSS, por exemplo `var(--color-01)`;
1819
* * Usando uma das cores do tema do PO:
1920
* Valores válidos:
2021
* - <span class="dot po-color-01"></span> `color-01`

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ describe('PoChartGridUtils', () => {
5959

6060
expect(serie.areaStyle.opacity).toBe(0.5);
6161
});
62+
63+
it('should set areaStyle.color using computedStyle when serie.color is var(...)', () => {
64+
const spyGetComputed = spyOn(globalThis, 'getComputedStyle').and.returnValue({
65+
getPropertyValue: () => '#445566'
66+
} as unknown as CSSStyleDeclaration);
67+
68+
const serie: any = {
69+
isTypeArea: true,
70+
color: 'var(--color-test)',
71+
overlayColor: 'rgba(255,255,255,0.5)'
72+
};
73+
74+
utils.setSerieTypeArea(serie, 1);
75+
76+
expect(spyGetComputed).toHaveBeenCalledWith(document.documentElement);
77+
expect(serie.areaStyle.color).toBe('#445566');
78+
});
6279
});
6380

6481
describe('setSerieTypeBarColumn', () => {

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,20 @@ export class PoChartGridUtils {
140140

141141
setSerieTypeArea(serie: any, index: number) {
142142
if (serie.isTypeArea) {
143+
let colorVariable: string;
144+
145+
if (serie.color?.startsWith('var(--')) {
146+
colorVariable = getComputedStyle(document.documentElement)
147+
.getPropertyValue(serie.color.replace(/^var\((--[^)]+)\)$/, '$1'))
148+
.trim();
149+
} else if (serie.color?.includes('color')) {
150+
colorVariable = this.component.getCSSVariable(`--${serie.color.replace('po-', '')}`);
151+
} else {
152+
colorVariable = serie.color ?? serie.overlayColor;
153+
}
154+
143155
serie.areaStyle = {
144-
color: serie.color?.includes('color')
145-
? this.component.getCSSVariable(`--${serie.color.replace('po-', '')}`)
146-
: serie.overlayColor
156+
color: colorVariable
147157
};
148158

149159
if (index > 7 || serie.isNotTokenColor) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,24 @@ describe('PoChartComponent', () => {
13501350
expect(result[0].name).toBe('Serie 1');
13511351
});
13521352

1353+
it('should call getComputedStyle with document.documentElement and use its value for var(...) color', () => {
1354+
const spyGetComputed = spyOn(window, 'getComputedStyle').and.returnValue({
1355+
getPropertyValue: (prop: string) => '#112233'
1356+
} as unknown as CSSStyleDeclaration);
1357+
1358+
component.series = [{ label: 'Serie Var2', data: [1, 2], color: 'var(--color-test)' }];
1359+
component.type = PoChartType.Column;
1360+
component.options = {};
1361+
1362+
const result = component['setSeries']();
1363+
1364+
expect(spyGetComputed).toHaveBeenCalledWith(document.documentElement);
1365+
1366+
const resolvedColor = result[0]?.emphasis?.itemStyle?.color || result[0]?.itemStyle?.color;
1367+
expect(resolvedColor).toBe('#112233');
1368+
expect(result[0]?.name).toBe('Serie Var2');
1369+
});
1370+
13531371
it('should transform series correctly with default configurations if not set type and data is not Array', () => {
13541372
component.series = [
13551373
{ label: 'Serie 1', data: 90, color: 'po-color-01' },

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,18 @@ export class PoChartComponent extends PoChartBaseComponent implements OnInit, Af
597597
serie.name = serie.name || (serie.label && typeof serie.label === 'string') ? (serie.name ?? serie.label) : ' ';
598598
!serie.type ? this.setTypeSerie(serie, this.type || typeDefault) : this.setTypeSerie(serie, serie.type);
599599

600-
const colorVariable: string = serie.color?.includes('color')
601-
? this.getCSSVariable(`--${serie.color.replace('po-', '')}`)
602-
: serie.color;
600+
let colorVariable: string;
601+
602+
if (serie.color?.startsWith('var(--')) {
603+
colorVariable = getComputedStyle(document.documentElement)
604+
.getPropertyValue(serie.color.replace(/^var\((--[^)]+)\)$/, '$1'))
605+
.trim();
606+
} else if (serie.color?.includes('color')) {
607+
colorVariable = this.getCSSVariable(`--${serie.color.replace('po-', '')}`);
608+
} else {
609+
colorVariable = serie.color;
610+
}
611+
603612
this.chartGridUtils.setSerieTypeDonutPie(serie, colorVariable);
604613
this.chartGaugeUtils.setSerieTypeGauge(serie, colorVariable);
605614
this.setSerieEmphasis(serie, colorVariable, tokenBorderWidthMd);

0 commit comments

Comments
 (0)