-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10082 from abpframework/feat/7472
Angular UI: Move ChartComponent to @abp/ng.components/chart.js
- Loading branch information
Showing
36 changed files
with
761 additions
and
773 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json", | ||
"dest": "../../../dist/packages/components/chart.js", | ||
"lib": { | ||
"entryFile": "src/public-api.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
npm/ng-packs/packages/components/chart.js/src/chart.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { | ||
AfterViewInit, | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
ElementRef, | ||
EventEmitter, | ||
Input, | ||
OnChanges, | ||
OnDestroy, | ||
Output, | ||
SimpleChanges, | ||
ViewChild, | ||
} from '@angular/core'; | ||
|
||
let Chart: any; | ||
|
||
@Component({ | ||
selector: 'abp-chart', | ||
template: ` | ||
<div | ||
style="position:relative" | ||
[style.width]="responsive && !width ? null : width" | ||
[style.height]="responsive && !height ? null : height" | ||
> | ||
<canvas | ||
#canvas | ||
[attr.width]="responsive && !width ? null : width" | ||
[attr.height]="responsive && !height ? null : height" | ||
(click)="onCanvasClick($event)" | ||
></canvas> | ||
</div> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
exportAs: 'abpChart', | ||
}) | ||
export class ChartComponent implements AfterViewInit, OnDestroy, OnChanges { | ||
@Input() type: string; | ||
|
||
@Input() data: any = {}; | ||
|
||
@Input() options: any = {}; | ||
|
||
@Input() plugins: any[] = []; | ||
|
||
@Input() width: string; | ||
|
||
@Input() height: string; | ||
|
||
@Input() responsive = true; | ||
|
||
@Output() dataSelect = new EventEmitter(); | ||
|
||
@Output() initialized = new EventEmitter<boolean>(); | ||
|
||
@ViewChild('canvas') canvas: ElementRef<HTMLCanvasElement>; | ||
|
||
chart: any; | ||
|
||
constructor(public el: ElementRef, private cdr: ChangeDetectorRef) {} | ||
|
||
ngAfterViewInit() { | ||
import('chart.js/auto').then(module => { | ||
Chart = module.default; | ||
this.initChart(); | ||
this.initialized.emit(true); | ||
}); | ||
} | ||
|
||
onCanvasClick(event) { | ||
if (this.chart) { | ||
const element = this.chart.getElementsAtEventForMode( | ||
event, | ||
'nearest', | ||
{ intersect: true }, | ||
false, | ||
); | ||
const dataset = this.chart.getElementsAtEventForMode( | ||
event, | ||
'dataset', | ||
{ intersect: true }, | ||
false, | ||
); | ||
|
||
if (element && element[0] && dataset) { | ||
this.dataSelect.emit({ originalEvent: event, element: element[0], dataset: dataset }); | ||
} | ||
} | ||
} | ||
|
||
initChart = () => { | ||
const opts = this.options || {}; | ||
opts.responsive = this.responsive; | ||
|
||
// allows chart to resize in responsive mode | ||
if (opts.responsive && (this.height || this.width)) { | ||
opts.maintainAspectRatio = false; | ||
} | ||
|
||
this.chart = new Chart(this.canvas.nativeElement, { | ||
type: this.type as any, | ||
data: this.data, | ||
options: this.options, | ||
}); | ||
}; | ||
|
||
getCanvas = () => { | ||
return this.canvas.nativeElement; | ||
}; | ||
|
||
getBase64Image = () => { | ||
return this.chart.toBase64Image(); | ||
}; | ||
|
||
generateLegend = () => { | ||
if (this.chart) { | ||
return this.chart.generateLegend(); | ||
} | ||
}; | ||
|
||
refresh = () => { | ||
if (this.chart) { | ||
this.chart.update(); | ||
this.cdr.detectChanges(); | ||
} | ||
}; | ||
|
||
reinit = () => { | ||
if (!this.chart) return; | ||
this.chart.destroy(); | ||
this.initChart(); | ||
}; | ||
|
||
ngOnDestroy() { | ||
if (this.chart) { | ||
this.chart.destroy(); | ||
this.chart = null; | ||
} | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges) { | ||
if (!this.chart) return; | ||
|
||
if (changes.data?.currentValue || changes.options?.currentValue) { | ||
this.chart.destroy(); | ||
this.initChart(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
npm/ng-packs/packages/components/chart.js/src/chart.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { ChartComponent } from './chart.component'; | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
exports: [ChartComponent], | ||
declarations: [ChartComponent], | ||
providers: [], | ||
}) | ||
export class ChartModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './chart.component'; | ||
export * from './chart.module'; | ||
export * from './widget-utils'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
import 'jest-canvas-mock'; | ||
import 'jest-preset-angular/setup-jest'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.