@@ -20,8 +20,9 @@ export type StackedBarChartData = Array<{
2020 [style.width.%]="asPercent(item.value)"
2121 [style.background-color]="item.color"
2222 (click)="toggleDisplayMode(item)"
23+ [attr.data-tooltip]="getTooltipText(item)"
2324 >
24- {{ getItemDisplayValue(item) }}
25+ {{ getItemDisplayValue(item, displayPercentage() ) }}
2526 </div>
2627 }
2728 }
@@ -58,8 +59,6 @@ export type StackedBarChartData = Array<{
5859 .stacked-bar {
5960 display: flex;
6061 height: 45px;
61- border-radius: 8px;
62- overflow: hidden;
6362 margin-bottom: 1rem;
6463 }
6564
@@ -88,6 +87,16 @@ export type StackedBarChartData = Array<{
8887 min-width: 50px;
8988 }
9089
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+
91100 .segment:hover {
92101 filter: brightness(1.1);
93102 }
@@ -111,6 +120,39 @@ export type StackedBarChartData = Array<{
111120 border-radius: 4px;
112121 margin-right: 8px;
113122 }
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+ }
114156 ` ,
115157} )
116158export class StackedBarChart {
@@ -121,7 +163,7 @@ export class StackedBarChart {
121163 total = computed ( ( ) =>
122164 this . data ( ) . reduce ( ( acc , item ) => acc + item . value , 0 )
123165 ) ;
124- private displayPercentage = signal ( false ) ;
166+ protected displayPercentage = signal ( false ) ;
125167
126168 asPercent ( value : number ) {
127169 if ( this . total ( ) === 0 ) return 0 ;
@@ -133,10 +175,19 @@ export class StackedBarChart {
133175 this . displayPercentage . update ( ( current ) => ! current ) ;
134176 }
135177
136- getItemDisplayValue ( item : StackedBarChartData [ 0 ] ) : string {
178+ getItemDisplayValue (
179+ item : StackedBarChartData [ 0 ] ,
180+ showPercent : boolean
181+ ) : string {
137182 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 ( ) ) ;
141192 }
142193}
0 commit comments