1
+ import { useLayoutEffect , useRef , useState } from 'react' ;
1
2
import type { Theme } from '@emotion/react' ;
2
3
import { useTheme } from '@emotion/react' ;
3
4
import styled from '@emotion/styled' ;
4
5
5
6
import BaseChart from 'sentry/components/charts/baseChart' ;
6
7
import { space } from 'sentry/styles/space' ;
8
+ import type { ReactEchartsRef } from 'sentry/types/echarts' ;
7
9
import type { SuspectAttributesResult } from 'sentry/views/explore/hooks/useSuspectAttributes' ;
8
10
11
+ const MAX_BAR_WIDTH = 20 ;
12
+
13
+ const SELECTED_SERIES_NAME = 'selected' ;
14
+ const BASELINE_SERIES_NAME = 'baseline' ;
15
+
9
16
type Props = {
10
17
rankedAttributes : SuspectAttributesResult [ 'rankedAttributes' ] ;
11
18
} ;
@@ -29,13 +36,36 @@ function Chart({
29
36
attribute : SuspectAttributesResult [ 'rankedAttributes' ] [ number ] ;
30
37
theme : Theme ;
31
38
} ) {
39
+ const chartRef = useRef < ReactEchartsRef > ( null ) ;
40
+ const [ hideLabels , setHideLabels ] = useState ( false ) ;
41
+
32
42
const cohort1Color = theme . chart . getColorPalette ( 0 ) ?. [ 0 ] ;
33
43
const cohort2Color = '#dddddd' ;
34
44
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
+
35
64
return (
36
65
< ChartWrapper >
37
66
< ChartTitle > { attribute . attributeName } </ ChartTitle >
38
67
< BaseChart
68
+ ref = { chartRef }
39
69
autoHeightResize
40
70
isGroupedByDate = { false }
41
71
tooltip = { {
@@ -52,14 +82,16 @@ function Chart({
52
82
type : 'category' ,
53
83
data : attribute . cohort1 . map ( cohort => cohort . label ) ,
54
84
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
+ } ,
63
95
} }
64
96
yAxis = { {
65
97
type : 'value' ,
@@ -72,18 +104,22 @@ function Chart({
72
104
{
73
105
type : 'bar' ,
74
106
data : attribute . cohort1 . map ( cohort => cohort . value ) ,
75
- name : 'Selected' ,
107
+ name : SELECTED_SERIES_NAME ,
76
108
itemStyle : {
77
109
color : cohort1Color ,
78
110
} ,
111
+ barMaxWidth : MAX_BAR_WIDTH ,
112
+ animation : false ,
79
113
} ,
80
114
{
81
115
type : 'bar' ,
82
116
data : attribute . cohort2 . map ( cohort => cohort . value ) ,
83
- name : 'Baseline' ,
117
+ name : BASELINE_SERIES_NAME ,
84
118
itemStyle : {
85
119
color : cohort2Color ,
86
120
} ,
121
+ barMaxWidth : MAX_BAR_WIDTH ,
122
+ animation : false ,
87
123
} ,
88
124
] }
89
125
/>
0 commit comments