1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import styled from '@emotion/styled';
import { scaleLinear } from 'd3-scale';
import React from 'react';
import tw from 'twin.macro';
import { themeColor } from '../helpers';
import { BasePlacement, PopupPlacement } from '../helpers/positioning';
import { Tooltip } from './Tooltip';
const SIZE_SCALE = scaleLinear().domain([3, 15]).range([11, 18]).clamp(true);
const TEXT_VISIBLE_AT_WIDTH = 40;
const TEXT_VISIBLE_AT_HEIGHT = 45;
const ICON_VISIBLE_AT_WIDTH = 24;
const ICON_VISIBLE_AT_HEIGHT = 26;
interface Props {
fill?: string;
gradient?: string;
height: number;
icon?: React.ReactNode;
itemKey: string;
label: string;
onClick: (item: string) => void;
placement?: BasePlacement;
prefix: string;
tooltip?: React.ReactNode;
width: number;
x: number;
y: number;
}
export function TreeMapRect(props: Props) {
const {
placement,
tooltip,
onClick,
itemKey,
x,
y,
width,
height,
fill,
gradient,
label,
icon,
prefix,
} = props;
const handleRectClick = React.useCallback(() => {
onClick(itemKey);
return false;
}, [onClick, itemKey]);
const cellStyles = {
left: x,
top: y,
width,
height,
backgroundColor: fill,
backgroundImage: gradient,
fontSize: SIZE_SCALE(width / label.length),
lineHeight: `${height}px`,
};
const isTextVisible = width >= TEXT_VISIBLE_AT_WIDTH && height >= TEXT_VISIBLE_AT_HEIGHT;
const isIconVisible = width >= ICON_VISIBLE_AT_WIDTH && height >= ICON_VISIBLE_AT_HEIGHT;
return (
<Tooltip overlay={tooltip} placement={placement ?? PopupPlacement.Left}>
<StyledCell style={cellStyles}>
<StyledCellLink href="#" onClick={handleRectClick}>
<StyledCellLabel width={width}>
{isIconVisible && <span className="shrink-0">{icon}</span>}
{isTextVisible &&
(prefix ? (
<span className="treemap-text">
{prefix}
<br />
{label.substring(prefix.length)}
</span>
) : (
<span className="treemap-text">{label}</span>
))}
<StyledA11yHidden>{tooltip}</StyledA11yHidden>
</StyledCellLabel>
</StyledCellLink>
</StyledCell>
</Tooltip>
);
}
const StyledCell = styled.li`
${tw`sw-absolute`};
${tw`sw-box-border`};
border-right: 1px solid #fff;
border-bottom: 1px solid #fff;
`;
const StyledCellLink = styled.a`
${tw`sw-w-full sw-h-full`};
${tw`sw-border-0`};
${tw`sw-flex sw-flex-col sw-items-center sw-justify-center`};
color: ${themeColor('pageContent')};
&:hover,
&:active,
&:focus {
${tw`sw-border-0`};
outline: none;
}
&:focus .treemap-text,
&:hover .treemap-text {
${tw`sw-underline`};
}
`;
const StyledCellLabel = styled.div<{ width: number }>`
${tw`sw-flex sw-flex-wrap sw-justify-center sw-items-center sw-gap-2`};
line-height: 1.2;
max-width: ${({ width }) => width}px;
.treemap-text {
${tw`sw-shrink sw-overflow-hidden sw-whitespace-nowrap sw-text-left sw-text-ellipsis`};
}
`;
const StyledA11yHidden = styled.span`
position: absolute !important;
left: -10000px !important;
top: auto !important;
width: 1px !important;
height: 1px !important;
overflow: hidden !important;
`;
|