Skip to content

Commit ae80bbe

Browse files
authored
Merge pull request #1173 from RedisInsight/fe/bugfix/RI-3550_no-data-display
#RI-3550 - fix donut chart rendering with 0 values
2 parents 09070f2 + ebc1b05 commit ae80bbe

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

redisinsight/ui/src/components/charts/donut-chart/DonutChart.spec.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ describe('DonutChart', () => {
2121
expect(render(<DonutChart data={mockData} />)).toBeTruthy()
2222
})
2323

24+
it('should not render donut with empty data', () => {
25+
const { container } = render(<DonutChart data={[]} name="test" />)
26+
expect(container).toBeEmptyDOMElement()
27+
})
28+
29+
it('should not render donut with 0 values', () => {
30+
const mockData: ChartData[] = [
31+
{ value: 0, name: 'A', color: [0, 0, 0] },
32+
{ value: 0, name: 'B', color: [10, 10, 10] },
33+
]
34+
const { container } = render(<DonutChart data={mockData} name="test" />)
35+
expect(container).toBeEmptyDOMElement()
36+
})
37+
2438
it('should render svg', () => {
2539
render(<DonutChart data={mockData} name="test" />)
2640
expect(screen.getByTestId('donut-test')).toBeInTheDocument()
@@ -34,6 +48,16 @@ describe('DonutChart', () => {
3448
})
3549
})
3650

51+
it('should not render arcs and labels with 0 value', () => {
52+
const mockData: ChartData[] = [
53+
{ value: 0, name: 'A', color: [0, 0, 0] },
54+
{ value: 10, name: 'B', color: [10, 10, 10] },
55+
]
56+
render(<DonutChart data={mockData} />)
57+
expect(screen.queryByTestId('arc-A-0')).not.toBeInTheDocument()
58+
expect(screen.queryByTestId('label-A-0')).not.toBeInTheDocument()
59+
})
60+
3761
it('should do not render label value if value less than 5%', () => {
3862
render(<DonutChart data={mockData} config={{ percentToShowLabel: 5 }} />)
3963
expect(screen.getByTestId('label-A-1')).toHaveTextContent('')

redisinsight/ui/src/components/charts/donut-chart/DonutChart.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import cx from 'classnames'
22
import * as d3 from 'd3'
3+
import { sumBy } from 'lodash'
34
import React, { useEffect, useRef } from 'react'
45
import { truncateNumberToRange } from 'uiSrc/utils'
56
import { rgb, RGBColor } from 'uiSrc/utils/colors'
@@ -104,7 +105,7 @@ const DonutChart = (props: IProps) => {
104105

105106
useEffect(() => {
106107
const pie = d3.pie<ChartData>().value((d: ChartData) => d.value).sort(null)
107-
const dataReady = pie(data)
108+
const dataReady = pie(data.filter((d) => d.value !== 0))
108109

109110
d3
110111
.select(svgRef.current)
@@ -151,6 +152,10 @@ const DonutChart = (props: IProps) => {
151152
.attr('class', cx(styles.chartLabelValue, classNames?.arcLabelValue))
152153
}, [data])
153154

155+
if (!data.length || sumBy(data, 'value') === 0) {
156+
return null
157+
}
158+
154159
return (
155160
<div className={styles.wrapper}>
156161
<svg ref={svgRef} />

0 commit comments

Comments
 (0)