Skip to content

Commit

Permalink
perf(module:chart): optimize g2 performance (#388)
Browse files Browse the repository at this point in the history
- close #377
  • Loading branch information
cipchk committed Jan 17, 2019
1 parent 06adf96 commit 94ef1fe
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 76 deletions.
9 changes: 6 additions & 3 deletions packages/chart/bar/bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ElementRef,
HostBinding,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -47,6 +48,8 @@ export class G2BarComponent implements OnInit, OnChanges, OnDestroy {

// #endregion

constructor(private ngZone: NgZone) {}

private getHeight() {
return this.title ? this.height - TITLE_HEIGHT : this.height;
}
Expand Down Expand Up @@ -119,15 +122,15 @@ export class G2BarComponent implements OnInit, OnChanges, OnDestroy {
filter(() => this.chart),
debounceTime(200),
)
.subscribe(() => this.updatelabel());
.subscribe(() => this.ngZone.runOutsideAngular(() => this.updatelabel()));
}

ngOnInit() {
setTimeout(() => this.install(), this.delay);
this.ngZone.runOutsideAngular(() => setTimeout(() => this.install(), this.delay));
}

ngOnChanges(): void {
this.attachChart();
this.ngZone.runOutsideAngular(() => this.attachChart());
}

ngOnDestroy(): void {
Expand Down
7 changes: 4 additions & 3 deletions packages/chart/gauge/gauge.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Component,
ElementRef,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -36,7 +37,7 @@ export class G2GaugeComponent implements OnInit, OnDestroy, OnChanges {

// #endregion

constructor(private el: ElementRef) { }
constructor(private el: ElementRef, private ngZone: NgZone) { }

private install() {
const Shape = G2.Shape;
Expand Down Expand Up @@ -158,11 +159,11 @@ export class G2GaugeComponent implements OnInit, OnDestroy, OnChanges {
}

ngOnInit(): void {
setTimeout(() => this.install(), this.delay);
this.ngZone.runOutsideAngular(() => setTimeout(() => this.install(), this.delay));
}

ngOnChanges(): void {
this.attachChart();
this.ngZone.runOutsideAngular(() => this.attachChart());
}

ngOnDestroy(): void {
Expand Down
7 changes: 4 additions & 3 deletions packages/chart/mini-area/mini-area.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ElementRef,
HostBinding,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -46,7 +47,7 @@ export class G2MiniAreaComponent implements OnInit, OnChanges, OnDestroy {

// #endregion

constructor(private el: ElementRef) { }
constructor(private el: ElementRef, private ngZone: NgZone) { }

private install() {
const { el, fit, height, padding, xAxis, yAxis, yTooltipSuffix, tooltipType, line } = this;
Expand Down Expand Up @@ -117,11 +118,11 @@ export class G2MiniAreaComponent implements OnInit, OnChanges, OnDestroy {
}

ngOnInit() {
setTimeout(() => this.install(), this.delay);
this.ngZone.runOutsideAngular(() => setTimeout(() => this.install(), this.delay));
}

ngOnChanges(): void {
this.attachChart();
this.ngZone.runOutsideAngular(() => this.attachChart());
}

ngOnDestroy(): void {
Expand Down
7 changes: 4 additions & 3 deletions packages/chart/mini-bar/mini-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ElementRef,
HostBinding,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -40,7 +41,7 @@ export class G2MiniBarComponent implements OnInit, OnChanges, OnDestroy {

// #endregion

constructor(private el: ElementRef) { }
constructor(private el: ElementRef, private ngZone: NgZone) { }

private install() {
const { el, height, padding, yTooltipSuffix, tooltipType } = this;
Expand Down Expand Up @@ -88,11 +89,11 @@ export class G2MiniBarComponent implements OnInit, OnChanges, OnDestroy {
}

ngOnInit() {
setTimeout(() => this.install(), this.delay);
this.ngZone.runOutsideAngular(() => setTimeout(() => this.install(), this.delay));
}

ngOnChanges(): void {
this.attachChart();
this.ngZone.runOutsideAngular(() => this.attachChart());
}

ngOnDestroy(): void {
Expand Down
84 changes: 54 additions & 30 deletions packages/chart/pie/pie.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Component,
ElementRef,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -59,7 +60,12 @@ export class G2PieComponent implements OnInit, OnDestroy, OnChanges {

// #endregion

constructor(private el: ElementRef, private rend: Renderer2, private cdr: ChangeDetectorRef) { }
constructor(
private el: ElementRef,
private rend: Renderer2,
private ngZone: NgZone,
private cdr: ChangeDetectorRef,
) {}

private setCls() {
const { el, rend, hasLegend, isPercent } = this;
Expand All @@ -83,7 +89,8 @@ export class G2PieComponent implements OnInit, OnDestroy, OnChanges {
if (this.isPercent) {
this.select = false;
this.tooltip = false;
this.percentColor = value => value === '占比' ? color || 'rgba(24, 144, 255, 0.85)' : '#F0F2F5';
this.percentColor = value =>
value === '占比' ? color || 'rgba(24, 144, 255, 0.85)' : '#F0F2F5';
this.data = [
{
x: '占比',
Expand All @@ -101,13 +108,13 @@ export class G2PieComponent implements OnInit, OnDestroy, OnChanges {
this.setCls();

const { node, height, padding, animate, tooltip, inner, hasLegend } = this;
const chart = this.chart = new G2.Chart({
const chart = (this.chart = new G2.Chart({
container: node.nativeElement,
forceFit: true,
height,
padding,
animate,
});
}));

if (!tooltip) {
chart.tooltip(false);
Expand All @@ -129,7 +136,10 @@ export class G2PieComponent implements OnInit, OnDestroy, OnChanges {
chart
.intervalStack()
.position('y')
.tooltip('x*percent', (name, p) => ({ name, value: hasLegend ? p : (p * 100).toFixed(2) }))
.tooltip('x*percent', (name, p) => ({
name,
value: hasLegend ? p : (p * 100).toFixed(2),
}))
.select(this.select);

chart.render();
Expand All @@ -138,49 +148,63 @@ export class G2PieComponent implements OnInit, OnDestroy, OnChanges {
}

private attachChart() {
const { chart, height, padding, animate, data, lineWidth, isPercent, percentColor, colors } = this;
const {
chart,
height,
padding,
animate,
data,
lineWidth,
isPercent,
percentColor,
colors,
} = this;
if (!chart) return;

chart.set('height', height);
chart.set('padding', padding);
chart.set('animate', animate);

chart.get('geoms')[0]
chart
.get('geoms')[0]
.style({ lineWidth, stroke: '#fff' })
.color('x', isPercent ? percentColor : colors);

const dv = new DataSet.DataView();
dv.source(data).transform({
type: 'percent',
field: 'y',
dimension: 'x',
as: 'percent',
});
type: 'percent',
field: 'y',
dimension: 'x',
as: 'percent',
});
chart.source(dv, {
x: {
type: 'cat',
range: [0, 1],
},
y: {
min: 0,
},
});
x: {
type: 'cat',
range: [0, 1],
},
y: {
min: 0,
},
});
chart.repaint();

this.genLegend();
this.ngZone.run(() => this.genLegend());
}

private genLegend() {
const { hasLegend, isPercent, cdr, chart } = this;
if (!hasLegend || isPercent) return;

this.legendData = chart.get('geoms')[0].get('dataArray').map((item: any) => {
const origin = item[0]._origin;
origin.color = item[0].color;
origin.checked = true;
origin.percent = (origin.percent * 100).toFixed(2);
return origin;
});
this.legendData = chart
.get('geoms')[0]
.get('dataArray')
.map((item: any) => {
const origin = item[0]._origin;
origin.color = item[0].color;
origin.checked = true;
origin.percent = (origin.percent * 100).toFixed(2);
return origin;
});

cdr.detectChanges();
}
Expand All @@ -200,13 +224,13 @@ export class G2PieComponent implements OnInit, OnDestroy, OnChanges {
}

ngOnInit(): void {
setTimeout(() => this.install(), this.delay);
this.ngZone.runOutsideAngular(() => setTimeout(() => this.install(), this.delay));
}

ngOnChanges(): void {
this.fixData();
this.setCls();
this.attachChart();
this.ngZone.runOutsideAngular(() => this.attachChart());
this.installResizeEvent();
}

Expand Down
9 changes: 5 additions & 4 deletions packages/chart/radar/radar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ElementRef,
HostBinding,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -56,7 +57,7 @@ export class G2RadarComponent implements OnInit, OnDestroy, OnChanges {

// #endregion

constructor(private cdr: ChangeDetectorRef) { }
constructor(private cdr: ChangeDetectorRef, private ngZone: NgZone) { }

private getHeight() {
return this.height - (this.hasLegend ? 80 : 22);
Expand Down Expand Up @@ -145,7 +146,7 @@ export class G2RadarComponent implements OnInit, OnDestroy, OnChanges {

chart.repaint();

this.genLegend();
this.ngZone.run(() => this.genLegend());
}

private genLegend() {
Expand Down Expand Up @@ -174,12 +175,12 @@ export class G2RadarComponent implements OnInit, OnDestroy, OnChanges {
}

ngOnInit(): void {
setTimeout(() => this.install(), this.delay);
this.ngZone.runOutsideAngular(() => setTimeout(() => this.install(), this.delay));
}

ngOnChanges(): void {
this.legendData.forEach(i => i.checked = true);
this.attachChart();
this.ngZone.runOutsideAngular(() => this.attachChart());
}

ngOnDestroy(): void {
Expand Down
7 changes: 4 additions & 3 deletions packages/chart/single-bar/single-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ElementRef,
HostBinding,
Input,
NgZone,
OnChanges,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -38,7 +39,7 @@ export class G2SingleBarComponent implements OnInit, OnChanges, OnDestroy {

// #endregion

constructor(private el: ElementRef) { }
constructor(private el: ElementRef, private ngZone: NgZone) { }

private install() {
const { el, height, padding, textStyle, line, format } = this;
Expand Down Expand Up @@ -96,11 +97,11 @@ export class G2SingleBarComponent implements OnInit, OnChanges, OnDestroy {
}

ngOnInit() {
setTimeout(() => this.install(), this.delay);
this.ngZone.runOutsideAngular(() => setTimeout(() => this.install(), this.delay));
}

ngOnChanges(): void {
this.attachChart();
this.ngZone.runOutsideAngular(() => this.attachChart());
}

ngOnDestroy(): void {
Expand Down
Loading

0 comments on commit 94ef1fe

Please sign in to comment.