Skip to content

Commit 65bce4f

Browse files
Abdullah KhanAbdullah Khan
authored andcommitted
feat(explore-suspect-attrs): Hiding long x-axis labels
1 parent e1ebed3 commit 65bce4f

File tree

1 file changed

+46
-10
lines changed
  • static/app/views/explore/components/suspectTags

1 file changed

+46
-10
lines changed

static/app/views/explore/components/suspectTags/charts.tsx

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
import {useLayoutEffect, useRef, useState} from 'react';
12
import type {Theme} from '@emotion/react';
23
import {useTheme} from '@emotion/react';
34
import styled from '@emotion/styled';
45

56
import BaseChart from 'sentry/components/charts/baseChart';
67
import {space} from 'sentry/styles/space';
8+
import type {ReactEchartsRef} from 'sentry/types/echarts';
79
import type {SuspectAttributesResult} from 'sentry/views/explore/hooks/useSuspectAttributes';
810

11+
const MAX_BAR_WIDTH = 20;
12+
13+
const SELECTED_SERIES_NAME = 'selected';
14+
const BASELINE_SERIES_NAME = 'baseline';
15+
916
type Props = {
1017
rankedAttributes: SuspectAttributesResult['rankedAttributes'];
1118
};
@@ -29,13 +36,36 @@ function Chart({
2936
attribute: SuspectAttributesResult['rankedAttributes'][number];
3037
theme: Theme;
3138
}) {
39+
const chartRef = useRef<ReactEchartsRef>(null);
40+
const [hideLabels, setHideLabels] = useState(false);
41+
3242
const cohort1Color = theme.chart.getColorPalette(0)?.[0];
3343
const cohort2Color = '#dddddd';
3444

45+
useLayoutEffect(() => {
46+
const chartContainer = chartRef.current?.getEchartsInstance().getDom();
47+
if (!chartContainer) return;
48+
49+
const labels = chartContainer.querySelectorAll('.echarts-for-react text');
50+
51+
for (const label of labels) {
52+
const labelRect = (label as SVGGraphicsElement).getBoundingClientRect();
53+
const containerRect = chartContainer.getBoundingClientRect();
54+
55+
// If there are any labels exceeding the boundaries of the chart container, we hide
56+
// hide all labels.
57+
if (labelRect.left < containerRect.left || labelRect.right > containerRect.right) {
58+
setHideLabels(true);
59+
break;
60+
}
61+
}
62+
}, [attribute]);
63+
3564
return (
3665
<ChartWrapper>
3766
<ChartTitle>{attribute.attributeName}</ChartTitle>
3867
<BaseChart
68+
ref={chartRef}
3969
autoHeightResize
4070
isGroupedByDate={false}
4171
tooltip={{
@@ -52,14 +82,16 @@ function Chart({
5282
type: 'category',
5383
data: attribute.cohort1.map(cohort => cohort.label),
5484
truncate: 14,
55-
axisLabel: {
56-
hideOverlap: true,
57-
showMaxLabel: false,
58-
showMinLabel: false,
59-
color: '#000',
60-
interval: 0,
61-
formatter: (value: string) => value,
62-
},
85+
axisLabel: hideLabels
86+
? {show: false}
87+
: {
88+
hideOverlap: true,
89+
showMaxLabel: false,
90+
showMinLabel: false,
91+
color: '#000',
92+
interval: 0,
93+
formatter: (value: string) => value,
94+
},
6395
}}
6496
yAxis={{
6597
type: 'value',
@@ -72,18 +104,22 @@ function Chart({
72104
{
73105
type: 'bar',
74106
data: attribute.cohort1.map(cohort => cohort.value),
75-
name: 'Selected',
107+
name: SELECTED_SERIES_NAME,
76108
itemStyle: {
77109
color: cohort1Color,
78110
},
111+
barMaxWidth: MAX_BAR_WIDTH,
112+
animation: false,
79113
},
80114
{
81115
type: 'bar',
82116
data: attribute.cohort2.map(cohort => cohort.value),
83-
name: 'Baseline',
117+
name: BASELINE_SERIES_NAME,
84118
itemStyle: {
85119
color: cohort2Color,
86120
},
121+
barMaxWidth: MAX_BAR_WIDTH,
122+
animation: false,
87123
},
88124
]}
89125
/>

0 commit comments

Comments
 (0)