Skip to content

Commit c8bffe3

Browse files
feat: adding font size property to legend component and style fix (#1521)
1 parent 22e03c7 commit c8bffe3

File tree

8 files changed

+66
-11
lines changed

8 files changed

+66
-11
lines changed

projects/observability/src/shared/components/donut/donut-builder.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { BaseType, Selection } from 'd3-selection';
1010
import { arc, pie, PieArcDatum } from 'd3-shape';
1111
import { isEmpty } from 'lodash-es';
12-
import { LegendPosition, LegendSeries } from '../legend/legend.component';
12+
import { LegendFontSize, LegendPosition, LegendSeries } from '../legend/legend.component';
1313
import { ChartTooltipBuilderService } from '../utils/chart-tooltip/chart-tooltip-builder.service';
1414
import { DefaultChartTooltipRenderData } from '../utils/chart-tooltip/default/default-chart-tooltip.component';
1515
import { D3UtilService } from '../utils/d3/d3-util.service';
@@ -166,7 +166,8 @@ export class DonutBuilderService extends D3VisualizationBuilderService<
166166
center: provided.center,
167167
legend: provided.legendPosition === undefined ? LegendPosition.None : provided.legendPosition,
168168
tooltipOption: provided.tooltipOption === undefined ? { title: '' } : provided.tooltipOption,
169-
displayLegendCounts: provided.displayLegendCounts ?? true
169+
displayLegendCounts: provided.displayLegendCounts ?? true,
170+
legendFontSize: provided.legendFontSize ?? LegendFontSize.ExtraSmall
170171
};
171172
}
172173

projects/observability/src/shared/components/donut/donut.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
OnDestroy,
1010
ViewChild
1111
} from '@angular/core';
12-
import { LegendPosition } from '../legend/legend.component';
12+
import { LegendFontSize, LegendPosition } from '../legend/legend.component';
1313
import { TooltipOption } from '../utils/d3/d3-visualization-builder.service';
1414
import { Donut, DonutAlignmentStyle, DonutCenter, DonutSeries } from './donut';
1515
import { DonutBuilderService } from './donut-builder.service';
@@ -37,6 +37,9 @@ export class DonutComponent implements OnChanges, OnDestroy, AfterViewInit {
3737
@Input()
3838
public legendPosition?: LegendPosition;
3939

40+
@Input()
41+
public legendFontSize?: LegendFontSize;
42+
4043
@Input()
4144
public tooltipOption?: TooltipOption;
4245

@@ -70,7 +73,8 @@ export class DonutComponent implements OnChanges, OnDestroy, AfterViewInit {
7073
center: this.center,
7174
legendPosition: this.legendPosition,
7275
tooltipOption: this.tooltipOption,
73-
displayLegendCounts: this.displayLegendCounts
76+
displayLegendCounts: this.displayLegendCounts,
77+
legendFontSize: this.legendFontSize
7478
});
7579
}
7680

projects/observability/src/shared/components/donut/donut.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LegendPosition } from '../legend/legend.component';
1+
import { LegendFontSize, LegendPosition } from '../legend/legend.component';
22
import { TooltipOption } from '../utils/d3/d3-visualization-builder.service';
33

44
export interface Donut {
@@ -12,6 +12,7 @@ export interface DonutConfiguration {
1212
legendPosition?: LegendPosition;
1313
tooltipOption?: TooltipOption;
1414
displayLegendCounts?: boolean;
15+
legendFontSize?: LegendFontSize;
1516
}
1617

1718
export interface DonutSeriesResults {

projects/observability/src/shared/components/legend/legend.component.scss

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,33 @@
4646
}
4747

4848
.legend-label {
49-
@include chart-small-regular($gray-4);
5049
@include ellipsis-overflow();
5150
flex: 1;
5251
min-width: 24px;
5352
}
5453

5554
.legend-value {
56-
@include chart-small-regular($gray-9);
5755
padding-left: 16px;
5856
}
57+
58+
&.font-size-extra-small {
59+
.legend-label {
60+
@include chart-small-regular($gray-4);
61+
}
62+
63+
.legend-value {
64+
@include chart-small-regular($gray-9);
65+
}
66+
}
67+
68+
&.font-size-small {
69+
.legend-label {
70+
@include body-small($gray-4);
71+
}
72+
73+
.legend-value {
74+
@include body-small($gray-9);
75+
}
76+
}
5977
}
6078
}

projects/observability/src/shared/components/legend/legend.component.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createComponentFactory } from '@ngneat/spectator/jest';
2-
import { LegendComponent, LegendLayout, LegendPosition } from './legend.component';
2+
import { LegendComponent, LegendFontSize, LegendLayout, LegendPosition } from './legend.component';
33

44
describe('Legend component', () => {
55
const componentFactory = createComponentFactory({
@@ -8,6 +8,7 @@ describe('Legend component', () => {
88
providers: LegendComponent.buildProviders({
99
layout: LegendLayout.Column,
1010
position: LegendPosition.Right,
11+
fontSize: LegendFontSize.ExtraSmall,
1112
series: []
1213
})
1314
});
@@ -17,6 +18,7 @@ describe('Legend component', () => {
1718
providers: LegendComponent.buildProviders({
1819
layout: LegendLayout.Column,
1920
position: LegendPosition.Right,
21+
fontSize: LegendFontSize.Small,
2022
series: [
2123
{
2224
name: 'alpha',
@@ -39,13 +41,15 @@ describe('Legend component', () => {
3941
expect(entries[1].querySelector<HTMLElement>('.legend-symbol')!.style.backgroundColor).toBe('red');
4042

4143
expect(spectator.query('.legend-entries')).toHaveClass('legend-column');
44+
expect(spectator.query('.legend-entry')).toHaveClass('font-size-small');
4245
});
4346

4447
test('should render in requested orientation', () => {
4548
const spectator = componentFactory({
4649
providers: LegendComponent.buildProviders({
4750
layout: LegendLayout.Row,
4851
position: LegendPosition.Right,
52+
fontSize: LegendFontSize.ExtraSmall,
4953
series: [
5054
{
5155
name: 'alpha',

projects/observability/src/shared/components/legend/legend.component.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ export const LEGEND_DATA = new InjectionToken<LegendData<unknown>>('LEGEND DATA'
88
styleUrls: ['./legend.component.scss'],
99
template: `
1010
<div class="legend-entries fill-container" [ngClass]="this.layoutClass">
11-
<div *ngFor="let entry of this.entries" class="legend-entry" [htTooltip]="entry.name">
11+
<div
12+
*ngFor="let entry of this.entries"
13+
class="legend-entry"
14+
[ngClass]="this.fontSizeClass"
15+
[htTooltip]="entry.name"
16+
>
1217
<span class="legend-symbol" [style.backgroundColor]="entry.color"></span>
1318
<span class="legend-label">{{ entry.name }}</span>
1419
<span *ngIf="entry.data.value !== undefined" class="legend-value">{{ entry.data.value }}</span>
@@ -31,6 +36,10 @@ export class LegendComponent {
3136
return this.legendData.series;
3237
}
3338

39+
public get fontSizeClass(): string {
40+
return `font-size-${this.legendData.fontSize}`;
41+
}
42+
3443
public get layoutClass(): string {
3544
switch (this.legendData.layout) {
3645
case LegendLayout.Row:
@@ -53,6 +62,7 @@ export interface LegendSeries<T> {
5362
export interface LegendData<T> {
5463
position: LegendPosition;
5564
layout: LegendLayout;
65+
fontSize: LegendFontSize;
5666
series: LegendSeries<T>[];
5767
}
5868

@@ -61,6 +71,11 @@ export const enum LegendLayout {
6171
Column = 'column'
6272
}
6373

74+
export const enum LegendFontSize {
75+
ExtraSmall = 'extra-small',
76+
Small = 'small'
77+
}
78+
6479
export const enum LegendPosition {
6580
// Kabob case because these are used for css classes
6681
Bottom = 'bottom',

projects/observability/src/shared/components/utils/d3/d3-visualization-builder.service.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { ElementRef, Injector, Renderer2, TemplateRef, Type } from '@angular/cor
22
import { assertUnreachable, DomElementMeasurerService, DynamicComponentService, selector } from '@hypertrace/common';
33
import { ContainerElement, mouse, Selection } from 'd3-selection';
44
import { isEqual } from 'lodash-es';
5-
import { LegendComponent, LegendLayout, LegendPosition, LegendSeries } from '../../legend/legend.component';
5+
import {
6+
LegendComponent,
7+
LegendFontSize,
8+
LegendLayout,
9+
LegendPosition,
10+
LegendSeries
11+
} from '../../legend/legend.component';
612
import { ChartTooltipBuilderService, ChartTooltipDataMapper } from '../chart-tooltip/chart-tooltip-builder.service';
713
import { ChartTooltipRef } from '../chart-tooltip/chart-tooltip-popover';
814
import { MouseDataLookupStrategy } from '../mouse-tracking/mouse-tracking';
@@ -137,6 +143,7 @@ export abstract class D3VisualizationBuilderService<
137143
LegendComponent.buildProviders({
138144
position: config.legend,
139145
layout: this.getLegendLayout(config),
146+
fontSize: this.getLegendFontSize(config),
140147
series: this.getLegendEntries(config)
141148
})
142149
);
@@ -327,6 +334,10 @@ export abstract class D3VisualizationBuilderService<
327334
}
328335
}
329336

337+
protected getLegendFontSize(configuration: TInternalConfig): LegendFontSize {
338+
return configuration.legendFontSize ?? LegendFontSize.ExtraSmall;
339+
}
340+
330341
protected getMaxLegendWidth(): number {
331342
return 200;
332343
}
@@ -371,6 +382,7 @@ export interface Chart {
371382

372383
export interface ChartConfig {
373384
legend: LegendPosition;
385+
legendFontSize?: LegendFontSize;
374386
tooltipOption?: TooltipOption;
375387
}
376388

projects/observability/src/shared/components/utils/d3/d3-visualization.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
flex-direction: row-reverse;
1818
.chart-legend-container {
1919
margin-left: 12px;
20-
margin-right: 12px;
20+
margin-right: 36px;
2121
}
2222
}
2323

0 commit comments

Comments
 (0)