Skip to content

Commit

Permalink
feat: add horizontal bar chart component
Browse files Browse the repository at this point in the history
  • Loading branch information
grantwforsythe committed Mar 21, 2024
1 parent 38f2786 commit 92ba778
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="container">
<ngx-charts-bar-horizontal
[results]="results$ | async"
[legend]="true"
[xAxis]="true"
[yAxis]="true"
[showGridLines]="true"
[rotateXAxisTicks]="true"
[noBarWhenZero]="true"
[showXAxisLabel]="true"
[showYAxisLabel]="true"
xAxisLabel="Category"
yAxisLabel="Amount"
>
</ngx-charts-bar-horizontal>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ChartsBarHorizontalComponent } from './bar-horizontal.component';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { CategoryGroup } from '../../../../../shared/services/ynab/interfaces/categories/categoryGroup';
import { Account } from '../../../../../shared/services/ynab/interfaces/accounts/account';
import { Transaction } from '../../../../../shared/services/ynab/interfaces/transactions/transaction';
import { selectReportResults } from '../../../../data-access/report.selectors';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';

describe('ChartsBarHorizontalComponent', () => {
let component: ChartsBarHorizontalComponent;
let fixture: ComponentFixture<ChartsBarHorizontalComponent>;
let store: MockStore;

const initialState: {
categoryGroups: CategoryGroup[];
accounts: Account[];
transactions: Transaction[];
} = {
categoryGroups: [],
accounts: [],
transactions: [],
};

afterEach(() => {
store?.resetSelectors();
});

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
providers: [
NgxChartsModule,
provideAnimationsAsync(),
provideMockStore({
initialState,
selectors: [
{
selector: selectReportResults,
value: [
{
name: 'Groceries',
value: 100,
},
{
name: 'Pet Care',
value: 25,
},
{
name: 'Internet',
value: 50,
},
],
},
],
}),
],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ChartsBarHorizontalComponent);
component = fixture.componentInstance;
store = TestBed.inject(MockStore);
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeDefined();
});

it('should call populate chart on #ngOnInit()', () => {
component.ngOnInit();
fixture.detectChanges();

const legend: HTMLElement = fixture.nativeElement.querySelector('ul.legend-labels');
expect(legend.textContent).toContain('Groceries');
expect(legend.textContent).toContain('Pet Care');
expect(legend.textContent).toContain('Internet');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AsyncPipe } from '@angular/common';
import { Component, inject, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { Store } from '@ngrx/store';
import { selectReportResults } from '../../../../data-access/report.selectors';

@Component({
selector: 'app-charts-bar-horizontal',
standalone: true,
imports: [AsyncPipe, NgxChartsModule],
templateUrl: './bar-horizontal.component.html',
})
export class ChartsBarHorizontalComponent implements OnInit {
store = inject(Store);

results$: Observable<{ value: number; name: string }[]> | undefined;

ngOnInit(): void {
this.results$ = this.store.select(selectReportResults);
}
}
2 changes: 2 additions & 0 deletions src/app/reports/feature/dashboard/dashboard.routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Routes } from '@angular/router';
import { ChartsBarVerticalComponent } from './charts/bar-vertical/bar-vertical.component';
import { ChartsBarHorizontalComponent } from './charts/bar-horizontal/bar-horizontal.component';

export const dashboardRoutes: Routes = [
{ path: 'bar-vert', component: ChartsBarVerticalComponent },
{ path: 'bar-horz', component: ChartsBarHorizontalComponent },
];

0 comments on commit 92ba778

Please sign in to comment.