Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFD-124: Limit the size/shape that the diagram can be #78

Merged
merged 6 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/app/carbon-estimation/carbon-estimation.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ describe('CarbonEstimationComponent', () => {
spyOn(component.chart as ChartComponent, 'updateOptions');
spyOnProperty(component.detailsPanel.nativeElement, 'clientHeight').and.returnValue(200);

component.onResize(2000, 1000);
component.onResize(2000, 1000, 2000);

expect(component.chart?.updateOptions).toHaveBeenCalledOnceWith({
chart: { height: 2000 - estimatorBaseHeight - 200 },
chart: { height: 1500 }, // Height will be capped at a percentage of the screen height
});
});

it('should recalculate chart height on window resize, for mobile screen', () => {
spyOn(component.chart as ChartComponent, 'updateOptions');
spyOnProperty(component.detailsPanel.nativeElement, 'clientHeight').and.returnValue(200);

component.onResize(1000, 500);
component.onResize(1000, 500, 1000);

expect(component.chart?.updateOptions).toHaveBeenCalledOnceWith({
chart: { height: 1000 - estimatorBaseHeight - 200 + estimatorHeights.title },
Expand Down
28 changes: 18 additions & 10 deletions src/app/carbon-estimation/carbon-estimation.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ export class CarbonEstimationComponent implements OnInit, OnDestroy {
}

public ngOnInit(): void {
const chartHeight = this.getChartHeight(window.innerHeight, window.innerWidth);
const chartHeight = this.getChartHeight(window.innerHeight, window.innerWidth, window.screen.height);
if (chartHeight > 0) {
this.chartOptions.chart.height = chartHeight;
}

this.resizeSubscription = fromEvent(window, 'resize')
.pipe(debounceTime(500))
.subscribe(() => this.onResize(window.innerHeight, window.innerWidth));
.subscribe(() => this.onResize(window.innerHeight, window.innerWidth, window.screen.height));
}

public ngOnDestroy(): void {
Expand All @@ -70,11 +70,11 @@ export class CarbonEstimationComponent implements OnInit, OnDestroy {

public onExpanded(): void {
this.changeDetectorRef.detectChanges();
this.onResize(window.innerHeight, window.innerWidth);
this.onResize(window.innerHeight, window.innerWidth, window.screen.height);
}

onResize(innerHeight: number, innerWidth: number): void {
const chartHeight = this.getChartHeight(innerHeight, innerWidth);
onResize(innerHeight: number, innerWidth: number, screenHeight: number): void {
const chartHeight = this.getChartHeight(innerHeight, innerWidth, screenHeight);
if (chartHeight > 0) {
this.chart?.updateOptions({ chart: { height: chartHeight } });
}
Expand Down Expand Up @@ -135,17 +135,25 @@ export class CarbonEstimationComponent implements OnInit, OnDestroy {
);
}

private getChartHeight(innerHeight: number, innerWidth: number): number {
private getChartHeight(innerHeight: number, innerWidth: number, screenHeight: number): number {
const expansionPanelHeight = this.detailsPanel.nativeElement.clientHeight;

let calculatedHeight: number;
mgriffin-scottlogic marked this conversation as resolved.
Show resolved Hide resolved

// medium tailwind responsive design breakpoint https://tailwindcss.com/docs/responsive-design
if (innerWidth < 768) {
return innerHeight - this.estimatorBaseHeight - expansionPanelHeight + estimatorHeights.title;
calculatedHeight = innerHeight - this.estimatorBaseHeight - expansionPanelHeight + estimatorHeights.title;
} else {
const extraHeightString = this.extraHeight();
const extraHeight = Number(extraHeightString) || 0;
calculatedHeight = innerHeight - this.estimatorBaseHeight - extraHeight - expansionPanelHeight;
}

const extraHeightString = this.extraHeight();
const extraHeight = Number(extraHeightString) || 0;
return innerHeight - this.estimatorBaseHeight - extraHeight - expansionPanelHeight;
const maxScreenHeightRatio = 0.75;

// Cap chart height based on screen height to prevent issues with the chart
// becoming stretched when the component is displayed in a tall iFrame
return Math.min(calculatedHeight, screenHeight * maxScreenHeightRatio);
}

private getDataItem(key: string, value: number, parent: string): ApexChartDataItem {
Expand Down