Skip to content

Commit b0dd0d5

Browse files
authored
Merge pull request #1297 from RedisInsight/fe/feature/RI-3659_hide-labels
#RI-3659 - hide chart labels for medium screens
2 parents ff42f88 + 0ced4e5 commit b0dd0d5

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ describe('DonutChart', () => {
7272
expect(screen.getByTestId('label-E-30')).toHaveTextContent('E: 30')
7373
})
7474

75+
it('should render label value without title', () => {
76+
render(<DonutChart data={mockData} config={{ percentToShowLabel: 5 }} hideLabelTitle />)
77+
expect(screen.getByTestId('label-E-30').textContent).toBe('30')
78+
})
79+
7580
it('should call render tooltip and label methods', () => {
7681
const renderLabel = jest.fn()
7782
const renderTooltip = jest.fn()

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface IProps {
4040
renderLabel?: (data: ChartData) => string
4141
renderTooltip?: (data: ChartData) => React.ReactElement | string
4242
labelAs?: 'value' | 'percentage'
43+
hideLabelTitle?: boolean
4344
}
4445

4546
const ANIMATION_DURATION_MS = 100
@@ -56,6 +57,7 @@ const DonutChart = (props: IProps) => {
5657
labelAs = 'value',
5758
renderLabel,
5859
renderTooltip,
60+
hideLabelTitle = false,
5961
} = props
6062

6163
const margin = config?.margin || 98
@@ -121,6 +123,15 @@ const DonutChart = (props: IProps) => {
121123
return `translate(${(x / h) * (radius + 12)}, ${((y + 4) / h) * (radius + 12)})`
122124
}
123125

126+
useEffect(() => {
127+
d3
128+
.select(svgRef.current)
129+
.attr('width', width)
130+
.attr('height', height)
131+
.select('g')
132+
.attr('transform', `translate(${width / 2},${height / 2})`)
133+
}, [height, width])
134+
124135
useEffect(() => {
125136
const pie = d3.pie<ChartData>().value((d: ChartData) => d.value).sort(null)
126137
const dataReady = pie(data.filter((d) => d.value !== 0))
@@ -134,7 +145,7 @@ const DonutChart = (props: IProps) => {
134145
.select(svgRef.current)
135146
.attr('width', width)
136147
.attr('height', height)
137-
.attr('data-testid', `donut-${name}`)
148+
.attr('data-testid', `donut-svg-${name}`)
138149
.attr('class', cx(classNames?.chart))
139150
.append('g')
140151
.attr('transform', `translate(${width / 2},${height / 2})`)
@@ -160,7 +171,7 @@ const DonutChart = (props: IProps) => {
160171
.append('text')
161172
.attr('class', cx(styles.chartLabel, classNames?.arcLabel))
162173
.attr('transform', getLabelPosition)
163-
.text((d) => (isShowLabel(d) ? d.data.name : ''))
174+
.text((d) => (isShowLabel(d) && !hideLabelTitle ? `${d.data.name}: ` : ''))
164175
.attr('data-testid', (d) => `label-${d.data.name}-${d.data.value}`)
165176
.style('text-anchor', (d) => ((d.endAngle + d.startAngle) / 2 > Math.PI ? 'end' : 'start'))
166177
.on('mouseenter mousemove', onMouseEnterSlice)
@@ -175,22 +186,21 @@ const DonutChart = (props: IProps) => {
175186
return renderLabel(d.data)
176187
}
177188

178-
const separator = ': '
179189
if (labelAs === 'percentage') {
180-
return `${separator}${getPercentage(d.value, sum)}%`
190+
return `${getPercentage(d.value, sum)}%`
181191
}
182192

183-
return `${separator}${truncateNumberToRange(d.value)}`
193+
return truncateNumberToRange(d.value)
184194
})
185195
.attr('class', cx(styles.chartLabelValue, classNames?.arcLabelValue))
186-
}, [data])
196+
}, [data, hideLabelTitle])
187197

188198
if (!data.length || sum === 0) {
189199
return null
190200
}
191201

192202
return (
193-
<div className={styles.wrapper}>
203+
<div className={styles.wrapper} data-testid={`donut-${name}`}>
194204
<svg ref={svgRef} />
195205
<div
196206
className={cx(styles.tooltip, classNames?.tooltip)}

redisinsight/ui/src/pages/databaseAnalysis/components/summary-per-data/SummaryPerData.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ export interface Props {
1818
loading: boolean
1919
}
2020

21+
const widthResponsiveSize = 1024
22+
const CHART_WITH_LABELS_WIDTH = 432
23+
const CHART_WIDTH = 320
24+
2125
const SummaryPerData = ({ data, loading }: Props) => {
2226
const { totalMemory, totalKeys } = data || {}
2327
const [memoryData, setMemoryData] = useState<ChartData[]>([])
2428
const [keysData, setKeysData] = useState<ChartData[]>([])
29+
const [hideLabelTitle, setHideLabelTitle] = useState(false)
2530

2631
const getChartData = (t: SimpleTypeSummary) => ({
2732
value: t.total,
@@ -30,6 +35,18 @@ const SummaryPerData = ({ data, loading }: Props) => {
3035
meta: { ...t }
3136
})
3237

38+
const updateChartSize = () => {
39+
setHideLabelTitle(globalThis.innerWidth < widthResponsiveSize)
40+
}
41+
42+
useEffect(() => {
43+
updateChartSize()
44+
globalThis.addEventListener('resize', updateChartSize)
45+
return () => {
46+
globalThis.removeEventListener('resize', updateChartSize)
47+
}
48+
}, [])
49+
3350
useEffect(() => {
3451
if (data && totalMemory && totalKeys) {
3552
setMemoryData(totalMemory.types?.map(getChartData) as ChartData[])
@@ -45,6 +62,7 @@ const SummaryPerData = ({ data, loading }: Props) => {
4562
</div>
4663
)
4764
}
65+
4866
if ((!totalMemory || memoryData.length === 0) && (!totalKeys || keysData.length === 0)) {
4967
return null
5068
}
@@ -83,7 +101,8 @@ const SummaryPerData = ({ data, loading }: Props) => {
83101
name="memory"
84102
data={memoryData}
85103
labelAs="percentage"
86-
width={432}
104+
hideLabelTitle={hideLabelTitle}
105+
width={hideLabelTitle ? CHART_WIDTH : CHART_WITH_LABELS_WIDTH}
87106
config={{ radius: 94 }}
88107
renderTooltip={renderMemoryTooltip}
89108
title={(
@@ -103,7 +122,8 @@ const SummaryPerData = ({ data, loading }: Props) => {
103122
name="keys"
104123
data={keysData}
105124
labelAs="percentage"
106-
width={432}
125+
hideLabelTitle={hideLabelTitle}
126+
width={hideLabelTitle ? CHART_WIDTH : CHART_WITH_LABELS_WIDTH}
107127
config={{ radius: 94 }}
108128
renderTooltip={renderKeysTooltip}
109129
title={(

0 commit comments

Comments
 (0)