-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: basic diagram component * feat: basic diagram component after review * feat: basic diagram component test harnesses adaption translations tested code review input * fix: remove comment, edit translations * fix: edit translations --------- Co-authored-by: kim.tran <[email protected]>
- Loading branch information
Showing
22 changed files
with
1,165 additions
and
29 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
21 changes: 21 additions & 0 deletions
21
libs/portal-integration-angular/src/lib/core/components/diagram/diagram.component.html
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,21 @@ | ||
<ng-container *ngIf="this.data"> | ||
<div class="w-full flex justify-content-center"> | ||
<p-chart | ||
id="diagram" | ||
type="pie" | ||
[data]="chartData" | ||
[responsive]="false" | ||
[options]="chartOptions" | ||
(onDataSelect)="dataClicked($event)" | ||
></p-chart> | ||
</div> | ||
<div class="w-full flex justify-content-center mt-2 sumKey"> | ||
<p class="text-md font-medium text-700"> | ||
<span name="sumLabel"> {{ sumKey | translate }}</span> : <span name="amountOfData">{{ amountOfData}}</span> | ||
</p> | ||
</div> | ||
</ng-container> | ||
|
||
<div *ngIf="!this.data" class="w-full flex justify-content-center"> | ||
<p-message severity="info" text="{{ 'OCX_DIAGRAM.NO_DATA' | translate }}"></p-message> | ||
</div> |
93 changes: 93 additions & 0 deletions
93
libs/portal-integration-angular/src/lib/core/components/diagram/diagram.component.spec.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,93 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing' | ||
import { DiagramComponent } from './diagram.component' | ||
import { NoopAnimationsModule } from '@angular/platform-browser/animations' | ||
import { TranslateTestingModule } from 'ngx-translate-testing' | ||
import { HttpClientTestingModule } from '@angular/common/http/testing' | ||
import { ChartModule } from 'primeng/chart' | ||
import { MessageModule } from 'primeng/message' | ||
import { MockAuthModule } from '../../../mock-auth/mock-auth.module' | ||
import { MFE_INFO } from '../../../api/injection-tokens' | ||
import { DiagramHarness, TestbedHarnessEnvironment } from '../../../../../testing' | ||
import { TranslateService } from '@ngx-translate/core' | ||
|
||
describe('DiagramComponent', () => { | ||
let translateService: TranslateService | ||
let component: DiagramComponent | ||
let fixture: ComponentFixture<DiagramComponent> | ||
|
||
const definedSumKey = 'OCX_DIAGRAM.SUM' | ||
|
||
// Use the power of 2 (2^n) to identify the error of a possibly failed test more quickly | ||
const diagramData: { label: string; value: number }[] = [ | ||
{ label: 'test0', value: 1 }, | ||
{ label: 'test1', value: 2 }, | ||
{ label: 'test2', value: 4 }, | ||
{ label: 'test3', value: 8 }, | ||
{ label: 'test4', value: 16 }, | ||
{ label: 'test5', value: 32 }, | ||
{ label: 'test6', value: 64 }, | ||
] | ||
const numberOfResults = Math.pow(2, diagramData.length) - 1 | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [DiagramComponent], | ||
imports: [ | ||
NoopAnimationsModule, | ||
ChartModule, | ||
MessageModule, | ||
MockAuthModule, | ||
TranslateTestingModule.withTranslations({ | ||
en: require('./../../../../../assets/i18n/en.json'), | ||
de: require('./../../../../../assets/i18n/de.json'), | ||
}), | ||
HttpClientTestingModule, | ||
], | ||
providers: [ | ||
{ | ||
provide: MFE_INFO, | ||
useValue: { | ||
baseHref: '/base/path', | ||
mountPath: '/base/path', | ||
remoteBaseUrl: 'http://localhost:4200', | ||
shellName: 'shell', | ||
}, | ||
}, | ||
], | ||
}).compileComponents() | ||
|
||
fixture = TestBed.createComponent(DiagramComponent) | ||
component = fixture.componentInstance | ||
component.data = diagramData | ||
component.sumKey = definedSumKey | ||
translateService = TestBed.inject(TranslateService) | ||
translateService.setDefaultLang('en') | ||
translateService.use('en') | ||
}) | ||
|
||
it('should create the diagram component', () => { | ||
expect(component).toBeTruthy() | ||
}) | ||
|
||
it('loads diagramHarness', async () => { | ||
const diagram = await TestbedHarnessEnvironment.harnessForFixture(fixture, DiagramHarness) | ||
expect(diagram).toBeTruthy() | ||
}) | ||
|
||
it('should display the sumKey on the diagram component', async () => { | ||
const diagram = await TestbedHarnessEnvironment.harnessForFixture(fixture, DiagramHarness) | ||
const displayedText = await diagram.getSumLabel() | ||
const definedSumKeyTranslation = translateService.instant(definedSumKey) | ||
expect(displayedText).toEqual(definedSumKeyTranslation) | ||
}) | ||
|
||
it('should be created', () => { | ||
expect(translateService).toBeTruthy() | ||
}) | ||
|
||
it('should display the amountOfData on the diagram component', async () => { | ||
const diagram = await TestbedHarnessEnvironment.harnessForFixture(fixture, DiagramHarness) | ||
const displayedNumber = await diagram.getTotalNumberOfResults() | ||
expect(displayedNumber).toEqual(numberOfResults) | ||
}) | ||
}) |
70 changes: 70 additions & 0 deletions
70
libs/portal-integration-angular/src/lib/core/components/diagram/diagram.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,70 @@ | ||
import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core' | ||
import { TranslateService } from '@ngx-translate/core' | ||
import { ChartData, ChartOptions } from 'chart.js' | ||
import * as d3 from 'd3-scale-chromatic' | ||
import { ColorUtils } from '../../utils/colorutils' | ||
import { DiagramData } from '../../../model/diagram-data' | ||
|
||
@Component({ | ||
selector: 'ocx-diagram', | ||
templateUrl: './diagram.component.html', | ||
}) | ||
export class DiagramComponent implements OnInit, OnChanges { | ||
@Input() data: DiagramData[] | undefined | ||
@Input() sumKey = 'OCX_DIAGRAM.SUM' | ||
@Output() dataSelected: EventEmitter<any> = new EventEmitter() | ||
chartOptions: ChartOptions | undefined | ||
chartData: ChartData | undefined | ||
amountOfData: number | undefined | null | ||
// Changing the colorRangeInfo, will change the range of the color palette of the diagram. | ||
private colorRangeInfo = { | ||
colorStart: 0, | ||
colorEnd: 1, | ||
useEndAsStart: false, | ||
} | ||
// Changing the colorScale, will change the thematic color appearance of the diagram. | ||
private colorScale = d3.interpolateCool | ||
|
||
constructor(private translateService: TranslateService) {} | ||
|
||
ngOnChanges(): void { | ||
this.generateChart(this.colorScale, this.colorRangeInfo) | ||
} | ||
ngOnInit(): void { | ||
this.generateChart(this.colorScale, this.colorRangeInfo) | ||
} | ||
|
||
public generateChart(colorScale: any, colorRangeInfo: any) { | ||
if (this.data) { | ||
const inputData = this.data.map((diagramData) => diagramData.value) | ||
|
||
this.amountOfData = this.data.reduce((acc, current) => acc + current.value, 0) | ||
const COLORS = interpolateColors(this.amountOfData, colorScale, colorRangeInfo) | ||
this.chartData = { | ||
labels: this.data.map((data) => data.label), | ||
datasets: [ | ||
{ | ||
data: inputData, | ||
backgroundColor: COLORS, | ||
}, | ||
], | ||
} | ||
} | ||
|
||
this.chartOptions = { | ||
plugins: { | ||
legend: { | ||
position: 'bottom', | ||
}, | ||
}, | ||
maintainAspectRatio: false, | ||
} | ||
} | ||
|
||
dataClicked(event: []) { | ||
this.dataSelected.emit(event.length) | ||
} | ||
} | ||
function interpolateColors(amountOfData: number, colorScale: any, colorRangeInfo: any) { | ||
return ColorUtils.interpolateColors(amountOfData, colorScale, colorRangeInfo) | ||
} |
1 change: 1 addition & 0 deletions
1
...ular/src/lib/core/components/group-by-count-diagram/group-by-count-diagram.component.html
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 @@ | ||
<ocx-diagram [data]="(diagramData$ | async) || []" [sumKey]="sumKey" (onDataSelect)="dataClicked($event)"></ocx-diagram> |
Oops, something went wrong.