@@ -20,8 +20,9 @@ export type StackedBarChartData = Array<{
20
20
[style.width.%]="asPercent(item.value)"
21
21
[style.background-color]="item.color"
22
22
(click)="toggleDisplayMode(item)"
23
+ [attr.data-tooltip]="getTooltipText(item)"
23
24
>
24
- {{ getItemDisplayValue(item) }}
25
+ {{ getItemDisplayValue(item, displayPercentage() ) }}
25
26
</div>
26
27
}
27
28
}
@@ -58,8 +59,6 @@ export type StackedBarChartData = Array<{
58
59
.stacked-bar {
59
60
display: flex;
60
61
height: 45px;
61
- border-radius: 8px;
62
- overflow: hidden;
63
62
margin-bottom: 1rem;
64
63
}
65
64
@@ -88,6 +87,16 @@ export type StackedBarChartData = Array<{
88
87
min-width: 50px;
89
88
}
90
89
90
+ .segment:first-child {
91
+ border-top-left-radius: 8px;
92
+ border-bottom-left-radius: 8px;
93
+ }
94
+
95
+ .segment:last-child {
96
+ border-top-right-radius: 8px;
97
+ border-bottom-right-radius: 8px;
98
+ }
99
+
91
100
.segment:hover {
92
101
filter: brightness(1.1);
93
102
}
@@ -111,6 +120,39 @@ export type StackedBarChartData = Array<{
111
120
border-radius: 4px;
112
121
margin-right: 8px;
113
122
}
123
+
124
+ .segment::before {
125
+ content: attr(data-tooltip); /* Use a data attribute for the text */
126
+ position: absolute;
127
+ bottom: 110%; /* Position it above the segment */
128
+ left: 50%;
129
+ transform: translateX(-50%);
130
+ background-color: var(--tooltip-background-color);
131
+ color: var(--tooltip-text-color);
132
+ padding: 6px 12px;
133
+ border-radius: 6px;
134
+ font-size: 13px;
135
+ white-space: nowrap;
136
+ opacity: 0;
137
+ visibility: hidden;
138
+ transition:
139
+ opacity 0.2s ease-in-out,
140
+ visibility 0.2s ease-in-out;
141
+ z-index: 10;
142
+ }
143
+
144
+ .segment:last-child:not(:only-child)::before {
145
+ right: 0;
146
+ left: auto;
147
+ transform: none;
148
+ }
149
+
150
+ /* Show tooltip on hover */
151
+ .segment:hover::before,
152
+ .segment:hover::after {
153
+ opacity: 1;
154
+ visibility: visible;
155
+ }
114
156
` ,
115
157
} )
116
158
export class StackedBarChart {
@@ -121,7 +163,7 @@ export class StackedBarChart {
121
163
total = computed ( ( ) =>
122
164
this . data ( ) . reduce ( ( acc , item ) => acc + item . value , 0 )
123
165
) ;
124
- private displayPercentage = signal ( false ) ;
166
+ protected displayPercentage = signal ( false ) ;
125
167
126
168
asPercent ( value : number ) {
127
169
if ( this . total ( ) === 0 ) return 0 ;
@@ -133,10 +175,19 @@ export class StackedBarChart {
133
175
this . displayPercentage . update ( ( current ) => ! current ) ;
134
176
}
135
177
136
- getItemDisplayValue ( item : StackedBarChartData [ 0 ] ) : string {
178
+ getItemDisplayValue (
179
+ item : StackedBarChartData [ 0 ] ,
180
+ showPercent : boolean
181
+ ) : string {
137
182
if ( item . value === 0 ) return '' ;
138
- return this . displayPercentage ( )
139
- ? `${ this . asPercent ( item . value ) } %`
140
- : `${ item . value } ` ;
183
+ return showPercent ? `${ this . asPercent ( item . value ) } %` : `${ item . value } ` ;
184
+ }
185
+
186
+ getTooltipText ( item : StackedBarChartData [ 0 ] ) {
187
+ if ( ! this . showLegend ( ) ) {
188
+ return item . label ;
189
+ }
190
+
191
+ return this . getItemDisplayValue ( item , ! this . displayPercentage ( ) ) ;
141
192
}
142
193
}
0 commit comments