Skip to content

Commit aa8e7cd

Browse files
denStrigonnixaa
authored andcommitted
fix(chart): resize chart on sidebar expand/collapse (#1816)
1 parent f20c371 commit aa8e7cd

File tree

16 files changed

+383
-162
lines changed

16 files changed

+383
-162
lines changed

src/app/@core/data/data.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { EarningService } from './earning.service';
1515
import { OrdersProfitChartService } from './orders-profit-chart.service';
1616
import { TrafficBarService } from './traffic-bar.service';
1717
import { ProfitBarAnimationChartService } from './profit-bar-animation-chart.service';
18+
import { LayoutService } from './layout.service';
1819

1920
const SERVICES = [
2021
UserService,
@@ -31,6 +32,7 @@ const SERVICES = [
3132
OrdersProfitChartService,
3233
TrafficBarService,
3334
ProfitBarAnimationChartService,
35+
LayoutService,
3436
];
3537

3638
@NgModule({

src/app/@core/data/layout.service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable } from '@angular/core';
2+
import { Observable, Subject } from 'rxjs';
3+
import { delay, share } from 'rxjs/operators';
4+
5+
@Injectable()
6+
export class LayoutService {
7+
8+
protected layoutSize$ = new Subject();
9+
10+
changeLayoutSize() {
11+
this.layoutSize$.next();
12+
}
13+
14+
onChangeLayoutSize(): Observable<any> {
15+
return this.layoutSize$.pipe(
16+
share(),
17+
delay(1),
18+
);
19+
}
20+
}

src/app/@theme/components/header/header.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Component, Input, OnInit } from '@angular/core';
33
import { NbMenuService, NbSidebarService } from '@nebular/theme';
44
import { UserService } from '../../../@core/data/users.service';
55
import { AnalyticsService } from '../../../@core/utils/analytics.service';
6+
import { LayoutService } from '../../../@core/data/layout.service';
67

78
@Component({
89
selector: 'ngx-header',
@@ -11,7 +12,6 @@ import { AnalyticsService } from '../../../@core/utils/analytics.service';
1112
})
1213
export class HeaderComponent implements OnInit {
1314

14-
1515
@Input() position = 'normal';
1616

1717
user: any;
@@ -21,7 +21,8 @@ export class HeaderComponent implements OnInit {
2121
constructor(private sidebarService: NbSidebarService,
2222
private menuService: NbMenuService,
2323
private userService: UserService,
24-
private analyticsService: AnalyticsService) {
24+
private analyticsService: AnalyticsService,
25+
private layoutService: LayoutService) {
2526
}
2627

2728
ngOnInit() {
@@ -31,11 +32,14 @@ export class HeaderComponent implements OnInit {
3132

3233
toggleSidebar(): boolean {
3334
this.sidebarService.toggle(true, 'menu-sidebar');
35+
this.layoutService.changeLayoutSize();
36+
3437
return false;
3538
}
3639

3740
toggleSettings(): boolean {
3841
this.sidebarService.toggle(false, 'settings-sidebar');
42+
3943
return false;
4044
}
4145

src/app/pages/dashboard/electricity/electricity-chart/electricity-chart.component.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,57 @@
1-
import { delay } from 'rxjs/operators';
1+
import { delay, takeWhile } from 'rxjs/operators';
22
import { AfterViewInit, Component, OnDestroy } from '@angular/core';
33
import { NbThemeService } from '@nebular/theme';
4-
5-
declare const echarts: any;
4+
import { LayoutService } from '../../../../@core/data/layout.service';
65

76
@Component({
87
selector: 'ngx-electricity-chart',
98
styleUrls: ['./electricity-chart.component.scss'],
109
template: `
11-
<div echarts [options]="option" class="echart"></div>
10+
<div echarts
11+
[options]="option"
12+
class="echart"
13+
(chartInit)="onChartInit($event)">
14+
</div>
1215
`,
1316
})
1417
export class ElectricityChartComponent implements AfterViewInit, OnDestroy {
1518

19+
private alive = true;
20+
1621
option: any;
1722
data: Array<any>;
18-
themeSubscription: any;
23+
echartsIntance: any;
1924

20-
constructor(private theme: NbThemeService) {
25+
constructor(private theme: NbThemeService,
26+
private layoutService: LayoutService) {
2127

2228
const points = [490, 490, 495, 500, 505, 510, 520, 530, 550, 580, 630,
2329
720, 800, 840, 860, 870, 870, 860, 840, 800, 720, 200, 145, 130, 130,
2430
145, 200, 570, 635, 660, 670, 670, 660, 630, 580, 460, 380, 350, 340,
2531
340, 340, 340, 340, 340, 340, 340, 340];
2632

27-
// const points = [];
28-
// let pointsCount = 100;
29-
// let min = -3;
30-
// let max = 3;
31-
// let xStep = (max - min) / pointsCount;
32-
//
33-
// for(let x = -3; x <= 3; x += xStep) {
34-
// let res = x**3 - 5*x + 17;
35-
// points.push(Math.round(res * 25));
36-
// }
37-
3833
this.data = points.map((p, index) => ({
3934
label: (index % 5 === 3) ? `${Math.round(index / 5)}` : '',
4035
value: p,
4136
}));
37+
38+
this.layoutService.onChangeLayoutSize()
39+
.pipe(
40+
takeWhile(() => this.alive),
41+
)
42+
.subscribe(() => this.resizeChart());
4243
}
4344

4445
ngAfterViewInit(): void {
45-
this.themeSubscription = this.theme.getJsTheme().pipe(delay(1)).subscribe(config => {
46-
const eTheme: any = config.variables.electricity;
46+
this.theme.getJsTheme()
47+
.pipe(
48+
takeWhile(() => this.alive),
49+
delay(1),
50+
)
51+
.subscribe(config => {
52+
const eTheme: any = config.variables.electricity;
4753

48-
this.option = {
54+
this.option = {
4955
grid: {
5056
left: 0,
5157
top: 0,
@@ -184,7 +190,17 @@ export class ElectricityChartComponent implements AfterViewInit, OnDestroy {
184190
});
185191
}
186192

193+
onChartInit(echarts) {
194+
this.echartsIntance = echarts;
195+
}
196+
197+
resizeChart() {
198+
if (this.echartsIntance) {
199+
this.echartsIntance.resize();
200+
}
201+
}
202+
187203
ngOnDestroy() {
188-
this.themeSubscription.unsubscribe();
204+
this.alive = false;
189205
}
190206
}

0 commit comments

Comments
 (0)