Skip to content

Commit 916f7bc

Browse files
authored
fix(table chart): Show Cell Bars correctly #25625 (#25707)
1 parent 80cf710 commit 916f7bc

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

superset-frontend/plugins/plugin-chart-table/src/TableChart.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,15 @@ export default function TableChart<D extends DataRecord = DataRecord>(
409409

410410
const getColumnConfigs = useCallback(
411411
(column: DataColumnMeta, i: number): ColumnWithLooseAccessor<D> => {
412-
const { key, label, isNumeric, dataType, isMetric, config = {} } = column;
412+
const {
413+
key,
414+
label,
415+
isNumeric,
416+
dataType,
417+
isMetric,
418+
isPercentMetric,
419+
config = {},
420+
} = column;
413421
const columnWidth = Number.isNaN(Number(config.columnWidth))
414422
? config.columnWidth
415423
: Number(config.columnWidth);
@@ -438,7 +446,7 @@ export default function TableChart<D extends DataRecord = DataRecord>(
438446
(config.showCellBars === undefined
439447
? showCellBars
440448
: config.showCellBars) &&
441-
(isMetric || isRawRecords) &&
449+
(isMetric || isRawRecords || isPercentMetric) &&
442450
getValueRange(key, alignPositiveNegative);
443451

444452
let className = '';

superset-frontend/plugins/plugin-chart-table/test/TableChart.test.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe('plugin-chart-table', () => {
7676
wrap = mount(
7777
<TableChart {...transformProps(testData.basic)} sticky={false} />,
7878
);
79+
7980
tree = wrap.render(); // returns a CheerioWrapper with jQuery-like API
8081
const cells = tree.find('td');
8182
expect(cells).toHaveLength(12);
@@ -158,6 +159,7 @@ describe('plugin-chart-table', () => {
158159
}),
159160
);
160161
const cells = document.querySelectorAll('td');
162+
161163
expect(document.querySelectorAll('th')[0]).toHaveTextContent('num');
162164
expect(cells[0]).toHaveTextContent('$ 1.23k');
163165
expect(cells[1]).toHaveTextContent('$ 10k');
@@ -242,4 +244,61 @@ describe('plugin-chart-table', () => {
242244
expect(getComputedStyle(screen.getByText('N/A')).background).toBe('');
243245
});
244246
});
247+
248+
it('render cell bars properly, and only when it is toggled on in both regular and percent metrics', () => {
249+
const props = transformProps({
250+
...testData.raw,
251+
rawFormData: { ...testData.raw.rawFormData },
252+
});
253+
254+
props.columns[0].isMetric = true;
255+
256+
render(
257+
ProviderWrapper({
258+
children: <TableChart {...props} sticky={false} />,
259+
}),
260+
);
261+
let cells = document.querySelectorAll('div.cell-bar');
262+
cells.forEach(cell => {
263+
expect(cell).toHaveClass('positive');
264+
});
265+
props.columns[0].isMetric = false;
266+
props.columns[0].isPercentMetric = true;
267+
268+
render(
269+
ProviderWrapper({
270+
children: <TableChart {...props} sticky={false} />,
271+
}),
272+
);
273+
cells = document.querySelectorAll('div.cell-bar');
274+
cells.forEach(cell => {
275+
expect(cell).toHaveClass('positive');
276+
});
277+
278+
props.showCellBars = false;
279+
280+
render(
281+
ProviderWrapper({
282+
children: <TableChart {...props} sticky={false} />,
283+
}),
284+
);
285+
cells = document.querySelectorAll('td');
286+
287+
cells.forEach(cell => {
288+
expect(cell).toHaveClass('test-c7w8t3');
289+
});
290+
291+
props.columns[0].isPercentMetric = false;
292+
props.columns[0].isMetric = true;
293+
294+
render(
295+
ProviderWrapper({
296+
children: <TableChart {...props} sticky={false} />,
297+
}),
298+
);
299+
cells = document.querySelectorAll('td');
300+
cells.forEach(cell => {
301+
expect(cell).toHaveClass('test-c7w8t3');
302+
});
303+
});
245304
});

0 commit comments

Comments
 (0)